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,
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)
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 def revert_beamer_article_styles(document):
161 " Include (scr)article styles in beamer article "
163 beamer_articles = ["article-beamer", "scrarticle-beamer"]
164 if document.textclass not in beamer_articles:
167 inclusion = "article.layout"
168 if document.textclass == "scrarticle-beamer":
169 inclusion = "scrartcl.layout"
171 i = find_token(document.header, "\\begin_local_layout", 0)
173 k = find_token(document.header, "\\language", 0)
175 # this should not happen
176 document.warning("Malformed LyX document! No \\language header found!")
178 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
181 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
183 # this should not happen
184 document.warning("Malformed LyX document: Can't find end of local layout!")
187 document.header[i+1 : i+1] = [
188 "### Inserted by lyx2lyx (more [scr]article styles) ###",
189 "Input " + inclusion,
190 "Input beamer.layout",
191 "Provides geometry 0",
192 "Provides hyperref 0",
201 " \\usepackage{beamerarticle,pgf}",
202 " % this default might be overridden by plain title style",
203 " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
204 " \\AtBeginDocument{",
205 " \\let\\origtableofcontents=\\tableofcontents",
206 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
207 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
210 "### End of insertion by lyx2lyx (more [scr]article styles) ###"
214 def convert_beamer_article_styles(document):
215 " Remove included (scr)article styles in beamer article "
217 beamer_articles = ["article-beamer", "scrarticle-beamer"]
218 if document.textclass not in beamer_articles:
221 i = find_token(document.header, "\\begin_local_layout", 0)
225 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
227 # this should not happen
228 document.warning("Malformed LyX document: Can't find end of local layout!")
231 k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
233 l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
235 # this should not happen
236 document.warning("End of lyx2lyx local layout insertion not found!")
239 if k == i + 1 and l == j - 1:
240 # that was all the local layout there was
241 document.header[i : j + 1] = []
243 document.header[k : l + 1] = []
246 def revert_new_babel_languages(document):
247 """Revert "bosnian", "friulan", "macedonian", "piedmontese", "romansh".
249 Set the document language to English but use correct babel setting.
251 # TODO: currently, text parts in these languages are kept as-is
252 # and are converted to the document language by LyX 2.2 with warnings like
253 # LyX: Unknown language `romansh' [around line 273 of file lyx_2_3_test.22.lyx current token: 'romansh' context: 'InsetSpaceParams::read']
255 if document.language not in ["bosnian", "friulan", "macedonian",
256 "piedmontese", "romansh"]:
258 i = find_token(document.header, "\\language")
260 document.header[i] = "\\language english"
261 # ensure we use Babel:
262 # TODO: Polyglossia supports friulan, piedmontese, romansh
263 # but requires "\resetdefaultlanguage{...}" at begin of document.
264 j = find_token(document.header, "\\language_package default")
266 document.header[j] = "\\language_package babel"
267 k = find_token(document.header, "\\options")
269 document.header[k] = document.header[k].replace("\\options",
270 "\\options %s," % document.language)
272 l = find_token(document.header, "\\use_default_options")
273 document.header.insert(l + 1, "\\options " + document.language)
274 document.language = "english"
277 # def convert_new_babel_languages(document)
278 # set to native support if get_value(document.header, "\\options") in
279 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
280 # and "\\language_package babel".
282 def revert_amharic(document):
283 "Set the document language to English but assure Amharic output"
285 if document.language == "amharic":
286 document.language = "english"
287 i = find_token(document.header, "\\language amharic", 0)
289 document.header[i] = "\\language english"
290 j = find_token(document.header, "\\language_package default", 0)
292 document.header[j] = "\\language_package default"
293 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
294 document.body[2 : 2] = ["\\begin_layout Standard",
295 "\\begin_inset ERT", "status open", "",
296 "\\begin_layout Plain Layout", "", "",
298 "resetdefaultlanguage{amharic}",
299 "\\end_layout", "", "\\end_inset", "", "",
303 def revert_asturian(document):
304 "Set the document language to English but assure Asturian output"
306 if document.language == "asturian":
307 document.language = "english"
308 i = find_token(document.header, "\\language asturian", 0)
310 document.header[i] = "\\language english"
311 j = find_token(document.header, "\\language_package default", 0)
313 document.header[j] = "\\language_package default"
314 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
315 document.body[2 : 2] = ["\\begin_layout Standard",
316 "\\begin_inset ERT", "status open", "",
317 "\\begin_layout Plain Layout", "", "",
319 "resetdefaultlanguage{asturian}",
320 "\\end_layout", "", "\\end_inset", "", "",
324 def revert_kannada(document):
325 "Set the document language to English but assure Kannada output"
327 if document.language == "kannada":
328 document.language = "english"
329 i = find_token(document.header, "\\language kannada", 0)
331 document.header[i] = "\\language english"
332 j = find_token(document.header, "\\language_package default", 0)
334 document.header[j] = "\\language_package default"
335 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
336 document.body[2 : 2] = ["\\begin_layout Standard",
337 "\\begin_inset ERT", "status open", "",
338 "\\begin_layout Plain Layout", "", "",
340 "resetdefaultlanguage{kannada}",
341 "\\end_layout", "", "\\end_inset", "", "",
345 def revert_khmer(document):
346 "Set the document language to English but assure Khmer output"
348 if document.language == "khmer":
349 document.language = "english"
350 i = find_token(document.header, "\\language khmer", 0)
352 document.header[i] = "\\language english"
353 j = find_token(document.header, "\\language_package default", 0)
355 document.header[j] = "\\language_package default"
356 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
357 document.body[2 : 2] = ["\\begin_layout Standard",
358 "\\begin_inset ERT", "status open", "",
359 "\\begin_layout Plain Layout", "", "",
361 "resetdefaultlanguage{khmer}",
362 "\\end_layout", "", "\\end_inset", "", "",
366 def revert_urdu(document):
367 "Set the document language to English but assure Urdu output"
369 if document.language == "urdu":
370 document.language = "english"
371 i = find_token(document.header, "\\language urdu", 0)
373 document.header[i] = "\\language english"
374 j = find_token(document.header, "\\language_package default", 0)
376 document.header[j] = "\\language_package default"
377 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
378 document.body[2 : 2] = ["\\begin_layout Standard",
379 "\\begin_inset ERT", "status open", "",
380 "\\begin_layout Plain Layout", "", "",
382 "resetdefaultlanguage{urdu}",
383 "\\end_layout", "", "\\end_inset", "", "",
387 def revert_syriac(document):
388 "Set the document language to English but assure Syriac output"
390 if document.language == "syriac":
391 document.language = "english"
392 i = find_token(document.header, "\\language syriac", 0)
394 document.header[i] = "\\language english"
395 j = find_token(document.header, "\\language_package default", 0)
397 document.header[j] = "\\language_package default"
398 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
399 document.body[2 : 2] = ["\\begin_layout Standard",
400 "\\begin_inset ERT", "status open", "",
401 "\\begin_layout Plain Layout", "", "",
403 "resetdefaultlanguage{syriac}",
404 "\\end_layout", "", "\\end_inset", "", "",
408 def revert_quotes(document):
409 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
411 # First handle verbatim insets
414 while i < len(document.body):
415 words = document.body[i].split()
416 if len(words) > 1 and words[0] == "\\begin_inset" and \
417 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
418 j = find_end_of_inset(document.body, i)
421 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
425 k = find_token(document.body, '\\begin_inset Quotes', i, j)
429 l = find_end_of_inset(document.body, k)
431 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
435 if document.body[k].endswith("s"):
437 document.body[k:l+2] = [replace]
442 # Now verbatim layouts
445 while i < len(document.body):
446 words = document.body[i].split()
447 if len(words) > 1 and words[0] == "\\begin_layout" and \
448 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
449 j = find_end_of_layout(document.body, i)
451 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
455 k = find_token(document.body, '\\begin_inset Quotes', i, j)
459 l = find_end_of_inset(document.body, k)
461 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
465 if document.body[k].endswith("s"):
467 document.body[k:l+2] = [replace]
473 if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
479 k = find_token(document.body, '\\begin_inset Quotes', i)
482 l = find_end_of_inset(document.body, k)
484 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
488 parent = get_containing_layout(document.body, k)
489 ql = find_token_backwards(document.body, "\\lang", k)
490 if ql == -1 or ql < parent[1]:
491 hebrew = document.language == "hebrew"
492 elif document.body[ql] == "\\lang hebrew":
496 if document.body[k].endswith("s"):
498 document.body[k:l+2] = [replace]
502 def revert_iopart(document):
503 " Input new styles via local layout "
504 if document.textclass != "iopart":
507 i = find_token(document.header, "\\begin_local_layout", 0)
509 k = find_token(document.header, "\\language", 0)
511 # this should not happen
512 document.warning("Malformed LyX document! No \\language header found!")
514 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
517 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
519 # this should not happen
520 document.warning("Malformed LyX document! Can't find end of local layout!")
523 document.header[i+1 : i+1] = [
524 "### Inserted by lyx2lyx (stdlayouts) ###",
525 "Input stdlayouts.inc",
526 "### End of insertion by lyx2lyx (stdlayouts) ###"
530 def convert_iopart(document):
531 " Remove local layout we added, if it is there "
532 if document.textclass != "iopart":
535 i = find_token(document.header, "\\begin_local_layout", 0)
539 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
541 # this should not happen
542 document.warning("Malformed LyX document! Can't find end of local layout!")
545 k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
547 l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
549 # this should not happen
550 document.warning("End of lyx2lyx local layout insertion not found!")
552 if k == i + 1 and l == j - 1:
553 # that was all the local layout there was
554 document.header[i : j + 1] = []
556 document.header[k : l + 1] = []
559 def convert_quotestyle(document):
560 " Convert \\quotes_language to \\quotes_style "
561 i = find_token(document.header, "\\quotes_language", 0)
563 document.warning("Malformed LyX document! Can't find \\quotes_language!")
565 val = get_value(document.header, "\\quotes_language", i)
566 document.header[i] = "\\quotes_style " + val
569 def revert_quotestyle(document):
570 " Revert \\quotes_style to \\quotes_language "
571 i = find_token(document.header, "\\quotes_style", 0)
573 document.warning("Malformed LyX document! Can't find \\quotes_style!")
575 val = get_value(document.header, "\\quotes_style", i)
576 document.header[i] = "\\quotes_language " + val
579 def revert_plainquote(document):
580 " Revert plain quote insets "
582 # First, revert style setting
583 i = find_token(document.header, "\\quotes_style plain", 0)
585 document.header[i] = "\\quotes_style english"
591 k = find_token(document.body, '\\begin_inset Quotes q', i)
594 l = find_end_of_inset(document.body, k)
596 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
600 if document.body[k].endswith("s"):
602 document.body[k:l+2] = [replace]
606 def convert_frenchquotes(document):
607 " Convert french quote insets to swiss "
609 # First, revert style setting
610 i = find_token(document.header, "\\quotes_style french", 0)
612 document.header[i] = "\\quotes_style swiss"
617 i = find_token(document.body, '\\begin_inset Quotes f', i)
620 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
621 newval = val.replace("f", "c", 1)
622 document.body[i] = document.body[i].replace(val, newval)
626 def revert_swissquotes(document):
627 " Revert swiss quote insets to french "
629 # First, revert style setting
630 i = find_token(document.header, "\\quotes_style swiss", 0)
632 document.header[i] = "\\quotes_style french"
637 i = find_token(document.body, '\\begin_inset Quotes c', i)
640 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
641 newval = val.replace("c", "f", 1)
642 document.body[i] = document.body[i].replace(val, newval)
646 def revert_britishquotes(document):
647 " Revert british quote insets to english "
649 # First, revert style setting
650 i = find_token(document.header, "\\quotes_style british", 0)
652 document.header[i] = "\\quotes_style english"
657 i = find_token(document.body, '\\begin_inset Quotes b', i)
660 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
661 newval = val.replace("b", "e", 1)
664 newval = newval.replace("d", "s")
667 newval = newval.replace("s", "d")
668 document.body[i] = document.body[i].replace(val, newval)
672 def revert_swedishgquotes(document):
673 " Revert swedish quote insets "
675 # First, revert style setting
676 i = find_token(document.header, "\\quotes_style swedishg", 0)
678 document.header[i] = "\\quotes_style danish"
683 i = find_token(document.body, '\\begin_inset Quotes w', i)
686 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
689 newval = val.replace("w", "a", 1).replace("r", "l")
692 newval = val.replace("w", "s", 1)
693 document.body[i] = document.body[i].replace(val, newval)
697 def revert_frenchquotes(document):
698 " Revert french inner quote insets "
702 i = find_token(document.body, '\\begin_inset Quotes f', i)
705 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
708 newval = val.replace("f", "e", 1).replace("s", "d")
709 document.body[i] = document.body[i].replace(val, newval)
713 def revert_frenchinquotes(document):
714 " Revert inner frenchin quote insets "
716 # First, revert style setting
717 i = find_token(document.header, "\\quotes_style frenchin", 0)
719 document.header[i] = "\\quotes_style french"
724 i = find_token(document.body, '\\begin_inset Quotes i', i)
727 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
728 newval = val.replace("i", "f", 1)
731 newval = newval.replace("s", "d")
732 document.body[i] = document.body[i].replace(val, newval)
736 def revert_russianquotes(document):
737 " Revert russian quote insets "
739 # First, revert style setting
740 i = find_token(document.header, "\\quotes_style russian", 0)
742 document.header[i] = "\\quotes_style french"
747 i = find_token(document.body, '\\begin_inset Quotes r', i)
750 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
754 newval = val.replace("r", "g", 1).replace("s", "d")
757 newval = val.replace("r", "f", 1)
758 document.body[i] = document.body[i].replace(val, newval)
762 def revert_dynamicquotes(document):
763 " Revert dynamic quote insets "
765 # First, revert header
766 i = find_token(document.header, "\\dynamic_quotes", 0)
768 del document.header[i]
772 i = find_token(document.header, "\\quotes_style", 0)
774 document.warning("Malformed document! Missing \\quotes_style")
776 style = get_value(document.header, "\\quotes_style", i)
779 if style == "english":
781 elif style == "swedish":
783 elif style == "german":
785 elif style == "polish":
787 elif style == "swiss":
789 elif style == "danish":
791 elif style == "plain":
793 elif style == "british":
795 elif style == "swedishg":
797 elif style == "french":
799 elif style == "frenchin":
801 elif style == "russian":
804 # now transform the insets
807 i = find_token(document.body, '\\begin_inset Quotes x', i)
810 document.body[i] = document.body[i].replace("x", s)
814 def revert_cjkquotes(document):
815 " Revert cjk quote insets "
819 i = find_token(document.header, "\\quotes_style", 0)
821 document.warning("Malformed document! Missing \\quotes_style")
823 style = get_value(document.header, "\\quotes_style", i)
825 global_cjk = style.find("cjk") != -1
828 document.header[i] = "\\quotes_style english"
829 # transform dynamic insets
831 if style == "cjkangle":
835 i = find_token(document.body, '\\begin_inset Quotes x', i)
838 document.body[i] = document.body[i].replace("x", s)
841 cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
846 k = find_token(document.body, '\\begin_inset Quotes j', i)
849 l = find_end_of_inset(document.body, k)
851 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
855 parent = get_containing_layout(document.body, k)
856 ql = find_token_backwards(document.body, "\\lang", k)
857 if ql == -1 or ql < parent[1]:
858 cjk = document.language in cjk_langs
859 elif document.body[ql].split()[1] in cjk_langs:
861 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
868 replace = [u"\u300E"]
870 replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
874 replace = [u"\u300F"]
876 replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
882 replace = [u"\u300C"]
884 replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
888 replace = [u"\u300D"]
890 replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
892 document.body[k:l+1] = replace
898 k = find_token(document.body, '\\begin_inset Quotes k', i)
901 l = find_end_of_inset(document.body, k)
903 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
907 parent = get_containing_layout(document.body, k)
908 ql = find_token_backwards(document.body, "\\lang", k)
909 if ql == -1 or ql < parent[1]:
910 cjk = document.language in cjk_langs
911 elif document.body[ql].split()[1] in cjk_langs:
913 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
920 replace = [u"\u3008"]
922 replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
926 replace = [u"\u3009"]
928 replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
934 replace = [u"\u300A"]
936 replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
940 replace = [u"\u300B"]
942 replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
944 document.body[k:l+1] = replace
948 def convert_crimson(document):
949 """Transform preamble code to native font setting."""
951 i = find_substring(document.preamble, "{cochineal}")
954 # Find and delete user-preamble code:
955 if document.preamble[i] == "\\usepackage[proportional,osf]{cochineal}":
957 elif document.preamble[i] == "\\usepackage{cochineal}":
961 del document.preamble[i]
962 if i and document.preamble[i-1] == "% Added by lyx2lyx":
963 del document.preamble[i-1]
965 # Convert to native font setting:
966 j = find_token(document.header, '\\font_roman')
968 romanfont = ['\font_roman', '"cochineal"', '"default"']
970 romanfont = document.header[j].split()
971 romanfont[1] = '"cochineal"'
972 document.header[j] = " ".join(romanfont)
974 set_bool_value(document.header, '\\font_osf', osf)
975 except ValueError: # no \\font_osf setting in document.header
977 document.header.insert(-1, "\\font_osf true")
980 def revert_crimson(document):
981 " Revert native Cochineal/Crimson font definition to LaTeX "
983 i = find_token(document.header, '\\font_roman "cochineal"')
986 # replace unsupported font setting
987 document.header[i] = document.header[i].replace("cochineal", "default")
988 # no need for preamble code with system fonts
989 if get_bool_value(document.header, "\\use_non_tex_fonts"):
991 # transfer old style figures setting to package options
992 j = find_token(document.header, "\\font_osf true")
994 options = "[proportional,osf]"
995 document.header[j] = "\\font_osf false"
998 add_to_preamble(document, ["\\usepackage%s{cochineal}"%options])
1001 def revert_cochinealmath(document):
1002 " Revert cochineal newtxmath definitions to LaTeX "
1004 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1005 i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
1007 add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
1008 document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
1011 def revert_labelonly(document):
1012 " Revert labelonly tag for InsetRef "
1015 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1018 j = find_end_of_inset(document.body, i)
1020 document.warning("Can't find end of reference inset at line %d!!" %(i))
1023 k = find_token(document.body, "LatexCommand labelonly", i, j)
1027 label = get_quoted_value(document.body, "reference", i, j)
1029 document.warning("Can't find label for reference at line %d!" %(i))
1032 document.body[i:j+1] = put_cmd_in_ert([label])
1036 def revert_plural_refs(document):
1037 " Revert plural and capitalized references "
1038 i = find_token(document.header, "\\use_refstyle 1", 0)
1039 use_refstyle = (i != 0)
1043 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1046 j = find_end_of_inset(document.body, i)
1048 document.warning("Can't find end of reference inset at line %d!!" %(i))
1052 plural = caps = suffix = False
1053 k = find_token(document.body, "LaTeXCommand formatted", i, j)
1054 if k != -1 and use_refstyle:
1055 plural = get_bool_value(document.body, "plural", i, j, False)
1056 caps = get_bool_value(document.body, "caps", i, j, False)
1057 label = get_quoted_value(document.body, "reference", i, j)
1060 (prefix, suffix) = label.split(":", 1)
1062 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1064 document.warning("Can't find label for reference at line %d!" % (i))
1066 # this effectively tests also for use_refstyle and a formatted reference
1067 # we do this complicated test because we would otherwise do this erasure
1068 # over and over and over
1069 if not ((plural or caps) and suffix):
1070 del_token(document.body, "plural", i, j)
1071 del_token(document.body, "caps", i, j - 1) # since we deleted a line
1076 prefix = prefix[0].title() + prefix[1:]
1077 cmd = "\\" + prefix + "ref"
1080 cmd += "{" + suffix + "}"
1081 document.body[i:j+1] = put_cmd_in_ert([cmd])
1085 def revert_noprefix(document):
1086 " Revert labelonly tags with 'noprefix' set "
1089 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1092 j = find_end_of_inset(document.body, i)
1094 document.warning("Can't find end of reference inset at line %d!!" %(i))
1097 k = find_token(document.body, "LatexCommand labelonly", i, j)
1100 noprefix = get_bool_value(document.body, "noprefix", i, j)
1102 # either it was not a labelonly command, or else noprefix was not set.
1103 # in that case, we just delete the option.
1104 del_token(document.body, "noprefix", i, j)
1107 label = get_quoted_value(document.body, "reference", i, j)
1109 document.warning("Can't find label for reference at line %d!" %(i))
1113 (prefix, suffix) = label.split(":", 1)
1115 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1116 # we'll leave this as an ordinary labelonly reference
1117 del_token(document.body, "noprefix", i, j)
1120 document.body[i:j+1] = put_cmd_in_ert([suffix])
1124 def revert_biblatex(document):
1125 " Revert biblatex support "
1131 # 1. Get cite engine
1133 i = find_token(document.header, "\\cite_engine", 0)
1135 document.warning("Malformed document! Missing \\cite_engine")
1137 engine = get_value(document.header, "\\cite_engine", i)
1139 # 2. Store biblatex state and revert to natbib
1141 if engine in ["biblatex", "biblatex-natbib"]:
1143 document.header[i] = "\\cite_engine natbib"
1145 # 3. Store and remove new document headers
1147 i = find_token(document.header, "\\biblatex_bibstyle", 0)
1149 bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
1150 del document.header[i]
1153 i = find_token(document.header, "\\biblatex_citestyle", 0)
1155 citestyle = get_value(document.header, "\\biblatex_citestyle", i)
1156 del document.header[i]
1159 i = find_token(document.header, "\\biblio_options", 0)
1161 biblio_options = get_value(document.header, "\\biblio_options", i)
1162 del document.header[i]
1165 bbxopts = "[natbib=true"
1167 bbxopts += ",bibstyle=" + bibstyle
1169 bbxopts += ",citestyle=" + citestyle
1170 if biblio_options != "":
1171 bbxopts += "," + biblio_options
1173 add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1183 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1186 j = find_end_of_inset(document.body, i)
1188 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1191 bibs = get_quoted_value(document.body, "bibfiles", i, j)
1192 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1195 bibresources += bibs.split(",")
1197 document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1198 # remove biblatexopts line
1199 k = find_token(document.body, "biblatexopts", i, j)
1201 del document.body[k]
1202 # Re-find inset end line
1203 j = find_end_of_inset(document.body, i)
1204 # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1206 pcmd = "printbibliography"
1208 pcmd += "[" + opts + "]"
1209 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1210 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1211 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1212 "status open", "", "\\begin_layout Plain Layout" ]
1213 repl += document.body[i:j+1]
1214 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1215 document.body[i:j+1] = repl
1221 for b in bibresources:
1222 add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1224 # 2. Citation insets
1226 # Specific citation insets used in biblatex that need to be reverted to ERT
1229 "citebyear" : "citeyear",
1230 "citeyear" : "cite*",
1231 "Footcite" : "Smartcite",
1232 "footcite" : "smartcite",
1233 "Autocite" : "Autocite",
1234 "autocite" : "autocite",
1235 "citetitle" : "citetitle",
1236 "citetitle*" : "citetitle*",
1237 "fullcite" : "fullcite",
1238 "footfullcite" : "footfullcite",
1239 "supercite" : "supercite",
1240 "citeauthor" : "citeauthor",
1241 "citeauthor*" : "citeauthor*",
1242 "Citeauthor" : "Citeauthor",
1243 "Citeauthor*" : "Citeauthor*"
1246 # All commands accepted by LyX < 2.3. Everything else throws an error.
1247 old_citations = ["cite", "nocite", "citet", "citep", "citealt", "citealp",
1248 "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",
1249 "citealt*", "citealp*", "citeauthor*", "Citet", "Citep",
1250 "Citealt", "Citealp", "Citeauthor", "Citet*", "Citep*",
1251 "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",
1252 "footcitet", "footcitep", "footcitealt", "footcitealp",
1253 "footciteauthor", "footciteyear", "footciteyearpar",
1254 "citefield", "citetitle", "cite*" ]
1258 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1261 j = find_end_of_inset(document.body, i)
1263 document.warning("Can't find end of citation inset at line %d!!" %(i))
1266 k = find_token(document.body, "LatexCommand", i, j)
1268 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1271 cmd = get_value(document.body, "LatexCommand", k)
1272 if biblatex and cmd in list(new_citations.keys()):
1273 pre = get_quoted_value(document.body, "before", i, j)
1274 post = get_quoted_value(document.body, "after", i, j)
1275 key = get_quoted_value(document.body, "key", i, j)
1277 document.warning("Citation inset at line %d does not have a key!" %(i))
1279 # Replace known new commands with ERT
1280 res = "\\" + new_citations[cmd]
1282 res += "[" + pre + "]"
1284 res += "[" + post + "]"
1287 res += "{" + key + "}"
1288 document.body[i:j+1] = put_cmd_in_ert([res])
1289 elif cmd not in old_citations:
1290 # Reset unknown commands to cite. This is what LyX does as well
1291 # (but LyX 2.2 would break on unknown commands)
1292 document.body[k] = "LatexCommand cite"
1293 document.warning("Reset unknown cite command '%s' with cite" % cmd)
1296 # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1298 i = find_token(document.header, "\\begin_local_layout", 0)
1300 k = find_token(document.header, "\\language", 0)
1302 # this should not happen
1303 document.warning("Malformed LyX document! No \\language header found!")
1305 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1308 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1310 # this should not happen
1311 document.warning("Malformed LyX document! Can't find end of local layout!")
1314 document.header[i+1 : i+1] = [
1315 "### Inserted by lyx2lyx (biblatex emulation) ###",
1316 "Provides natbib 1",
1317 "### End of insertion by lyx2lyx (biblatex emulation) ###"
1321 def revert_citekeyonly(document):
1322 " Revert keyonly cite command to ERT "
1326 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1329 j = find_end_of_inset(document.body, i)
1331 document.warning("Can't find end of citation inset at line %d!!" %(i))
1334 k = find_token(document.body, "LatexCommand", i, j)
1336 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1339 cmd = get_value(document.body, "LatexCommand", k)
1340 if cmd != "keyonly":
1344 key = get_quoted_value(document.body, "key", i, j)
1346 document.warning("Citation inset at line %d does not have a key!" %(i))
1347 # Replace known new commands with ERT
1348 document.body[i:j+1] = put_cmd_in_ert([key])
1353 def revert_bibpackopts(document):
1354 " Revert support for natbib/jurabib package options "
1357 i = find_token(document.header, "\\cite_engine", 0)
1359 document.warning("Malformed document! Missing \\cite_engine")
1361 engine = get_value(document.header, "\\cite_engine", i)
1364 if engine not in ["natbib", "jurabib"]:
1367 i = find_token(document.header, "\\biblio_options", 0)
1369 # Nothing to do if we have no options
1372 biblio_options = get_value(document.header, "\\biblio_options", i)
1373 del document.header[i]
1375 if not biblio_options:
1376 # Nothing to do for empty options
1379 i = find_token(document.header, "\\begin_local_layout", 0)
1381 k = find_token(document.header, "\\language", 0)
1383 # this should not happen
1384 document.warning("Malformed LyX document! No \\language header found!")
1386 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1389 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1391 # this should not happen
1392 document.warning("Malformed LyX document! Can't find end of local layout!")
1395 document.header[i+1 : i+1] = [
1396 "### Inserted by lyx2lyx (bibliography package options) ###",
1397 "PackageOptions " + engine + " " + biblio_options,
1398 "### End of insertion by lyx2lyx (bibliography package options) ###"
1402 def revert_qualicites(document):
1403 " Revert qualified citation list commands to ERT "
1405 # Citation insets that support qualified lists, with their LaTeX code
1409 "citet" : "textcites",
1410 "Citet" : "Textcites",
1411 "citep" : "parencites",
1412 "Citep" : "Parencites",
1413 "Footcite" : "Smartcites",
1414 "footcite" : "smartcites",
1415 "Autocite" : "Autocites",
1416 "autocite" : "autocites",
1421 i = find_token(document.header, "\\cite_engine", 0)
1423 document.warning("Malformed document! Missing \\cite_engine")
1425 engine = get_value(document.header, "\\cite_engine", i)
1427 biblatex = engine in ["biblatex", "biblatex-natbib"]
1431 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1434 j = find_end_of_inset(document.body, i)
1436 document.warning("Can't find end of citation inset at line %d!!" %(i))
1439 pres = find_token(document.body, "pretextlist", i, j)
1440 posts = find_token(document.body, "posttextlist", i, j)
1441 if pres == -1 and posts == -1:
1445 pretexts = get_quoted_value(document.body, "pretextlist", pres)
1446 posttexts = get_quoted_value(document.body, "posttextlist", posts)
1447 k = find_token(document.body, "LatexCommand", i, j)
1449 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1452 cmd = get_value(document.body, "LatexCommand", k)
1453 if biblatex and cmd in list(ql_citations.keys()):
1454 pre = get_quoted_value(document.body, "before", i, j)
1455 post = get_quoted_value(document.body, "after", i, j)
1456 key = get_quoted_value(document.body, "key", i, j)
1458 document.warning("Citation inset at line %d does not have a key!" %(i))
1460 keys = key.split(",")
1461 prelist = pretexts.split("\t")
1464 ppp = pp.split(" ", 1)
1465 premap[ppp[0]] = ppp[1]
1466 postlist = posttexts.split("\t")
1469 ppp = pp.split(" ", 1)
1470 postmap[ppp[0]] = ppp[1]
1471 # Replace known new commands with ERT
1472 if "(" in pre or ")" in pre:
1473 pre = "{" + pre + "}"
1474 if "(" in post or ")" in post:
1475 post = "{" + post + "}"
1476 res = "\\" + ql_citations[cmd]
1478 res += "(" + pre + ")"
1480 res += "(" + post + ")"
1484 if premap.get(kk, "") != "":
1485 res += "[" + premap[kk] + "]"
1486 if postmap.get(kk, "") != "":
1487 res += "[" + postmap[kk] + "]"
1488 elif premap.get(kk, "") != "":
1490 res += "{" + kk + "}"
1491 document.body[i:j+1] = put_cmd_in_ert([res])
1493 # just remove the params
1494 del document.body[posttexts]
1495 del document.body[pretexts]
1499 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1500 def convert_literalparam(document):
1501 " Add param literal "
1503 pos = len("\\begin_inset CommandInset ")
1506 i = find_token(document.body, '\\begin_inset CommandInset', i)
1509 inset = document.body[i][pos:].strip()
1510 if not inset in command_insets:
1513 j = find_end_of_inset(document.body, i)
1515 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1518 while i < j and document.body[i].strip() != '':
1520 # href is already fully latexified. Here we can switch off literal.
1522 document.body.insert(i, "literal \"false\"")
1524 document.body.insert(i, "literal \"true\"")
1528 def revert_literalparam(document):
1529 " Remove param literal "
1531 for inset in command_insets:
1534 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1537 j = find_end_of_inset(document.body, i)
1539 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1542 k = find_token(document.body, 'literal', i, j)
1546 del document.body[k]
1549 def revert_multibib(document):
1550 " Revert multibib support "
1552 # 1. Get cite engine
1554 i = find_token(document.header, "\\cite_engine", 0)
1556 document.warning("Malformed document! Missing \\cite_engine")
1558 engine = get_value(document.header, "\\cite_engine", i)
1560 # 2. Do we use biblatex?
1562 if engine in ["biblatex", "biblatex-natbib"]:
1565 # 3. Store and remove multibib document header
1567 i = find_token(document.header, "\\multibib", 0)
1569 multibib = get_value(document.header, "\\multibib", i)
1570 del document.header[i]
1575 # 4. The easy part: Biblatex
1577 i = find_token(document.header, "\\biblio_options", 0)
1579 k = find_token(document.header, "\\use_bibtopic", 0)
1581 # this should not happen
1582 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1584 document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1586 biblio_options = get_value(document.header, "\\biblio_options", i)
1588 biblio_options += ","
1589 biblio_options += "refsection=" + multibib
1590 document.header[i] = "\\biblio_options " + biblio_options
1595 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1598 j = find_end_of_inset(document.body, i)
1600 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1603 btprint = get_quoted_value(document.body, "btprint", i, j)
1604 if btprint != "bibbysection":
1607 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1608 # change btprint line
1609 k = find_token(document.body, "btprint", i, j)
1611 document.body[k] = "btprint \"btPrintCited\""
1612 # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1613 pcmd = "bibbysection"
1615 pcmd += "[" + opts + "]"
1616 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1617 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1618 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1619 "status open", "", "\\begin_layout Plain Layout" ]
1620 repl += document.body[i:j+1]
1621 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1622 document.body[i:j+1] = repl
1628 # 5. More tricky: Bibtex/Bibtopic
1629 k = find_token(document.header, "\\use_bibtopic", 0)
1631 # this should not happen
1632 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1634 document.header[k] = "\\use_bibtopic true"
1636 # Possible units. This assumes that the LyX name follows the std,
1637 # which might not always be the case. But it's as good as we can get.
1640 "chapter" : "Chapter",
1641 "section" : "Section",
1642 "subsection" : "Subsection",
1645 if multibib not in units.keys():
1646 document.warning("Unknown multibib value `%s'!" % nultibib)
1648 unit = units[multibib]
1652 i = find_token(document.body, "\\begin_layout " + unit, i)
1656 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1657 "\\begin_inset ERT", "status open", "",
1658 "\\begin_layout Plain Layout", "", "",
1660 "end{btUnit}", "\\end_layout",
1661 "\\begin_layout Plain Layout", "",
1664 "\\end_layout", "", "\\end_inset", "", "",
1668 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1669 "\\begin_inset ERT", "status open", "",
1670 "\\begin_layout Plain Layout", "", "",
1673 "\\end_layout", "", "\\end_inset", "", "",
1680 i = find_token(document.body, "\\end_body", i)
1681 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1682 "\\begin_inset ERT", "status open", "",
1683 "\\begin_layout Plain Layout", "", "",
1686 "\\end_layout", "", "\\end_inset", "", "",
1690 def revert_chapterbib(document):
1691 " Revert chapterbib support "
1693 # 1. Get cite engine
1695 i = find_token(document.header, "\\cite_engine", 0)
1697 document.warning("Malformed document! Missing \\cite_engine")
1699 engine = get_value(document.header, "\\cite_engine", i)
1701 # 2. Do we use biblatex?
1703 if engine in ["biblatex", "biblatex-natbib"]:
1706 # 3. Store multibib document header value
1708 i = find_token(document.header, "\\multibib", 0)
1710 multibib = get_value(document.header, "\\multibib", i)
1712 if not multibib or multibib != "child":
1716 # 4. remove multibib header
1717 del document.header[i]
1721 # find include insets
1724 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1727 j = find_end_of_inset(document.body, i)
1729 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1732 parent = get_containing_layout(document.body, i)
1735 # Insert ERT \\newrefsection before inset
1736 beg = ["\\begin_layout Standard",
1737 "\\begin_inset ERT", "status open", "",
1738 "\\begin_layout Plain Layout", "", "",
1741 "\\end_layout", "", "\\end_inset", "", "",
1743 document.body[parbeg-1:parbeg-1] = beg
1748 # 6. Bibtex/Bibtopic
1749 i = find_token(document.header, "\\use_bibtopic", 0)
1751 # this should not happen
1752 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1754 if get_value(document.header, "\\use_bibtopic", i) == "true":
1755 # find include insets
1758 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1761 j = find_end_of_inset(document.body, i)
1763 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1766 parent = get_containing_layout(document.body, i)
1770 # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1771 beg = ["\\begin_layout Standard",
1772 "\\begin_inset ERT", "status open", "",
1773 "\\begin_layout Plain Layout", "", "",
1776 "\\end_layout", "", "\\end_inset", "", "",
1778 end = ["\\begin_layout Standard",
1779 "\\begin_inset ERT", "status open", "",
1780 "\\begin_layout Plain Layout", "", "",
1783 "\\end_layout", "", "\\end_inset", "", "",
1785 document.body[parend+1:parend+1] = end
1786 document.body[parbeg-1:parbeg-1] = beg
1787 j += len(beg) + len(end)
1791 # 7. Chapterbib proper
1792 add_to_preamble(document, ["\\usepackage{chapterbib}"])
1795 def convert_dashligatures(document):
1796 """Set 'use_dash_ligatures' according to content.
1798 # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
1799 # set use_dash_ligatures to True if found, to None else.
1800 use_dash_ligatures = del_complete_lines(document.preamble,
1801 ['% Added by lyx2lyx',
1802 r'\renewcommand{\textendash}{--}',
1803 r'\renewcommand{\textemdash}{---}']) or None
1805 if use_dash_ligatures is None:
1806 # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
1807 # or "\threehyphens\n" as interim representation for -- an ---.)
1808 lines = document.body
1809 has_literal_dashes = has_ligature_dashes = False
1810 dash_pattern = re.compile(u".*[\u2013\u2014]|\\twohyphens|\\threehyphens")
1813 # skip lines without dashes:
1814 i = find_re(lines, dash_pattern, i+1)
1818 # skip label width string (see bug 10243):
1819 if line.startswith("\\labelwidthstring"):
1821 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1823 inset_type, start, end = get_containing_inset(lines, i)
1824 except TypeError: # no containing inset
1825 inset_type, start, end = "no inset", -1, -1
1826 if (inset_type.split()[0] in
1827 ["CommandInset", "ERT", "External", "Formula",
1828 "FormulaMacro", "Graphics", "IPA", "listings"]
1829 or inset_type == "Flex Code"):
1833 layoutname, start, end, j = get_containing_layout(lines, i)
1834 except TypeError: # no (or malformed) containing layout
1835 document.warning("Malformed LyX document: "
1836 "Can't find layout at line %d" % i)
1839 document.warning("Malformed LyX document: "
1840 "Missing layout name on line %d"%start)
1841 if layoutname == "LyX-Code":
1845 # literal dash followed by a non-white-character or no-break space:
1846 if re.search(u"[\u2013\u2014]([\S\u00A0\u202F\u2060]|$)",
1847 line, flags=re.UNICODE):
1848 has_literal_dashes = True
1849 # ligature dash followed by non-white-char or no-break space on next line:
1850 if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
1851 re.match(u"[\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
1852 has_ligature_dashes = True
1853 if has_literal_dashes and has_ligature_dashes:
1854 # TODO: insert a warning note in the document?
1855 document.warning('This document contained both literal and '
1856 '"ligature" dashes.\n Line breaks may have changed. '
1857 'See UserGuide chapter 3.9.1 for details.')
1860 if has_literal_dashes and not has_ligature_dashes:
1861 use_dash_ligatures = False
1862 elif has_ligature_dashes and not has_literal_dashes:
1863 use_dash_ligatures = True
1865 # insert the setting if there is a preferred value
1866 if use_dash_ligatures is True:
1867 document.header.insert(-1, "\\use_dash_ligatures true")
1868 elif use_dash_ligatures is False:
1869 document.header.insert(-1, "\\use_dash_ligatures false")
1872 def revert_dashligatures(document):
1873 """Remove font ligature settings for en- and em-dashes.
1874 Revert conversion of \twodashes or \threedashes to literal dashes.
1876 use_dash_ligatures = del_value(document.header, "\\use_dash_ligatures")
1877 if use_dash_ligatures != "true" or document.backend != "latex":
1880 dash_pattern = re.compile(u".*[\u2013\u2014]")
1882 # skip lines without dashes:
1883 i = find_re(document.body, dash_pattern, i+1)
1886 line = document.body[i]
1887 # skip label width string (see bug 10243):
1888 if line.startswith("\\labelwidthstring"):
1890 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1892 inset_type, start, end = get_containing_inset(document.body, i)
1893 except TypeError: # no containing inset
1894 inset_type, start, end = "no inset", -1, -1
1895 if (inset_type.split()[0] in
1896 ["CommandInset", "ERT", "External", "Formula",
1897 "FormulaMacro", "Graphics", "IPA", "listings"]
1898 or inset_type == "Flex Code"):
1902 layoutname, start, end, j = get_containing_layout(document.body, i)
1903 except TypeError: # no (or malformed) containing layout
1904 document.warning("Malformed LyX document: "
1905 "Can't find layout at body line %d" % i)
1907 if layoutname == "LyX-Code":
1910 # TODO: skip replacement in typewriter fonts
1911 line = line.replace(u'\u2013', '\\twohyphens\n')
1912 line = line.replace(u'\u2014', '\\threehyphens\n')
1913 document.body[i:i+1] = line.split('\n')
1914 # redefine the dash LICRs to use ligature dashes:
1915 add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1916 r'\renewcommand{\textemdash}{---}'])
1919 def revert_noto(document):
1920 " Revert Noto font definitions to LaTeX "
1922 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1924 i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1926 add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1927 document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1928 i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1930 add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1931 document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1932 i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1934 add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1935 document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1938 def revert_xout(document):
1939 " Reverts \\xout font attribute "
1940 changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1942 insert_to_preamble(document, \
1943 ['% for proper cross-out',
1944 '\\PassOptionsToPackage{normalem}{ulem}',
1945 '\\usepackage{ulem}'])
1948 def convert_mathindent(document):
1949 """Add the \\is_math_indent tag.
1951 k = find_token(document.header, "\\quotes_style") # where to insert
1952 # check if the document uses the class option "fleqn"
1953 options = get_value(document.header, "\\options")
1954 if 'fleqn' in options:
1955 document.header.insert(k, "\\is_math_indent 1")
1956 # delete the fleqn option
1957 i = find_token(document.header, "\\options")
1958 options = [option for option in options.split(",")
1959 if option.strip() != "fleqn"]
1961 document.header[i] = "\\options " + ",".join(options)
1963 del document.header[i]
1965 document.header.insert(k, "\\is_math_indent 0")
1967 def revert_mathindent(document):
1968 " Define mathindent if set in the document "
1969 # emulate and delete \math_indentation
1970 value = get_value(document.header, "\\math_indentation",
1971 default="default", delete=True)
1972 if value != "default":
1973 add_to_preamble(document, [r"\setlength{\mathindent}{%s}"%value])
1974 # delete \is_math_indent and emulate via document class option
1975 if not get_bool_value(document.header, "\\is_math_indent", delete=True):
1977 i = find_token(document.header, "\\options")
1979 document.header[i] = document.header[i].replace("\\options ",
1982 l = find_token(document.header, "\\use_default_options")
1983 document.header.insert(l, "\\options fleqn")
1986 def revert_baselineskip(document):
1987 " Revert baselineskips to TeX code "
1990 i = find_substring(document.body, "baselineskip%", i+1)
1993 if document.body[i].startswith("\\begin_inset VSpace"):
1994 # output VSpace inset as TeX code
1995 end = find_end_of_inset(document.body, i)
1997 document.warning("Malformed LyX document: "
1998 "Can't find end of VSpace inset at line %d." % i)
2000 # read out the value
2001 baselineskip = document.body[i].split()[-1]
2002 # check if it is the starred version
2003 star = '*' if '*' in document.body[i] else ''
2004 # now output TeX code
2005 cmd = "\\vspace%s{%s}" %(star, latex_length(baselineskip)[1])
2006 document.body[i:end+1] = put_cmd_in_ert(cmd)
2009 begin, end = is_in_inset(document.body, i, "\\begin_inset space \\hspace")
2011 # output space inset as TeX code
2012 baselineskip = document.body[i].split()[-1]
2013 star = '*' if '*' in document.body[i-1] else ''
2014 cmd = "\\hspace%s{%s}" %(star, latex_length(baselineskip)[1])
2015 document.body[begin:end+1] = put_cmd_in_ert(cmd)
2018 def revert_rotfloat(document):
2019 " Revert placement options for rotated floats "
2024 i = find_token(document.body, "sideways true", i)
2027 if not document.body[i-2].startswith('placement '):
2030 # we found a sideways float with placement options
2031 # at first store the placement
2032 beg = document.body[i-2].rfind(" ");
2033 placement = document.body[i-2][beg+1:]
2034 # check if the option'H' is used
2035 if placement.find("H") != -1:
2036 add_to_preamble(document, ["\\usepackage{float}"])
2037 # now check if it is a starred type
2038 if document.body[i-1].find("wide true") != -1:
2042 # store the float type
2043 beg = document.body[i-3].rfind(" ");
2044 fType = document.body[i-3][beg+1:]
2045 # now output TeX code
2046 endInset = find_end_of_inset(document.body, i-3)
2048 document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2051 document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
2052 document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
2053 add_to_preamble(document, ["\\usepackage{rotfloat}"])
2058 allowbreak_emulation = [r"\begin_inset space \hspace{}",
2063 def convert_allowbreak(document):
2064 " Zero widths Space-inset -> \SpecialChar allowbreak. "
2065 lines = document.body
2066 i = find_complete_lines(lines, allowbreak_emulation, 2)
2068 lines[i-1:i+4] = [lines[i-1] + r"\SpecialChar allowbreak"]
2069 i = find_complete_lines(lines, allowbreak_emulation, i+3)
2072 def revert_allowbreak(document):
2073 " \SpecialChar allowbreak -> Zero widths Space-inset. "
2075 lines = document.body
2076 while i < len(lines):
2077 if lines[i].endswith(r"\SpecialChar allowbreak"):
2078 lines[i:i+1] = [lines[i].replace(r"\SpecialChar allowbreak", "")
2079 ] + allowbreak_emulation
2085 def convert_mathnumberpos(document):
2086 " add the \\math_number_before tag "
2087 # check if the document uses the class option "leqno"
2088 i = find_token(document.header, "\\options")
2089 k = find_token(document.header, "\\quotes_style")
2090 if 'leqno' in document.header[i]:
2091 document.header.insert(k, "\\math_number_before 1")
2092 # delete the found option
2093 document.header[i] = document.header[i].replace(",leqno", "")
2094 document.header[i] = document.header[i].replace(", leqno", "")
2095 document.header[i] = document.header[i].replace("leqno,", "")
2096 if 'leqno' in document.header[i]:
2097 # then we have leqno as the only option
2098 del document.header[i]
2100 document.header.insert(k, "\\math_number_before 0")
2103 def revert_mathnumberpos(document):
2104 """Remove \\math_number_before tag,
2105 add the document class option leqno if required.
2107 math_number_before = get_bool_value(document.header,
2108 '\\math_number_before', delete=True)
2109 if math_number_before:
2110 i = find_token(document.header, "\\options")
2111 if i != -1 and 'leqno' not in document.header[i]:
2112 document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
2114 i = find_token(document.header, "\\use_default_options")
2115 document.header.insert(i, "\\options leqno")
2118 def convert_mathnumberingname(document):
2119 " rename the \\math_number_before tag to \\math_numbering_side "
2120 i = find_token(document.header, "\\math_number_before")
2121 math_number_before = get_bool_value(document.header, '\\math_number_before', i)
2122 if math_number_before:
2123 document.header[i] = "\\math_numbering_side left"
2125 # check if the document uses the class option "reqno"
2126 k = find_token(document.header, "\\options")
2127 if 'reqno' in document.header[k]:
2128 document.header[i] = "\\math_numbering_side right"
2129 # delete the found option
2130 document.header[k] = document.header[k].replace(",reqno", "")
2131 document.header[k] = document.header[k].replace(", reqno", "")
2132 document.header[k] = document.header[k].replace("reqno,", "")
2133 if 'reqno' in document.header[k]:
2134 # then we have reqno as the only option
2135 del document.header[k]
2137 document.header[i] = "\\math_numbering_side default"
2140 def revert_mathnumberingname(document):
2141 " rename the \\math_numbering_side tag back to \\math_number_before "
2142 i = find_token(document.header, "\\math_numbering_side")
2143 math_numbering_side = get_value(document.header, '\\math_numbering_side', i)
2144 # rename tag and set boolean value:
2145 if math_numbering_side == "left":
2146 document.header[i] = "\\math_number_before 1"
2147 elif math_numbering_side == "right":
2148 # also add the option reqno:
2149 document.header[i] = "\\math_number_before 0"
2150 k = find_token(document.header, "\\options")
2151 if k != -1 and 'reqno' not in document.header[k]:
2152 document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
2154 l = find_token(document.header, "\\use_default_options", 0)
2155 document.header.insert(l, "\\options reqno")
2157 document.header[i] = "\\math_number_before 0"
2160 def convert_minted(document):
2161 " add the \\use_minted tag "
2162 i = find_token(document.header, "\\index ")
2163 document.header.insert(i, "\\use_minted 0")
2166 def revert_minted(document):
2167 " remove the \\use_minted tag "
2168 del_token(document.header, "\\use_minted")
2171 def revert_longtable_lscape(document):
2172 " revert the longtable landcape mode to ERT "
2174 regexp = re.compile(r'^<features rotate=\"90\"\s.*islongtable=\"true\"\s.*$', re.IGNORECASE)
2176 i = find_re(document.body, regexp, i)
2180 document.body[i] = document.body[i].replace(" rotate=\"90\"", "")
2181 lay = get_containing_layout(document.body, i)
2183 document.warning("Longtable has not layout!")
2186 begcmd = put_cmd_in_ert("\\begin{landscape}")
2187 endcmd = put_cmd_in_ert("\\end{landscape}")
2188 document.body[lay[2] : lay[2]] = endcmd + ["\\end_layout"]
2189 document.body[lay[1] : lay[1]] = ["\\begin_layout " + lay[0], ""] + begcmd
2191 add_to_preamble(document, ["\\usepackage{pdflscape}"])
2199 supported_versions = ["2.3.0", "2.3"]
2201 [509, [convert_microtype]],
2202 [510, [convert_dateinset]],
2203 [511, [convert_ibranches]],
2204 [512, [convert_beamer_article_styles]],
2208 [516, [convert_inputenc]],
2210 [518, [convert_iopart]],
2211 [519, [convert_quotestyle]],
2213 [521, [convert_frenchquotes]],
2216 [524, [convert_crimson]],
2224 [532, [convert_literalparam]],
2227 [535, [convert_dashligatures]],
2230 [538, [convert_mathindent]],
2233 [541, [convert_allowbreak]],
2234 [542, [convert_mathnumberpos]],
2235 [543, [convert_mathnumberingname]],
2236 [544, [convert_minted]]
2240 [543, [revert_minted, revert_longtable_lscape]],
2241 [542, [revert_mathnumberingname]],
2242 [541, [revert_mathnumberpos]],
2243 [540, [revert_allowbreak]],
2244 [539, [revert_rotfloat]],
2245 [538, [revert_baselineskip]],
2246 [537, [revert_mathindent]],
2247 [536, [revert_xout]],
2248 [535, [revert_noto]],
2249 [534, [revert_dashligatures]],
2250 [533, [revert_chapterbib]],
2251 [532, [revert_multibib]],
2252 [531, [revert_literalparam]],
2253 [530, [revert_qualicites]],
2254 [529, [revert_bibpackopts]],
2255 [528, [revert_citekeyonly]],
2256 [527, [revert_biblatex]],
2257 [526, [revert_noprefix]],
2258 [525, [revert_plural_refs]],
2259 [524, [revert_labelonly]],
2260 [523, [revert_crimson, revert_cochinealmath]],
2261 [522, [revert_cjkquotes]],
2262 [521, [revert_dynamicquotes]],
2263 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2264 [519, [revert_plainquote]],
2265 [518, [revert_quotestyle]],
2266 [517, [revert_iopart]],
2267 [516, [revert_quotes]],
2269 [514, [revert_urdu, revert_syriac]],
2270 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2271 [512, [revert_new_babel_languages]],
2272 [511, [revert_beamer_article_styles]],
2273 [510, [revert_ibranches]],
2275 [508, [revert_microtype]]
2279 if __name__ == "__main__":