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)
36 # get_ert, lyx2latex, lyx2verbatim, length_in_bp, convert_info_insets
37 # revert_flex_inset, hex2ratio, str2bool
39 ####################################################################
40 # Private helper functions
44 ###############################################################################
46 ### Conversion and reversion routines
48 ###############################################################################
50 def convert_microtype(document):
51 " Add microtype settings. "
52 i = find_token(document.header, "\\font_tt_scale")
53 j = find_token(document.preamble, "\\usepackage{microtype}")
55 document.header.insert(i + 1, "\\use_microtype false")
57 document.header.insert(i + 1, "\\use_microtype true")
58 del document.preamble[j]
59 if j and document.preamble[j-1] == "% Added by lyx2lyx":
60 del document.preamble[j-1]
63 def revert_microtype(document):
64 " Remove microtype settings. "
65 use_microtype = get_bool_value(document.header, "\\use_microtype", delete=True)
67 add_to_preamble(document, ["\\usepackage{microtype}"])
70 def convert_dateinset(document):
71 ' Convert date external inset to ERT '
74 i = find_token(document.body, "\\begin_inset External", i+1)
77 j = find_end_of_inset(document.body, i)
79 document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
81 if get_value(document.body, 'template', i, j) == "Date":
82 document.body[i : j + 1] = put_cmd_in_ert("\\today ")
86 def convert_inputenc(document):
87 """Replace no longer supported input encoding setting."""
88 i = find_token(document.header, "\\inputencoding pt254")
90 document.header[i] = "\\inputencoding pt154"
93 def convert_ibranches(document):
94 ' Add "inverted 0" to branch insets'
97 i = find_token(document.body, "\\begin_inset Branch", i+1)
100 document.body.insert(i + 1, "inverted 0")
103 def revert_ibranches(document):
104 ' Convert inverted branches to explicit anti-branches'
105 # Get list of branches
109 i = find_token(document.header, "\\branch", i+1)
112 branch = document.header[i][8:].strip()
113 selected = get_bool_value(document.header, "\\selected", i+1, i+2)
115 document.warning("Malformed LyX document: No selection indicator "
116 "for branch %s." % branch)
118 # the value tells us whether the branch is selected
119 ourbranches[branch] = selected
121 # Find branch insets, remove "inverted" tag and
122 # convert inverted insets to "Anti-OldBranch" insets
126 i = find_token(document.body, "\\begin_inset Branch", i+1)
129 inverted = get_bool_value(document.body, "inverted", i+1, i+2, delete=True)
131 document.warning("Malformed LyX document: Missing 'inverted' tag in branch inset.")
134 branch = document.body[i][20:].strip()
135 if not branch in antibranches:
136 antibranch = "Anti-" + branch
137 while antibranch in antibranches:
138 antibranch = "x" + antibranch
139 antibranches[branch] = antibranch
141 antibranch = antibranches[branch]
142 document.body[i] = "\\begin_inset Branch " + antibranch
144 # now we need to add the new branches to the header
145 for old, new in antibranches.items():
146 i = find_token(document.header, "\\branch " + old, 0)
148 document.warning("Can't find branch %s even though we found it before!" % (old))
150 j = find_token(document.header, "\\end_branch", i)
152 document.warning("Malformed LyX document! Can't find end of branch " + old)
154 # ourbranches[old] - 1 inverts the selection status of the old branch
155 lines = ["\\branch " + new,
156 "\\selected " + str(ourbranches[old] - 1)]
157 # these are the old lines telling us color, etc.
158 lines += document.header[i+2 : j+1]
159 document.header[i:i] = lines
162 def revert_beamer_article_styles(document):
163 " Include (scr)article styles in beamer article "
165 beamer_articles = ["article-beamer", "scrarticle-beamer"]
166 if document.textclass not in beamer_articles:
169 inclusion = "article.layout"
170 if document.textclass == "scrarticle-beamer":
171 inclusion = "scrartcl.layout"
173 i = find_token(document.header, "\\begin_local_layout", 0)
175 k = find_token(document.header, "\\language", 0)
177 # this should not happen
178 document.warning("Malformed LyX document! No \\language header found!")
180 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
183 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
185 # this should not happen
186 document.warning("Malformed LyX document: Can't find end of local layout!")
189 document.header[i+1 : i+1] = [
190 "### Inserted by lyx2lyx (more [scr]article styles) ###",
191 "Input " + inclusion,
192 "Input beamer.layout",
193 "Provides geometry 0",
194 "Provides hyperref 0",
203 " \\usepackage{beamerarticle,pgf}",
204 " % this default might be overridden by plain title style",
205 " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
206 " \\AtBeginDocument{",
207 " \\let\\origtableofcontents=\\tableofcontents",
208 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
209 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
212 "### End of insertion by lyx2lyx (more [scr]article styles) ###"
216 def convert_beamer_article_styles(document):
217 " Remove included (scr)article styles in beamer article "
219 beamer_articles = ["article-beamer", "scrarticle-beamer"]
220 if document.textclass not in beamer_articles:
223 i = find_token(document.header, "\\begin_local_layout", 0)
227 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
229 # this should not happen
230 document.warning("Malformed LyX document: Can't find end of local layout!")
233 k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
235 l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
237 # this should not happen
238 document.warning("End of lyx2lyx local layout insertion not found!")
241 if k == i + 1 and l == j - 1:
242 # that was all the local layout there was
243 document.header[i : j + 1] = []
245 document.header[k : l + 1] = []
248 def revert_new_babel_languages(document):
249 """Revert "bosnian", "friulan", "macedonian", "piedmontese", "romansh".
251 Set the document language to English but use correct babel setting.
253 # TODO: currently, text parts in these languages are kept as-is
254 # and are converted to the document language by LyX 2.2 with warnings like
255 # LyX: Unknown language `romansh' [around line 273 of file lyx_2_3_test.22.lyx current token: 'romansh' context: 'InsetSpaceParams::read']
257 if document.language not in ["bosnian", "friulan", "macedonian",
258 "piedmontese", "romansh"]:
260 i = find_token(document.header, "\\language")
262 document.header[i] = "\\language english"
263 # ensure we use Babel:
264 # TODO: Polyglossia supports friulan, piedmontese, romansh
265 # but requires "\resetdefaultlanguage{...}" at begin of document.
266 j = find_token(document.header, "\\language_package default")
268 document.header[j] = "\\language_package babel"
269 k = find_token(document.header, "\\options")
271 document.header[k] = document.header[k].replace("\\options",
272 "\\options %s," % document.language)
274 l = find_token(document.header, "\\use_default_options")
275 document.header.insert(l + 1, "\\options " + document.language)
276 document.language = "english"
279 # def convert_new_babel_languages(document)
280 # set to native support if get_value(document.header, "\\options") in
281 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
282 # and "\\language_package babel".
284 def revert_amharic(document):
285 "Set the document language to English but assure Amharic output"
287 if document.language == "amharic":
288 document.language = "english"
289 i = find_token(document.header, "\\language amharic", 0)
291 document.header[i] = "\\language english"
292 j = find_token(document.header, "\\language_package default", 0)
294 document.header[j] = "\\language_package default"
295 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
296 document.body[2 : 2] = ["\\begin_layout Standard",
297 "\\begin_inset ERT", "status open", "",
298 "\\begin_layout Plain Layout", "", "",
300 "resetdefaultlanguage{amharic}",
301 "\\end_layout", "", "\\end_inset", "", "",
305 def revert_asturian(document):
306 "Set the document language to English but assure Asturian output"
308 if document.language == "asturian":
309 document.language = "english"
310 i = find_token(document.header, "\\language asturian", 0)
312 document.header[i] = "\\language english"
313 j = find_token(document.header, "\\language_package default", 0)
315 document.header[j] = "\\language_package default"
316 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
317 document.body[2 : 2] = ["\\begin_layout Standard",
318 "\\begin_inset ERT", "status open", "",
319 "\\begin_layout Plain Layout", "", "",
321 "resetdefaultlanguage{asturian}",
322 "\\end_layout", "", "\\end_inset", "", "",
326 def revert_kannada(document):
327 "Set the document language to English but assure Kannada output"
329 if document.language == "kannada":
330 document.language = "english"
331 i = find_token(document.header, "\\language kannada", 0)
333 document.header[i] = "\\language english"
334 j = find_token(document.header, "\\language_package default", 0)
336 document.header[j] = "\\language_package default"
337 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
338 document.body[2 : 2] = ["\\begin_layout Standard",
339 "\\begin_inset ERT", "status open", "",
340 "\\begin_layout Plain Layout", "", "",
342 "resetdefaultlanguage{kannada}",
343 "\\end_layout", "", "\\end_inset", "", "",
347 def revert_khmer(document):
348 "Set the document language to English but assure Khmer output"
350 if document.language == "khmer":
351 document.language = "english"
352 i = find_token(document.header, "\\language khmer", 0)
354 document.header[i] = "\\language english"
355 j = find_token(document.header, "\\language_package default", 0)
357 document.header[j] = "\\language_package default"
358 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
359 document.body[2 : 2] = ["\\begin_layout Standard",
360 "\\begin_inset ERT", "status open", "",
361 "\\begin_layout Plain Layout", "", "",
363 "resetdefaultlanguage{khmer}",
364 "\\end_layout", "", "\\end_inset", "", "",
368 def revert_urdu(document):
369 "Set the document language to English but assure Urdu output"
371 if document.language == "urdu":
372 document.language = "english"
373 i = find_token(document.header, "\\language urdu", 0)
375 document.header[i] = "\\language english"
376 j = find_token(document.header, "\\language_package default", 0)
378 document.header[j] = "\\language_package default"
379 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
380 document.body[2 : 2] = ["\\begin_layout Standard",
381 "\\begin_inset ERT", "status open", "",
382 "\\begin_layout Plain Layout", "", "",
384 "resetdefaultlanguage{urdu}",
385 "\\end_layout", "", "\\end_inset", "", "",
389 def revert_syriac(document):
390 "Set the document language to English but assure Syriac output"
392 if document.language == "syriac":
393 document.language = "english"
394 i = find_token(document.header, "\\language syriac", 0)
396 document.header[i] = "\\language english"
397 j = find_token(document.header, "\\language_package default", 0)
399 document.header[j] = "\\language_package default"
400 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
401 document.body[2 : 2] = ["\\begin_layout Standard",
402 "\\begin_inset ERT", "status open", "",
403 "\\begin_layout Plain Layout", "", "",
405 "resetdefaultlanguage{syriac}",
406 "\\end_layout", "", "\\end_inset", "", "",
410 def revert_quotes(document):
411 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
413 # First handle verbatim insets
416 while i < len(document.body):
417 words = document.body[i].split()
418 if len(words) > 1 and words[0] == "\\begin_inset" and \
419 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
420 j = find_end_of_inset(document.body, i)
423 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
427 k = find_token(document.body, '\\begin_inset Quotes', i, j)
431 l = find_end_of_inset(document.body, k)
433 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
437 if document.body[k].endswith("s"):
439 document.body[k:l+2] = [replace]
444 # Now verbatim layouts
447 while i < len(document.body):
448 words = document.body[i].split()
449 if len(words) > 1 and words[0] == "\\begin_layout" and \
450 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
451 j = find_end_of_layout(document.body, i)
453 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
457 k = find_token(document.body, '\\begin_inset Quotes', i, j)
461 l = find_end_of_inset(document.body, k)
463 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
467 if document.body[k].endswith("s"):
469 document.body[k:l+2] = [replace]
475 if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
481 k = find_token(document.body, '\\begin_inset Quotes', i)
484 l = find_end_of_inset(document.body, k)
486 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
490 parent = get_containing_layout(document.body, k)
491 ql = find_token_backwards(document.body, "\\lang", k)
492 if ql == -1 or ql < parent[1]:
493 hebrew = document.language == "hebrew"
494 elif document.body[ql] == "\\lang hebrew":
498 if document.body[k].endswith("s"):
500 document.body[k:l+2] = [replace]
504 def revert_iopart(document):
505 " Input new styles via local layout "
506 if document.textclass != "iopart":
509 i = find_token(document.header, "\\begin_local_layout", 0)
511 k = find_token(document.header, "\\language", 0)
513 # this should not happen
514 document.warning("Malformed LyX document! No \\language header found!")
516 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
519 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
521 # this should not happen
522 document.warning("Malformed LyX document! Can't find end of local layout!")
525 document.header[i+1 : i+1] = [
526 "### Inserted by lyx2lyx (stdlayouts) ###",
527 "Input stdlayouts.inc",
528 "### End of insertion by lyx2lyx (stdlayouts) ###"
532 def convert_iopart(document):
533 " Remove local layout we added, if it is there "
534 if document.textclass != "iopart":
537 i = find_token(document.header, "\\begin_local_layout", 0)
541 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
543 # this should not happen
544 document.warning("Malformed LyX document! Can't find end of local layout!")
547 k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
549 l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
551 # this should not happen
552 document.warning("End of lyx2lyx local layout insertion not found!")
554 if k == i + 1 and l == j - 1:
555 # that was all the local layout there was
556 document.header[i : j + 1] = []
558 document.header[k : l + 1] = []
561 def convert_quotestyle(document):
562 " Convert \\quotes_language to \\quotes_style "
563 i = find_token(document.header, "\\quotes_language", 0)
565 document.warning("Malformed LyX document! Can't find \\quotes_language!")
567 val = get_value(document.header, "\\quotes_language", i)
568 document.header[i] = "\\quotes_style " + val
571 def revert_quotestyle(document):
572 " Revert \\quotes_style to \\quotes_language "
573 i = find_token(document.header, "\\quotes_style", 0)
575 document.warning("Malformed LyX document! Can't find \\quotes_style!")
577 val = get_value(document.header, "\\quotes_style", i)
578 document.header[i] = "\\quotes_language " + val
581 def revert_plainquote(document):
582 " Revert plain quote insets "
584 # First, revert style setting
585 i = find_token(document.header, "\\quotes_style plain", 0)
587 document.header[i] = "\\quotes_style english"
593 k = find_token(document.body, '\\begin_inset Quotes q', i)
596 l = find_end_of_inset(document.body, k)
598 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
602 if document.body[k].endswith("s"):
604 document.body[k:l+2] = [replace]
608 def convert_frenchquotes(document):
609 " Convert french quote insets to swiss "
611 # First, revert style setting
612 i = find_token(document.header, "\\quotes_style french", 0)
614 document.header[i] = "\\quotes_style swiss"
619 i = find_token(document.body, '\\begin_inset Quotes f', i)
622 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
623 newval = val.replace("f", "c", 1)
624 document.body[i] = document.body[i].replace(val, newval)
628 def revert_swissquotes(document):
629 " Revert swiss quote insets to french "
631 # First, revert style setting
632 i = find_token(document.header, "\\quotes_style swiss", 0)
634 document.header[i] = "\\quotes_style french"
639 i = find_token(document.body, '\\begin_inset Quotes c', i)
642 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
643 newval = val.replace("c", "f", 1)
644 document.body[i] = document.body[i].replace(val, newval)
648 def revert_britishquotes(document):
649 " Revert british quote insets to english "
651 # First, revert style setting
652 i = find_token(document.header, "\\quotes_style british", 0)
654 document.header[i] = "\\quotes_style english"
659 i = find_token(document.body, '\\begin_inset Quotes b', i)
662 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
663 newval = val.replace("b", "e", 1)
666 newval = newval.replace("d", "s")
669 newval = newval.replace("s", "d")
670 document.body[i] = document.body[i].replace(val, newval)
674 def revert_swedishgquotes(document):
675 " Revert swedish quote insets "
677 # First, revert style setting
678 i = find_token(document.header, "\\quotes_style swedishg", 0)
680 document.header[i] = "\\quotes_style danish"
685 i = find_token(document.body, '\\begin_inset Quotes w', i)
688 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
691 newval = val.replace("w", "a", 1).replace("r", "l")
694 newval = val.replace("w", "s", 1)
695 document.body[i] = document.body[i].replace(val, newval)
699 def revert_frenchquotes(document):
700 " Revert french inner quote insets "
704 i = find_token(document.body, '\\begin_inset Quotes f', i)
707 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
710 newval = val.replace("f", "e", 1).replace("s", "d")
711 document.body[i] = document.body[i].replace(val, newval)
715 def revert_frenchinquotes(document):
716 " Revert inner frenchin quote insets "
718 # First, revert style setting
719 i = find_token(document.header, "\\quotes_style frenchin", 0)
721 document.header[i] = "\\quotes_style french"
726 i = find_token(document.body, '\\begin_inset Quotes i', i)
729 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
730 newval = val.replace("i", "f", 1)
733 newval = newval.replace("s", "d")
734 document.body[i] = document.body[i].replace(val, newval)
738 def revert_russianquotes(document):
739 " Revert russian quote insets "
741 # First, revert style setting
742 i = find_token(document.header, "\\quotes_style russian", 0)
744 document.header[i] = "\\quotes_style french"
749 i = find_token(document.body, '\\begin_inset Quotes r', i)
752 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
756 newval = val.replace("r", "g", 1).replace("s", "d")
759 newval = val.replace("r", "f", 1)
760 document.body[i] = document.body[i].replace(val, newval)
764 def revert_dynamicquotes(document):
765 " Revert dynamic quote insets "
767 # First, revert header
768 i = find_token(document.header, "\\dynamic_quotes", 0)
770 del document.header[i]
774 i = find_token(document.header, "\\quotes_style", 0)
776 document.warning("Malformed document! Missing \\quotes_style")
778 style = get_value(document.header, "\\quotes_style", i)
781 if style == "english":
783 elif style == "swedish":
785 elif style == "german":
787 elif style == "polish":
789 elif style == "swiss":
791 elif style == "danish":
793 elif style == "plain":
795 elif style == "british":
797 elif style == "swedishg":
799 elif style == "french":
801 elif style == "frenchin":
803 elif style == "russian":
806 # now transform the insets
809 i = find_token(document.body, '\\begin_inset Quotes x', i)
812 document.body[i] = document.body[i].replace("x", s)
816 def revert_cjkquotes(document):
817 " Revert cjk quote insets "
821 i = find_token(document.header, "\\quotes_style", 0)
823 document.warning("Malformed document! Missing \\quotes_style")
825 style = get_value(document.header, "\\quotes_style", i)
827 global_cjk = style.find("cjk") != -1
830 document.header[i] = "\\quotes_style english"
831 # transform dynamic insets
833 if style == "cjkangle":
837 i = find_token(document.body, '\\begin_inset Quotes x', i)
840 document.body[i] = document.body[i].replace("x", s)
843 cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
848 k = find_token(document.body, '\\begin_inset Quotes j', i)
851 l = find_end_of_inset(document.body, k)
853 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
857 parent = get_containing_layout(document.body, k)
858 ql = find_token_backwards(document.body, "\\lang", k)
859 if ql == -1 or ql < parent[1]:
860 cjk = document.language in cjk_langs
861 elif document.body[ql].split()[1] in cjk_langs:
863 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
870 replace = [u"\u300E"]
872 replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
876 replace = [u"\u300F"]
878 replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
884 replace = [u"\u300C"]
886 replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
890 replace = [u"\u300D"]
892 replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
894 document.body[k:l+1] = replace
900 k = find_token(document.body, '\\begin_inset Quotes k', i)
903 l = find_end_of_inset(document.body, k)
905 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
909 parent = get_containing_layout(document.body, k)
910 ql = find_token_backwards(document.body, "\\lang", k)
911 if ql == -1 or ql < parent[1]:
912 cjk = document.language in cjk_langs
913 elif document.body[ql].split()[1] in cjk_langs:
915 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
922 replace = [u"\u3008"]
924 replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
928 replace = [u"\u3009"]
930 replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
936 replace = [u"\u300A"]
938 replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
942 replace = [u"\u300B"]
944 replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
946 document.body[k:l+1] = replace
950 def convert_crimson(document):
951 """Transform preamble code to native font setting."""
953 i = find_substring(document.preamble, "{cochineal}")
956 # Find and delete user-preamble code:
957 if document.preamble[i] == "\\usepackage[proportional,osf]{cochineal}":
959 elif document.preamble[i] == "\\usepackage{cochineal}":
963 del document.preamble[i]
964 if i and document.preamble[i-1] == "% Added by lyx2lyx":
965 del document.preamble[i-1]
967 # Convert to native font setting:
968 j = find_token(document.header, '\\font_roman')
970 romanfont = ['\font_roman', '"cochineal"', '"default"']
972 romanfont = document.header[j].split()
973 romanfont[1] = '"cochineal"'
974 document.header[j] = " ".join(romanfont)
976 set_bool_value(document.header, '\\font_osf', osf)
977 except ValueError: # no \\font_osf setting in document.header
979 document.header.insert(-1, "\\font_osf true")
982 def revert_crimson(document):
983 " Revert native Cochineal/Crimson font definition to LaTeX "
985 i = find_token(document.header, '\\font_roman "cochineal"')
988 # replace unsupported font setting
989 document.header[i] = document.header[i].replace("cochineal", "default")
990 # no need for preamble code with system fonts
991 if get_bool_value(document.header, "\\use_non_tex_fonts"):
993 # transfer old style figures setting to package options
994 j = find_token(document.header, "\\font_osf true")
996 options = "[proportional,osf]"
997 document.header[j] = "\\font_osf false"
1000 add_to_preamble(document, ["\\usepackage%s{cochineal}"%options])
1003 def revert_cochinealmath(document):
1004 " Revert cochineal newtxmath definitions to LaTeX "
1006 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1007 i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
1009 add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
1010 document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
1013 def revert_labelonly(document):
1014 " Revert labelonly tag for InsetRef "
1017 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1020 j = find_end_of_inset(document.body, i)
1022 document.warning("Can't find end of reference inset at line %d!!" %(i))
1025 k = find_token(document.body, "LatexCommand labelonly", i, j)
1029 label = get_quoted_value(document.body, "reference", i, j)
1031 document.warning("Can't find label for reference at line %d!" %(i))
1034 document.body[i:j+1] = put_cmd_in_ert([label])
1038 def revert_plural_refs(document):
1039 " Revert plural and capitalized references "
1040 i = find_token(document.header, "\\use_refstyle 1", 0)
1041 use_refstyle = (i != 0)
1045 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1048 j = find_end_of_inset(document.body, i)
1050 document.warning("Can't find end of reference inset at line %d!!" %(i))
1054 plural = caps = suffix = False
1055 k = find_token(document.body, "LaTeXCommand formatted", i, j)
1056 if k != -1 and use_refstyle:
1057 plural = get_bool_value(document.body, "plural", i, j, False)
1058 caps = get_bool_value(document.body, "caps", i, j, False)
1059 label = get_quoted_value(document.body, "reference", i, j)
1062 (prefix, suffix) = label.split(":", 1)
1064 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1066 document.warning("Can't find label for reference at line %d!" % (i))
1068 # this effectively tests also for use_refstyle and a formatted reference
1069 # we do this complicated test because we would otherwise do this erasure
1070 # over and over and over
1071 if not ((plural or caps) and suffix):
1072 del_token(document.body, "plural", i, j)
1073 del_token(document.body, "caps", i, j - 1) # since we deleted a line
1078 prefix = prefix[0].title() + prefix[1:]
1079 cmd = "\\" + prefix + "ref"
1082 cmd += "{" + suffix + "}"
1083 document.body[i:j+1] = put_cmd_in_ert([cmd])
1087 def revert_noprefix(document):
1088 " Revert labelonly tags with 'noprefix' set "
1091 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1094 j = find_end_of_inset(document.body, i)
1096 document.warning("Can't find end of reference inset at line %d!!" %(i))
1099 k = find_token(document.body, "LatexCommand labelonly", i, j)
1102 noprefix = get_bool_value(document.body, "noprefix", i, j)
1104 # either it was not a labelonly command, or else noprefix was not set.
1105 # in that case, we just delete the option.
1106 del_token(document.body, "noprefix", i, j)
1109 label = get_quoted_value(document.body, "reference", i, j)
1111 document.warning("Can't find label for reference at line %d!" %(i))
1115 (prefix, suffix) = label.split(":", 1)
1117 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1118 # we'll leave this as an ordinary labelonly reference
1119 del_token(document.body, "noprefix", i, j)
1122 document.body[i:j+1] = put_cmd_in_ert([suffix])
1126 def revert_biblatex(document):
1127 " Revert biblatex support "
1133 # 1. Get cite engine
1135 i = find_token(document.header, "\\cite_engine", 0)
1137 document.warning("Malformed document! Missing \\cite_engine")
1139 engine = get_value(document.header, "\\cite_engine", i)
1141 # 2. Store biblatex state and revert to natbib
1143 if engine in ["biblatex", "biblatex-natbib"]:
1145 document.header[i] = "\\cite_engine natbib"
1147 # 3. Store and remove new document headers
1149 i = find_token(document.header, "\\biblatex_bibstyle", 0)
1151 bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
1152 del document.header[i]
1155 i = find_token(document.header, "\\biblatex_citestyle", 0)
1157 citestyle = get_value(document.header, "\\biblatex_citestyle", i)
1158 del document.header[i]
1161 i = find_token(document.header, "\\biblio_options", 0)
1163 biblio_options = get_value(document.header, "\\biblio_options", i)
1164 del document.header[i]
1167 bbxopts = "[natbib=true"
1169 bbxopts += ",bibstyle=" + bibstyle
1171 bbxopts += ",citestyle=" + citestyle
1172 if biblio_options != "":
1173 bbxopts += "," + biblio_options
1175 add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1185 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1188 j = find_end_of_inset(document.body, i)
1190 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1193 bibs = get_quoted_value(document.body, "bibfiles", i, j)
1194 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1197 bibresources += bibs.split(",")
1199 document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1200 # remove biblatexopts line
1201 k = find_token(document.body, "biblatexopts", i, j)
1203 del document.body[k]
1204 # Re-find inset end line
1205 j = find_end_of_inset(document.body, i)
1206 # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1208 pcmd = "printbibliography"
1210 pcmd += "[" + opts + "]"
1211 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1212 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1213 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1214 "status open", "", "\\begin_layout Plain Layout" ]
1215 repl += document.body[i:j+1]
1216 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1217 document.body[i:j+1] = repl
1223 for b in bibresources:
1224 add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1226 # 2. Citation insets
1228 # Specific citation insets used in biblatex that need to be reverted to ERT
1231 "citebyear" : "citeyear",
1232 "citeyear" : "cite*",
1233 "Footcite" : "Smartcite",
1234 "footcite" : "smartcite",
1235 "Autocite" : "Autocite",
1236 "autocite" : "autocite",
1237 "citetitle" : "citetitle",
1238 "citetitle*" : "citetitle*",
1239 "fullcite" : "fullcite",
1240 "footfullcite" : "footfullcite",
1241 "supercite" : "supercite",
1242 "citeauthor" : "citeauthor",
1243 "citeauthor*" : "citeauthor*",
1244 "Citeauthor" : "Citeauthor",
1245 "Citeauthor*" : "Citeauthor*"
1248 # All commands accepted by LyX < 2.3. Everything else throws an error.
1249 old_citations = ["cite", "nocite", "citet", "citep", "citealt", "citealp",
1250 "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",
1251 "citealt*", "citealp*", "citeauthor*", "Citet", "Citep",
1252 "Citealt", "Citealp", "Citeauthor", "Citet*", "Citep*",
1253 "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",
1254 "footcitet", "footcitep", "footcitealt", "footcitealp",
1255 "footciteauthor", "footciteyear", "footciteyearpar",
1256 "citefield", "citetitle", "cite*" ]
1260 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1263 j = find_end_of_inset(document.body, i)
1265 document.warning("Can't find end of citation inset at line %d!!" %(i))
1268 k = find_token(document.body, "LatexCommand", i, j)
1270 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1273 cmd = get_value(document.body, "LatexCommand", k)
1274 if biblatex and cmd in list(new_citations.keys()):
1275 pre = get_quoted_value(document.body, "before", i, j)
1276 post = get_quoted_value(document.body, "after", i, j)
1277 key = get_quoted_value(document.body, "key", i, j)
1279 document.warning("Citation inset at line %d does not have a key!" %(i))
1281 # Replace known new commands with ERT
1282 res = "\\" + new_citations[cmd]
1284 res += "[" + pre + "]"
1286 res += "[" + post + "]"
1289 res += "{" + key + "}"
1290 document.body[i:j+1] = put_cmd_in_ert([res])
1291 elif cmd not in old_citations:
1292 # Reset unknown commands to cite. This is what LyX does as well
1293 # (but LyX 2.2 would break on unknown commands)
1294 document.body[k] = "LatexCommand cite"
1295 document.warning("Reset unknown cite command '%s' with cite" % cmd)
1298 # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1300 i = find_token(document.header, "\\begin_local_layout", 0)
1302 k = find_token(document.header, "\\language", 0)
1304 # this should not happen
1305 document.warning("Malformed LyX document! No \\language header found!")
1307 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1310 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1312 # this should not happen
1313 document.warning("Malformed LyX document! Can't find end of local layout!")
1316 document.header[i+1 : i+1] = [
1317 "### Inserted by lyx2lyx (biblatex emulation) ###",
1318 "Provides natbib 1",
1319 "### End of insertion by lyx2lyx (biblatex emulation) ###"
1323 def revert_citekeyonly(document):
1324 " Revert keyonly cite command to ERT "
1328 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1331 j = find_end_of_inset(document.body, i)
1333 document.warning("Can't find end of citation inset at line %d!!" %(i))
1336 k = find_token(document.body, "LatexCommand", i, j)
1338 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1341 cmd = get_value(document.body, "LatexCommand", k)
1342 if cmd != "keyonly":
1346 key = get_quoted_value(document.body, "key", i, j)
1348 document.warning("Citation inset at line %d does not have a key!" %(i))
1349 # Replace known new commands with ERT
1350 document.body[i:j+1] = put_cmd_in_ert([key])
1355 def revert_bibpackopts(document):
1356 " Revert support for natbib/jurabib package options "
1359 i = find_token(document.header, "\\cite_engine", 0)
1361 document.warning("Malformed document! Missing \\cite_engine")
1363 engine = get_value(document.header, "\\cite_engine", i)
1366 if engine not in ["natbib", "jurabib"]:
1369 i = find_token(document.header, "\\biblio_options", 0)
1371 # Nothing to do if we have no options
1374 biblio_options = get_value(document.header, "\\biblio_options", i)
1375 del document.header[i]
1377 if not biblio_options:
1378 # Nothing to do for empty options
1381 i = find_token(document.header, "\\begin_local_layout", 0)
1383 k = find_token(document.header, "\\language", 0)
1385 # this should not happen
1386 document.warning("Malformed LyX document! No \\language header found!")
1388 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1391 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1393 # this should not happen
1394 document.warning("Malformed LyX document! Can't find end of local layout!")
1397 document.header[i+1 : i+1] = [
1398 "### Inserted by lyx2lyx (bibliography package options) ###",
1399 "PackageOptions " + engine + " " + biblio_options,
1400 "### End of insertion by lyx2lyx (bibliography package options) ###"
1404 def revert_qualicites(document):
1405 " Revert qualified citation list commands to ERT "
1407 # Citation insets that support qualified lists, with their LaTeX code
1411 "citet" : "textcites",
1412 "Citet" : "Textcites",
1413 "citep" : "parencites",
1414 "Citep" : "Parencites",
1415 "Footcite" : "Smartcites",
1416 "footcite" : "smartcites",
1417 "Autocite" : "Autocites",
1418 "autocite" : "autocites",
1423 i = find_token(document.header, "\\cite_engine", 0)
1425 document.warning("Malformed document! Missing \\cite_engine")
1427 engine = get_value(document.header, "\\cite_engine", i)
1429 biblatex = engine in ["biblatex", "biblatex-natbib"]
1433 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1436 j = find_end_of_inset(document.body, i)
1438 document.warning("Can't find end of citation inset at line %d!!" %(i))
1441 pres = find_token(document.body, "pretextlist", i, j)
1442 posts = find_token(document.body, "posttextlist", i, j)
1443 if pres == -1 and posts == -1:
1447 pretexts = get_quoted_value(document.body, "pretextlist", pres)
1448 posttexts = get_quoted_value(document.body, "posttextlist", posts)
1449 k = find_token(document.body, "LatexCommand", i, j)
1451 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1454 cmd = get_value(document.body, "LatexCommand", k)
1455 if biblatex and cmd in list(ql_citations.keys()):
1456 pre = get_quoted_value(document.body, "before", i, j)
1457 post = get_quoted_value(document.body, "after", i, j)
1458 key = get_quoted_value(document.body, "key", i, j)
1460 document.warning("Citation inset at line %d does not have a key!" %(i))
1462 keys = key.split(",")
1463 prelist = pretexts.split("\t")
1466 ppp = pp.split(" ", 1)
1467 premap[ppp[0]] = ppp[1]
1468 postlist = posttexts.split("\t")
1471 ppp = pp.split(" ", 1)
1472 postmap[ppp[0]] = ppp[1]
1473 # Replace known new commands with ERT
1474 if "(" in pre or ")" in pre:
1475 pre = "{" + pre + "}"
1476 if "(" in post or ")" in post:
1477 post = "{" + post + "}"
1478 res = "\\" + ql_citations[cmd]
1480 res += "(" + pre + ")"
1482 res += "(" + post + ")"
1486 if premap.get(kk, "") != "":
1487 res += "[" + premap[kk] + "]"
1488 if postmap.get(kk, "") != "":
1489 res += "[" + postmap[kk] + "]"
1490 elif premap.get(kk, "") != "":
1492 res += "{" + kk + "}"
1493 document.body[i:j+1] = put_cmd_in_ert([res])
1495 # just remove the params
1496 del document.body[posttexts]
1497 del document.body[pretexts]
1501 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1502 def convert_literalparam(document):
1503 " Add param literal "
1505 for inset in command_insets:
1508 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1511 j = find_end_of_inset(document.body, i)
1513 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1516 while i < j and document.body[i].strip() != '':
1518 # href is already fully latexified. Here we can switch off literal.
1520 document.body.insert(i, "literal \"false\"")
1522 document.body.insert(i, "literal \"true\"")
1525 def revert_literalparam(document):
1526 " Remove param literal "
1528 for inset in command_insets:
1531 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1534 j = find_end_of_inset(document.body, i)
1536 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1539 k = find_token(document.body, 'literal', i, j)
1543 del document.body[k]
1546 def revert_multibib(document):
1547 " Revert multibib support "
1549 # 1. Get cite engine
1551 i = find_token(document.header, "\\cite_engine", 0)
1553 document.warning("Malformed document! Missing \\cite_engine")
1555 engine = get_value(document.header, "\\cite_engine", i)
1557 # 2. Do we use biblatex?
1559 if engine in ["biblatex", "biblatex-natbib"]:
1562 # 3. Store and remove multibib document header
1564 i = find_token(document.header, "\\multibib", 0)
1566 multibib = get_value(document.header, "\\multibib", i)
1567 del document.header[i]
1572 # 4. The easy part: Biblatex
1574 i = find_token(document.header, "\\biblio_options", 0)
1576 k = find_token(document.header, "\\use_bibtopic", 0)
1578 # this should not happen
1579 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1581 document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1583 biblio_options = get_value(document.header, "\\biblio_options", i)
1585 biblio_options += ","
1586 biblio_options += "refsection=" + multibib
1587 document.header[i] = "\\biblio_options " + biblio_options
1592 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1595 j = find_end_of_inset(document.body, i)
1597 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1600 btprint = get_quoted_value(document.body, "btprint", i, j)
1601 if btprint != "bibbysection":
1604 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1605 # change btprint line
1606 k = find_token(document.body, "btprint", i, j)
1608 document.body[k] = "btprint \"btPrintCited\""
1609 # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1610 pcmd = "bibbysection"
1612 pcmd += "[" + opts + "]"
1613 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1614 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1615 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1616 "status open", "", "\\begin_layout Plain Layout" ]
1617 repl += document.body[i:j+1]
1618 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1619 document.body[i:j+1] = repl
1625 # 5. More tricky: Bibtex/Bibtopic
1626 k = find_token(document.header, "\\use_bibtopic", 0)
1628 # this should not happen
1629 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1631 document.header[k] = "\\use_bibtopic true"
1633 # Possible units. This assumes that the LyX name follows the std,
1634 # which might not always be the case. But it's as good as we can get.
1637 "chapter" : "Chapter",
1638 "section" : "Section",
1639 "subsection" : "Subsection",
1642 if multibib not in units.keys():
1643 document.warning("Unknown multibib value `%s'!" % nultibib)
1645 unit = units[multibib]
1649 i = find_token(document.body, "\\begin_layout " + unit, i)
1653 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1654 "\\begin_inset ERT", "status open", "",
1655 "\\begin_layout Plain Layout", "", "",
1657 "end{btUnit}", "\\end_layout",
1658 "\\begin_layout Plain Layout", "",
1661 "\\end_layout", "", "\\end_inset", "", "",
1665 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1666 "\\begin_inset ERT", "status open", "",
1667 "\\begin_layout Plain Layout", "", "",
1670 "\\end_layout", "", "\\end_inset", "", "",
1677 i = find_token(document.body, "\\end_body", i)
1678 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1679 "\\begin_inset ERT", "status open", "",
1680 "\\begin_layout Plain Layout", "", "",
1683 "\\end_layout", "", "\\end_inset", "", "",
1687 def revert_chapterbib(document):
1688 " Revert chapterbib support "
1690 # 1. Get cite engine
1692 i = find_token(document.header, "\\cite_engine", 0)
1694 document.warning("Malformed document! Missing \\cite_engine")
1696 engine = get_value(document.header, "\\cite_engine", i)
1698 # 2. Do we use biblatex?
1700 if engine in ["biblatex", "biblatex-natbib"]:
1703 # 3. Store multibib document header value
1705 i = find_token(document.header, "\\multibib", 0)
1707 multibib = get_value(document.header, "\\multibib", i)
1709 if not multibib or multibib != "child":
1713 # 4. remove multibib header
1714 del document.header[i]
1718 # find include insets
1721 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1724 j = find_end_of_inset(document.body, i)
1726 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1729 parent = get_containing_layout(document.body, i)
1732 # Insert ERT \\newrefsection before inset
1733 beg = ["\\begin_layout Standard",
1734 "\\begin_inset ERT", "status open", "",
1735 "\\begin_layout Plain Layout", "", "",
1738 "\\end_layout", "", "\\end_inset", "", "",
1740 document.body[parbeg-1:parbeg-1] = beg
1745 # 6. Bibtex/Bibtopic
1746 i = find_token(document.header, "\\use_bibtopic", 0)
1748 # this should not happen
1749 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1751 if get_value(document.header, "\\use_bibtopic", i) == "true":
1752 # find include insets
1755 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1758 j = find_end_of_inset(document.body, i)
1760 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1763 parent = get_containing_layout(document.body, i)
1767 # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1768 beg = ["\\begin_layout Standard",
1769 "\\begin_inset ERT", "status open", "",
1770 "\\begin_layout Plain Layout", "", "",
1773 "\\end_layout", "", "\\end_inset", "", "",
1775 end = ["\\begin_layout Standard",
1776 "\\begin_inset ERT", "status open", "",
1777 "\\begin_layout Plain Layout", "", "",
1780 "\\end_layout", "", "\\end_inset", "", "",
1782 document.body[parend+1:parend+1] = end
1783 document.body[parbeg-1:parbeg-1] = beg
1784 j += len(beg) + len(end)
1788 # 7. Chapterbib proper
1789 add_to_preamble(document, ["\\usepackage{chapterbib}"])
1792 def convert_dashligatures(document):
1793 """Set 'use_dash_ligatures' according to content.
1795 # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
1796 # set use_dash_ligatures to True if found, to None else.
1797 use_dash_ligatures = del_complete_lines(document.preamble,
1798 ['% Added by lyx2lyx',
1799 r'\renewcommand{\textendash}{--}',
1800 r'\renewcommand{\textemdash}{---}']) or None
1802 if use_dash_ligatures is None:
1803 # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
1804 # or "\threehyphens\n" as interim representation for -- an ---.)
1805 lines = document.body
1806 has_literal_dashes = has_ligature_dashes = False
1807 dash_pattern = re.compile(u".*[\u2013\u2014]|\\twohyphens|\\threehyphens")
1810 # skip lines without dashes:
1811 i = find_re(lines, dash_pattern, i+1)
1815 # skip label width string (see bug 10243):
1816 if line.startswith("\\labelwidthstring"):
1818 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1820 inset_type, start, end = get_containing_inset(lines, i)
1821 except TypeError: # no containing inset
1822 inset_type, start, end = "no inset", -1, -1
1823 if (inset_type.split()[0] in
1824 ["CommandInset", "ERT", "External", "Formula",
1825 "FormulaMacro", "Graphics", "IPA", "listings"]
1826 or inset_type == "Flex Code"):
1830 layoutname, start, end, j = get_containing_layout(lines, i)
1831 except TypeError: # no (or malformed) containing layout
1832 document.warning("Malformed LyX document: "
1833 "Can't find layout at line %d" % i)
1835 if layoutname == "LyX-Code":
1839 # literal dash followed by a non-white-character or no-break space:
1840 if re.search(u"[\u2013\u2014]([\S\u00A0\u202F\u2060]|$)",
1841 line, flags=re.UNICODE):
1842 has_literal_dashes = True
1843 # ligature dash followed by non-white-char or no-break space on next line:
1844 if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
1845 re.match(u"[\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
1846 has_ligature_dashes = True
1847 if has_literal_dashes and has_ligature_dashes:
1848 # TODO: insert a warning note in the document?
1849 document.warning('This document contained both literal and '
1850 '"ligature" dashes.\n Line breaks may have changed. '
1851 'See UserGuide chapter 3.9.1 for details.')
1854 if has_literal_dashes and not has_ligature_dashes:
1855 use_dash_ligatures = False
1856 elif has_ligature_dashes and not has_literal_dashes:
1857 use_dash_ligatures = True
1859 # insert the setting if there is a preferred value
1860 if use_dash_ligatures is True:
1861 document.header.insert(-1, "\\use_dash_ligatures true")
1862 elif use_dash_ligatures is False:
1863 document.header.insert(-1, "\\use_dash_ligatures false")
1866 def revert_dashligatures(document):
1867 """Remove font ligature settings for en- and em-dashes.
1868 Revert conversion of \twodashes or \threedashes to literal dashes.
1870 use_dash_ligatures = del_value(document.header, "\\use_dash_ligatures")
1871 if use_dash_ligatures != "true" or document.backend != "latex":
1874 dash_pattern = re.compile(u".*[\u2013\u2014]")
1876 # skip lines without dashes:
1877 i = find_re(document.body, dash_pattern, i+1)
1880 line = document.body[i]
1881 # skip label width string (see bug 10243):
1882 if line.startswith("\\labelwidthstring"):
1884 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1886 inset_type, start, end = get_containing_inset(document.body, i)
1887 except TypeError: # no containing inset
1888 inset_type, start, end = "no inset", -1, -1
1889 if (inset_type.split()[0] in
1890 ["CommandInset", "ERT", "External", "Formula",
1891 "FormulaMacro", "Graphics", "IPA", "listings"]
1892 or inset_type == "Flex Code"):
1896 layoutname, start, end, j = get_containing_layout(document.body, i)
1897 except TypeError: # no (or malformed) containing layout
1898 document.warning("Malformed LyX document: "
1899 "Can't find layout at body line %d" % i)
1901 if layoutname == "LyX-Code":
1904 # TODO: skip replacement in typewriter fonts
1905 line = line.replace(u'\u2013', '\\twohyphens\n')
1906 line = line.replace(u'\u2014', '\\threehyphens\n')
1907 document.body[i:i+1] = line.split('\n')
1908 # redefine the dash LICRs to use ligature dashes:
1909 add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1910 r'\renewcommand{\textemdash}{---}'])
1913 def revert_noto(document):
1914 " Revert Noto font definitions to LaTeX "
1916 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1918 i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1920 add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1921 document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1922 i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1924 add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1925 document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1926 i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1928 add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1929 document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1932 def revert_xout(document):
1933 " Reverts \\xout font attribute "
1934 changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1936 insert_to_preamble(document, \
1937 ['% for proper cross-out',
1938 '\\PassOptionsToPackage{normalem}{ulem}',
1939 '\\usepackage{ulem}'])
1942 def convert_mathindent(document):
1943 """Add the \\is_math_indent tag.
1945 k = find_token(document.header, "\\quotes_style") # where to insert
1946 # check if the document uses the class option "fleqn"
1947 options = get_value(document.header, "\\options")
1948 if 'fleqn' in options:
1949 document.header.insert(k, "\\is_math_indent 1")
1950 # delete the fleqn option
1951 i = find_token(document.header, "\\options")
1952 options = [option for option in options.split(",")
1953 if option.strip() != "fleqn"]
1955 document.header[i] = "\\options " + ",".join(options)
1957 del document.header[i]
1959 document.header.insert(k, "\\is_math_indent 0")
1961 def revert_mathindent(document):
1962 " Define mathindent if set in the document "
1963 # emulate and delete \math_indentation
1964 value = get_value(document.header, "\\math_indentation",
1965 default="default", delete=True)
1966 if value != "default":
1967 add_to_preamble(document, [r"\setlength{\mathindent}{%s}"%value])
1968 # delete \is_math_indent and emulate via document class option
1969 if not get_bool_value(document.header, "\\is_math_indent", delete=True):
1971 i = find_token(document.header, "\\options")
1973 document.header[i] = document.header[i].replace("\\options ",
1976 l = find_token(document.header, "\\use_default_options")
1977 document.header.insert(l, "\\options fleqn")
1980 def revert_baselineskip(document):
1981 " Revert baselineskips to TeX code "
1984 i = find_substring(document.body, "baselineskip%", i+1)
1987 if document.body[i].startswith("\\begin_inset VSpace"):
1988 # output VSpace inset as TeX code
1989 end = find_end_of_inset(document.body, i)
1991 document.warning("Malformed LyX document: "
1992 "Can't find end of VSpace inset at line %d." % i)
1994 # read out the value
1995 baselineskip = document.body[i].split()[-1]
1996 # check if it is the starred version
1997 star = '*' if '*' in document.body[i] else ''
1998 # now output TeX code
1999 cmd = "\\vspace%s{%s}" %(star, latex_length(baselineskip)[1])
2000 document.body[i:end+1] = put_cmd_in_ert(cmd)
2003 begin, end = is_in_inset(document.body, i, "\\begin_inset space \\hspace")
2005 # output space inset as TeX code
2006 baselineskip = document.body[i].split()[-1]
2007 star = '*' if '*' in document.body[i-1] else ''
2008 cmd = "\\hspace%s{%s}" %(star, latex_length(baselineskip)[1])
2009 document.body[begin:end+1] = put_cmd_in_ert(cmd)
2012 def revert_rotfloat(document):
2013 " Revert placement options for rotated floats "
2018 i = find_token(document.body, "sideways true", i)
2021 if not document.body[i-2].startswith('placement '):
2024 # we found a sideways float with placement options
2025 # at first store the placement
2026 beg = document.body[i-2].rfind(" ");
2027 placement = document.body[i-2][beg+1:]
2028 # check if the option'H' is used
2029 if placement.find("H") != -1:
2030 add_to_preamble(document, ["\\usepackage{float}"])
2031 # now check if it is a starred type
2032 if document.body[i-1].find("wide true") != -1:
2036 # store the float type
2037 beg = document.body[i-3].rfind(" ");
2038 fType = document.body[i-3][beg+1:]
2039 # now output TeX code
2040 endInset = find_end_of_inset(document.body, i-3)
2042 document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2045 document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
2046 document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
2047 add_to_preamble(document, ["\\usepackage{rotfloat}"])
2052 allowbreak_emulation = [r"\begin_inset space \hspace{}",
2057 def convert_allowbreak(document):
2058 " Zero widths Space-inset -> \SpecialChar allowbreak. "
2059 lines = document.body
2060 i = find_complete_lines(lines, allowbreak_emulation, 2)
2062 lines[i-1:i+4] = [lines[i-1] + r"\SpecialChar allowbreak"]
2063 i = find_complete_lines(lines, allowbreak_emulation, i+3)
2066 def revert_allowbreak(document):
2067 " \SpecialChar allowbreak -> Zero widths Space-inset. "
2069 lines = document.body
2070 while i < len(lines):
2071 if lines[i].endswith(r"\SpecialChar allowbreak"):
2072 lines[i:i+1] = [lines[i].replace(r"\SpecialChar allowbreak", "")
2073 ] + allowbreak_emulation
2079 def convert_mathnumberpos(document):
2080 " add the \\math_number_before tag "
2081 # check if the document uses the class option "leqno"
2082 i = find_token(document.header, "\\options")
2083 k = find_token(document.header, "\\quotes_style")
2084 if 'leqno' in document.header[i]:
2085 document.header.insert(k, "\\math_number_before 1")
2086 # delete the found option
2087 document.header[i] = document.header[i].replace(",leqno", "")
2088 document.header[i] = document.header[i].replace(", leqno", "")
2089 document.header[i] = document.header[i].replace("leqno,", "")
2090 if 'leqno' in document.header[i]:
2091 # then we have leqno as the only option
2092 del document.header[i]
2094 document.header.insert(k, "\\math_number_before 0")
2097 def revert_mathnumberpos(document):
2098 """Remove \\math_number_before tag,
2099 add the document class option leqno if required.
2101 math_number_before = get_bool_value(document.header,
2102 '\\math_number_before', delete=True)
2103 if math_number_before:
2104 i = find_token(document.header, "\\options")
2105 if i != -1 and 'leqno' not in document.header[i]:
2106 document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
2108 i = find_token(document.header, "\\use_default_options")
2109 document.header.insert(i, "\\options leqno")
2112 def convert_mathnumberingname(document):
2113 " rename the \\math_number_before tag to \\math_numbering_side "
2114 i = find_token(document.header, "\\math_number_before")
2115 math_number_before = get_bool_value(document.header, '\\math_number_before', i)
2116 if math_number_before:
2117 document.header[i] = "\\math_numbering_side left"
2119 # check if the document uses the class option "reqno"
2120 k = find_token(document.header, "\\options")
2121 if 'reqno' in document.header[k]:
2122 document.header[i] = "\\math_numbering_side right"
2123 # delete the found option
2124 document.header[k] = document.header[k].replace(",reqno", "")
2125 document.header[k] = document.header[k].replace(", reqno", "")
2126 document.header[k] = document.header[k].replace("reqno,", "")
2127 if 'reqno' in document.header[k]:
2128 # then we have reqno as the only option
2129 del document.header[k]
2131 document.header[i] = "\\math_numbering_side default"
2134 def revert_mathnumberingname(document):
2135 " rename the \\math_numbering_side tag back to \\math_number_before "
2136 i = find_token(document.header, "\\math_numbering_side")
2137 math_numbering_side = get_value(document.header, '\\math_numbering_side', i)
2138 # rename tag and set boolean value:
2139 if math_numbering_side == "left":
2140 document.header[i] = "\\math_number_before 1"
2141 elif math_numbering_side == "right":
2142 # also add the option reqno:
2143 document.header[i] = "\\math_number_before 0"
2144 k = find_token(document.header, "\\options")
2145 if k != -1 and 'reqno' not in document.header[k]:
2146 document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
2148 l = find_token(document.header, "\\use_default_options", 0)
2149 document.header.insert(l, "\\options reqno")
2151 document.header[i] = "\\math_number_before 0"
2154 def convert_minted(document):
2155 " add the \\use_minted tag "
2156 i = find_token(document.header, "\\index ")
2157 document.header.insert(i, "\\use_minted 0")
2160 def revert_minted(document):
2161 " remove the \\use_minted tag "
2162 del_token(document.header, "\\use_minted")
2169 supported_versions = ["2.3.0", "2.3"]
2171 [509, [convert_microtype]],
2172 [510, [convert_dateinset]],
2173 [511, [convert_ibranches]],
2174 [512, [convert_beamer_article_styles]],
2178 [516, [convert_inputenc]],
2180 [518, [convert_iopart]],
2181 [519, [convert_quotestyle]],
2183 [521, [convert_frenchquotes]],
2186 [524, [convert_crimson]],
2194 [532, [convert_literalparam]],
2197 [535, [convert_dashligatures]],
2200 [538, [convert_mathindent]],
2203 [541, [convert_allowbreak]],
2204 [542, [convert_mathnumberpos]],
2205 [543, [convert_mathnumberingname]],
2206 [544, [convert_minted]]
2210 [543, [revert_minted]],
2211 [542, [revert_mathnumberingname]],
2212 [541, [revert_mathnumberpos]],
2213 [540, [revert_allowbreak]],
2214 [539, [revert_rotfloat]],
2215 [538, [revert_baselineskip]],
2216 [537, [revert_mathindent]],
2217 [536, [revert_xout]],
2218 [535, [revert_noto]],
2219 [534, [revert_dashligatures]],
2220 [533, [revert_chapterbib]],
2221 [532, [revert_multibib]],
2222 [531, [revert_literalparam]],
2223 [530, [revert_qualicites]],
2224 [529, [revert_bibpackopts]],
2225 [528, [revert_citekeyonly]],
2226 [527, [revert_biblatex]],
2227 [526, [revert_noprefix]],
2228 [525, [revert_plural_refs]],
2229 [524, [revert_labelonly]],
2230 [523, [revert_crimson, revert_cochinealmath]],
2231 [522, [revert_cjkquotes]],
2232 [521, [revert_dynamicquotes]],
2233 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2234 [519, [revert_plainquote]],
2235 [518, [revert_quotestyle]],
2236 [517, [revert_iopart]],
2237 [516, [revert_quotes]],
2239 [514, [revert_urdu, revert_syriac]],
2240 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2241 [512, [revert_new_babel_languages]],
2242 [511, [revert_beamer_article_styles]],
2243 [510, [revert_ibranches]],
2245 [508, [revert_microtype]]
2249 if __name__ == "__main__":