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, revert_language)
37 ####################################################################
38 # Private helper functions
42 ###############################################################################
44 ### Conversion and reversion routines
46 ###############################################################################
48 def convert_microtype(document):
49 " Add microtype settings. "
50 i = find_token(document.header, "\\font_tt_scale")
51 j = find_token(document.preamble, "\\usepackage{microtype}")
53 document.header.insert(i + 1, "\\use_microtype false")
55 document.header.insert(i + 1, "\\use_microtype true")
56 del document.preamble[j]
57 if j and document.preamble[j-1] == "% Added by lyx2lyx":
58 del document.preamble[j-1]
61 def revert_microtype(document):
62 " Remove microtype settings. "
63 use_microtype = get_bool_value(document.header, "\\use_microtype", delete=True)
65 add_to_preamble(document, ["\\usepackage{microtype}"])
68 def convert_dateinset(document):
69 ' Convert date external inset to ERT '
72 i = find_token(document.body, "\\begin_inset External", i+1)
75 j = find_end_of_inset(document.body, i)
77 document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
79 if get_value(document.body, 'template', i, j) == "Date":
80 document.body[i : j + 1] = put_cmd_in_ert("\\today ")
84 def convert_inputenc(document):
85 """Replace no longer supported input encoding setting."""
86 i = find_token(document.header, "\\inputencoding pt254")
88 document.header[i] = "\\inputencoding pt154"
91 def convert_ibranches(document):
92 ' Add "inverted 0" to branch insets'
95 i = find_token(document.body, "\\begin_inset Branch", i+1)
98 document.body.insert(i + 1, "inverted 0")
101 def revert_ibranches(document):
102 ' Convert inverted branches to explicit anti-branches'
103 # Get list of branches
107 i = find_token(document.header, "\\branch", i+1)
110 branch = document.header[i][8:].strip()
111 selected = get_bool_value(document.header, "\\selected", i+1, i+2)
113 document.warning("Malformed LyX document: No selection indicator "
114 "for branch %s." % branch)
116 # the value tells us whether the branch is selected
117 ourbranches[branch] = selected
119 # Find branch insets, remove "inverted" tag and
120 # convert inverted insets to "Anti-OldBranch" insets
124 i = find_token(document.body, "\\begin_inset Branch", i+1)
127 inverted = get_bool_value(document.body, "inverted", i+1, i+2, delete=True)
129 document.warning("Malformed LyX document: Missing 'inverted' tag in branch inset.")
132 branch = document.body[i][20:].strip()
133 if not branch in antibranches:
134 antibranch = "Anti-" + branch
135 while antibranch in antibranches:
136 antibranch = "x" + antibranch
137 antibranches[branch] = antibranch
139 antibranch = antibranches[branch]
140 document.body[i] = "\\begin_inset Branch " + antibranch
142 # now we need to add the new branches to the header
143 for old, new in antibranches.items():
144 i = find_token(document.header, "\\branch " + old, 0)
146 document.warning("Can't find branch %s even though we found it before!" % (old))
148 j = find_token(document.header, "\\end_branch", i)
150 document.warning("Malformed LyX document! Can't find end of branch " + old)
152 # ourbranches[old] - 1 inverts the selection status of the old branch
153 lines = ["\\branch " + new,
154 "\\selected " + str(ourbranches[old] - 1)]
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)
226 # def convert_new_babel_languages(document)
227 # set to native support if get_value(document.header, "\\options") in
228 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
229 # and "\\language_package babel".
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)
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))
1331 k = find_token(document.body, 'literal', i, j)
1335 del document.body[k]
1338 def revert_multibib(document):
1339 " Revert multibib support "
1341 # 1. Get cite engine
1343 i = find_token(document.header, "\\cite_engine", 0)
1345 document.warning("Malformed document! Missing \\cite_engine")
1347 engine = get_value(document.header, "\\cite_engine", i)
1349 # 2. Do we use biblatex?
1351 if engine in ["biblatex", "biblatex-natbib"]:
1354 # 3. Store and remove multibib document header
1356 i = find_token(document.header, "\\multibib", 0)
1358 multibib = get_value(document.header, "\\multibib", i)
1359 del document.header[i]
1364 # 4. The easy part: Biblatex
1366 i = find_token(document.header, "\\biblio_options", 0)
1368 k = find_token(document.header, "\\use_bibtopic", 0)
1370 # this should not happen
1371 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1373 document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1375 biblio_options = get_value(document.header, "\\biblio_options", i)
1377 biblio_options += ","
1378 biblio_options += "refsection=" + multibib
1379 document.header[i] = "\\biblio_options " + biblio_options
1384 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1387 j = find_end_of_inset(document.body, i)
1389 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1392 btprint = get_quoted_value(document.body, "btprint", i, j)
1393 if btprint != "bibbysection":
1396 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1397 # change btprint line
1398 k = find_token(document.body, "btprint", i, j)
1400 document.body[k] = "btprint \"btPrintCited\""
1401 # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1402 pcmd = "bibbysection"
1404 pcmd += "[" + opts + "]"
1405 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1406 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1407 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1408 "status open", "", "\\begin_layout Plain Layout" ]
1409 repl += document.body[i:j+1]
1410 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1411 document.body[i:j+1] = repl
1417 # 5. More tricky: Bibtex/Bibtopic
1418 k = find_token(document.header, "\\use_bibtopic", 0)
1420 # this should not happen
1421 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1423 document.header[k] = "\\use_bibtopic true"
1425 # Possible units. This assumes that the LyX name follows the std,
1426 # which might not always be the case. But it's as good as we can get.
1429 "chapter" : "Chapter",
1430 "section" : "Section",
1431 "subsection" : "Subsection",
1434 if multibib not in units.keys():
1435 document.warning("Unknown multibib value `%s'!" % nultibib)
1437 unit = units[multibib]
1441 i = find_token(document.body, "\\begin_layout " + unit, i)
1445 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1446 "\\begin_inset ERT", "status open", "",
1447 "\\begin_layout Plain Layout", "", "",
1449 "end{btUnit}", "\\end_layout",
1450 "\\begin_layout Plain Layout", "",
1453 "\\end_layout", "", "\\end_inset", "", "",
1457 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1458 "\\begin_inset ERT", "status open", "",
1459 "\\begin_layout Plain Layout", "", "",
1462 "\\end_layout", "", "\\end_inset", "", "",
1469 i = find_token(document.body, "\\end_body", i)
1470 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1471 "\\begin_inset ERT", "status open", "",
1472 "\\begin_layout Plain Layout", "", "",
1475 "\\end_layout", "", "\\end_inset", "", "",
1479 def revert_chapterbib(document):
1480 " Revert chapterbib support "
1482 # 1. Get cite engine
1484 i = find_token(document.header, "\\cite_engine", 0)
1486 document.warning("Malformed document! Missing \\cite_engine")
1488 engine = get_value(document.header, "\\cite_engine", i)
1490 # 2. Do we use biblatex?
1492 if engine in ["biblatex", "biblatex-natbib"]:
1495 # 3. Store multibib document header value
1497 i = find_token(document.header, "\\multibib", 0)
1499 multibib = get_value(document.header, "\\multibib", i)
1501 if not multibib or multibib != "child":
1505 # 4. remove multibib header
1506 del document.header[i]
1510 # find include insets
1513 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1516 j = find_end_of_inset(document.body, i)
1518 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1521 parent = get_containing_layout(document.body, i)
1524 # Insert ERT \\newrefsection before inset
1525 beg = ["\\begin_layout Standard",
1526 "\\begin_inset ERT", "status open", "",
1527 "\\begin_layout Plain Layout", "", "",
1530 "\\end_layout", "", "\\end_inset", "", "",
1532 document.body[parbeg-1:parbeg-1] = beg
1537 # 6. Bibtex/Bibtopic
1538 i = find_token(document.header, "\\use_bibtopic", 0)
1540 # this should not happen
1541 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1543 if get_value(document.header, "\\use_bibtopic", i) == "true":
1544 # find include insets
1547 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1550 j = find_end_of_inset(document.body, i)
1552 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1555 parent = get_containing_layout(document.body, i)
1559 # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1560 beg = ["\\begin_layout Standard",
1561 "\\begin_inset ERT", "status open", "",
1562 "\\begin_layout Plain Layout", "", "",
1565 "\\end_layout", "", "\\end_inset", "", "",
1567 end = ["\\begin_layout Standard",
1568 "\\begin_inset ERT", "status open", "",
1569 "\\begin_layout Plain Layout", "", "",
1572 "\\end_layout", "", "\\end_inset", "", "",
1574 document.body[parend+1:parend+1] = end
1575 document.body[parbeg-1:parbeg-1] = beg
1576 j += len(beg) + len(end)
1580 # 7. Chapterbib proper
1581 add_to_preamble(document, ["\\usepackage{chapterbib}"])
1584 def convert_dashligatures(document):
1585 """Set 'use_dash_ligatures' according to content.
1587 # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
1588 # set use_dash_ligatures to True if found, to None else.
1589 use_dash_ligatures = del_complete_lines(document.preamble,
1590 ['% Added by lyx2lyx',
1591 r'\renewcommand{\textendash}{--}',
1592 r'\renewcommand{\textemdash}{---}']) or None
1594 if use_dash_ligatures is None:
1595 # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
1596 # or "\threehyphens\n" as interim representation for -- an ---.)
1597 lines = document.body
1598 has_literal_dashes = has_ligature_dashes = False
1599 dash_pattern = re.compile(u".*[\u2013\u2014]|\\twohyphens|\\threehyphens")
1602 # skip lines without dashes:
1603 i = find_re(lines, dash_pattern, i+1)
1607 # skip label width string (see bug 10243):
1608 if line.startswith("\\labelwidthstring"):
1610 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1612 inset_type, start, end = get_containing_inset(lines, i)
1613 except TypeError: # no containing inset
1614 inset_type, start, end = "no inset", -1, -1
1615 if (inset_type.split()[0] in
1616 ["CommandInset", "ERT", "External", "Formula",
1617 "FormulaMacro", "Graphics", "IPA", "listings"]
1618 or inset_type == "Flex Code"):
1622 layoutname, start, end, j = get_containing_layout(lines, i)
1623 except TypeError: # no (or malformed) containing layout
1624 document.warning("Malformed LyX document: "
1625 "Can't find layout at line %d" % i)
1628 document.warning("Malformed LyX document: "
1629 "Missing layout name on line %d"%start)
1630 if layoutname == "LyX-Code":
1634 # literal dash followed by a non-white-character or no-break space:
1635 if re.search(u"[\u2013\u2014]([\S\u00A0\u202F\u2060]|$)",
1636 line, flags=re.UNICODE):
1637 has_literal_dashes = True
1638 # ligature dash followed by non-white-char or no-break space on next line:
1639 if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
1640 re.match(u"[\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
1641 has_ligature_dashes = True
1642 if has_literal_dashes and has_ligature_dashes:
1643 # TODO: insert a warning note in the document?
1644 document.warning('This document contained both literal and '
1645 '"ligature" dashes.\n Line breaks may have changed. '
1646 'See UserGuide chapter 3.9.1 for details.')
1649 if has_literal_dashes and not has_ligature_dashes:
1650 use_dash_ligatures = False
1651 elif has_ligature_dashes and not has_literal_dashes:
1652 use_dash_ligatures = True
1654 # insert the setting if there is a preferred value
1655 if use_dash_ligatures is True:
1656 document.header.insert(-1, "\\use_dash_ligatures true")
1657 elif use_dash_ligatures is False:
1658 document.header.insert(-1, "\\use_dash_ligatures false")
1661 def revert_dashligatures(document):
1662 """Remove font ligature settings for en- and em-dashes.
1663 Revert conversion of \twodashes or \threedashes to literal dashes.
1665 use_dash_ligatures = del_value(document.header, "\\use_dash_ligatures")
1666 if use_dash_ligatures != "true" or document.backend != "latex":
1669 dash_pattern = re.compile(u".*[\u2013\u2014]")
1671 # skip lines without dashes:
1672 i = find_re(document.body, dash_pattern, i+1)
1675 line = document.body[i]
1676 # skip label width string (see bug 10243):
1677 if line.startswith("\\labelwidthstring"):
1679 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1681 inset_type, start, end = get_containing_inset(document.body, i)
1682 except TypeError: # no containing inset
1683 inset_type, start, end = "no inset", -1, -1
1684 if (inset_type.split()[0] in
1685 ["CommandInset", "ERT", "External", "Formula",
1686 "FormulaMacro", "Graphics", "IPA", "listings"]
1687 or inset_type == "Flex Code"):
1691 layoutname, start, end, j = get_containing_layout(document.body, i)
1692 except TypeError: # no (or malformed) containing layout
1693 document.warning("Malformed LyX document: "
1694 "Can't find layout at body line %d" % i)
1696 if layoutname == "LyX-Code":
1699 # TODO: skip replacement in typewriter fonts
1700 line = line.replace(u'\u2013', '\\twohyphens\n')
1701 line = line.replace(u'\u2014', '\\threehyphens\n')
1702 document.body[i:i+1] = line.split('\n')
1703 # redefine the dash LICRs to use ligature dashes:
1704 add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1705 r'\renewcommand{\textemdash}{---}'])
1708 def revert_noto(document):
1709 " Revert Noto font definitions to LaTeX "
1711 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1713 i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1715 add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1716 document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1717 i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1719 add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1720 document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1721 i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1723 add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1724 document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1727 def revert_xout(document):
1728 " Reverts \\xout font attribute "
1729 changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1731 insert_to_preamble(document, \
1732 ['% for proper cross-out',
1733 '\\PassOptionsToPackage{normalem}{ulem}',
1734 '\\usepackage{ulem}'])
1737 def convert_mathindent(document):
1738 """Add the \\is_math_indent tag.
1740 k = find_token(document.header, "\\quotes_style") # where to insert
1741 # check if the document uses the class option "fleqn"
1742 options = get_value(document.header, "\\options")
1743 if 'fleqn' in options:
1744 document.header.insert(k, "\\is_math_indent 1")
1745 # delete the fleqn option
1746 i = find_token(document.header, "\\options")
1747 options = [option for option in options.split(",")
1748 if option.strip() != "fleqn"]
1750 document.header[i] = "\\options " + ",".join(options)
1752 del document.header[i]
1754 document.header.insert(k, "\\is_math_indent 0")
1756 def revert_mathindent(document):
1757 " Define mathindent if set in the document "
1758 # emulate and delete \math_indentation
1759 value = get_value(document.header, "\\math_indentation",
1760 default="default", delete=True)
1761 if value != "default":
1762 add_to_preamble(document, [r"\setlength{\mathindent}{%s}"%value])
1763 # delete \is_math_indent and emulate via document class option
1764 if not get_bool_value(document.header, "\\is_math_indent", delete=True):
1766 i = find_token(document.header, "\\options")
1768 document.header[i] = document.header[i].replace("\\options ",
1771 l = find_token(document.header, "\\use_default_options")
1772 document.header.insert(l, "\\options fleqn")
1775 def revert_baselineskip(document):
1776 " Revert baselineskips to TeX code "
1779 i = find_substring(document.body, "baselineskip%", i+1)
1782 if document.body[i].startswith("\\begin_inset VSpace"):
1783 # output VSpace inset as TeX code
1784 end = find_end_of_inset(document.body, i)
1786 document.warning("Malformed LyX document: "
1787 "Can't find end of VSpace inset at line %d." % i)
1789 # read out the value
1790 baselineskip = document.body[i].split()[-1]
1791 # check if it is the starred version
1792 star = '*' if '*' in document.body[i] else ''
1793 # now output TeX code
1794 cmd = "\\vspace%s{%s}" %(star, latex_length(baselineskip)[1])
1795 document.body[i:end+1] = put_cmd_in_ert(cmd)
1798 begin, end = is_in_inset(document.body, i, "\\begin_inset space \\hspace")
1800 # output space inset as TeX code
1801 baselineskip = document.body[i].split()[-1]
1802 star = '*' if '*' in document.body[i-1] else ''
1803 cmd = "\\hspace%s{%s}" %(star, latex_length(baselineskip)[1])
1804 document.body[begin:end+1] = put_cmd_in_ert(cmd)
1807 def revert_rotfloat(document):
1808 " Revert placement options for rotated floats "
1813 i = find_token(document.body, "sideways true", i)
1816 if not document.body[i-2].startswith('placement '):
1819 # we found a sideways float with placement options
1820 # at first store the placement
1821 beg = document.body[i-2].rfind(" ");
1822 placement = document.body[i-2][beg+1:]
1823 # check if the option'H' is used
1824 if placement.find("H") != -1:
1825 add_to_preamble(document, ["\\usepackage{float}"])
1826 # now check if it is a starred type
1827 if document.body[i-1].find("wide true") != -1:
1831 # store the float type
1832 beg = document.body[i-3].rfind(" ");
1833 fType = document.body[i-3][beg+1:]
1834 # now output TeX code
1835 endInset = find_end_of_inset(document.body, i-3)
1837 document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
1840 document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
1841 document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
1842 add_to_preamble(document, ["\\usepackage{rotfloat}"])
1847 allowbreak_emulation = [r"\begin_inset space \hspace{}",
1852 def convert_allowbreak(document):
1853 " Zero widths Space-inset -> \SpecialChar allowbreak. "
1854 lines = document.body
1855 i = find_complete_lines(lines, allowbreak_emulation, 2)
1857 lines[i-1:i+4] = [lines[i-1] + r"\SpecialChar allowbreak"]
1858 i = find_complete_lines(lines, allowbreak_emulation, i+3)
1861 def revert_allowbreak(document):
1862 " \SpecialChar allowbreak -> Zero widths Space-inset. "
1864 lines = document.body
1865 while i < len(lines):
1866 if lines[i].endswith(r"\SpecialChar allowbreak"):
1867 lines[i:i+1] = [lines[i].replace(r"\SpecialChar allowbreak", "")
1868 ] + allowbreak_emulation
1874 def convert_mathnumberpos(document):
1875 " add the \\math_number_before tag "
1876 # check if the document uses the class option "leqno"
1877 i = find_token(document.header, "\\options")
1878 k = find_token(document.header, "\\quotes_style")
1879 if 'leqno' in document.header[i]:
1880 document.header.insert(k, "\\math_number_before 1")
1881 # delete the found option
1882 document.header[i] = document.header[i].replace(",leqno", "")
1883 document.header[i] = document.header[i].replace(", leqno", "")
1884 document.header[i] = document.header[i].replace("leqno,", "")
1885 if 'leqno' in document.header[i]:
1886 # then we have leqno as the only option
1887 del document.header[i]
1889 document.header.insert(k, "\\math_number_before 0")
1892 def revert_mathnumberpos(document):
1893 """Remove \\math_number_before tag,
1894 add the document class option leqno if required.
1896 math_number_before = get_bool_value(document.header,
1897 '\\math_number_before', delete=True)
1898 if math_number_before:
1899 i = find_token(document.header, "\\options")
1900 if i != -1 and 'leqno' not in document.header[i]:
1901 document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
1903 i = find_token(document.header, "\\use_default_options")
1904 document.header.insert(i, "\\options leqno")
1907 def convert_mathnumberingname(document):
1908 " rename the \\math_number_before tag to \\math_numbering_side "
1909 i = find_token(document.header, "\\math_number_before")
1910 math_number_before = get_bool_value(document.header, '\\math_number_before', i)
1911 if math_number_before:
1912 document.header[i] = "\\math_numbering_side left"
1914 # check if the document uses the class option "reqno"
1915 k = find_token(document.header, "\\options")
1916 if 'reqno' in document.header[k]:
1917 document.header[i] = "\\math_numbering_side right"
1918 # delete the found option
1919 document.header[k] = document.header[k].replace(",reqno", "")
1920 document.header[k] = document.header[k].replace(", reqno", "")
1921 document.header[k] = document.header[k].replace("reqno,", "")
1922 if 'reqno' in document.header[k]:
1923 # then we have reqno as the only option
1924 del document.header[k]
1926 document.header[i] = "\\math_numbering_side default"
1929 def revert_mathnumberingname(document):
1930 " rename the \\math_numbering_side tag back to \\math_number_before "
1931 i = find_token(document.header, "\\math_numbering_side")
1932 math_numbering_side = get_value(document.header, '\\math_numbering_side', i)
1933 # rename tag and set boolean value:
1934 if math_numbering_side == "left":
1935 document.header[i] = "\\math_number_before 1"
1936 elif math_numbering_side == "right":
1937 # also add the option reqno:
1938 document.header[i] = "\\math_number_before 0"
1939 k = find_token(document.header, "\\options")
1940 if k != -1 and 'reqno' not in document.header[k]:
1941 document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
1943 l = find_token(document.header, "\\use_default_options", 0)
1944 document.header.insert(l, "\\options reqno")
1946 document.header[i] = "\\math_number_before 0"
1949 def convert_minted(document):
1950 " add the \\use_minted tag "
1951 i = find_token(document.header, "\\index ")
1952 document.header.insert(i, "\\use_minted 0")
1955 def revert_minted(document):
1956 " remove the \\use_minted tag "
1957 del_token(document.header, "\\use_minted")
1960 def revert_longtable_lscape(document):
1961 " revert the longtable landcape mode to ERT "
1963 regexp = re.compile(r'^<features rotate=\"90\"\s.*islongtable=\"true\"\s.*$', re.IGNORECASE)
1965 i = find_re(document.body, regexp, i)
1969 document.body[i] = document.body[i].replace(" rotate=\"90\"", "")
1970 lay = get_containing_layout(document.body, i)
1972 document.warning("Longtable has not layout!")
1975 begcmd = put_cmd_in_ert("\\begin{landscape}")
1976 endcmd = put_cmd_in_ert("\\end{landscape}")
1977 document.body[lay[2] : lay[2]] = endcmd + ["\\end_layout"]
1978 document.body[lay[1] : lay[1]] = ["\\begin_layout " + lay[0], ""] + begcmd
1980 add_to_preamble(document, ["\\usepackage{pdflscape}"])
1988 supported_versions = ["2.3.0", "2.3"]
1990 [509, [convert_microtype]],
1991 [510, [convert_dateinset]],
1992 [511, [convert_ibranches]],
1993 [512, [convert_beamer_article_styles]],
1997 [516, [convert_inputenc]],
1999 [518, [convert_iopart]],
2000 [519, [convert_quotestyle]],
2002 [521, [convert_frenchquotes]],
2005 [524, [convert_crimson]],
2013 [532, [convert_literalparam]],
2016 [535, [convert_dashligatures]],
2019 [538, [convert_mathindent]],
2022 [541, [convert_allowbreak]],
2023 [542, [convert_mathnumberpos]],
2024 [543, [convert_mathnumberingname]],
2025 [544, [convert_minted]]
2029 [543, [revert_minted, revert_longtable_lscape]],
2030 [542, [revert_mathnumberingname]],
2031 [541, [revert_mathnumberpos]],
2032 [540, [revert_allowbreak]],
2033 [539, [revert_rotfloat]],
2034 [538, [revert_baselineskip]],
2035 [537, [revert_mathindent]],
2036 [536, [revert_xout]],
2037 [535, [revert_noto]],
2038 [534, [revert_dashligatures]],
2039 [533, [revert_chapterbib]],
2040 [532, [revert_multibib]],
2041 [531, [revert_literalparam]],
2042 [530, [revert_qualicites]],
2043 [529, [revert_bibpackopts]],
2044 [528, [revert_citekeyonly]],
2045 [527, [revert_biblatex]],
2046 [526, [revert_noprefix]],
2047 [525, [revert_plural_refs]],
2048 [524, [revert_labelonly]],
2049 [523, [revert_crimson, revert_cochinealmath]],
2050 [522, [revert_cjkquotes]],
2051 [521, [revert_dynamicquotes]],
2052 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2053 [519, [revert_plainquote]],
2054 [518, [revert_quotestyle]],
2055 [517, [revert_iopart]],
2056 [516, [revert_quotes]],
2058 [514, [revert_urdu, revert_syriac]],
2059 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2060 [512, [revert_new_babel_languages]],
2061 [511, [revert_beamer_article_styles]],
2062 [510, [revert_ibranches]],
2064 [508, [revert_microtype]]
2068 if __name__ == "__main__":