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 #document.warning(branch)
136 if not branch in antibranches:
137 antibranch = "Anti-" + branch
138 while antibranch in antibranches:
139 antibranch = "x" + antibranch
140 antibranches[branch] = antibranch
142 antibranch = antibranches[branch]
143 #document.warning(antibranch)
144 document.body[i] = "\\begin_inset Branch " + antibranch
146 # now we need to add the new branches to the header
147 for old, new in antibranches.items():
148 i = find_token(document.header, "\\branch " + old, 0)
150 document.warning("Can't find branch %s even though we found it before!" % (old))
152 j = find_token(document.header, "\\end_branch", i)
154 document.warning("Malformed LyX document! Can't find end of branch " + old)
156 # ourbranches[old] - 1 inverts the selection status of the old branch
157 lines = ["\\branch " + new,
158 "\\selected " + str(ourbranches[old] - 1)]
159 # these are the old lines telling us color, etc.
160 lines += document.header[i+2 : j+1]
161 document.header[i:i] = lines
164 def revert_beamer_article_styles(document):
165 " Include (scr)article styles in beamer article "
167 beamer_articles = ["article-beamer", "scrarticle-beamer"]
168 if document.textclass not in beamer_articles:
171 inclusion = "article.layout"
172 if document.textclass == "scrarticle-beamer":
173 inclusion = "scrartcl.layout"
175 i = find_token(document.header, "\\begin_local_layout", 0)
177 k = find_token(document.header, "\\language", 0)
179 # this should not happen
180 document.warning("Malformed LyX document! No \\language header found!")
182 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
185 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
187 # this should not happen
188 document.warning("Malformed LyX document: Can't find end of local layout!")
191 document.header[i+1 : i+1] = [
192 "### Inserted by lyx2lyx (more [scr]article styles) ###",
193 "Input " + inclusion,
194 "Input beamer.layout",
195 "Provides geometry 0",
196 "Provides hyperref 0",
205 " \\usepackage{beamerarticle,pgf}",
206 " % this default might be overridden by plain title style",
207 " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
208 " \\AtBeginDocument{",
209 " \\let\\origtableofcontents=\\tableofcontents",
210 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
211 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
214 "### End of insertion by lyx2lyx (more [scr]article styles) ###"
218 def convert_beamer_article_styles(document):
219 " Remove included (scr)article styles in beamer article "
221 beamer_articles = ["article-beamer", "scrarticle-beamer"]
222 if document.textclass not in beamer_articles:
225 i = find_token(document.header, "\\begin_local_layout", 0)
229 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
231 # this should not happen
232 document.warning("Malformed LyX document: Can't find end of local layout!")
235 k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
237 l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
239 # this should not happen
240 document.warning("End of lyx2lyx local layout insertion not found!")
243 if k == i + 1 and l == j - 1:
244 # that was all the local layout there was
245 document.header[i : j + 1] = []
247 document.header[k : l + 1] = []
250 def revert_new_babel_languages(document):
251 """Revert "bosnian", "friulan", "macedonian", "piedmontese", "romansh".
253 Set the document language to English but use correct babel setting.
255 # TODO: currently, text parts in these languages are kept as-is
256 # and are converted to the document language by LyX 2.2 with warnings like
257 # LyX: Unknown language `romansh' [around line 273 of file lyx_2_3_test.22.lyx current token: 'romansh' context: 'InsetSpaceParams::read']
259 if document.language not in ["bosnian", "friulan", "macedonian",
260 "piedmontese", "romansh"]:
262 i = find_token(document.header, "\\language")
264 document.header[i] = "\\language english"
265 # ensure we use Babel:
266 # TODO: Polyglossia supports friulan, piedmontese, romansh
267 # but requires "\resetdefaultlanguage{...}" at begin of document.
268 j = find_token(document.header, "\\language_package default")
270 document.header[j] = "\\language_package babel"
271 k = find_token(document.header, "\\options")
273 document.header[k] = document.header[k].replace("\\options",
274 "\\options %s," % document.language)
276 l = find_token(document.header, "\\use_default_options")
277 document.header.insert(l + 1, "\\options " + document.language)
278 document.language = "english"
281 # def convert_new_babel_languages(document)
282 # set to native support if get_value(document.header, "\\options") in
283 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
284 # and "\\language_package babel".
286 def revert_amharic(document):
287 "Set the document language to English but assure Amharic output"
289 if document.language == "amharic":
290 document.language = "english"
291 i = find_token(document.header, "\\language amharic", 0)
293 document.header[i] = "\\language english"
294 j = find_token(document.header, "\\language_package default", 0)
296 document.header[j] = "\\language_package default"
297 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
298 document.body[2 : 2] = ["\\begin_layout Standard",
299 "\\begin_inset ERT", "status open", "",
300 "\\begin_layout Plain Layout", "", "",
302 "resetdefaultlanguage{amharic}",
303 "\\end_layout", "", "\\end_inset", "", "",
307 def revert_asturian(document):
308 "Set the document language to English but assure Asturian output"
310 if document.language == "asturian":
311 document.language = "english"
312 i = find_token(document.header, "\\language asturian", 0)
314 document.header[i] = "\\language english"
315 j = find_token(document.header, "\\language_package default", 0)
317 document.header[j] = "\\language_package default"
318 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
319 document.body[2 : 2] = ["\\begin_layout Standard",
320 "\\begin_inset ERT", "status open", "",
321 "\\begin_layout Plain Layout", "", "",
323 "resetdefaultlanguage{asturian}",
324 "\\end_layout", "", "\\end_inset", "", "",
328 def revert_kannada(document):
329 "Set the document language to English but assure Kannada output"
331 if document.language == "kannada":
332 document.language = "english"
333 i = find_token(document.header, "\\language kannada", 0)
335 document.header[i] = "\\language english"
336 j = find_token(document.header, "\\language_package default", 0)
338 document.header[j] = "\\language_package default"
339 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
340 document.body[2 : 2] = ["\\begin_layout Standard",
341 "\\begin_inset ERT", "status open", "",
342 "\\begin_layout Plain Layout", "", "",
344 "resetdefaultlanguage{kannada}",
345 "\\end_layout", "", "\\end_inset", "", "",
349 def revert_khmer(document):
350 "Set the document language to English but assure Khmer output"
352 if document.language == "khmer":
353 document.language = "english"
354 i = find_token(document.header, "\\language khmer", 0)
356 document.header[i] = "\\language english"
357 j = find_token(document.header, "\\language_package default", 0)
359 document.header[j] = "\\language_package default"
360 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
361 document.body[2 : 2] = ["\\begin_layout Standard",
362 "\\begin_inset ERT", "status open", "",
363 "\\begin_layout Plain Layout", "", "",
365 "resetdefaultlanguage{khmer}",
366 "\\end_layout", "", "\\end_inset", "", "",
370 def revert_urdu(document):
371 "Set the document language to English but assure Urdu output"
373 if document.language == "urdu":
374 document.language = "english"
375 i = find_token(document.header, "\\language urdu", 0)
377 document.header[i] = "\\language english"
378 j = find_token(document.header, "\\language_package default", 0)
380 document.header[j] = "\\language_package default"
381 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
382 document.body[2 : 2] = ["\\begin_layout Standard",
383 "\\begin_inset ERT", "status open", "",
384 "\\begin_layout Plain Layout", "", "",
386 "resetdefaultlanguage{urdu}",
387 "\\end_layout", "", "\\end_inset", "", "",
391 def revert_syriac(document):
392 "Set the document language to English but assure Syriac output"
394 if document.language == "syriac":
395 document.language = "english"
396 i = find_token(document.header, "\\language syriac", 0)
398 document.header[i] = "\\language english"
399 j = find_token(document.header, "\\language_package default", 0)
401 document.header[j] = "\\language_package default"
402 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
403 document.body[2 : 2] = ["\\begin_layout Standard",
404 "\\begin_inset ERT", "status open", "",
405 "\\begin_layout Plain Layout", "", "",
407 "resetdefaultlanguage{syriac}",
408 "\\end_layout", "", "\\end_inset", "", "",
412 def revert_quotes(document):
413 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
415 # First handle verbatim insets
418 while i < len(document.body):
419 words = document.body[i].split()
420 if len(words) > 1 and words[0] == "\\begin_inset" and \
421 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
422 j = find_end_of_inset(document.body, i)
424 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
428 k = find_token(document.body, '\\begin_inset Quotes', i, j)
432 l = find_end_of_inset(document.body, k)
434 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
438 if document.body[k].endswith("s"):
440 document.body[k:l+1] = [replace]
445 # Now verbatim layouts
448 while i < len(document.body):
449 words = document.body[i].split()
450 if len(words) > 1 and words[0] == "\\begin_layout" and \
451 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
452 j = find_end_of_layout(document.body, i)
454 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
458 k = find_token(document.body, '\\begin_inset Quotes', i, j)
462 l = find_end_of_inset(document.body, k)
464 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
468 if document.body[k].endswith("s"):
470 document.body[k:l+1] = [replace]
476 if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
482 k = find_token(document.body, '\\begin_inset Quotes', i)
485 l = find_end_of_inset(document.body, k)
487 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
491 parent = get_containing_layout(document.body, k)
492 ql = find_token_backwards(document.body, "\\lang", k)
493 if ql == -1 or ql < parent[1]:
494 hebrew = document.language == "hebrew"
495 elif document.body[ql] == "\\lang hebrew":
499 if document.body[k].endswith("s"):
501 document.body[k:l+1] = [replace]
505 def revert_iopart(document):
506 " Input new styles via local layout "
507 if document.textclass != "iopart":
510 i = find_token(document.header, "\\begin_local_layout", 0)
512 k = find_token(document.header, "\\language", 0)
514 # this should not happen
515 document.warning("Malformed LyX document! No \\language header found!")
517 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
520 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
522 # this should not happen
523 document.warning("Malformed LyX document! Can't find end of local layout!")
526 document.header[i+1 : i+1] = [
527 "### Inserted by lyx2lyx (stdlayouts) ###",
528 "Input stdlayouts.inc",
529 "### End of insertion by lyx2lyx (stdlayouts) ###"
533 def convert_iopart(document):
534 " Remove local layout we added, if it is there "
535 if document.textclass != "iopart":
538 i = find_token(document.header, "\\begin_local_layout", 0)
542 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
544 # this should not happen
545 document.warning("Malformed LyX document! Can't find end of local layout!")
548 k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
550 l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
552 # this should not happen
553 document.warning("End of lyx2lyx local layout insertion not found!")
555 if k == i + 1 and l == j - 1:
556 # that was all the local layout there was
557 document.header[i : j + 1] = []
559 document.header[k : l + 1] = []
562 def convert_quotestyle(document):
563 " Convert \\quotes_language to \\quotes_style "
564 i = find_token(document.header, "\\quotes_language", 0)
566 document.warning("Malformed LyX document! Can't find \\quotes_language!")
568 val = get_value(document.header, "\\quotes_language", i)
569 document.header[i] = "\\quotes_style " + val
572 def revert_quotestyle(document):
573 " Revert \\quotes_style to \\quotes_language "
574 i = find_token(document.header, "\\quotes_style", 0)
576 document.warning("Malformed LyX document! Can't find \\quotes_style!")
578 val = get_value(document.header, "\\quotes_style", i)
579 document.header[i] = "\\quotes_language " + val
582 def revert_plainquote(document):
583 " Revert plain quote insets "
585 # First, revert style setting
586 i = find_token(document.header, "\\quotes_style plain", 0)
588 document.header[i] = "\\quotes_style english"
594 k = find_token(document.body, '\\begin_inset Quotes q', i)
597 l = find_end_of_inset(document.body, k)
599 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
603 if document.body[k].endswith("s"):
605 document.body[k:l+1] = [replace]
609 def convert_frenchquotes(document):
610 " Convert french quote insets to swiss "
612 # First, revert style setting
613 i = find_token(document.header, "\\quotes_style french", 0)
615 document.header[i] = "\\quotes_style swiss"
620 i = find_token(document.body, '\\begin_inset Quotes f', i)
623 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
624 newval = val.replace("f", "c", 1)
625 document.body[i] = document.body[i].replace(val, newval)
629 def revert_swissquotes(document):
630 " Revert swiss quote insets to french "
632 # First, revert style setting
633 i = find_token(document.header, "\\quotes_style swiss", 0)
635 document.header[i] = "\\quotes_style french"
640 i = find_token(document.body, '\\begin_inset Quotes c', i)
643 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
644 newval = val.replace("c", "f", 1)
645 document.body[i] = document.body[i].replace(val, newval)
649 def revert_britishquotes(document):
650 " Revert british quote insets to english "
652 # First, revert style setting
653 i = find_token(document.header, "\\quotes_style british", 0)
655 document.header[i] = "\\quotes_style english"
660 i = find_token(document.body, '\\begin_inset Quotes b', i)
663 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
664 newval = val.replace("b", "e", 1)
667 newval = newval.replace("d", "s")
670 newval = newval.replace("s", "d")
671 document.body[i] = document.body[i].replace(val, newval)
675 def revert_swedishgquotes(document):
676 " Revert swedish quote insets "
678 # First, revert style setting
679 i = find_token(document.header, "\\quotes_style swedishg", 0)
681 document.header[i] = "\\quotes_style danish"
686 i = find_token(document.body, '\\begin_inset Quotes w', i)
689 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
692 newval = val.replace("w", "a", 1).replace("r", "l")
695 newval = val.replace("w", "s", 1)
696 document.body[i] = document.body[i].replace(val, newval)
700 def revert_frenchquotes(document):
701 " Revert french inner quote insets "
705 i = find_token(document.body, '\\begin_inset Quotes f', i)
708 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
711 newval = val.replace("f", "e", 1).replace("s", "d")
712 document.body[i] = document.body[i].replace(val, newval)
716 def revert_frenchinquotes(document):
717 " Revert inner frenchin quote insets "
719 # First, revert style setting
720 i = find_token(document.header, "\\quotes_style frenchin", 0)
722 document.header[i] = "\\quotes_style french"
727 i = find_token(document.body, '\\begin_inset Quotes i', i)
730 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
731 newval = val.replace("i", "f", 1)
734 newval = newval.replace("s", "d")
735 document.body[i] = document.body[i].replace(val, newval)
739 def revert_russianquotes(document):
740 " Revert russian quote insets "
742 # First, revert style setting
743 i = find_token(document.header, "\\quotes_style russian", 0)
745 document.header[i] = "\\quotes_style french"
750 i = find_token(document.body, '\\begin_inset Quotes r', i)
753 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
757 newval = val.replace("r", "g", 1).replace("s", "d")
760 newval = val.replace("r", "f", 1)
761 document.body[i] = document.body[i].replace(val, newval)
765 def revert_dynamicquotes(document):
766 " Revert dynamic quote insets "
768 # First, revert header
769 i = find_token(document.header, "\\dynamic_quotes", 0)
771 del document.header[i]
775 i = find_token(document.header, "\\quotes_style", 0)
777 document.warning("Malformed document! Missing \\quotes_style")
779 style = get_value(document.header, "\\quotes_style", i)
782 if style == "english":
784 elif style == "swedish":
786 elif style == "german":
788 elif style == "polish":
790 elif style == "swiss":
792 elif style == "danish":
794 elif style == "plain":
796 elif style == "british":
798 elif style == "swedishg":
800 elif style == "french":
802 elif style == "frenchin":
804 elif style == "russian":
807 # now transform the insets
810 i = find_token(document.body, '\\begin_inset Quotes x', i)
813 document.body[i] = document.body[i].replace("x", s)
817 def revert_cjkquotes(document):
818 " Revert cjk quote insets "
822 i = find_token(document.header, "\\quotes_style", 0)
824 document.warning("Malformed document! Missing \\quotes_style")
826 style = get_value(document.header, "\\quotes_style", i)
828 global_cjk = style.find("cjk") != -1
831 document.header[i] = "\\quotes_style english"
832 # transform dynamic insets
834 if style == "cjkangle":
838 i = find_token(document.body, '\\begin_inset Quotes x', i)
841 document.body[i] = document.body[i].replace("x", s)
844 cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
849 k = find_token(document.body, '\\begin_inset Quotes j', i)
852 l = find_end_of_inset(document.body, k)
854 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
858 parent = get_containing_layout(document.body, k)
859 ql = find_token_backwards(document.body, "\\lang", k)
860 if ql == -1 or ql < parent[1]:
861 cjk = document.language in cjk_langs
862 elif document.body[ql].split()[1] in cjk_langs:
864 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
871 replace = [u"\u300E"]
873 replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
877 replace = [u"\u300F"]
879 replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
885 replace = [u"\u300C"]
887 replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
891 replace = [u"\u300D"]
893 replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
895 document.body[k:l+1] = replace
901 k = find_token(document.body, '\\begin_inset Quotes k', i)
904 l = find_end_of_inset(document.body, k)
906 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
910 parent = get_containing_layout(document.body, k)
911 ql = find_token_backwards(document.body, "\\lang", k)
912 if ql == -1 or ql < parent[1]:
913 cjk = document.language in cjk_langs
914 elif document.body[ql].split()[1] in cjk_langs:
916 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
923 replace = [u"\u3008"]
925 replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
929 replace = [u"\u3009"]
931 replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
937 replace = [u"\u300A"]
939 replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
943 replace = [u"\u300B"]
945 replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
947 document.body[k:l+1] = replace
951 def convert_crimson(document):
952 """Transform preamble code to native font setting."""
954 i = find_substring(document.preamble, "{cochineal}")
957 # Find and delete user-preamble code:
958 if document.preamble[i] == "\\usepackage[proportional,osf]{cochineal}":
960 elif document.preamble[i] == "\\usepackage{cochineal}":
964 del document.preamble[i]
965 if i and document.preamble[i-1] == "% Added by lyx2lyx":
966 del document.preamble[i-1]
968 # Convert to native font setting:
969 j = find_token(document.header, '\\font_roman')
971 romanfont = ['\font_roman', '"cochineal"', '"default"']
973 romanfont = document.header[j].split()
974 romanfont[1] = '"cochineal"'
975 document.header[j] = " ".join(romanfont)
977 set_bool_value(document.header, '\\font_osf', osf)
978 except ValueError: # no \\font_osf setting in document.header
980 document.header.insert(-1, "\\font_osf true")
983 def revert_crimson(document):
984 " Revert native Cochineal/Crimson font definition to LaTeX "
986 i = find_token(document.header, '\\font_roman "cochineal"')
989 # replace unsupported font setting
990 document.header[i] = document.header[i].replace("cochineal", "default")
991 # no need for preamble code with system fonts
992 if get_bool_value(document.header, "\\use_non_tex_fonts"):
994 # transfer old style figures setting to package options
995 j = find_token(document.header, "\\font_osf true")
997 options = "[proportional,osf]"
998 document.header[j] = "\\font_osf false"
1001 add_to_preamble(document, ["\\usepackage%s{cochineal}"%options])
1004 def revert_cochinealmath(document):
1005 " Revert cochineal newtxmath definitions to LaTeX "
1007 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1008 i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
1010 add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
1011 document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
1014 def revert_labelonly(document):
1015 " Revert labelonly tag for InsetRef "
1018 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1021 j = find_end_of_inset(document.body, i)
1023 document.warning("Can't find end of reference inset at line %d!!" %(i))
1026 k = find_token(document.body, "LatexCommand labelonly", i, j)
1030 label = get_quoted_value(document.body, "reference", i, j)
1032 document.warning("Can't find label for reference at line %d!" %(i))
1035 document.body[i:j+1] = put_cmd_in_ert([label])
1039 def revert_plural_refs(document):
1040 " Revert plural and capitalized references "
1041 i = find_token(document.header, "\\use_refstyle 1", 0)
1042 use_refstyle = (i != 0)
1046 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1049 j = find_end_of_inset(document.body, i)
1051 document.warning("Can't find end of reference inset at line %d!!" %(i))
1055 plural = caps = suffix = False
1056 k = find_token(document.body, "LaTeXCommand formatted", i, j)
1057 if k != -1 and use_refstyle:
1058 plural = get_bool_value(document.body, "plural", i, j, False)
1059 caps = get_bool_value(document.body, "caps", i, j, False)
1060 label = get_quoted_value(document.body, "reference", i, j)
1063 (prefix, suffix) = label.split(":", 1)
1065 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1067 document.warning("Can't find label for reference at line %d!" % (i))
1069 # this effectively tests also for use_refstyle and a formatted reference
1070 # we do this complicated test because we would otherwise do this erasure
1071 # over and over and over
1072 if not ((plural or caps) and suffix):
1073 del_token(document.body, "plural", i, j)
1074 del_token(document.body, "caps", i, j - 1) # since we deleted a line
1079 prefix = prefix[0].title() + prefix[1:]
1080 cmd = "\\" + prefix + "ref"
1083 cmd += "{" + suffix + "}"
1084 document.body[i:j+1] = put_cmd_in_ert([cmd])
1088 def revert_noprefix(document):
1089 " Revert labelonly tags with 'noprefix' set "
1092 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1095 j = find_end_of_inset(document.body, i)
1097 document.warning("Can't find end of reference inset at line %d!!" %(i))
1100 k = find_token(document.body, "LatexCommand labelonly", i, j)
1103 noprefix = get_bool_value(document.body, "noprefix", i, j)
1105 # either it was not a labelonly command, or else noprefix was not set.
1106 # in that case, we just delete the option.
1107 del_token(document.body, "noprefix", i, j)
1110 label = get_quoted_value(document.body, "reference", i, j)
1112 document.warning("Can't find label for reference at line %d!" %(i))
1116 (prefix, suffix) = label.split(":", 1)
1118 document.warning("No `:' separator in formatted reference at line %d!" % (i))
1119 # we'll leave this as an ordinary labelonly reference
1120 del_token(document.body, "noprefix", i, j)
1123 document.body[i:j+1] = put_cmd_in_ert([suffix])
1127 def revert_biblatex(document):
1128 " Revert biblatex support "
1134 # 1. Get cite engine
1136 i = find_token(document.header, "\\cite_engine", 0)
1138 document.warning("Malformed document! Missing \\cite_engine")
1140 engine = get_value(document.header, "\\cite_engine", i)
1142 # 2. Store biblatex state and revert to natbib
1144 if engine in ["biblatex", "biblatex-natbib"]:
1146 document.header[i] = "\\cite_engine natbib"
1148 # 3. Store and remove new document headers
1150 i = find_token(document.header, "\\biblatex_bibstyle", 0)
1152 bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
1153 del document.header[i]
1156 i = find_token(document.header, "\\biblatex_citestyle", 0)
1158 citestyle = get_value(document.header, "\\biblatex_citestyle", i)
1159 del document.header[i]
1162 i = find_token(document.header, "\\biblio_options", 0)
1164 biblio_options = get_value(document.header, "\\biblio_options", i)
1165 del document.header[i]
1168 bbxopts = "[natbib=true"
1170 bbxopts += ",bibstyle=" + bibstyle
1172 bbxopts += ",citestyle=" + citestyle
1173 if biblio_options != "":
1174 bbxopts += "," + biblio_options
1176 add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1186 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1189 j = find_end_of_inset(document.body, i)
1191 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1194 bibs = get_quoted_value(document.body, "bibfiles", i, j)
1195 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1198 bibresources += bibs.split(",")
1200 document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1201 # remove biblatexopts line
1202 k = find_token(document.body, "biblatexopts", i, j)
1204 del document.body[k]
1205 # Re-find inset end line
1206 j = find_end_of_inset(document.body, i)
1207 # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1209 pcmd = "printbibliography"
1211 pcmd += "[" + opts + "]"
1212 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1213 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1214 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1215 "status open", "", "\\begin_layout Plain Layout" ]
1216 repl += document.body[i:j+1]
1217 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1218 document.body[i:j+1] = repl
1224 for b in bibresources:
1225 add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1227 # 2. Citation insets
1229 # Specific citation insets used in biblatex that need to be reverted to ERT
1232 "citebyear" : "citeyear",
1233 "citeyear" : "cite*",
1234 "Footcite" : "Smartcite",
1235 "footcite" : "smartcite",
1236 "Autocite" : "Autocite",
1237 "autocite" : "autocite",
1238 "citetitle" : "citetitle",
1239 "citetitle*" : "citetitle*",
1240 "fullcite" : "fullcite",
1241 "footfullcite" : "footfullcite",
1242 "supercite" : "supercite",
1243 "citeauthor" : "citeauthor",
1244 "citeauthor*" : "citeauthor*",
1245 "Citeauthor" : "Citeauthor",
1246 "Citeauthor*" : "Citeauthor*"
1249 # All commands accepted by LyX < 2.3. Everything else throws an error.
1250 old_citations = ["cite", "nocite", "citet", "citep", "citealt", "citealp",
1251 "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",
1252 "citealt*", "citealp*", "citeauthor*", "Citet", "Citep",
1253 "Citealt", "Citealp", "Citeauthor", "Citet*", "Citep*",
1254 "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",
1255 "footcitet", "footcitep", "footcitealt", "footcitealp",
1256 "footciteauthor", "footciteyear", "footciteyearpar",
1257 "citefield", "citetitle", "cite*" ]
1261 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1264 j = find_end_of_inset(document.body, i)
1266 document.warning("Can't find end of citation inset at line %d!!" %(i))
1269 k = find_token(document.body, "LatexCommand", i, j)
1271 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1274 cmd = get_value(document.body, "LatexCommand", k)
1275 if biblatex and cmd in list(new_citations.keys()):
1276 pre = get_quoted_value(document.body, "before", i, j)
1277 post = get_quoted_value(document.body, "after", i, j)
1278 key = get_quoted_value(document.body, "key", i, j)
1280 document.warning("Citation inset at line %d does not have a key!" %(i))
1282 # Replace known new commands with ERT
1283 res = "\\" + new_citations[cmd]
1285 res += "[" + pre + "]"
1287 res += "[" + post + "]"
1290 res += "{" + key + "}"
1291 document.body[i:j+1] = put_cmd_in_ert([res])
1292 elif cmd not in old_citations:
1293 # Reset unknown commands to cite. This is what LyX does as well
1294 # (but LyX 2.2 would break on unknown commands)
1295 document.body[k] = "LatexCommand cite"
1296 document.warning("Reset unknown cite command '%s' with cite" % cmd)
1299 # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1301 i = find_token(document.header, "\\begin_local_layout", 0)
1303 k = find_token(document.header, "\\language", 0)
1305 # this should not happen
1306 document.warning("Malformed LyX document! No \\language header found!")
1308 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1311 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1313 # this should not happen
1314 document.warning("Malformed LyX document! Can't find end of local layout!")
1317 document.header[i+1 : i+1] = [
1318 "### Inserted by lyx2lyx (biblatex emulation) ###",
1319 "Provides natbib 1",
1320 "### End of insertion by lyx2lyx (biblatex emulation) ###"
1324 def revert_citekeyonly(document):
1325 " Revert keyonly cite command to ERT "
1329 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1332 j = find_end_of_inset(document.body, i)
1334 document.warning("Can't find end of citation inset at line %d!!" %(i))
1337 k = find_token(document.body, "LatexCommand", i, j)
1339 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1342 cmd = get_value(document.body, "LatexCommand", k)
1343 if cmd != "keyonly":
1347 key = get_quoted_value(document.body, "key", i, j)
1349 document.warning("Citation inset at line %d does not have a key!" %(i))
1350 # Replace known new commands with ERT
1351 document.body[i:j+1] = put_cmd_in_ert([key])
1356 def revert_bibpackopts(document):
1357 " Revert support for natbib/jurabib package options "
1360 i = find_token(document.header, "\\cite_engine", 0)
1362 document.warning("Malformed document! Missing \\cite_engine")
1364 engine = get_value(document.header, "\\cite_engine", i)
1367 if engine not in ["natbib", "jurabib"]:
1370 i = find_token(document.header, "\\biblio_options", 0)
1372 # Nothing to do if we have no options
1375 biblio_options = get_value(document.header, "\\biblio_options", i)
1376 del document.header[i]
1378 if not biblio_options:
1379 # Nothing to do for empty options
1382 i = find_token(document.header, "\\begin_local_layout", 0)
1384 k = find_token(document.header, "\\language", 0)
1386 # this should not happen
1387 document.warning("Malformed LyX document! No \\language header found!")
1389 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1392 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1394 # this should not happen
1395 document.warning("Malformed LyX document! Can't find end of local layout!")
1398 document.header[i+1 : i+1] = [
1399 "### Inserted by lyx2lyx (bibliography package options) ###",
1400 "PackageOptions " + engine + " " + biblio_options,
1401 "### End of insertion by lyx2lyx (bibliography package options) ###"
1405 def revert_qualicites(document):
1406 " Revert qualified citation list commands to ERT "
1408 # Citation insets that support qualified lists, with their LaTeX code
1412 "citet" : "textcites",
1413 "Citet" : "Textcites",
1414 "citep" : "parencites",
1415 "Citep" : "Parencites",
1416 "Footcite" : "Smartcites",
1417 "footcite" : "smartcites",
1418 "Autocite" : "Autocites",
1419 "autocite" : "autocites",
1424 i = find_token(document.header, "\\cite_engine", 0)
1426 document.warning("Malformed document! Missing \\cite_engine")
1428 engine = get_value(document.header, "\\cite_engine", i)
1430 biblatex = engine in ["biblatex", "biblatex-natbib"]
1434 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1437 j = find_end_of_inset(document.body, i)
1439 document.warning("Can't find end of citation inset at line %d!!" %(i))
1442 pres = find_token(document.body, "pretextlist", i, j)
1443 posts = find_token(document.body, "posttextlist", i, j)
1444 if pres == -1 and posts == -1:
1448 pretexts = get_quoted_value(document.body, "pretextlist", pres)
1449 posttexts = get_quoted_value(document.body, "posttextlist", posts)
1450 k = find_token(document.body, "LatexCommand", i, j)
1452 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1455 cmd = get_value(document.body, "LatexCommand", k)
1456 if biblatex and cmd in list(ql_citations.keys()):
1457 pre = get_quoted_value(document.body, "before", i, j)
1458 post = get_quoted_value(document.body, "after", i, j)
1459 key = get_quoted_value(document.body, "key", i, j)
1461 document.warning("Citation inset at line %d does not have a key!" %(i))
1463 keys = key.split(",")
1464 prelist = pretexts.split("\t")
1467 ppp = pp.split(" ", 1)
1468 premap[ppp[0]] = ppp[1]
1469 postlist = posttexts.split("\t")
1472 ppp = pp.split(" ", 1)
1473 postmap[ppp[0]] = ppp[1]
1474 # Replace known new commands with ERT
1475 if "(" in pre or ")" in pre:
1476 pre = "{" + pre + "}"
1477 if "(" in post or ")" in post:
1478 post = "{" + post + "}"
1479 res = "\\" + ql_citations[cmd]
1481 res += "(" + pre + ")"
1483 res += "(" + post + ")"
1487 if premap.get(kk, "") != "":
1488 res += "[" + premap[kk] + "]"
1489 if postmap.get(kk, "") != "":
1490 res += "[" + postmap[kk] + "]"
1491 elif premap.get(kk, "") != "":
1493 res += "{" + kk + "}"
1494 document.body[i:j+1] = put_cmd_in_ert([res])
1496 # just remove the params
1497 del document.body[posttexts]
1498 del document.body[pretexts]
1502 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1503 def convert_literalparam(document):
1504 " Add param literal "
1506 for inset in command_insets:
1509 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1512 j = find_end_of_inset(document.body, i)
1514 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1517 while i < j and document.body[i].strip() != '':
1519 # href is already fully latexified. Here we can switch off literal.
1521 document.body.insert(i, "literal \"false\"")
1523 document.body.insert(i, "literal \"true\"")
1526 def revert_literalparam(document):
1527 " Remove param literal "
1529 for inset in command_insets:
1532 i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1535 j = find_end_of_inset(document.body, i)
1537 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1540 k = find_token(document.body, 'literal', i, j)
1544 del document.body[k]
1547 def revert_multibib(document):
1548 " Revert multibib support "
1550 # 1. Get cite engine
1552 i = find_token(document.header, "\\cite_engine", 0)
1554 document.warning("Malformed document! Missing \\cite_engine")
1556 engine = get_value(document.header, "\\cite_engine", i)
1558 # 2. Do we use biblatex?
1560 if engine in ["biblatex", "biblatex-natbib"]:
1563 # 3. Store and remove multibib document header
1565 i = find_token(document.header, "\\multibib", 0)
1567 multibib = get_value(document.header, "\\multibib", i)
1568 del document.header[i]
1573 # 4. The easy part: Biblatex
1575 i = find_token(document.header, "\\biblio_options", 0)
1577 k = find_token(document.header, "\\use_bibtopic", 0)
1579 # this should not happen
1580 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1582 document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1584 biblio_options = get_value(document.header, "\\biblio_options", i)
1586 biblio_options += ","
1587 biblio_options += "refsection=" + multibib
1588 document.header[i] = "\\biblio_options " + biblio_options
1593 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1596 j = find_end_of_inset(document.body, i)
1598 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1601 btprint = get_quoted_value(document.body, "btprint", i, j)
1602 if btprint != "bibbysection":
1605 opts = get_quoted_value(document.body, "biblatexopts", i, j)
1606 # change btprint line
1607 k = find_token(document.body, "btprint", i, j)
1609 document.body[k] = "btprint \"btPrintCited\""
1610 # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1611 pcmd = "bibbysection"
1613 pcmd += "[" + opts + "]"
1614 repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1615 "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1616 "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1617 "status open", "", "\\begin_layout Plain Layout" ]
1618 repl += document.body[i:j+1]
1619 repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1620 document.body[i:j+1] = repl
1626 # 5. More tricky: Bibtex/Bibtopic
1627 k = find_token(document.header, "\\use_bibtopic", 0)
1629 # this should not happen
1630 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1632 document.header[k] = "\\use_bibtopic true"
1634 # Possible units. This assumes that the LyX name follows the std,
1635 # which might not always be the case. But it's as good as we can get.
1638 "chapter" : "Chapter",
1639 "section" : "Section",
1640 "subsection" : "Subsection",
1643 if multibib not in units.keys():
1644 document.warning("Unknown multibib value `%s'!" % nultibib)
1646 unit = units[multibib]
1650 i = find_token(document.body, "\\begin_layout " + unit, i)
1654 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1655 "\\begin_inset ERT", "status open", "",
1656 "\\begin_layout Plain Layout", "", "",
1658 "end{btUnit}", "\\end_layout",
1659 "\\begin_layout Plain Layout", "",
1662 "\\end_layout", "", "\\end_inset", "", "",
1666 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1667 "\\begin_inset ERT", "status open", "",
1668 "\\begin_layout Plain Layout", "", "",
1671 "\\end_layout", "", "\\end_inset", "", "",
1678 i = find_token(document.body, "\\end_body", i)
1679 document.body[i-1 : i-1] = ["\\begin_layout Standard",
1680 "\\begin_inset ERT", "status open", "",
1681 "\\begin_layout Plain Layout", "", "",
1684 "\\end_layout", "", "\\end_inset", "", "",
1688 def revert_chapterbib(document):
1689 " Revert chapterbib support "
1691 # 1. Get cite engine
1693 i = find_token(document.header, "\\cite_engine", 0)
1695 document.warning("Malformed document! Missing \\cite_engine")
1697 engine = get_value(document.header, "\\cite_engine", i)
1699 # 2. Do we use biblatex?
1701 if engine in ["biblatex", "biblatex-natbib"]:
1704 # 3. Store multibib document header value
1706 i = find_token(document.header, "\\multibib", 0)
1708 multibib = get_value(document.header, "\\multibib", i)
1710 if not multibib or multibib != "child":
1714 # 4. remove multibib header
1715 del document.header[i]
1719 # find include insets
1722 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1725 j = find_end_of_inset(document.body, i)
1727 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1730 parent = get_containing_layout(document.body, i)
1733 # Insert ERT \\newrefsection before inset
1734 beg = ["\\begin_layout Standard",
1735 "\\begin_inset ERT", "status open", "",
1736 "\\begin_layout Plain Layout", "", "",
1739 "\\end_layout", "", "\\end_inset", "", "",
1741 document.body[parbeg-1:parbeg-1] = beg
1746 # 6. Bibtex/Bibtopic
1747 i = find_token(document.header, "\\use_bibtopic", 0)
1749 # this should not happen
1750 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1752 if get_value(document.header, "\\use_bibtopic", i) == "true":
1753 # find include insets
1756 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1759 j = find_end_of_inset(document.body, i)
1761 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1764 parent = get_containing_layout(document.body, i)
1768 # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1769 beg = ["\\begin_layout Standard",
1770 "\\begin_inset ERT", "status open", "",
1771 "\\begin_layout Plain Layout", "", "",
1774 "\\end_layout", "", "\\end_inset", "", "",
1776 end = ["\\begin_layout Standard",
1777 "\\begin_inset ERT", "status open", "",
1778 "\\begin_layout Plain Layout", "", "",
1781 "\\end_layout", "", "\\end_inset", "", "",
1783 document.body[parend+1:parend+1] = end
1784 document.body[parbeg-1:parbeg-1] = beg
1785 j += len(beg) + len(end)
1789 # 7. Chapterbib proper
1790 add_to_preamble(document, ["\\usepackage{chapterbib}"])
1793 def convert_dashligatures(document):
1794 """Set 'use_dash_ligatures' according to content.
1796 # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
1797 # set use_dash_ligatures to True if found, to None else.
1798 use_dash_ligatures = del_complete_lines(document.preamble,
1799 ['% Added by lyx2lyx',
1800 r'\renewcommand{\textendash}{--}',
1801 r'\renewcommand{\textemdash}{---}']) or None
1803 if use_dash_ligatures is None:
1804 # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
1805 # or "\threehyphens\n" as interim representation for -- an ---.)
1806 lines = document.body
1807 has_literal_dashes = has_ligature_dashes = False
1808 dash_pattern = re.compile(u"[\u2013\u2014]|\\twohyphens|\\threehyphens")
1811 # skip lines without dashes:
1812 i = find_re(lines, dash_pattern, i+1)
1816 # skip label width string (see bug 10243):
1817 if line.startswith("\\labelwidthstring"):
1819 # do not touch hyphens in some insets (cf. lyx_2_2.convert_dashes):
1821 inset_type, start, end = get_containing_inset(lines, i)
1822 except TypeError: # no containing inset
1823 inset_type, start, end = "no inset", -1, -1
1824 if (inset_type.split()[0] in
1825 ["CommandInset", "ERT", "External", "Formula",
1826 "FormulaMacro", "Graphics", "IPA", "listings"]
1827 or inset_type == "Flex Code"):
1831 layoutname, start, end, j = get_containing_layout(lines, i)
1832 except TypeError: # no (or malformed) containing layout
1833 document.warning("Malformed LyX document: "
1834 "Can't find layout at line %d" % i)
1836 if layoutname == "LyX-Code":
1840 # literal dash followed by a word or no-break space:
1841 if re.search(u"[\u2013\u2014]([\w\u00A0]|$)",
1842 line, flags=re.UNICODE):
1843 has_literal_dashes = True
1844 # ligature dash followed by word or no-break space on next line:
1845 if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
1846 re.match(u"[\w\u00A0]", lines[i+1], flags=re.UNICODE)):
1847 has_ligature_dashes = True
1848 if has_literal_dashes and has_ligature_dashes:
1849 # TODO: insert a warning note in the document?
1850 document.warning('This document contained both literal and '
1851 '"ligature" dashes.\n Line breaks may have changed. '
1852 'See UserGuide chapter 3.9.1 for details.')
1855 if has_literal_dashes and not has_ligature_dashes:
1856 use_dash_ligatures = False
1857 elif has_ligature_dashes and not has_literal_dashes:
1858 use_dash_ligatures = True
1860 # insert the setting if there is a preferred value
1861 if use_dash_ligatures is True:
1862 document.header.insert(-1, "\\use_dash_ligatures true")
1863 elif use_dash_ligatures is False:
1864 document.header.insert(-1, "\\use_dash_ligatures false")
1867 def revert_dashligatures(document):
1868 """Remove font ligature settings for en- and em-dashes.
1869 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":
1875 for i, line in enumerate(document.body):
1876 # Skip some document parts where dashes are not converted
1877 if (i < j) or line.startswith("\\labelwidthstring"):
1878 new_body.append(line)
1880 if (line.startswith("\\begin_inset ") and
1881 line[13:].split()[0] in ["CommandInset", "ERT", "External",
1882 "Formula", "FormulaMacro", "Graphics", "IPA", "listings"]
1883 or line == "\\begin_inset Flex Code"):
1884 j = find_end_of_inset(document.body, i)
1886 document.warning("Malformed LyX document: Can't find end of "
1887 + words[1] + " inset at line " + str(i))
1888 new_body.append(line)
1890 if line == "\\begin_layout LyX-Code":
1891 j = find_end_of_layout(document.body, i)
1893 document.warning("Malformed LyX document: "
1894 "Can't find end of %s layout at line %d" % (words[1],i))
1895 new_body.append(line)
1897 # TODO: skip replacement in typewriter fonts
1898 line = line.replace(u'\u2013', '\\twohyphens\n')
1899 line = line.replace(u'\u2014', '\\threehyphens\n')
1900 lines = line.split('\n')
1901 new_body.extend(line.split('\n'))
1902 document.body = new_body
1903 # redefine the dash LICRs to use ligature dashes:
1904 add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1905 r'\renewcommand{\textemdash}{---}'])
1908 def revert_noto(document):
1909 " Revert Noto font definitions to LaTeX "
1911 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1913 i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1915 add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1916 document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1917 i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1919 add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1920 document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1921 i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1923 add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1924 document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1927 def revert_xout(document):
1928 " Reverts \\xout font attribute "
1929 changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1931 insert_to_preamble(document, \
1932 ['% for proper cross-out',
1933 '\\PassOptionsToPackage{normalem}{ulem}',
1934 '\\usepackage{ulem}'])
1937 def convert_mathindent(document):
1938 """Add the \\is_math_indent tag.
1940 k = find_token(document.header, "\\quotes_style") # where to insert
1941 # check if the document uses the class option "fleqn"
1942 options = get_value(document.header, "\\options")
1943 if 'fleqn' in options:
1944 document.header.insert(k, "\\is_math_indent 1")
1945 # delete the fleqn option
1946 i = find_token(document.header, "\\options")
1947 options = [option for option in options.split(",")
1948 if option.strip() != "fleqn"]
1950 document.header[i] = "\\options " + ",".join(options)
1952 del document.header[i]
1954 document.header.insert(k, "\\is_math_indent 0")
1956 def revert_mathindent(document):
1957 " Define mathindent if set in the document "
1958 # emulate and delete \math_indentation
1959 value = get_value(document.header, "\\math_indentation",
1960 default="default", delete=True)
1961 if value != "default":
1962 add_to_preamble(document, [r"\setlength{\mathindent}{%s}"%value])
1963 # delete \is_math_indent and emulate via document class option
1964 if not get_bool_value(document.header, "\\is_math_indent", delete=True):
1966 i = find_token(document.header, "\\options")
1968 document.header[i] = document.header[i].replace("\\options ",
1971 l = find_token(document.header, "\\use_default_options")
1972 document.header.insert(l, "\\options fleqn")
1975 def revert_baselineskip(document):
1976 " Revert baselineskips to TeX code "
1979 i = find_substring(document.body, "baselineskip%", i+1)
1982 if document.body[i].startswith("\\begin_inset VSpace"):
1983 # output VSpace inset as TeX code
1984 end = find_end_of_inset(document.body, i)
1986 document.warning("Malformed LyX document: "
1987 "Can't find end of VSpace inset at line %d." % i)
1989 # read out the value
1990 baselineskip = document.body[i].split()[-1]
1991 # check if it is the starred version
1992 star = '*' if '*' in document.body[i] else ''
1993 # now output TeX code
1994 cmd = "\\vspace%s{%s}" %(star, latex_length(baselineskip)[1])
1995 document.body[i:end+1] = put_cmd_in_ert(cmd)
1998 begin, end = is_in_inset(document.body, i, "\\begin_inset space \\hspace")
2000 # output space inset as TeX code
2001 baselineskip = document.body[i].split()[-1]
2002 star = '*' if '*' in document.body[i-1] else ''
2003 cmd = "\\hspace%s{%s}" %(star, latex_length(baselineskip)[1])
2004 document.body[begin:end+1] = put_cmd_in_ert(cmd)
2007 def revert_rotfloat(document):
2008 " Revert placement options for rotated floats "
2013 i = find_token(document.body, "sideways true", i)
2016 if not document.body[i-2].startswith('placement '):
2019 # we found a sideways float with placement options
2020 # at first store the placement
2021 beg = document.body[i-2].rfind(" ");
2022 placement = document.body[i-2][beg+1:]
2023 # check if the option'H' is used
2024 if placement.find("H") != -1:
2025 add_to_preamble(document, ["\\usepackage{float}"])
2026 # now check if it is a starred type
2027 if document.body[i-1].find("wide true") != -1:
2031 # store the float type
2032 beg = document.body[i-3].rfind(" ");
2033 fType = document.body[i-3][beg+1:]
2034 # now output TeX code
2035 endInset = find_end_of_inset(document.body, i-3)
2037 document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2040 document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
2041 document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
2042 add_to_preamble(document, ["\\usepackage{rotfloat}"])
2047 allowbreak_emulation = [r"\begin_inset space \hspace{}",
2052 def convert_allowbreak(document):
2053 " Zero widths Space-inset -> \SpecialChar allowbreak. "
2054 lines = document.body
2055 i = find_complete_lines(lines, allowbreak_emulation, 2)
2057 lines[i-1:i+4] = [lines[i-1] + r"\SpecialChar allowbreak"]
2058 i = find_complete_lines(lines, allowbreak_emulation, i+3)
2061 def revert_allowbreak(document):
2062 " \SpecialChar allowbreak -> Zero widths Space-inset. "
2064 lines = document.body
2065 while i < len(lines):
2066 if lines[i].endswith(r"\SpecialChar allowbreak"):
2067 lines[i:i+1] = [lines[i].replace(r"\SpecialChar allowbreak", "")
2068 ] + allowbreak_emulation
2074 def convert_mathnumberpos(document):
2075 " add the \\math_number_before tag "
2076 # check if the document uses the class option "leqno"
2077 i = find_token(document.header, "\\options")
2078 k = find_token(document.header, "\\quotes_style")
2079 if 'leqno' in document.header[i]:
2080 document.header.insert(k, "\\math_number_before 1")
2081 # delete the found option
2082 document.header[i] = document.header[i].replace(",leqno", "")
2083 document.header[i] = document.header[i].replace(", leqno", "")
2084 document.header[i] = document.header[i].replace("leqno,", "")
2085 if 'leqno' in document.header[i]:
2086 # then we have leqno as the only option
2087 del document.header[i]
2089 document.header.insert(k, "\\math_number_before 0")
2092 def revert_mathnumberpos(document):
2093 """Remove \\math_number_before tag,
2094 add the document class option leqno if required.
2096 math_number_before = get_bool_value(document.header,
2097 '\\math_number_before', delete=True)
2098 if math_number_before:
2099 i = find_token(document.header, "\\options")
2100 if i != -1 and 'leqno' not in document.header[i]:
2101 document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
2103 i = find_token(document.header, "\\use_default_options")
2104 document.header.insert(i, "\\options leqno")
2107 def convert_mathnumberingname(document):
2108 " rename the \\math_number_before tag to \\math_numbering_side "
2109 i = find_token(document.header, "\\math_number_before")
2110 math_number_before = get_bool_value(document.header, '\\math_number_before', i)
2111 if math_number_before:
2112 document.header[i] = "\\math_numbering_side left"
2114 # check if the document uses the class option "reqno"
2115 k = find_token(document.header, "\\options")
2116 if 'reqno' in document.header[k]:
2117 document.header[i] = "\\math_numbering_side right"
2118 # delete the found option
2119 document.header[k] = document.header[k].replace(",reqno", "")
2120 document.header[k] = document.header[k].replace(", reqno", "")
2121 document.header[k] = document.header[k].replace("reqno,", "")
2122 if 'reqno' in document.header[k]:
2123 # then we have reqno as the only option
2124 del document.header[k]
2126 document.header[i] = "\\math_numbering_side default"
2129 def revert_mathnumberingname(document):
2130 " rename the \\math_numbering_side tag back to \\math_number_before "
2131 i = find_token(document.header, "\\math_numbering_side")
2132 math_numbering_side = get_value(document.header, '\\math_numbering_side', i)
2133 # rename tag and set boolean value:
2134 if math_numbering_side == "left":
2135 document.header[i] = "\\math_number_before 1"
2136 elif math_numbering_side == "right":
2137 # also add the option reqno:
2138 document.header[i] = "\\math_number_before 0"
2139 k = find_token(document.header, "\\options")
2140 if k != -1 and 'reqno' not in document.header[k]:
2141 document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
2143 l = find_token(document.header, "\\use_default_options", 0)
2144 document.header.insert(l, "\\options reqno")
2146 document.header[i] = "\\math_number_before 0"
2149 def convert_minted(document):
2150 " add the \\use_minted tag "
2151 i = find_token(document.header, "\\index ")
2152 document.header.insert(i, "\\use_minted 0")
2155 def revert_minted(document):
2156 " remove the \\use_minted tag "
2157 del_token(document.header, "\\use_minted")
2164 supported_versions = ["2.3.0", "2.3"]
2166 [509, [convert_microtype]],
2167 [510, [convert_dateinset]],
2168 [511, [convert_ibranches]],
2169 [512, [convert_beamer_article_styles]],
2173 [516, [convert_inputenc]],
2175 [518, [convert_iopart]],
2176 [519, [convert_quotestyle]],
2178 [521, [convert_frenchquotes]],
2181 [524, [convert_crimson]],
2189 [532, [convert_literalparam]],
2192 [535, [convert_dashligatures]],
2195 [538, [convert_mathindent]],
2198 [541, [convert_allowbreak]],
2199 [542, [convert_mathnumberpos]],
2200 [543, [convert_mathnumberingname]],
2201 [544, [convert_minted]]
2205 [543, [revert_minted]],
2206 [542, [revert_mathnumberingname]],
2207 [541, [revert_mathnumberpos]],
2208 [540, [revert_allowbreak]],
2209 [539, [revert_rotfloat]],
2210 [538, [revert_baselineskip]],
2211 [537, [revert_mathindent]],
2212 [536, [revert_xout]],
2213 [535, [revert_noto]],
2214 [534, [revert_dashligatures]],
2215 [533, [revert_chapterbib]],
2216 [532, [revert_multibib]],
2217 [531, [revert_literalparam]],
2218 [530, [revert_qualicites]],
2219 [529, [revert_bibpackopts]],
2220 [528, [revert_citekeyonly]],
2221 [527, [revert_biblatex]],
2222 [526, [revert_noprefix]],
2223 [525, [revert_plural_refs]],
2224 [524, [revert_labelonly]],
2225 [523, [revert_crimson, revert_cochinealmath]],
2226 [522, [revert_cjkquotes]],
2227 [521, [revert_dynamicquotes]],
2228 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2229 [519, [revert_plainquote]],
2230 [518, [revert_quotestyle]],
2231 [517, [revert_iopart]],
2232 [516, [revert_quotes]],
2234 [514, [revert_urdu, revert_syriac]],
2235 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2236 [512, [revert_new_babel_languages]],
2237 [511, [revert_beamer_article_styles]],
2238 [510, [revert_ibranches]],
2240 [508, [revert_microtype]]
2244 if __name__ == "__main__":