1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # Copyright (C) 2016 The LyX team
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 """ Convert files to the file format generated by lyx 2.3"""
25 # Uncomment only what you need to import, please.
27 from parser_tools import (del_token, del_value, del_complete_lines,
28 find_complete_lines, find_end_of, find_end_of_layout, find_end_of_inset,
29 find_re, find_substring, find_token, find_token_backwards, find_across_lines,
30 get_containing_inset, get_containing_layout, get_bool_value, get_value,
31 get_quoted_value, is_in_inset, set_bool_value)
32 # find_tokens, find_token_exact, check_token, get_option_value
34 from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, revert_font_attrs,
35 insert_to_preamble, latex_length, is_document_option,
36 insert_document_option, remove_document_option, revert_language)
38 ####################################################################
39 # Private helper functions
43 ###############################################################################
45 ### Conversion and reversion routines
47 ###############################################################################
49 def convert_microtype(document):
50 " Add microtype settings. "
51 i = find_token(document.header, "\\font_tt_scale")
52 j = find_token(document.preamble, "\\usepackage{microtype}")
54 document.header.insert(i + 1, "\\use_microtype false")
56 document.header.insert(i + 1, "\\use_microtype true")
57 del document.preamble[j]
58 if j and document.preamble[j-1] == "% Added by lyx2lyx":
59 del document.preamble[j-1]
62 def revert_microtype(document):
63 " Remove microtype settings. "
64 use_microtype = get_bool_value(document.header, "\\use_microtype", delete=True)
66 add_to_preamble(document, ["\\usepackage{microtype}"])
69 def convert_dateinset(document):
70 ' Convert date external inset to ERT '
73 i = find_token(document.body, "\\begin_inset External", i+1)
76 j = find_end_of_inset(document.body, i)
78 document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
80 if get_value(document.body, 'template', i, j) == "Date":
81 document.body[i : j + 1] = put_cmd_in_ert("\\today ")
85 def convert_inputenc(document):
86 """Replace no longer supported input encoding setting."""
87 i = find_token(document.header, "\\inputencoding pt254")
89 document.header[i] = "\\inputencoding pt154"
92 def convert_ibranches(document):
93 ' Add "inverted 0" to branch insets'
96 i = find_token(document.body, "\\begin_inset Branch", i+1)
99 document.body.insert(i + 1, "inverted 0")
102 def revert_ibranches(document):
103 ' Convert inverted branches to explicit anti-branches'
104 # Get list of branches
108 i = find_token(document.header, "\\branch", i+1)
111 branch = document.header[i][8:].strip()
112 selected = get_bool_value(document.header, "\\selected", i+1, i+2)
114 document.warning("Malformed LyX document: No selection indicator "
115 "for branch %s." % branch)
117 # the value tells us whether the branch is selected
118 ourbranches[branch] = selected
120 # Find branch insets, remove "inverted" tag and
121 # convert inverted insets to "Anti-OldBranch" insets
125 i = find_token(document.body, "\\begin_inset Branch", i+1)
128 inverted = get_bool_value(document.body, "inverted", i+1, i+2, delete=True)
130 document.warning("Malformed LyX document: Missing 'inverted' tag in branch inset.")
133 branch = document.body[i][20:].strip()
134 if not branch in antibranches:
135 antibranch = "Anti-" + branch
136 while antibranch in antibranches:
137 antibranch = "x" + antibranch
138 antibranches[branch] = antibranch
140 antibranch = antibranches[branch]
141 document.body[i] = "\\begin_inset Branch " + antibranch
143 # now we need to add the new branches to the header
144 for old, new in antibranches.items():
145 i = find_token(document.header, "\\branch " + old, 0)
147 document.warning("Can't find branch %s even though we found it before!" % (old))
149 j = find_token(document.header, "\\end_branch", i)
151 document.warning("Malformed LyX document! Can't find end of branch " + old)
153 lines = ["\\branch " + new,
154 "\\selected %d" % (not ourbranches[old])]
155 # these are the old lines telling us color, etc.
156 lines += document.header[i+2 : j+1]
157 document.header[i:i] = lines
160 beamer_article_styles = [
161 "### Inserted by lyx2lyx (more [scr]article styles) ###",
162 "Input article.layout",
163 "Input beamer.layout",
164 "Provides geometry 0",
165 "Provides hyperref 0",
174 " \\usepackage{beamerarticle,pgf}",
175 " % this default might be overridden by plain title style",
176 " \\newcommand\\makebeamertitle{\\frame{\\maketitle}}%",
177 " \\AtBeginDocument{",
178 " \\let\\origtableofcontents=\\tableofcontents",
179 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
180 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
183 "### End of insertion by lyx2lyx (more [scr]article styles) ###"]
185 def revert_beamer_article_styles(document):
186 " Include (scr)article styles in beamer article "
188 beamer_articles = ["article-beamer", "scrarticle-beamer"]
189 if document.textclass not in beamer_articles:
192 if document.textclass == "scrarticle-beamer":
193 beamer_article_styles[1] = "Input scrartcl.layout"
194 document.append_local_layout(beamer_article_styles)
196 def convert_beamer_article_styles(document):
197 " Remove included (scr)article styles in beamer article "
199 beamer_articles = ["article-beamer", "scrarticle-beamer"]
200 if document.textclass not in beamer_articles:
203 if document.textclass == "scrarticle-beamer":
204 beamer_article_styles[1] = "Input scrartcl.layout"
205 document.del_local_layout(beamer_article_styles)
208 def revert_new_babel_languages(document):
209 """Revert "bosnian", "friulan", "macedonian", "piedmontese", "romansh".
211 Set the document language to English but use correct babel setting.
214 nblanguages = ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
216 for lang in nblanguages:
217 if lang == "bosnian" or lang == "macedonian":
218 # These are only supported by babel
219 revert_language(document, lang, lang, "")
221 # These are supported by babel and polyglossia
222 revert_language(document, lang, lang, lang)
225 # def convert_new_babel_languages(document)
226 # set to native support if get_value(document.header, "\\options") in
227 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
231 def revert_amharic(document):
232 "Set the document language to English but assure Amharic output"
234 revert_language(document, "amharic", "", "amharic")
237 def revert_asturian(document):
238 "Set the document language to English but assure Asturian output"
240 revert_language(document, "asturian", "", "asturian")
243 def revert_kannada(document):
244 "Set the document language to English but assure Kannada output"
246 revert_language(document, "kannada", "", "kannada")
249 def revert_khmer(document):
250 "Set the document language to English but assure Khmer output"
252 revert_language(document, "khmer", "", "khmer")
255 def revert_urdu(document):
256 "Set the document language to English but assure Urdu output"
258 revert_language(document, "urdu", "", "urdu")
261 def revert_syriac(document):
262 "Set the document language to English but assure Syriac output"
264 revert_language(document, "syriac", "", "syriac")
267 def revert_quotes(document):
268 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
270 # First handle verbatim insets
273 while i < len(document.body):
274 words = document.body[i].split()
275 if len(words) > 1 and words[0] == "\\begin_inset" and \
276 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
277 j = find_end_of_inset(document.body, i)
280 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
284 k = find_token(document.body, '\\begin_inset Quotes', i, j)
288 l = find_end_of_inset(document.body, k)
290 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
294 if document.body[k].endswith("s"):
296 document.body[k:l+2] = [replace]
301 # Now verbatim layouts
304 while i < len(document.body):
305 words = document.body[i].split()
306 if len(words) > 1 and words[0] == "\\begin_layout" and \
307 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
308 j = find_end_of_layout(document.body, i)
310 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
314 k = find_token(document.body, '\\begin_inset Quotes', i, j)
318 l = find_end_of_inset(document.body, k)
320 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
324 if document.body[k].endswith("s"):
326 document.body[k:l+2] = [replace]
332 if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
338 k = find_token(document.body, '\\begin_inset Quotes', i)
341 l = find_end_of_inset(document.body, k)
343 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
347 parent = get_containing_layout(document.body, k)
348 ql = find_token_backwards(document.body, "\\lang", k)
349 if ql == -1 or ql < parent[1]:
350 hebrew = document.language == "hebrew"
351 elif document.body[ql] == "\\lang hebrew":
355 if document.body[k].endswith("s"):
357 document.body[k:l+2] = [replace]
361 iopart_local_layout = ["### Inserted by lyx2lyx (stdlayouts) ###",
362 "Input stdlayouts.inc",
363 "### End of insertion by lyx2lyx (stdlayouts) ###"""]
365 def revert_iopart(document):
366 " Input new styles via local layout "
367 if document.textclass != "iopart":
369 document.append_local_layout(iopart_local_layout)
372 def convert_iopart(document):
373 " Remove local layout we added, if it is there "
374 if document.textclass != "iopart":
376 document.del_local_layout(iopart_local_layout)
379 def convert_quotestyle(document):
380 " Convert \\quotes_language to \\quotes_style "
381 i = find_token(document.header, "\\quotes_language", 0)
383 document.warning("Malformed LyX document! Can't find \\quotes_language!")
385 val = get_value(document.header, "\\quotes_language", i)
386 document.header[i] = "\\quotes_style " + val
389 def revert_quotestyle(document):
390 " Revert \\quotes_style to \\quotes_language "
391 i = find_token(document.header, "\\quotes_style", 0)
393 document.warning("Malformed LyX document! Can't find \\quotes_style!")
395 val = get_value(document.header, "\\quotes_style", i)
396 document.header[i] = "\\quotes_language " + val
399 def revert_plainquote(document):
400 " Revert plain quote insets "
402 # First, revert style setting
403 i = find_token(document.header, "\\quotes_style plain", 0)
405 document.header[i] = "\\quotes_style english"
411 k = find_token(document.body, '\\begin_inset Quotes q', i)
414 l = find_end_of_inset(document.body, k)
416 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
420 if document.body[k].endswith("s"):
422 document.body[k:l+2] = [replace]
426 def convert_frenchquotes(document):
427 " Convert french quote insets to swiss "
429 # First, revert style setting
430 i = find_token(document.header, "\\quotes_style french", 0)
432 document.header[i] = "\\quotes_style swiss"
437 i = find_token(document.body, '\\begin_inset Quotes f', i)
440 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
441 newval = val.replace("f", "c", 1)
442 document.body[i] = document.body[i].replace(val, newval)
446 def revert_swissquotes(document):
447 " Revert swiss quote insets to french "
449 # First, revert style setting
450 i = find_token(document.header, "\\quotes_style swiss", 0)
452 document.header[i] = "\\quotes_style french"
457 i = find_token(document.body, '\\begin_inset Quotes c', i)
460 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
461 newval = val.replace("c", "f", 1)
462 document.body[i] = document.body[i].replace(val, newval)
466 def revert_britishquotes(document):
467 " Revert british quote insets to english "
469 # First, revert style setting
470 i = find_token(document.header, "\\quotes_style british", 0)
472 document.header[i] = "\\quotes_style english"
477 i = find_token(document.body, '\\begin_inset Quotes b', i)
480 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
481 newval = val.replace("b", "e", 1)
484 newval = newval.replace("d", "s")
487 newval = newval.replace("s", "d")
488 document.body[i] = document.body[i].replace(val, newval)
492 def revert_swedishgquotes(document):
493 " Revert swedish quote insets "
495 # First, revert style setting
496 i = find_token(document.header, "\\quotes_style swedishg", 0)
498 document.header[i] = "\\quotes_style danish"
503 i = find_token(document.body, '\\begin_inset Quotes w', i)
506 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
509 newval = val.replace("w", "a", 1).replace("r", "l")
512 newval = val.replace("w", "s", 1)
513 document.body[i] = document.body[i].replace(val, newval)
517 def revert_frenchquotes(document):
518 " Revert french inner quote insets "
522 i = find_token(document.body, '\\begin_inset Quotes f', i)
525 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
528 newval = val.replace("f", "e", 1).replace("s", "d")
529 document.body[i] = document.body[i].replace(val, newval)
533 def revert_frenchinquotes(document):
534 " Revert inner frenchin quote insets "
536 # First, revert style setting
537 i = find_token(document.header, "\\quotes_style frenchin", 0)
539 document.header[i] = "\\quotes_style french"
544 i = find_token(document.body, '\\begin_inset Quotes i', i)
547 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
548 newval = val.replace("i", "f", 1)
551 newval = newval.replace("s", "d")
552 document.body[i] = document.body[i].replace(val, newval)
556 def revert_russianquotes(document):
557 " Revert russian quote insets "
559 # First, revert style setting
560 i = find_token(document.header, "\\quotes_style russian", 0)
562 document.header[i] = "\\quotes_style french"
567 i = find_token(document.body, '\\begin_inset Quotes r', i)
570 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
574 newval = val.replace("r", "g", 1).replace("s", "d")
577 newval = val.replace("r", "f", 1)
578 document.body[i] = document.body[i].replace(val, newval)
582 def revert_dynamicquotes(document):
583 " Revert dynamic quote insets "
585 # First, revert header
586 i = find_token(document.header, "\\dynamic_quotes", 0)
588 del document.header[i]
592 i = find_token(document.header, "\\quotes_style", 0)
594 document.warning("Malformed document! Missing \\quotes_style")
596 style = get_value(document.header, "\\quotes_style", i)
599 if style == "english":
601 elif style == "swedish":
603 elif style == "german":
605 elif style == "polish":
607 elif style == "swiss":
609 elif style == "danish":
611 elif style == "plain":
613 elif style == "british":
615 elif style == "swedishg":
617 elif style == "french":
619 elif style == "frenchin":
621 elif style == "russian":
624 # now transform the insets
627 i = find_token(document.body, '\\begin_inset Quotes x', i)
630 document.body[i] = document.body[i].replace("x", s)
634 def revert_cjkquotes(document):
635 " Revert cjk quote insets "
639 i = find_token(document.header, "\\quotes_style", 0)
641 document.warning("Malformed document! Missing \\quotes_style")
643 style = get_value(document.header, "\\quotes_style", i)
645 global_cjk = style.find("cjk") != -1
648 document.header[i] = "\\quotes_style english"
649 # transform dynamic insets
651 if style == "cjkangle":
655 i = find_token(document.body, '\\begin_inset Quotes x', i)
658 document.body[i] = document.body[i].replace("x", s)
661 cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
666 k = find_token(document.body, '\\begin_inset Quotes j', i)
669 l = find_end_of_inset(document.body, k)
671 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
675 parent = get_containing_layout(document.body, k)
676 ql = find_token_backwards(document.body, "\\lang", k)
677 if ql == -1 or ql < parent[1]:
678 cjk = document.language in cjk_langs
679 elif document.body[ql].split()[1] in cjk_langs:
681 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
688 replace = [u"\u300E"]
690 replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
694 replace = [u"\u300F"]
696 replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
702 replace = [u"\u300C"]
704 replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
708 replace = [u"\u300D"]
710 replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
712 document.body[k:l+1] = replace
718 k = find_token(document.body, '\\begin_inset Quotes k', i)
721 l = find_end_of_inset(document.body, k)
723 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
727 parent = get_containing_layout(document.body, k)
728 ql = find_token_backwards(document.body, "\\lang", k)
729 if ql == -1 or ql < parent[1]:
730 cjk = document.language in cjk_langs
731 elif document.body[ql].split()[1] in cjk_langs:
733 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
740 replace = [u"\u3008"]
742 replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
746 replace = [u"\u3009"]
748 replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
754 replace = [u"\u300A"]
756 replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
760 replace = [u"\u300B"]
762 replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
764 document.body[k:l+1] = replace
768 def convert_crimson(document):
769 """Transform preamble code to native font setting."""
771 i = find_substring(document.preamble, "{cochineal}")
774 # Find and delete user-preamble code:
775 if document.preamble[i] == "\\usepackage[proportional,osf]{cochineal}":
777 elif document.preamble[i] == "\\usepackage{cochineal}":
781 del document.preamble[i]
782 if i and document.preamble[i-1] == "% Added by lyx2lyx":
783 del document.preamble[i-1]
785 # Convert to native font setting:
786 j = find_token(document.header, '\\font_roman')
788 romanfont = ['\font_roman', '"cochineal"', '"default"']
790 romanfont = document.header[j].split()
791 romanfont[1] = '"cochineal"'
792 document.header[j] = " ".join(romanfont)
794 set_bool_value(document.header, '\\font_osf', osf)
795 except ValueError: # no \\font_osf setting in document.header
797 document.header.insert(-1, "\\font_osf true")
800 def revert_crimson(document):
801 " Revert native Cochineal/Crimson font definition to LaTeX "
803 i = find_token(document.header, '\\font_roman "cochineal"')
806 # replace unsupported font setting
807 document.header[i] = document.header[i].replace("cochineal", "default")
808 # no need for preamble code with system fonts
809 if get_bool_value(document.header, "\\use_non_tex_fonts"):
811 # transfer old style figures setting to package options
812 j = find_token(document.header, "\\font_osf true")
814 options = "[proportional,osf]"
815 document.header[j] = "\\font_osf false"
818 add_to_preamble(document, ["\\usepackage%s{cochineal}"%options])
821 def revert_cochinealmath(document):
822 " Revert cochineal newtxmath definitions to LaTeX "
824 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
825 i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
827 add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
828 document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
831 def revert_labelonly(document):
832 " Revert labelonly tag for InsetRef "
835 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
838 j = find_end_of_inset(document.body, i)
840 document.warning("Can't find end of reference inset at line %d!!" %(i))
843 k = find_token(document.body, "LatexCommand labelonly", i, j)
847 label = get_quoted_value(document.body, "reference", i, j)
849 document.warning("Can't find label for reference at line %d!" %(i))
852 document.body[i:j+1] = put_cmd_in_ert([label])
856 def revert_plural_refs(document):
857 " Revert plural and capitalized references "
858 i = find_token(document.header, "\\use_refstyle 1", 0)
859 use_refstyle = (i != 0)
863 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
866 j = find_end_of_inset(document.body, i)
868 document.warning("Can't find end of reference inset at line %d!!" %(i))
872 plural = caps = suffix = False
873 k = find_token(document.body, "LaTeXCommand formatted", i, j)
874 if k != -1 and use_refstyle:
875 plural = get_bool_value(document.body, "plural", i, j, False)
876 caps = get_bool_value(document.body, "caps", i, j, False)
877 label = get_quoted_value(document.body, "reference", i, j)
880 (prefix, suffix) = label.split(":", 1)
882 document.warning("No `:' separator in formatted reference at line %d!" % (i))
884 document.warning("Can't find label for reference at line %d!" % (i))
886 # this effectively tests also for use_refstyle and a formatted reference
887 # we do this complicated test because we would otherwise do this erasure
888 # over and over and over
889 if not ((plural or caps) and suffix):
890 del_token(document.body, "plural", i, j)
891 del_token(document.body, "caps", i, j - 1) # since we deleted a line
896 prefix = prefix[0].title() + prefix[1:]
897 cmd = "\\" + prefix + "ref"
900 cmd += "{" + suffix + "}"
901 document.body[i:j+1] = put_cmd_in_ert([cmd])
905 def revert_noprefix(document):
906 " Revert labelonly tags with 'noprefix' set "
909 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
912 j = find_end_of_inset(document.body, i)
914 document.warning("Can't find end of reference inset at line %d!!" %(i))
917 k = find_token(document.body, "LatexCommand labelonly", i, j)
920 noprefix = get_bool_value(document.body, "noprefix", i, j)
922 # either it was not a labelonly command, or else noprefix was not set.
923 # in that case, we just delete the option.
924 del_token(document.body, "noprefix", i, j)
927 label = get_quoted_value(document.body, "reference", i, j)
929 document.warning("Can't find label for reference at line %d!" %(i))
933 (prefix, suffix) = label.split(":", 1)
935 document.warning("No `:' separator in formatted reference at line %d!" % (i))
936 # we'll leave this as an ordinary labelonly reference
937 del_token(document.body, "noprefix", i, j)
940 document.body[i:j+1] = put_cmd_in_ert([suffix])
944 def revert_biblatex(document):
945 " Revert biblatex support "
953 i = find_token(document.header, "\\cite_engine", 0)
955 document.warning("Malformed document! Missing \\cite_engine")
957 engine = get_value(document.header, "\\cite_engine", i)
959 # 2. Store biblatex state and revert to natbib
961 if engine in ["biblatex", "biblatex-natbib"]:
963 document.header[i] = "\\cite_engine natbib"
965 # 3. Store and remove new document headers
967 i = find_token(document.header, "\\biblatex_bibstyle", 0)
969 bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
970 del document.header[i]
973 i = find_token(document.header, "\\biblatex_citestyle", 0)
975 citestyle = get_value(document.header, "\\biblatex_citestyle", i)
976 del document.header[i]
979 i = find_token(document.header, "\\biblio_options", 0)
981 biblio_options = get_value(document.header, "\\biblio_options", i)
982 del document.header[i]
985 bbxopts = "[natbib=true"
987 bbxopts += ",bibstyle=" + bibstyle
989 bbxopts += ",citestyle=" + citestyle
990 if biblio_options != "":
991 bbxopts += "," + biblio_options
993 add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1003 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1006 j = find_end_of_inset(document.body, i)
1008 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1011 bibs = get_quoted_value(document.body, "bibfiles", i, j)
1012 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1015 bibresources += bibs.split(",")
1017 document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1018 # remove biblatexopts line
1019 k = find_token(document.body, "biblatexopts", i, j)
1021 del document.body[k]
1022 # Re-find inset end line
1023 j = find_end_of_inset(document.body, i)
1024 # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1026 pcmd = "printbibliography"
1028 pcmd += "[" + opts + "]"
1029 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1030 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1031 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1032 "status open", "", "\\begin_layout Plain Layout" ]
1033 repl += document.body[i:j+1]
1034 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1035 document.body[i:j+1] = repl
1041 for b in bibresources:
1042 add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1044 # 2. Citation insets
1046 # Specific citation insets used in biblatex that need to be reverted to ERT
1049 "citebyear" : "citeyear",
1050 "citeyear" : "cite*",
1051 "Footcite" : "Smartcite",
1052 "footcite" : "smartcite",
1053 "Autocite" : "Autocite",
1054 "autocite" : "autocite",
1055 "citetitle" : "citetitle",
1056 "citetitle*" : "citetitle*",
1057 "fullcite" : "fullcite",
1058 "footfullcite" : "footfullcite",
1059 "supercite" : "supercite",
1060 "citeauthor" : "citeauthor",
1061 "citeauthor*" : "citeauthor*",
1062 "Citeauthor" : "Citeauthor",
1063 "Citeauthor*" : "Citeauthor*"
1066 # All commands accepted by LyX < 2.3. Everything else throws an error.
1067 old_citations = ["cite", "nocite", "citet", "citep", "citealt", "citealp",
1068 "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",
1069 "citealt*", "citealp*", "citeauthor*", "Citet", "Citep",
1070 "Citealt", "Citealp", "Citeauthor", "Citet*", "Citep*",
1071 "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",
1072 "footcitet", "footcitep", "footcitealt", "footcitealp",
1073 "footciteauthor", "footciteyear", "footciteyearpar",
1074 "citefield", "citetitle", "cite*" ]
1078 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1081 j = find_end_of_inset(document.body, i)
1083 document.warning("Can't find end of citation inset at line %d!!" %(i))
1086 k = find_token(document.body, "LatexCommand", i, j)
1088 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1091 cmd = get_value(document.body, "LatexCommand", k)
1092 if biblatex and cmd in list(new_citations.keys()):
1093 pre = get_quoted_value(document.body, "before", i, j)
1094 post = get_quoted_value(document.body, "after", i, j)
1095 key = get_quoted_value(document.body, "key", i, j)
1097 document.warning("Citation inset at line %d does not have a key!" %(i))
1099 # Replace known new commands with ERT
1100 res = "\\" + new_citations[cmd]
1102 res += "[" + pre + "]"
1104 res += "[" + post + "]"
1107 res += "{" + key + "}"
1108 document.body[i:j+1] = put_cmd_in_ert([res])
1109 elif cmd not in old_citations:
1110 # Reset unknown commands to cite. This is what LyX does as well
1111 # (but LyX 2.2 would break on unknown commands)
1112 document.body[k] = "LatexCommand cite"
1113 document.warning("Reset unknown cite command '%s' with cite" % cmd)
1116 # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1118 biblatex_emulation = [
1119 "### Inserted by lyx2lyx (biblatex emulation) ###",
1120 "Provides natbib 1",
1121 "### End of insertion by lyx2lyx (biblatex emulation) ###"
1123 document.append_local_layout(biblatex_emulation)
1126 def revert_citekeyonly(document):
1127 " Revert keyonly cite command to ERT "
1131 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1134 j = find_end_of_inset(document.body, i)
1136 document.warning("Can't find end of citation inset at line %d!!" %(i))
1139 k = find_token(document.body, "LatexCommand", i, j)
1141 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1144 cmd = get_value(document.body, "LatexCommand", k)
1145 if cmd != "keyonly":
1149 key = get_quoted_value(document.body, "key", i, j)
1151 document.warning("Citation inset at line %d does not have a key!" %(i))
1152 # Replace known new commands with ERT
1153 document.body[i:j+1] = put_cmd_in_ert([key])
1158 def revert_bibpackopts(document):
1159 " Revert support for natbib/jurabib package options "
1162 i = find_token(document.header, "\\cite_engine", 0)
1164 document.warning("Malformed document! Missing \\cite_engine")
1166 engine = get_value(document.header, "\\cite_engine", i)
1169 if engine not in ["natbib", "jurabib"]:
1172 i = find_token(document.header, "\\biblio_options", 0)
1174 # Nothing to do if we have no options
1177 biblio_options = get_value(document.header, "\\biblio_options", i)
1178 del document.header[i]
1180 if not biblio_options:
1181 # Nothing to do for empty options
1184 bibliography_package_options = [
1185 "### Inserted by lyx2lyx (bibliography package options) ###",
1186 "PackageOptions " + engine + " " + biblio_options,
1187 "### End of insertion by lyx2lyx (bibliography package options) ###"
1189 document.append_local_layout(bibliography_package_options)
1191 def revert_qualicites(document):
1192 " Revert qualified citation list commands to ERT "
1194 # Citation insets that support qualified lists, with their LaTeX code
1198 "citet" : "textcites",
1199 "Citet" : "Textcites",
1200 "citep" : "parencites",
1201 "Citep" : "Parencites",
1202 "Footcite" : "Smartcites",
1203 "footcite" : "smartcites",
1204 "Autocite" : "Autocites",
1205 "autocite" : "autocites",
1210 i = find_token(document.header, "\\cite_engine", 0)
1212 document.warning("Malformed document! Missing \\cite_engine")
1214 engine = get_value(document.header, "\\cite_engine", i)
1216 biblatex = engine in ["biblatex", "biblatex-natbib"]
1220 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1223 j = find_end_of_inset(document.body, i)
1225 document.warning("Can't find end of citation inset at line %d!!" %(i))
1228 pres = find_token(document.body, "pretextlist", i, j)
1229 posts = find_token(document.body, "posttextlist", i, j)
1230 if pres == -1 and posts == -1:
1234 pretexts = get_quoted_value(document.body, "pretextlist", pres)
1235 posttexts = get_quoted_value(document.body, "posttextlist", posts)
1236 k = find_token(document.body, "LatexCommand", i, j)
1238 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1241 cmd = get_value(document.body, "LatexCommand", k)
1242 if biblatex and cmd in list(ql_citations.keys()):
1243 pre = get_quoted_value(document.body, "before", i, j)
1244 post = get_quoted_value(document.body, "after", i, j)
1245 key = get_quoted_value(document.body, "key", i, j)
1247 document.warning("Citation inset at line %d does not have a key!" %(i))
1249 keys = key.split(",")
1250 prelist = pretexts.split("\t")
1253 ppp = pp.split(" ", 1)
1254 premap[ppp[0]] = ppp[1]
1255 postlist = posttexts.split("\t")
1258 ppp = pp.split(" ", 1)
1259 postmap[ppp[0]] = ppp[1]
1260 # Replace known new commands with ERT
1261 if "(" in pre or ")" in pre:
1262 pre = "{" + pre + "}"
1263 if "(" in post or ")" in post:
1264 post = "{" + post + "}"
1265 res = "\\" + ql_citations[cmd]
1267 res += "(" + pre + ")"
1269 res += "(" + post + ")"
1273 if premap.get(kk, "") != "":
1274 res += "[" + premap[kk] + "]"
1275 if postmap.get(kk, "") != "":
1276 res += "[" + postmap[kk] + "]"
1277 elif premap.get(kk, "") != "":
1279 res += "{" + kk + "}"
1280 document.body[i:j+1] = put_cmd_in_ert([res])
1282 # just remove the params
1283 del document.body[posttexts]
1284 del document.body[pretexts]
1288 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1289 def convert_literalparam(document):
1290 " Add param literal "
1292 pos = len("\\begin_inset CommandInset ")
1295 i = find_token(document.body, '\\begin_inset CommandInset', i)
1298 inset = document.body[i][pos:].strip()
1299 if not inset in command_insets:
1302 j = find_end_of_inset(document.body, i)
1304 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1307 while i < j and document.body[i].strip() != '':
1309 # href is already fully latexified. Here we can switch off literal.
1311 document.body.insert(i, 'literal "false"')
1313 document.body.insert(i, 'literal "true"')
1317 def revert_literalparam(document):
1318 " Remove param literal "
1320 for inset in command_insets:
1323 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i+1)
1326 j = find_end_of_inset(document.body, i)
1328 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1330 del_token(document.body, 'literal', i, j)
1333 def revert_multibib(document):
1334 " Revert multibib support "
1336 # 1. Get cite engine
1338 i = find_token(document.header, "\\cite_engine", 0)
1340 document.warning("Malformed document! Missing \\cite_engine")
1342 engine = get_value(document.header, "\\cite_engine", i)
1344 # 2. Do we use biblatex?
1346 if engine in ["biblatex", "biblatex-natbib"]:
1349 # 3. Store and remove multibib document header
1351 i = find_token(document.header, "\\multibib", 0)
1353 multibib = get_value(document.header, "\\multibib", i)
1354 del document.header[i]
1359 # 4. The easy part: Biblatex
1361 i = find_token(document.header, "\\biblio_options", 0)
1363 k = find_token(document.header, "\\use_bibtopic", 0)
1365 # this should not happen
1366 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1368 document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1370 biblio_options = get_value(document.header, "\\biblio_options", i)
1372 biblio_options += ","
1373 biblio_options += "refsection=" + multibib
1374 document.header[i] = "\\biblio_options " + biblio_options
1379 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1382 j = find_end_of_inset(document.body, i)
1384 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1387 btprint = get_quoted_value(document.body, "btprint", i, j)
1388 if btprint != "bibbysection":
1391 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1392 # change btprint line
1393 k = find_token(document.body, "btprint", i, j)
1395 document.body[k] = "btprint \"btPrintCited\""
1396 # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1397 pcmd = "bibbysection"
1399 pcmd += "[" + opts + "]"
1400 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1401 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1402 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1403 "status open", "", "\\begin_layout Plain Layout" ]
1404 repl += document.body[i:j+1]
1405 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1406 document.body[i:j+1] = repl
1412 # 5. More tricky: Bibtex/Bibtopic
1413 k = find_token(document.header, "\\use_bibtopic", 0)
1415 # this should not happen
1416 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1418 document.header[k] = "\\use_bibtopic true"
1420 # Possible units. This assumes that the LyX name follows the std,
1421 # which might not always be the case. But it's as good as we can get.
1424 "chapter" : "Chapter",
1425 "section" : "Section",
1426 "subsection" : "Subsection",
1429 if multibib not in units.keys():
1430 document.warning("Unknown multibib value `%s'!" % nultibib)
1432 unit = units[multibib]
1436 i = find_token(document.body, "\\begin_layout " + unit, i)
1440 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1441 "\\begin_inset ERT", "status open", "",
1442 "\\begin_layout Plain Layout", "", "",
1444 "end{btUnit}", "\\end_layout",
1445 "\\begin_layout Plain Layout", "",
1448 "\\end_layout", "", "\\end_inset", "", "",
1452 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1453 "\\begin_inset ERT", "status open", "",
1454 "\\begin_layout Plain Layout", "", "",
1457 "\\end_layout", "", "\\end_inset", "", "",
1464 i = find_token(document.body, "\\end_body", i)
1465 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1466 "\\begin_inset ERT", "status open", "",
1467 "\\begin_layout Plain Layout", "", "",
1470 "\\end_layout", "", "\\end_inset", "", "",
1474 def revert_chapterbib(document):
1475 " Revert chapterbib support "
1477 # 1. Get cite engine
1479 i = find_token(document.header, "\\cite_engine", 0)
1481 document.warning("Malformed document! Missing \\cite_engine")
1483 engine = get_value(document.header, "\\cite_engine", i)
1485 # 2. Do we use biblatex?
1487 if engine in ["biblatex", "biblatex-natbib"]:
1490 # 3. Store multibib document header value
1492 i = find_token(document.header, "\\multibib", 0)
1494 multibib = get_value(document.header, "\\multibib", i)
1496 if not multibib or multibib != "child":
1500 # 4. remove multibib header
1501 del document.header[i]
1505 # find include insets
1508 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1511 j = find_end_of_inset(document.body, i)
1513 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1516 parent = get_containing_layout(document.body, i)
1519 # Insert ERT \\newrefsection before inset
1520 beg = ["\\begin_layout Standard",
1521 "\\begin_inset ERT", "status open", "",
1522 "\\begin_layout Plain Layout", "", "",
1525 "\\end_layout", "", "\\end_inset", "", "",
1527 document.body[parbeg-1:parbeg-1] = beg
1532 # 6. Bibtex/Bibtopic
1533 i = find_token(document.header, "\\use_bibtopic", 0)
1535 # this should not happen
1536 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1538 if get_value(document.header, "\\use_bibtopic", i) == "true":
1539 # find include insets
1542 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1545 j = find_end_of_inset(document.body, i)
1547 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1550 parent = get_containing_layout(document.body, i)
1554 # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1555 beg = ["\\begin_layout Standard",
1556 "\\begin_inset ERT", "status open", "",
1557 "\\begin_layout Plain Layout", "", "",
1560 "\\end_layout", "", "\\end_inset", "", "",
1562 end = ["\\begin_layout Standard",
1563 "\\begin_inset ERT", "status open", "",
1564 "\\begin_layout Plain Layout", "", "",
1567 "\\end_layout", "", "\\end_inset", "", "",
1569 document.body[parend+1:parend+1] = end
1570 document.body[parbeg-1:parbeg-1] = beg
1571 j += len(beg) + len(end)
1575 # 7. Chapterbib proper
1576 add_to_preamble(document, ["\\usepackage{chapterbib}"])
1579 def convert_dashligatures(document):
1580 """Set 'use_dash_ligatures' according to content.
1582 # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
1583 # set use_dash_ligatures to True if found, to None else.
1584 use_dash_ligatures = del_complete_lines(document.preamble,
1585 ['% Added by lyx2lyx',
1586 r'\renewcommand{\textendash}{--}',
1587 r'\renewcommand{\textemdash}{---}']) or None
1589 if use_dash_ligatures is None:
1590 # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
1591 # or "\threehyphens\n" as interim representation for -- an ---.)
1592 lines = document.body
1593 has_literal_dashes = has_ligature_dashes = False
1594 dash_pattern = re.compile(u".*[\u2013\u2014]|\\twohyphens|\\threehyphens")
1597 # skip lines without dashes:
1598 i = find_re(lines, dash_pattern, i+1)
1602 # skip label width string (see bug 10243):
1603 if line.startswith("\\labelwidthstring"):
1605 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1607 inset_type, start, end = get_containing_inset(lines, i)
1608 except TypeError: # no containing inset
1609 inset_type, start, end = "no inset", -1, -1
1610 if (inset_type.split()[0] in
1611 ["CommandInset", "ERT", "External", "Formula",
1612 "FormulaMacro", "Graphics", "IPA", "listings"]
1613 or inset_type == "Flex Code"):
1617 layoutname, start, end, j = get_containing_layout(lines, i)
1618 except TypeError: # no (or malformed) containing layout
1619 document.warning("Malformed LyX document: "
1620 "Can't find layout at line %d" % i)
1623 document.warning("Malformed LyX document: "
1624 "Missing layout name on line %d"%start)
1625 if layoutname == "LyX-Code":
1629 # literal dash followed by a non-white-character or no-break space:
1630 if re.search(u"[\u2013\u2014]([\\S\u00A0\u202F\u2060]|$)",
1631 line, flags=re.UNICODE):
1632 has_literal_dashes = True
1633 # ligature dash followed by non-white-char or no-break space on next line:
1634 if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
1635 re.match(u"[\\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
1636 has_ligature_dashes = True
1637 if has_literal_dashes and has_ligature_dashes:
1638 # TODO: insert a warning note in the document?
1639 document.warning('This document contained both literal and '
1640 '"ligature" dashes.\n Line breaks may have changed. '
1641 'See UserGuide chapter 3.9.1 for details.')
1644 if has_literal_dashes and not has_ligature_dashes:
1645 use_dash_ligatures = False
1646 elif has_ligature_dashes and not has_literal_dashes:
1647 use_dash_ligatures = True
1649 # insert the setting if there is a preferred value
1650 if use_dash_ligatures is True:
1651 document.header.insert(-1, "\\use_dash_ligatures true")
1652 elif use_dash_ligatures is False:
1653 document.header.insert(-1, "\\use_dash_ligatures false")
1656 def revert_dashligatures(document):
1657 """Remove font ligature settings for en- and em-dashes.
1658 Revert conversion of \twodashes or \threedashes to literal dashes.
1660 use_dash_ligatures = del_value(document.header, "\\use_dash_ligatures")
1661 if use_dash_ligatures != "true" or document.backend != "latex":
1664 dash_pattern = re.compile(u".*[\u2013\u2014]")
1666 # skip lines without dashes:
1667 i = find_re(document.body, dash_pattern, i+1)
1670 line = document.body[i]
1671 # skip label width string (see bug 10243):
1672 if line.startswith("\\labelwidthstring"):
1674 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1676 inset_type, start, end = get_containing_inset(document.body, i)
1677 except TypeError: # no containing inset
1678 inset_type, start, end = "no inset", -1, -1
1679 if (inset_type.split()[0] in
1680 ["CommandInset", "ERT", "External", "Formula",
1681 "FormulaMacro", "Graphics", "IPA", "listings"]
1682 or inset_type == "Flex Code"):
1686 layoutname, start, end, j = get_containing_layout(document.body, i)
1687 except TypeError: # no (or malformed) containing layout
1688 document.warning("Malformed LyX document: "
1689 "Can't find layout at body line %d" % i)
1691 if layoutname == "LyX-Code":
1694 # TODO: skip replacement in typewriter fonts
1695 line = line.replace(u'\u2013', '\\twohyphens\n')
1696 line = line.replace(u'\u2014', '\\threehyphens\n')
1697 document.body[i:i+1] = line.split('\n')
1698 # redefine the dash LICRs to use ligature dashes:
1699 add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1700 r'\renewcommand{\textemdash}{---}'])
1703 def revert_noto(document):
1704 " Revert Noto font definitions to LaTeX "
1706 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1708 i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1710 add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1711 document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1712 i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1714 add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1715 document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1716 i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1718 add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1719 document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1722 def revert_xout(document):
1723 " Reverts \\xout font attribute "
1724 changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1726 insert_to_preamble(document, \
1727 ['% for proper cross-out',
1728 '\\PassOptionsToPackage{normalem}{ulem}',
1729 '\\usepackage{ulem}'])
1732 def convert_mathindent(document):
1733 """Add the \\is_math_indent tag.
1735 k = find_token(document.header, "\\quotes_style") # where to insert
1736 # check if the document uses the class option "fleqn"
1737 options = get_value(document.header, "\\options")
1738 if 'fleqn' in options:
1739 document.header.insert(k, "\\is_math_indent 1")
1740 # delete the fleqn option
1741 i = find_token(document.header, "\\options")
1742 options = [option for option in options.split(",")
1743 if option.strip() != "fleqn"]
1745 document.header[i] = "\\options " + ",".join(options)
1747 del document.header[i]
1749 document.header.insert(k, "\\is_math_indent 0")
1751 def revert_mathindent(document):
1752 " Define mathindent if set in the document "
1753 # emulate and delete \math_indentation
1754 value = get_value(document.header, "\\math_indentation",
1755 default="default", delete=True)
1756 if value != "default":
1757 add_to_preamble(document, [r"\setlength{\mathindent}{%s}"%value])
1758 # delete \is_math_indent and emulate via document class option
1759 if not get_bool_value(document.header, "\\is_math_indent", delete=True):
1761 i = find_token(document.header, "\\options")
1763 document.header[i] = document.header[i].replace("\\options ",
1766 l = find_token(document.header, "\\use_default_options")
1767 document.header.insert(l, "\\options fleqn")
1770 def revert_baselineskip(document):
1771 " Revert baselineskips to TeX code "
1774 i = find_substring(document.body, "baselineskip%", i+1)
1777 if document.body[i].startswith("\\begin_inset VSpace"):
1778 # output VSpace inset as TeX code
1779 end = find_end_of_inset(document.body, i)
1781 document.warning("Malformed LyX document: "
1782 "Can't find end of VSpace inset at line %d." % i)
1784 # read out the value
1785 baselineskip = document.body[i].split()[-1]
1786 # check if it is the starred version
1787 star = '*' if '*' in document.body[i] else ''
1788 # now output TeX code
1789 cmd = "\\vspace%s{%s}" %(star, latex_length(baselineskip)[1])
1790 document.body[i:end+1] = put_cmd_in_ert(cmd)
1793 begin, end = is_in_inset(document.body, i, "\\begin_inset space \\hspace")
1795 # output space inset as TeX code
1796 baselineskip = document.body[i].split()[-1]
1797 star = '*' if '*' in document.body[i-1] else ''
1798 cmd = "\\hspace%s{%s}" %(star, latex_length(baselineskip)[1])
1799 document.body[begin:end+1] = put_cmd_in_ert(cmd)
1802 def revert_rotfloat(document):
1803 " Revert placement options for rotated floats "
1808 i = find_token(document.body, "sideways true", i)
1811 if not document.body[i-2].startswith('placement '):
1814 # we found a sideways float with placement options
1815 # at first store the placement
1816 beg = document.body[i-2].rfind(" ");
1817 placement = document.body[i-2][beg+1:]
1818 # check if the option'H' is used
1819 if placement.find("H") != -1:
1820 add_to_preamble(document, ["\\usepackage{float}"])
1821 # now check if it is a starred type
1822 if document.body[i-1].find("wide true") != -1:
1826 # store the float type
1827 beg = document.body[i-3].rfind(" ");
1828 fType = document.body[i-3][beg+1:]
1829 # now output TeX code
1830 endInset = find_end_of_inset(document.body, i-3)
1832 document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
1835 document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
1836 document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
1837 add_to_preamble(document, ["\\usepackage{rotfloat}"])
1842 allowbreak_emulation = [r"\begin_inset space \hspace{}",
1847 def convert_allowbreak(document):
1848 r" Zero widths Space-inset -> \SpecialChar allowbreak. "
1849 lines = document.body
1850 i = find_complete_lines(lines, allowbreak_emulation, 2)
1852 lines[i-1:i+4] = [lines[i-1] + r"\SpecialChar allowbreak"]
1853 i = find_complete_lines(lines, allowbreak_emulation, i+3)
1856 def revert_allowbreak(document):
1857 r" \SpecialChar allowbreak -> Zero widths Space-inset. "
1859 lines = document.body
1860 while i < len(lines):
1861 if lines[i].endswith(r"\SpecialChar allowbreak"):
1862 lines[i:i+1] = [lines[i].replace(r"\SpecialChar allowbreak", "")
1863 ] + allowbreak_emulation
1869 def convert_mathnumberpos(document):
1870 " add the \\math_number_before tag "
1871 i = find_token(document.header, "\\quotes_style")
1872 # check if the document uses the class option "leqno"
1873 if is_document_option(document, "leqno"):
1874 remove_document_option(document, "leqno")
1875 document.header.insert(i, "\\math_number_before 1")
1877 document.header.insert(i, "\\math_number_before 0")
1880 def revert_mathnumberpos(document):
1881 """Remove \\math_number_before tag,
1882 add the document class option leqno if required.
1884 math_number_before = get_bool_value(document.header, '\\math_number_before',
1886 if math_number_before:
1887 insert_document_option(document, "leqno")
1890 def convert_mathnumberingname(document):
1891 " rename the \\math_number_before tag to \\math_numbering_side "
1892 i = find_token(document.header, "\\math_number_before")
1893 math_number_before = get_bool_value(document.header, '\\math_number_before', i)
1894 if math_number_before:
1895 document.header[i] = "\\math_numbering_side left"
1897 # check if the document uses the class option "reqno"
1898 k = find_token(document.header, "\\options")
1899 if 'reqno' in document.header[k]:
1900 document.header[i] = "\\math_numbering_side right"
1901 # delete the found option
1902 document.header[k] = document.header[k].replace(",reqno", "")
1903 document.header[k] = document.header[k].replace(", reqno", "")
1904 document.header[k] = document.header[k].replace("reqno,", "")
1905 if 'reqno' in document.header[k]:
1906 # then we have reqno as the only option
1907 del document.header[k]
1909 document.header[i] = "\\math_numbering_side default"
1912 def revert_mathnumberingname(document):
1913 " rename the \\math_numbering_side tag back to \\math_number_before "
1914 i = find_token(document.header, "\\math_numbering_side")
1915 math_numbering_side = get_value(document.header, '\\math_numbering_side', i)
1916 # rename tag and set boolean value:
1917 if math_numbering_side == "left":
1918 document.header[i] = "\\math_number_before 1"
1919 elif math_numbering_side == "right":
1920 # also add the option reqno:
1921 document.header[i] = "\\math_number_before 0"
1922 k = find_token(document.header, "\\options")
1923 if k != -1 and 'reqno' not in document.header[k]:
1924 document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
1926 l = find_token(document.header, "\\use_default_options", 0)
1927 document.header.insert(l, "\\options reqno")
1929 document.header[i] = "\\math_number_before 0"
1932 def convert_minted(document):
1933 " add the \\use_minted tag "
1934 i = find_token(document.header, "\\index ")
1935 document.header.insert(i, "\\use_minted 0")
1938 def revert_minted(document):
1939 " remove the \\use_minted tag "
1940 del_token(document.header, "\\use_minted")
1943 def revert_longtable_lscape(document):
1944 " revert the longtable landcape mode to ERT "
1946 regexp = re.compile(r'^<features rotate=\"90\"\s.*islongtable=\"true\"\s.*$', re.IGNORECASE)
1948 i = find_re(document.body, regexp, i)
1952 document.body[i] = document.body[i].replace(" rotate=\"90\"", "")
1953 lay = get_containing_layout(document.body, i)
1955 document.warning("Longtable has not layout!")
1958 begcmd = put_cmd_in_ert("\\begin{landscape}")
1959 endcmd = put_cmd_in_ert("\\end{landscape}")
1960 document.body[lay[2] : lay[2]] = endcmd + ["\\end_layout"]
1961 document.body[lay[1] : lay[1]] = ["\\begin_layout " + lay[0], ""] + begcmd
1963 add_to_preamble(document, ["\\usepackage{pdflscape}"])
1971 supported_versions = ["2.3.0", "2.3"]
1973 [509, [convert_microtype]],
1974 [510, [convert_dateinset]],
1975 [511, [convert_ibranches]],
1976 [512, [convert_beamer_article_styles]],
1980 [516, [convert_inputenc]],
1982 [518, [convert_iopart]],
1983 [519, [convert_quotestyle]],
1985 [521, [convert_frenchquotes]],
1988 [524, [convert_crimson]],
1996 [532, [convert_literalparam]],
1999 [535, [convert_dashligatures]],
2002 [538, [convert_mathindent]],
2005 [541, [convert_allowbreak]],
2006 [542, [convert_mathnumberpos]],
2007 [543, [convert_mathnumberingname]],
2008 [544, [convert_minted]]
2012 [543, [revert_minted, revert_longtable_lscape]],
2013 [542, [revert_mathnumberingname]],
2014 [541, [revert_mathnumberpos]],
2015 [540, [revert_allowbreak]],
2016 [539, [revert_rotfloat]],
2017 [538, [revert_baselineskip]],
2018 [537, [revert_mathindent]],
2019 [536, [revert_xout]],
2020 [535, [revert_noto]],
2021 [534, [revert_dashligatures]],
2022 [533, [revert_chapterbib]],
2023 [532, [revert_multibib]],
2024 [531, [revert_literalparam]],
2025 [530, [revert_qualicites]],
2026 [529, [revert_bibpackopts]],
2027 [528, [revert_citekeyonly]],
2028 [527, [revert_biblatex]],
2029 [526, [revert_noprefix]],
2030 [525, [revert_plural_refs]],
2031 [524, [revert_labelonly]],
2032 [523, [revert_crimson, revert_cochinealmath]],
2033 [522, [revert_cjkquotes]],
2034 [521, [revert_dynamicquotes]],
2035 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2036 [519, [revert_plainquote]],
2037 [518, [revert_quotestyle]],
2038 [517, [revert_iopart]],
2039 [516, [revert_quotes]],
2041 [514, [revert_urdu, revert_syriac]],
2042 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2043 [512, [revert_new_babel_languages]],
2044 [511, [revert_beamer_article_styles]],
2045 [510, [revert_ibranches]],
2047 [508, [revert_microtype]]
2051 if __name__ == "__main__":