1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2015 The LyX team
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 """ Convert files to the file format generated by lyx 2.2"""
26 # Uncomment only what you need to import, please.
28 #from parser_tools import find_token, find_end_of, find_tokens, \
29 # find_token_exact, find_end_of_inset, find_end_of_layout, \
30 # find_token_backwards, is_in_inset, get_value, get_quoted_value, \
31 # del_token, check_token, get_option_value
33 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, lyx2latex, \
35 # insert_to_preamble, latex_length, revert_flex_inset, \
36 # revert_font_attrs, hex2ratio, str2bool
38 from parser_tools import find_token, find_token_backwards, find_re, \
39 find_end_of_inset, find_end_of_layout, find_nonempty_line, \
40 get_containing_layout, get_value, check_token
42 ####################################################################
43 # Private helper functions
45 def revert_Argument_to_TeX_brace(document, line, endline, n, nmax, environment, opt, nolastopt):
47 Reverts an InsetArgument to TeX-code
49 revert_Argument_to_TeX_brace(document, LineOfBegin, LineOfEnd, StartArgument, EndArgument, isEnvironment, isOpt, notLastOpt)
50 LineOfBegin is the line of the \begin_layout or \begin_inset statement
51 LineOfEnd is the line of the \end_layout or \end_inset statement, if "0" is given, the end of the file is used instead
52 StartArgument is the number of the first argument that needs to be converted
53 EndArgument is the number of the last argument that needs to be converted or the last defined one
54 isEnvironment must be true, if the layout is for a LaTeX environment
55 isOpt must be true, if the argument is an optional one
56 notLastOpt must be true if the argument is mandatory and followed by optional ones
60 while lineArg != -1 and n < nmax + 1:
61 lineArg = find_token(document.body, "\\begin_inset Argument " + str(n), line)
62 if lineArg > endline and endline != 0:
65 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", lineArg)
66 # we have to assure that no other inset is in the Argument
67 beginInset = find_token(document.body, "\\begin_inset", beginPlain)
68 endInset = find_token(document.body, "\\end_inset", beginPlain)
71 while beginInset < endInset and beginInset != -1:
72 beginInset = find_token(document.body, "\\begin_inset", k)
73 endInset = find_token(document.body, "\\end_inset", l)
76 if environment == False:
78 if nolastopt == False:
79 document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("}{")
81 document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("}")
82 del(document.body[lineArg : beginPlain + 1])
85 document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("]")
86 document.body[lineArg : beginPlain + 1] = put_cmd_in_ert("[")
90 document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("}")
91 document.body[lineArg : beginPlain + 1] = put_cmd_in_ert("{")
94 document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("]")
95 document.body[lineArg : beginPlain + 1] = put_cmd_in_ert("[")
101 ###############################################################################
103 ### Conversion and reversion routines
105 ###############################################################################
107 def convert_separator(document):
109 Convert layout separators to separator insets and add (LaTeX) paragraph
110 breaks in order to mimic previous LaTeX export.
113 parins = ["\\begin_inset Separator parbreak", "\\end_inset", ""]
114 parlay = ["\\begin_layout Standard", "\\begin_inset Separator parbreak",
115 "\\end_inset", "", "\\end_layout", ""]
117 "family" : "default",
118 "series" : "default",
127 i = find_token(document.body, "\\begin_deeper", i)
131 j = find_token_backwards(document.body, "\\end_layout", i-1)
133 # reset any text style before inserting the inset
134 lay = get_containing_layout(document.body, j-1)
136 content = "\n".join(document.body[lay[1]:lay[2]])
137 for val in list(sty_dict.keys()):
138 if content.find("\\%s" % val) != -1:
139 document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])]
142 document.body[j:j] = parins
143 i = i + len(parins) + 1
149 i = find_token(document.body, "\\align", i)
153 lay = get_containing_layout(document.body, i)
154 if lay != False and lay[0] == "Plain Layout":
158 j = find_token_backwards(document.body, "\\end_layout", i-1)
160 lay = get_containing_layout(document.body, j-1)
161 if lay != False and lay[0] == "Standard" \
162 and find_token(document.body, "\\align", lay[1], lay[2]) == -1 \
163 and find_token(document.body, "\\begin_inset VSpace", lay[1], lay[2]) == -1:
164 # reset any text style before inserting the inset
165 content = "\n".join(document.body[lay[1]:lay[2]])
166 for val in list(sty_dict.keys()):
167 if content.find("\\%s" % val) != -1:
168 document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])]
171 document.body[j:j] = parins
172 i = i + len(parins) + 1
178 regexp = re.compile(r'^\\begin_layout (?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE)
182 i = find_re(document.body, regexp, i)
186 j = find_end_of_layout(document.body, i)
188 document.warning("Malformed LyX document: Missing `\\end_layout'.")
191 lay = get_containing_layout(document.body, j-1)
193 lines = document.body[lay[3]:lay[2]]
197 document.body[i:j+1] = parlay
199 document.body[i+1:i+1] = lines
201 i = i + len(parlay) + len(lines) + 1
204 def revert_separator(document):
205 " Revert separator insets to layout separators "
207 beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
208 if document.textclass in beamer_classes:
209 beglaysep = "\\begin_layout Separator"
211 beglaysep = "\\begin_layout --Separator--"
213 parsep = [beglaysep, "", "\\end_layout", ""]
214 comert = ["\\begin_inset ERT", "status collapsed", "",
215 "\\begin_layout Plain Layout", "%", "\\end_layout",
216 "", "\\end_inset", ""]
217 empert = ["\\begin_inset ERT", "status collapsed", "",
218 "\\begin_layout Plain Layout", " ", "\\end_layout",
219 "", "\\end_inset", ""]
223 i = find_token(document.body, "\\begin_inset Separator", i)
227 lay = get_containing_layout(document.body, i)
229 document.warning("Malformed LyX document: Can't convert separator inset at line " + str(i))
236 kind = get_value(document.body, "\\begin_inset Separator", i, i+1, "plain").split()[1]
237 before = document.body[beg+1:i]
238 something_before = len(before) > 0 and len("".join(before)) > 0
239 j = find_end_of_inset(document.body, i)
240 after = document.body[j+1:end]
241 something_after = len(after) > 0 and len("".join(after)) > 0
243 beg = beg + len(before) + 1
244 elif something_before:
245 document.body[i:i] = ["\\end_layout", ""]
253 document.body[beg:j+1] = empert
256 document.body[beg:j+1] = comert
260 if layoutname == "Standard":
261 if not something_before:
262 document.body[beg:j+1] = parsep
264 document.body[i:i] = ["", "\\begin_layout Standard"]
267 document.body[beg:j+1] = ["\\begin_layout Standard"]
270 document.body[beg:j+1] = ["\\begin_deeper"]
272 end = end + 1 - (j + 1 - beg)
273 if not something_before:
274 document.body[i:i] = parsep
276 end = end + len(parsep)
277 document.body[i:i] = ["\\begin_layout Standard"]
278 document.body[end+2:end+2] = ["", "\\end_deeper", ""]
281 next_par_is_aligned = False
282 k = find_nonempty_line(document.body, end+1)
283 if k != -1 and check_token(document.body[k], "\\begin_layout"):
284 lay = get_containing_layout(document.body, k)
285 next_par_is_aligned = lay != False and \
286 find_token(document.body, "\\align", lay[1], lay[2]) != -1
287 if k != -1 and not next_par_is_aligned \
288 and not check_token(document.body[k], "\\end_deeper") \
289 and not check_token(document.body[k], "\\begin_deeper"):
290 if layoutname == "Standard":
291 document.body[beg:j+1] = [beglaysep]
294 document.body[beg:j+1] = ["\\begin_deeper", beglaysep]
295 end = end + 2 - (j + 1 - beg)
296 document.body[end+1:end+1] = ["", "\\end_deeper", ""]
300 del document.body[i:end+1]
302 del document.body[i:end-1]
307 def revert_smash(document):
308 " Set amsmath to on if smash commands are used "
310 commands = ["smash[t]", "smash[b]", "notag"]
311 i = find_token(document.header, "\\use_package amsmath", 0)
313 document.warning("Malformed LyX document: Can't find \\use_package amsmath.")
315 value = get_value(document.header, "\\use_package amsmath", i).split()[1]
317 # nothing to do if package is not auto but on or off
321 j = find_token(document.body, '\\begin_inset Formula', j)
324 k = find_end_of_inset(document.body, j)
326 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(j))
329 code = "\n".join(document.body[j:k])
331 if code.find("\\%s" % c) != -1:
332 # set amsmath to on, since it is loaded by the newer format
333 document.header[i] = "\\use_package amsmath 2"
338 def revert_swissgerman(document):
339 " Set language german-ch-old to german "
341 if document.language == "german-ch-old":
342 document.language = "german"
343 i = find_token(document.header, "\\language", 0)
345 document.header[i] = "\\language german"
348 j = find_token(document.body, "\\lang german-ch-old", j)
351 document.body[j] = document.body[j].replace("\\lang german-ch-old", "\\lang german")
355 def revert_use_package(document, pkg, commands, oldauto, supported):
356 # oldauto defines how the version we are reverting to behaves:
357 # if it is true, the old version uses the package automatically.
358 # if it is false, the old version never uses the package.
359 # If "supported" is true, the target version also supports this
361 regexp = re.compile(r'(\\use_package\s+%s)' % pkg)
362 p = find_re(document.header, regexp, 0)
363 value = "1" # default is auto
365 value = get_value(document.header, "\\use_package" , p).split()[1]
367 del document.header[p]
368 if value == "2" and not supported: # on
369 add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
370 elif value == "1" and not oldauto: # auto
373 i = find_token(document.body, '\\begin_inset Formula', i)
376 j = find_end_of_inset(document.body, i)
378 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
381 code = "\n".join(document.body[i:j])
383 if code.find("\\%s" % c) != -1:
385 document.header[p] = "\\use_package " + pkg + " 2"
387 add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
392 mathtools_commands = ["xhookrightarrow", "xhookleftarrow", "xRightarrow", \
393 "xrightharpoondown", "xrightharpoonup", "xrightleftharpoons", \
394 "xLeftarrow", "xleftharpoondown", "xleftharpoonup", \
395 "xleftrightarrow", "xLeftrightarrow", "xleftrightharpoons", \
398 def revert_xarrow(document):
399 "remove use_package mathtools"
400 revert_use_package(document, "mathtools", mathtools_commands, False, True)
403 def revert_beamer_lemma(document):
404 " Reverts beamer lemma layout to ERT "
406 beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
407 if document.textclass not in beamer_classes:
413 i = find_token(document.body, "\\begin_layout Lemma", i)
416 j = find_end_of_layout(document.body, i)
418 document.warning("Malformed LyX document: Can't find end of Lemma layout")
421 arg1 = find_token(document.body, "\\begin_inset Argument 1", i, j)
422 endarg1 = find_end_of_inset(document.body, arg1)
423 arg2 = find_token(document.body, "\\begin_inset Argument 2", i, j)
424 endarg2 = find_end_of_inset(document.body, arg2)
428 beginPlain1 = find_token(document.body, "\\begin_layout Plain Layout", arg1, endarg1)
429 if beginPlain1 == -1:
430 document.warning("Malformed LyX document: Can't find arg1 plain Layout")
433 endPlain1 = find_end_of_inset(document.body, beginPlain1)
434 content1 = document.body[beginPlain1 + 1 : endPlain1 - 2]
435 subst1 = put_cmd_in_ert("<") + content1 + put_cmd_in_ert(">")
437 beginPlain2 = find_token(document.body, "\\begin_layout Plain Layout", arg2, endarg2)
438 if beginPlain2 == -1:
439 document.warning("Malformed LyX document: Can't find arg2 plain Layout")
442 endPlain2 = find_end_of_inset(document.body, beginPlain2)
443 content2 = document.body[beginPlain2 + 1 : endPlain2 - 2]
444 subst2 = put_cmd_in_ert("[") + content2 + put_cmd_in_ert("]")
448 del document.body[arg2 : endarg2 + 1]
450 del document.body[arg1 : endarg1 + 1]
452 del document.body[arg1 : endarg1 + 1]
454 del document.body[arg2 : endarg2 + 1]
456 # index of end layout has probably changed
457 j = find_end_of_layout(document.body, i)
459 document.warning("Malformed LyX document: Can't find end of Lemma layout")
465 # if this is not a consecutive env, add start command
467 begcmd = put_cmd_in_ert("\\begin{lemma}")
469 # has this a consecutive lemma?
470 consecutive = document.body[j + 2] == "\\begin_layout Lemma"
472 # if this is not followed by a consecutive env, add end command
474 document.body[j : j + 1] = put_cmd_in_ert("\\end{lemma}") + ["\\end_layout"]
476 document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd + subst1 + subst2
482 def revert_question_env(document):
484 Reverts question and question* environments of
485 theorems-ams-extended-bytype module to ERT
488 # Do we use theorems-ams-extended-bytype module?
490 mods = document.get_module_list()
492 if mod == "theorems-ams-extended-bytype":
502 i = find_token(document.body, "\\begin_layout Question", i)
506 starred = document.body[i] == "\\begin_layout Question*"
508 j = find_end_of_layout(document.body, i)
510 document.warning("Malformed LyX document: Can't find end of Question layout")
514 # if this is not a consecutive env, add start command
518 begcmd = put_cmd_in_ert("\\begin{question*}")
520 begcmd = put_cmd_in_ert("\\begin{question}")
522 # has this a consecutive theorem of same type?
525 consecutive = document.body[j + 2] == "\\begin_layout Question*"
527 consecutive = document.body[j + 2] == "\\begin_layout Question"
529 # if this is not followed by a consecutive env, add end command
532 document.body[j : j + 1] = put_cmd_in_ert("\\end{question*}") + ["\\end_layout"]
534 document.body[j : j + 1] = put_cmd_in_ert("\\end{question}") + ["\\end_layout"]
536 document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd
538 add_to_preamble(document, "\\providecommand{\questionname}{Question}")
541 add_to_preamble(document, "\\theoremstyle{plain}\n" \
542 "\\newtheorem*{question*}{\\protect\\questionname}")
544 add_to_preamble(document, "\\theoremstyle{plain}\n" \
545 "\\newtheorem{question}{\\protect\\questionname}")
550 def convert_dashes(document):
551 "convert -- and --- to \\twohyphens and \\threehyphens"
553 if document.backend != "latex":
557 while i < len(document.body):
558 words = document.body[i].split()
559 if len(words) > 1 and words[0] == "\\begin_inset" and \
560 words[1] in ["CommandInset", "ERT", "External", "Formula", "Graphics", "IPA", "listings"]:
561 # must not replace anything in insets that store LaTeX contents in .lyx files
562 # (math and command insets withut overridden read() and write() methods
563 # filtering out IPA makes Text::readParToken() more simple
564 # skip ERT as well since it is not needed there
565 j = find_end_of_inset(document.body, i)
567 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
573 j = document.body[i].find("--")
576 front = document.body[i][:j]
577 back = document.body[i][j+2:]
578 # We can have an arbitrary number of consecutive hyphens.
579 # These must be split into the corresponding number of two and three hyphens
580 # We must match what LaTeX does: First try emdash, then endash, then single hyphen
581 if back.find("-") == 0:
584 document.body.insert(i+1, back)
585 document.body[i] = front + "\\threehyphens"
588 document.body.insert(i+1, back)
589 document.body[i] = front + "\\twohyphens"
593 def revert_dashes(document):
594 "convert \\twohyphens and \\threehyphens to -- and ---"
597 while i < len(document.body):
598 words = document.body[i].split()
599 if len(words) > 1 and words[0] == "\\begin_inset" and \
600 words[1] in ["CommandInset", "ERT", "External", "Formula", "Graphics", "IPA", "listings"]:
602 j = find_end_of_inset(document.body, i)
604 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
610 if document.body[i].find("\\twohyphens") >= 0:
611 document.body[i] = document.body[i].replace("\\twohyphens", "--")
613 if document.body[i].find("\\threehyphens") >= 0:
614 document.body[i] = document.body[i].replace("\\threehyphens", "---")
616 if replaced and i+1 < len(document.body) and \
617 (document.body[i+1].find("\\") != 0 or \
618 document.body[i+1].find("\\twohyphens") == 0 or
619 document.body[i+1].find("\\threehyphens") == 0) and \
620 len(document.body[i]) + len(document.body[i+1]) <= 80:
621 document.body[i] = document.body[i] + document.body[i+1]
622 document.body[i+1:i+2] = []
627 # order is important for the last three!
628 phrases = ["LyX", "LaTeX2e", "LaTeX", "TeX"]
630 def is_part_of_converted_phrase(line, j, phrase):
631 "is phrase part of an already converted phrase?"
633 converted = "\\SpecialCharNoPassThru \\" + p
634 pos = j + len(phrase) - len(converted)
636 if line[pos:pos+len(converted)] == converted:
641 def convert_phrases(document):
642 "convert special phrases from plain text to \\SpecialCharNoPassThru"
644 if document.backend != "latex":
647 for phrase in phrases:
649 while i < len(document.body):
650 words = document.body[i].split()
651 if len(words) > 1 and words[0] == "\\begin_inset" and \
652 words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]:
653 # must not replace anything in insets that store LaTeX contents in .lyx files
654 # (math and command insets withut overridden read() and write() methods
655 j = find_end_of_inset(document.body, i)
657 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
662 if document.body[i].find("\\") == 0:
665 j = document.body[i].find(phrase)
669 if not is_part_of_converted_phrase(document.body[i], j, phrase):
670 front = document.body[i][:j]
671 back = document.body[i][j+len(phrase):]
673 document.body.insert(i+1, back)
674 # We cannot use SpecialChar since we do not know whether we are outside passThru
675 document.body[i] = front + "\\SpecialCharNoPassThru \\" + phrase
679 def revert_phrases(document):
680 "convert special phrases to plain text"
683 while i < len(document.body):
684 words = document.body[i].split()
685 if len(words) > 1 and words[0] == "\\begin_inset" and \
686 words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]:
687 # see convert_phrases
688 j = find_end_of_inset(document.body, i)
690 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
696 for phrase in phrases:
697 # we can replace SpecialChar since LyX ensures that it cannot be inserted into passThru parts
698 if document.body[i].find("\\SpecialChar \\" + phrase) >= 0:
699 document.body[i] = document.body[i].replace("\\SpecialChar \\" + phrase, phrase)
701 if document.body[i].find("\\SpecialCharNoPassThru \\" + phrase) >= 0:
702 document.body[i] = document.body[i].replace("\\SpecialCharNoPassThru \\" + phrase, phrase)
704 if replaced and i+1 < len(document.body) and \
705 (document.body[i+1].find("\\") != 0 or \
706 document.body[i+1].find("\\SpecialChar") == 0) and \
707 len(document.body[i]) + len(document.body[i+1]) <= 80:
708 document.body[i] = document.body[i] + document.body[i+1]
709 document.body[i+1:i+2] = []
714 def convert_specialchar_internal(document, forward):
715 specialchars = {"\\-":"softhyphen", "\\textcompwordmark{}":"ligaturebreak", \
716 "\\@.":"endofsentence", "\\ldots{}":"ldots", \
717 "\\menuseparator":"menuseparator", "\\slash{}":"breakableslash", \
718 "\\nobreakdash-":"nobreakdash", "\\LyX":"LyX", \
719 "\\TeX":"TeX", "\\LaTeX2e":"LaTeX2e", \
720 "\\LaTeX":"LaTeX" # must be after LaTeX2e
724 while i < len(document.body):
725 words = document.body[i].split()
726 if len(words) > 1 and words[0] == "\\begin_inset" and \
727 words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]:
728 # see convert_phrases
729 j = find_end_of_inset(document.body, i)
731 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
736 for key, value in specialchars.iteritems():
738 document.body[i] = document.body[i].replace("\\SpecialChar " + key, "\\SpecialChar " + value)
739 document.body[i] = document.body[i].replace("\\SpecialCharNoPassThru " + key, "\\SpecialCharNoPassThru " + value)
741 document.body[i] = document.body[i].replace("\\SpecialChar " + value, "\\SpecialChar " + key)
742 document.body[i] = document.body[i].replace("\\SpecialCharNoPassThru " + value, "\\SpecialCharNoPassThru " + key)
746 def convert_specialchar(document):
747 "convert special characters to new syntax"
748 convert_specialchar_internal(document, True)
751 def revert_specialchar(document):
752 "convert special characters to old syntax"
753 convert_specialchar_internal(document, False)
756 def revert_georgian(document):
757 "Set the document language to English but assure Georgian output"
759 if document.language == "georgian":
760 document.language = "english"
761 i = find_token(document.header, "\\language georgian", 0)
763 document.header[i] = "\\language english"
764 j = find_token(document.header, "\\language_package default", 0)
766 document.header[j] = "\\language_package babel"
767 k = find_token(document.header, "\\options", 0)
769 document.header[k] = document.header[k].replace("\\options", "\\options georgian,")
771 l = find_token(document.header, "\\use_default_options", 0)
772 document.header.insert(l + 1, "\\options georgian")
775 def revert_sigplan_doi(document):
776 " Reverts sigplanconf DOI layout to ERT "
778 if document.textclass != "sigplanconf":
783 i = find_token(document.body, "\\begin_layout DOI", i)
786 j = find_end_of_layout(document.body, i)
788 document.warning("Malformed LyX document: Can't find end of DOI layout")
792 content = lyx2latex(document, document.body[i:j + 1])
793 add_to_preamble(document, ["\\doi{" + content + "}"])
794 del document.body[i:j + 1]
798 def revert_ex_itemargs(document):
799 " Reverts \\item arguments of the example environments (Linguistics module) to TeX-code "
801 # Do we use the linguistics module?
803 mods = document.get_module_list()
805 if mod == "linguistics":
813 example_layouts = ["Numbered Examples (consecutive)", "Subexample"]
815 i = find_token(document.body, "\\begin_inset Argument item:", i)
818 j = find_end_of_inset(document.body, i)
819 # Find containing paragraph layout
820 parent = get_containing_layout(document.body, i)
822 document.warning("Malformed LyX document: Can't find parent paragraph layout")
826 layoutname = parent[0]
827 if layoutname in example_layouts:
828 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
829 endPlain = find_end_of_layout(document.body, beginPlain)
830 content = document.body[beginPlain + 1 : endPlain]
831 del document.body[i:j+1]
832 subst = put_cmd_in_ert("[") + content + put_cmd_in_ert("]")
833 document.body[parbeg : parbeg] = subst
837 def revert_forest(document):
838 " Reverts the forest environment (Linguistics module) to TeX-code "
840 # Do we use the linguistics module?
842 mods = document.get_module_list()
844 if mod == "linguistics":
853 i = find_token(document.body, "\\begin_inset Flex Structure Tree", i)
856 j = find_end_of_inset(document.body, i)
858 document.warning("Malformed LyX document: Can't find end of Structure Tree inset")
862 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
863 endPlain = find_end_of_layout(document.body, beginPlain)
864 content = lyx2latex(document, document.body[beginPlain : endPlain])
866 add_to_preamble(document, ["\\usepackage{forest}"])
868 document.body[i:j + 1] = ["\\begin_inset ERT", "status collapsed", "",
869 "\\begin_layout Plain Layout", "", "\\backslash",
870 "begin{forest}", "\\end_layout", "", "\\begin_layout Plain Layout",
871 content, "\\end_layout", "", "\\begin_layout Plain Layout",
872 "\\backslash", "end{forest}", "", "\\end_layout", "", "\\end_inset"]
876 def revert_glossgroup(document):
877 " Reverts the GroupGlossedWords inset (Linguistics module) to TeX-code "
879 # Do we use the linguistics module?
881 mods = document.get_module_list()
883 if mod == "linguistics":
892 i = find_token(document.body, "\\begin_inset Flex GroupGlossedWords", i)
895 j = find_end_of_inset(document.body, i)
897 document.warning("Malformed LyX document: Can't find end of GroupGlossedWords inset")
901 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
902 endPlain = find_end_of_layout(document.body, beginPlain)
903 content = lyx2latex(document, document.body[beginPlain : endPlain])
905 document.body[i:j + 1] = ["{", "", content, "", "}"]
909 def revert_newgloss(document):
910 " Reverts the new Glosse insets (Linguistics module) to the old format "
912 # Do we use the linguistics module?
914 mods = document.get_module_list()
916 if mod == "linguistics":
923 glosses = ("\\begin_inset Flex Glosse", "\\begin_inset Flex Tri-Glosse")
924 for glosse in glosses:
927 i = find_token(document.body, glosse, i)
930 j = find_end_of_inset(document.body, i)
932 document.warning("Malformed LyX document: Can't find end of Glosse inset")
936 arg = find_token(document.body, "\\begin_inset Argument 1", i, j)
937 endarg = find_end_of_inset(document.body, arg)
940 argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
941 if argbeginPlain == -1:
942 document.warning("Malformed LyX document: Can't find arg plain Layout")
945 argendPlain = find_end_of_inset(document.body, argbeginPlain)
946 argcontent = lyx2latex(document, document.body[argbeginPlain : argendPlain - 2])
948 document.body[j:j] = ["", "\\begin_layout Plain Layout","\\backslash", "glt ",
949 argcontent, "\\end_layout"]
951 # remove Arg insets and paragraph, if it only contains this inset
952 if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
953 del document.body[arg - 1 : endarg + 4]
955 del document.body[arg : endarg + 1]
957 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
958 endPlain = find_end_of_layout(document.body, beginPlain)
959 content = lyx2latex(document, document.body[beginPlain : endPlain])
961 document.body[beginPlain + 1:endPlain] = [content]
965 def convert_newgloss(document):
966 " Converts Glosse insets (Linguistics module) to the new format "
968 # Do we use the linguistics module?
970 mods = document.get_module_list()
972 if mod == "linguistics":
979 glosses = ("\\begin_inset Flex Glosse", "\\begin_inset Flex Tri-Glosse")
980 for glosse in glosses:
983 i = find_token(document.body, glosse, i)
986 j = find_end_of_inset(document.body, i)
988 document.warning("Malformed LyX document: Can't find end of Glosse inset")
995 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", k, j)
998 endPlain = find_end_of_layout(document.body, beginPlain)
1000 document.warning("Malformed LyX document: Can't find end of Glosse layout")
1004 glt = find_token(document.body, "\\backslash", beginPlain, endPlain)
1005 if glt != -1 and document.body[glt + 1].startswith("glt"):
1006 document.body[glt + 1] = document.body[glt + 1].lstrip("glt").lstrip()
1007 argcontent = document.body[glt + 1 : endPlain]
1008 document.body[beginPlain + 1 : endPlain] = ["\\begin_inset Argument 1", "status open", "",
1009 "\\begin_layout Plain Layout", "\\begin_inset ERT", "status open", "",
1010 "\\begin_layout Plain Layout", ""] + argcontent + ["\\end_layout", "", "\\end_inset", "",
1011 "\\end_layout", "", "\\end_inset"]
1013 content = document.body[beginPlain + 1 : endPlain]
1014 document.body[beginPlain + 1 : endPlain] = ["\\begin_inset ERT", "status open", "",
1015 "\\begin_layout Plain Layout"] + content + ["\\end_layout", "", "\\end_inset"]
1017 endPlain = find_end_of_layout(document.body, beginPlain)
1019 j = find_end_of_inset(document.body, i)
1024 def convert_BoxFeatures(document):
1025 " adds new box features "
1029 i = find_token(document.body, "height_special", i)
1032 document.body[i+1:i+1] = ['thickness "0.4pt"', 'separation "3pt"', 'shadowsize "4pt"']
1036 def revert_BoxFeatures(document):
1037 " outputs new box features as TeX code "
1041 defaultThick = "0.4pt"
1042 defaultShadow = "4pt"
1044 i = find_token(document.body, "height_special", i)
1047 # read out the values
1048 beg = document.body[i+1].find('"');
1049 end = document.body[i+1].rfind('"');
1050 thickness = document.body[i+1][beg+1:end];
1051 beg = document.body[i+2].find('"');
1052 end = document.body[i+2].rfind('"');
1053 separation = document.body[i+2][beg+1:end];
1054 beg = document.body[i+3].find('"');
1055 end = document.body[i+3].rfind('"');
1056 shadowsize = document.body[i+3][beg+1:end];
1057 # delete the specification
1058 del document.body[i+1:i+4]
1060 # first output the closing brace
1061 if shadowsize != defaultShadow or separation != defaultSep or thickness != defaultThick:
1062 document.body[i + 10 : i + 10] = put_cmd_in_ert("}")
1063 # now output the lengths
1064 if shadowsize != defaultShadow or separation != defaultSep or thickness != defaultThick:
1065 document.body[i - 10 : i - 10] = put_cmd_in_ert("{")
1066 if thickness != defaultThick:
1067 document.body[i - 5 : i - 4] = ["{\\backslash fboxrule " + thickness]
1068 if separation != defaultSep and thickness == defaultThick:
1069 document.body[i - 5 : i - 4] = ["{\\backslash fboxsep " + separation]
1070 if separation != defaultSep and thickness != defaultThick:
1071 document.body[i - 5 : i - 4] = ["{\\backslash fboxrule " + thickness + "\\backslash fboxsep " + separation]
1072 if shadowsize != defaultShadow and separation == defaultSep and thickness == defaultThick:
1073 document.body[i - 5 : i - 4] = ["{\\backslash shadowsize " + shadowsize]
1074 if shadowsize != defaultShadow and separation != defaultSep and thickness == defaultThick:
1075 document.body[i - 5 : i - 4] = ["{\\backslash fboxsep " + separation + "\\backslash shadowsize " + shadowsize]
1076 if shadowsize != defaultShadow and separation == defaultSep and thickness != defaultThick:
1077 document.body[i - 5 : i - 4] = ["{\\backslash fboxrule " + thickness + "\\backslash shadowsize " + shadowsize]
1078 if shadowsize != defaultShadow and separation != defaultSep and thickness != defaultThick:
1079 document.body[i - 5 : i - 4] = ["{\\backslash fboxrule " + thickness + "\\backslash fboxsep " + separation + "\\backslash shadowsize " + shadowsize]
1083 def convert_origin(document):
1084 " Insert the origin tag "
1086 i = find_token(document.header, "\\textclass ", 0)
1088 document.warning("Malformed LyX document: No \\textclass!!")
1090 if document.dir == "":
1093 origin = document.dir.replace('\\', '/') + '/'
1095 origin = unicode(origin, sys.getfilesystemencoding())
1096 document.header[i:i] = ["\\origin " + origin]
1099 def revert_origin(document):
1100 " Remove the origin tag "
1102 i = find_token(document.header, "\\origin ", 0)
1104 document.warning("Malformed LyX document: No \\origin!!")
1106 del document.header[i]
1109 color_names = ["brown", "darkgray", "gray", \
1110 "lightgray", "lime", "olive", "orange", \
1111 "pink", "purple", "teal", "violet"]
1113 def revert_textcolor(document):
1114 " revert new \\textcolor colors to TeX code "
1120 i = find_token(document.body, "\\color ", i)
1124 for color in list(color_names):
1125 if document.body[i] == "\\color " + color:
1126 # register that xcolor must be loaded in the preamble
1129 add_to_preamble(document, ["\\@ifundefined{rangeHsb}{\usepackage{xcolor}}{}"])
1130 # find the next \\color and/or the next \\end_layout
1131 j = find_token(document.body, "\\color", i + 1)
1132 k = find_token(document.body, "\\end_layout", i + 1)
1133 if j == -1 and k != -1:
1136 # first output the closing brace
1138 document.body[k: k] = put_cmd_in_ert("}")
1140 document.body[j: j] = put_cmd_in_ert("}")
1141 # now output the \textcolor command
1142 document.body[i : i + 1] = put_cmd_in_ert("\\textcolor{" + color + "}{")
1146 def convert_colorbox(document):
1147 " adds color settings for boxes "
1151 i = find_token(document.body, "shadowsize", i)
1154 document.body[i+1:i+1] = ['framecolor "black"', 'backgroundcolor "none"']
1158 def revert_colorbox(document):
1159 " outputs color settings for boxes as TeX code "
1162 defaultframecolor = "black"
1163 defaultbackcolor = "none"
1165 binset = find_token(document.body, "\\begin_inset Box", binset)
1169 einset = find_end_of_inset(document.body, binset)
1171 document.warning("Malformed LyX document: Can't find end of box inset!")
1175 blay = find_token(document.body, "\\begin_layout", binset, einset)
1177 document.warning("Malformed LyX document: Can't find start of layout!")
1181 # doing it this way, we make sure only to find a framecolor option
1182 frame = find_token(document.body, "framecolor", binset, blay)
1187 beg = document.body[frame].find('"')
1188 end = document.body[frame].rfind('"')
1189 framecolor = document.body[frame][beg + 1 : end]
1191 # this should be on the next line
1193 beg = document.body[bgcolor].find('"')
1194 end = document.body[bgcolor].rfind('"')
1195 backcolor = document.body[bgcolor][beg + 1 : end]
1198 del document.body[frame : frame + 2]
1199 # adjust end of inset
1202 if document.body[binset] == "\\begin_inset Box Boxed" and \
1203 framecolor != defaultframecolor:
1204 document.body[binset] = "\\begin_inset Box Frameless"
1207 # first output the closing brace
1208 if framecolor == defaultframecolor and backcolor == defaultbackcolor:
1212 # we also neeed to load xcolor in the preamble but only once
1213 add_to_preamble(document, ["\\@ifundefined{rangeHsb}{\usepackage{xcolor}}{}"])
1214 document.body[einset + 1 : einset + 1] = put_cmd_in_ert("}")
1215 if framecolor != defaultframecolor:
1216 document.body[binset:binset] = put_cmd_in_ert("\\fcolorbox{" + framecolor + "}{" + backcolor + "}{")
1218 document.body[binset:binset] = put_cmd_in_ert("\\colorbox{" + backcolor + "}{")
1223 def revert_mathmulticol(document):
1224 " Convert formulas to ERT if they contain multicolumns "
1228 i = find_token(document.body, '\\begin_inset Formula', i)
1231 j = find_end_of_inset(document.body, i)
1233 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
1236 lines = document.body[i:j]
1237 lines[0] = lines[0].replace('\\begin_inset Formula', '').lstrip()
1238 code = "\n".join(lines)
1243 n = code.find("\\multicolumn", k)
1244 # no need to convert degenerated multicolumn cells,
1245 # they work in old LyX versions as "math ERT"
1246 if n != -1 and code.find("\\multicolumn{1}", k) != n:
1247 ert = put_cmd_in_ert(code)
1248 document.body[i:j+1] = ert
1254 i = find_end_of_inset(document.body, i)
1259 def revert_jss(document):
1260 " Reverts JSS In_Preamble commands to ERT in preamble "
1262 if document.textclass != "jss":
1271 # at first revert the inset layouts because they can be part of the In_Preamble layouts
1272 while m != -1 or j != -1 or h != -1 or k != -1 or n != -1:
1275 h = find_token(document.body, "\\begin_inset Flex Pkg", h)
1277 endh = find_end_of_inset(document.body, h)
1278 document.body[endh - 2 : endh + 1] = put_cmd_in_ert("}")
1279 document.body[h : h + 4] = put_cmd_in_ert("\\pkg{")
1283 m = find_token(document.body, "\\begin_inset Flex Proglang", m)
1285 endm = find_end_of_inset(document.body, m)
1286 document.body[endm - 2 : endm + 1] = put_cmd_in_ert("}")
1287 document.body[m : m + 4] = put_cmd_in_ert("\\proglang{")
1291 j = find_token(document.body, "\\begin_inset Flex Code", j)
1293 # assure that we are not in a Code Chunk inset
1294 if document.body[j][-1] == "e":
1295 endj = find_end_of_inset(document.body, j)
1296 document.body[endj - 2 : endj + 1] = put_cmd_in_ert("}")
1297 document.body[j : j + 4] = put_cmd_in_ert("\\code{")
1303 k = find_token(document.body, "\\begin_inset Flex E-mail", k)
1305 endk = find_end_of_inset(document.body, k)
1306 document.body[endk - 2 : endk + 1] = put_cmd_in_ert("}")
1307 document.body[k : k + 4] = put_cmd_in_ert("\\email{")
1311 n = find_token(document.body, "\\begin_inset Flex URL", n)
1313 endn = find_end_of_inset(document.body, n)
1314 document.body[endn - 2 : endn + 1] = put_cmd_in_ert("}")
1315 document.body[n : n + 4] = put_cmd_in_ert("\\url{")
1317 # now revert the In_Preamble layouts
1319 i = find_token(document.body, "\\begin_layout Title", 0)
1322 j = find_end_of_layout(document.body, i)
1324 document.warning("Malformed LyX document: Can't find end of Title layout")
1327 content = lyx2latex(document, document.body[i:j + 1])
1328 add_to_preamble(document, ["\\title{" + content + "}"])
1329 del document.body[i:j + 1]
1331 i = find_token(document.body, "\\begin_layout Author", 0)
1334 j = find_end_of_layout(document.body, i)
1336 document.warning("Malformed LyX document: Can't find end of Author layout")
1339 content = lyx2latex(document, document.body[i:j + 1])
1340 add_to_preamble(document, ["\\author{" + content + "}"])
1341 del document.body[i:j + 1]
1343 i = find_token(document.body, "\\begin_layout Plain Author", 0)
1346 j = find_end_of_layout(document.body, i)
1348 document.warning("Malformed LyX document: Can't find end of Plain Author layout")
1351 content = lyx2latex(document, document.body[i:j + 1])
1352 add_to_preamble(document, ["\\Plainauthor{" + content + "}"])
1353 del document.body[i:j + 1]
1355 i = find_token(document.body, "\\begin_layout Plain Title", 0)
1358 j = find_end_of_layout(document.body, i)
1360 document.warning("Malformed LyX document: Can't find end of Plain Title layout")
1363 content = lyx2latex(document, document.body[i:j + 1])
1364 add_to_preamble(document, ["\\Plaintitle{" + content + "}"])
1365 del document.body[i:j + 1]
1367 i = find_token(document.body, "\\begin_layout Short Title", 0)
1370 j = find_end_of_layout(document.body, i)
1372 document.warning("Malformed LyX document: Can't find end of Short Title layout")
1375 content = lyx2latex(document, document.body[i:j + 1])
1376 add_to_preamble(document, ["\\Shorttitle{" + content + "}"])
1377 del document.body[i:j + 1]
1379 i = find_token(document.body, "\\begin_layout Abstract", 0)
1382 j = find_end_of_layout(document.body, i)
1384 document.warning("Malformed LyX document: Can't find end of Abstract layout")
1387 content = lyx2latex(document, document.body[i:j + 1])
1388 add_to_preamble(document, ["\\Abstract{" + content + "}"])
1389 del document.body[i:j + 1]
1391 i = find_token(document.body, "\\begin_layout Keywords", 0)
1394 j = find_end_of_layout(document.body, i)
1396 document.warning("Malformed LyX document: Can't find end of Keywords layout")
1399 content = lyx2latex(document, document.body[i:j + 1])
1400 add_to_preamble(document, ["\\Keywords{" + content + "}"])
1401 del document.body[i:j + 1]
1403 i = find_token(document.body, "\\begin_layout Plain Keywords", 0)
1406 j = find_end_of_layout(document.body, i)
1408 document.warning("Malformed LyX document: Can't find end of Plain Keywords layout")
1411 content = lyx2latex(document, document.body[i:j + 1])
1412 add_to_preamble(document, ["\\Plainkeywords{" + content + "}"])
1413 del document.body[i:j + 1]
1415 i = find_token(document.body, "\\begin_layout Address", 0)
1418 j = find_end_of_layout(document.body, i)
1420 document.warning("Malformed LyX document: Can't find end of Address layout")
1423 content = lyx2latex(document, document.body[i:j + 1])
1424 add_to_preamble(document, ["\\Address{" + content + "}"])
1425 del document.body[i:j + 1]
1426 # finally handle the code layouts
1431 while m != -1 or j != -1 or h != -1 or k != -1:
1434 h = find_token(document.body, "\\begin_inset Flex Code Chunk", h)
1436 endh = find_end_of_inset(document.body, h)
1437 document.body[endh + 1 : endh] = ["\\end_layout"]
1438 document.body[endh : endh + 1] = put_cmd_in_ert("\\end{CodeChunk}")
1439 document.body[h : h + 3] = put_cmd_in_ert("\\begin{CodeChunk}")
1440 document.body[h - 1 : h] = ["\\begin_layout Standard"]
1444 j = find_token(document.body, "\\begin_layout Code Input", j)
1446 endj = find_end_of_layout(document.body, j)
1447 document.body[endj : endj + 1] = ["\\end_layout", "", "\\begin_layout Standard"]
1448 document.body[endj + 3 : endj + 4] = put_cmd_in_ert("\\end{CodeInput}")
1449 document.body[endj + 13 : endj + 13] = ["\\end_layout", "", "\\begin_layout Standard"]
1450 document.body[j + 1 : j] = ["\\end_layout", "", "\\begin_layout Standard"]
1451 document.body[j : j + 1] = put_cmd_in_ert("\\begin{CodeInput}")
1455 k = find_token(document.body, "\\begin_layout Code Output", k)
1457 endk = find_end_of_layout(document.body, k)
1458 document.body[endk : endk + 1] = ["\\end_layout", "", "\\begin_layout Standard"]
1459 document.body[endk + 3 : endk + 4] = put_cmd_in_ert("\\end{CodeOutput}")
1460 document.body[endk + 13 : endk + 13] = ["\\end_layout", "", "\\begin_layout Standard"]
1461 document.body[k + 1 : k] = ["\\end_layout", "", "\\begin_layout Standard"]
1462 document.body[k : k + 1] = put_cmd_in_ert("\\begin{CodeOutput}")
1466 m = find_token(document.body, "\\begin_layout Code", m)
1468 endm = find_end_of_layout(document.body, m)
1469 document.body[endm : endm + 1] = ["\\end_layout", "", "\\begin_layout Standard"]
1470 document.body[endm + 3 : endm + 4] = put_cmd_in_ert("\\end{Code}")
1471 document.body[endm + 13 : endm + 13] = ["\\end_layout", "", "\\begin_layout Standard"]
1472 document.body[m + 1 : m] = ["\\end_layout", "", "\\begin_layout Standard"]
1473 document.body[m : m + 1] = put_cmd_in_ert("\\begin{Code}")
1477 def convert_subref(document):
1478 " converts sub: ref prefixes to subref: "
1481 rx = re.compile(r'^name \"sub:(.+)$')
1484 i = find_token(document.body, "\\begin_inset CommandInset label", i)
1487 j = find_end_of_inset(document.body, i)
1489 document.warning("Malformed LyX document: Can't find end of Label inset at line " + str(i))
1493 for p in range(i, j):
1494 m = rx.match(document.body[p])
1497 document.body[p] = "name \"subsec:" + label
1501 rx = re.compile(r'^reference \"sub:(.+)$')
1504 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1507 j = find_end_of_inset(document.body, i)
1509 document.warning("Malformed LyX document: Can't find end of Ref inset at line " + str(i))
1513 for p in range(i, j):
1514 m = rx.match(document.body[p])
1517 document.body[p] = "reference \"subsec:" + label
1523 def revert_subref(document):
1524 " reverts subref: ref prefixes to sub: "
1527 rx = re.compile(r'^name \"subsec:(.+)$')
1530 i = find_token(document.body, "\\begin_inset CommandInset label", i)
1533 j = find_end_of_inset(document.body, i)
1535 document.warning("Malformed LyX document: Can't find end of Label inset at line " + str(i))
1539 for p in range(i, j):
1540 m = rx.match(document.body[p])
1543 document.body[p] = "name \"sub:" + label
1548 rx = re.compile(r'^reference \"subsec:(.+)$')
1551 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1554 j = find_end_of_inset(document.body, i)
1556 document.warning("Malformed LyX document: Can't find end of Ref inset at line " + str(i))
1560 for p in range(i, j):
1561 m = rx.match(document.body[p])
1564 document.body[p] = "reference \"sub:" + label
1569 def convert_nounzip(document):
1570 " remove the noUnzip parameter of graphics insets "
1572 rx = re.compile(r'\s*noUnzip\s*$')
1575 i = find_token(document.body, "\\begin_inset Graphics", i)
1578 j = find_end_of_inset(document.body, i)
1580 document.warning("Malformed LyX document: Can't find end of graphics inset at line " + str(i))
1584 k = find_re(document.body, rx, i, j)
1586 del document.body[k]
1591 def convert_revert_external_bbox(document, forward):
1592 " add units to bounding box of external insets "
1594 rx = re.compile(r'^\s*boundingBox\s+\S+\s+\S+\s+\S+\s+\S+\s*$')
1597 i = find_token(document.body, "\\begin_inset External", i)
1600 j = find_end_of_inset(document.body, i)
1602 document.warning("Malformed LyX document: Can't find end of external inset at line " + str(i))
1605 k = find_re(document.body, rx, i, j)
1609 tokens = document.body[k].split()
1611 for t in range(1, 5):
1614 for t in range(1, 5):
1615 tokens[t] = length_in_bp(tokens[t])
1616 document.body[k] = "\tboundingBox " + tokens[1] + " " + tokens[2] + " " + \
1617 tokens[3] + " " + tokens[4]
1621 def convert_external_bbox(document):
1622 convert_revert_external_bbox(document, True)
1625 def revert_external_bbox(document):
1626 convert_revert_external_bbox(document, False)
1629 def revert_tcolorbox_1(document):
1630 " Reverts the Flex:Subtitle inset of tcolorbox to TeX-code "
1633 i = find_token(document.header, "tcolorbox", i)
1639 flex = find_token(document.body, "\\begin_inset Flex Subtitle", flex)
1642 flexEnd = find_end_of_inset(document.body, flex)
1643 wasOpt = revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, False, True, False)
1644 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, False, False, False)
1645 flexEnd = find_end_of_inset(document.body, flex)
1647 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\tcbsubtitle")
1649 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\tcbsubtitle{")
1650 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("}")
1654 def revert_tcolorbox_2(document):
1655 " Reverts the Flex:Raster_Color_Box inset of tcolorbox to TeX-code "
1658 i = find_token(document.header, "tcolorbox", i)
1664 flex = find_token(document.body, "\\begin_inset Flex Raster Color Box", flex)
1667 flexEnd = find_end_of_inset(document.body, flex)
1668 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1669 flexEnd = find_end_of_inset(document.body, flex)
1670 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{tcbraster}")
1671 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("\\end{tcbraster}")
1675 def revert_tcolorbox_3(document):
1676 " Reverts the Flex:Custom_Color_Box_1 inset of tcolorbox to TeX-code "
1679 i = find_token(document.header, "tcolorbox", i)
1685 flex = find_token(document.body, "\\begin_inset Flex Custom Color Box 1", flex)
1688 flexEnd = find_end_of_inset(document.body, flex)
1689 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1690 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, True, False, False)
1691 flexEnd = find_end_of_inset(document.body, flex)
1692 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{cBoxA}")
1693 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("{}\\end{cBoxA}")
1697 def revert_tcolorbox_4(document):
1698 " Reverts the Flex:Custom_Color_Box_2 inset of tcolorbox to TeX-code "
1701 i = find_token(document.header, "tcolorbox", i)
1707 flex = find_token(document.body, "\\begin_inset Flex Custom Color Box 2", flex)
1710 flexEnd = find_end_of_inset(document.body, flex)
1711 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1712 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, True, False, False)
1713 flexEnd = find_end_of_inset(document.body, flex)
1714 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{cBoxB}")
1715 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("{}\\end{cBoxB}")
1719 def revert_tcolorbox_5(document):
1720 " Reverts the Flex:Custom_Color_Box_3 inset of tcolorbox to TeX-code "
1723 i = find_token(document.header, "tcolorbox", i)
1729 flex = find_token(document.body, "\\begin_inset Flex Custom Color Box 3", flex)
1732 flexEnd = find_end_of_inset(document.body, flex)
1733 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1734 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, True, False, False)
1735 flexEnd = find_end_of_inset(document.body, flex)
1736 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{cBoxC}")
1737 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("{}\\end{cBoxC}")
1741 def revert_tcolorbox_6(document):
1742 " Reverts the Flex:Custom_Color_Box_4 inset of tcolorbox to TeX-code "
1745 i = find_token(document.header, "tcolorbox", i)
1751 flex = find_token(document.body, "\\begin_inset Flex Custom Color Box 4", flex)
1754 flexEnd = find_end_of_inset(document.body, flex)
1755 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1756 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, True, False, False)
1757 flexEnd = find_end_of_inset(document.body, flex)
1758 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{cBoxD}")
1759 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("{}\\end{cBoxD}")
1763 def revert_tcolorbox_7(document):
1764 " Reverts the Flex:Custom_Color_Box_5 inset of tcolorbox to TeX-code "
1767 i = find_token(document.header, "tcolorbox", i)
1773 flex = find_token(document.body, "\\begin_inset Flex Custom Color Box 5", flex)
1776 flexEnd = find_end_of_inset(document.body, flex)
1777 revert_Argument_to_TeX_brace(document, flex, flexEnd, 1, 1, True, True, False)
1778 revert_Argument_to_TeX_brace(document, flex, 0, 2, 2, True, False, False)
1779 flexEnd = find_end_of_inset(document.body, flex)
1780 document.body[flex + 0 : flex + 4] = put_cmd_in_ert("\\begin{cBoxE}")
1781 document.body[flexEnd + 4 : flexEnd + 7] = put_cmd_in_ert("{}\\end{cBoxE}")
1785 def revert_tcolorbox_8(document):
1786 " Reverts the layout New Color Box Type of tcolorbox to TeX-code "
1792 i = find_token(document.body, "\\begin_layout New Color Box Type", i)
1794 j = find_end_of_layout(document.body, i)
1795 wasOpt = revert_Argument_to_TeX_brace(document, i, j, 1, 1, False, True, False)
1796 revert_Argument_to_TeX_brace(document, i, 0, 2, 2, False, False, True)
1797 revert_Argument_to_TeX_brace(document, i, 0, 3, 4, False, True, False)
1798 document.body[i] = document.body[i].replace("\\begin_layout New Color Box Type", "\\begin_layout Standard")
1800 document.body[i + 1 : i + 1] = put_cmd_in_ert("\\newtcolorbox")
1802 document.body[i + 1 : i + 1] = put_cmd_in_ert("\\newtcolorbox{")
1803 k = find_end_of_inset(document.body, j)
1804 k = find_token(document.body, "\\end_inset", k + 1)
1805 k = find_token(document.body, "\\end_inset", k + 1)
1807 k = find_token(document.body, "\\end_inset", k + 1)
1808 document.body[k + 2 : j + 2] = put_cmd_in_ert("{")
1809 j = find_token(document.body, "\\begin_layout Standard", j + 1)
1810 document.body[j - 2 : j - 2] = put_cmd_in_ert("}")
1816 def revert_moderncv_1(document):
1817 " Reverts the new inset of moderncv to TeX-code in preamble "
1819 if document.textclass != "moderncv":
1825 # at first revert the new styles
1827 i = find_token(document.body, "\\begin_layout CVIcons", 0)
1830 j = find_end_of_layout(document.body, i)
1832 document.warning("Malformed LyX document: Can't find end of CVIcons layout")
1835 content = lyx2latex(document, document.body[i:j + 1])
1836 add_to_preamble(document, ["\\moderncvicons{" + content + "}"])
1837 del document.body[i:j + 1]
1839 i = find_token(document.body, "\\begin_layout CVColumnWidth", 0)
1842 j = find_end_of_layout(document.body, i)
1844 document.warning("Malformed LyX document: Can't find end of CVColumnWidth layout")
1847 content = lyx2latex(document, document.body[i:j + 1])
1848 add_to_preamble(document, ["\\setlength{\hintscolumnwidth}{" + content + "}"])
1849 del document.body[i:j + 1]
1850 # now change the new styles to the obsolete ones
1852 i = find_token(document.body, "\\begin_layout Name", 0)
1855 j = find_end_of_layout(document.body, i)
1857 document.warning("Malformed LyX document: Can't find end of Name layout")
1860 lineArg = find_token(document.body, "\\begin_inset Argument 1", i)
1861 if lineArg > j and j != 0:
1864 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", lineArg)
1865 # we have to assure that no other inset is in the Argument
1866 beginInset = find_token(document.body, "\\begin_inset", beginPlain)
1867 endInset = find_token(document.body, "\\end_inset", beginPlain)
1870 while beginInset < endInset and beginInset != -1:
1871 beginInset = find_token(document.body, "\\begin_inset", k)
1872 endInset = find_token(document.body, "\\end_inset", l)
1875 Arg2 = document.body[l + 5 : l + 6]
1877 document.body[i : i + 1]= ["\\begin_layout FirstName"]
1878 # delete the Argument inset
1879 del( document.body[endInset - 2 : endInset + 3])
1880 del( document.body[lineArg : beginPlain + 1])
1881 document.body[i + 4 : i + 4]= ["\\begin_layout FamilyName"] + Arg2 + ["\\end_layout"] + [""]
1884 def revert_moderncv_2(document):
1885 " Reverts the phone inset of moderncv to the obsoleted mobile or fax "
1887 if document.textclass != "moderncv":
1894 i = find_token(document.body, "\\begin_layout Phone", i)
1897 j = find_end_of_layout(document.body, i)
1899 document.warning("Malformed LyX document: Can't find end of Phone layout")
1902 lineArg = find_token(document.body, "\\begin_inset Argument 1", i)
1903 if lineArg > j and j != 0:
1907 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", lineArg)
1908 # we have to assure that no other inset is in the Argument
1909 beginInset = find_token(document.body, "\\begin_inset", beginPlain)
1910 endInset = find_token(document.body, "\\end_inset", beginPlain)
1913 while beginInset < endInset and beginInset != -1:
1914 beginInset = find_token(document.body, "\\begin_inset", k)
1915 endInset = find_token(document.body, "\\end_inset", l)
1918 Arg = document.body[beginPlain + 1 : beginPlain + 2]
1920 if Arg[0] == "mobile":
1921 document.body[i : i + 1]= ["\\begin_layout Mobile"]
1923 document.body[i : i + 1]= ["\\begin_layout Fax"]
1924 # delete the Argument inset
1925 del(document.body[endInset - 2 : endInset + 1])
1926 del(document.body[lineArg : beginPlain + 3])
1930 def convert_moderncv(document):
1931 " Convert the Fax and Mobile inset of moderncv to the new phone inset "
1933 if document.textclass != "moderncv":
1940 i = find_token(document.body, "\\begin_layout Mobile", i)
1943 j = find_end_of_layout(document.body, i)
1945 document.warning("Malformed LyX document: Can't find end of Mobile layout")
1948 document.body[i + 1 : i + 1] = ["\\begin_inset Argument 1", "status open", "",
1949 "\\begin_layout Plain Layout", "mobile", "\\end_layout", "",
1952 i = find_token(document.body, "\\begin_layout Fax", i)
1955 j = find_end_of_layout(document.body, i)
1957 document.warning("Malformed LyX document: Can't find end of Fax layout")
1960 document.body[i + 1 : i + 1] = ["\\begin_inset Argument 1", "status open", "",
1961 "\\begin_layout Plain Layout", "fax", "\\end_layout", "",
1963 # \firstname and \familyname
1964 i1 = find_token(document.body, "\\begin_layout FirstName", 0)
1967 j1 = find_end_of_layout(document.body, i1)
1969 document.warning("Malformed LyX document: Can't find end of FirstName layout")
1972 FirstName = document.body[i1 + 1 : i1 + 2]
1973 i2 = find_token(document.body, "\\begin_layout FamilyName", 0)
1976 j2 = find_end_of_layout(document.body, i2)
1978 document.warning("Malformed LyX document: Can't find end of FamilyName layout")
1981 FamilyName = document.body[i2 + 1 : i2 + 2]
1988 document.body[k + 1 : k + 1] = ["\\begin_layout Name", "\\begin_inset Argument 1", "status open", "",
1989 "\\begin_layout Plain Layout", FirstName[0], "\\end_layout", "",
1990 "\\end_inset", "", FamilyName[0], "\\end_layout", ""]
1991 #document.body[i2 + 1 : i2 + 1] = ["hellok: ", str(k)]
1992 del(document.body[l : k])
1998 def revert_achemso(document):
1999 " Reverts the flex inset Latin to TeX code "
2003 i = find_token(document.body, "\\begin_inset Flex Latin", i)
2005 j = find_end_of_inset(document.body, i)
2009 beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
2010 endPlain = find_end_of_layout(document.body, beginPlain)
2011 content = lyx2latex(document, document.body[beginPlain : endPlain])
2012 document.body[i:j + 1] = put_cmd_in_ert("\\latin{" + content + "}")
2014 document.warning("Malformed LyX document: Can't find end of flex inset Latin")
2023 supported_versions = ["2.2.0", "2.2"]
2025 [475, [convert_separator]],
2026 # nothing to do for 476: We consider it a bug that older versions
2027 # did not load amsmath automatically for these commands, and do not
2028 # want to hardcode amsmath off.
2034 [481, [convert_dashes]],
2035 [482, [convert_phrases]],
2036 [483, [convert_specialchar]],
2041 [488, [convert_newgloss]],
2042 [489, [convert_BoxFeatures]],
2043 [490, [convert_origin]],
2045 [492, [convert_colorbox]],
2048 [495, [convert_subref]],
2049 [496, [convert_nounzip]],
2050 [497, [convert_external_bbox]],
2052 [499, [convert_moderncv]],
2057 [499, [revert_achemso]],
2058 [498, [revert_moderncv_1, revert_moderncv_2]],
2059 [497, [revert_tcolorbox_1, revert_tcolorbox_2,
2060 revert_tcolorbox_3, revert_tcolorbox_4, revert_tcolorbox_5,
2061 revert_tcolorbox_6, revert_tcolorbox_7, revert_tcolorbox_8]],
2062 [496, [revert_external_bbox]],
2063 [495, []], # nothing to do since the noUnzip parameter was optional
2064 [494, [revert_subref]],
2065 [493, [revert_jss]],
2066 [492, [revert_mathmulticol]],
2067 [491, [revert_colorbox]],
2068 [490, [revert_textcolor]],
2069 [489, [revert_origin]],
2070 [488, [revert_BoxFeatures]],
2071 [487, [revert_newgloss, revert_glossgroup]],
2072 [486, [revert_forest]],
2073 [485, [revert_ex_itemargs]],
2074 [484, [revert_sigplan_doi]],
2075 [483, [revert_georgian]],
2076 [482, [revert_specialchar]],
2077 [481, [revert_phrases]],
2078 [480, [revert_dashes]],
2079 [479, [revert_question_env]],
2080 [478, [revert_beamer_lemma]],
2081 [477, [revert_xarrow]],
2082 [476, [revert_swissgerman]],
2083 [475, [revert_smash]],
2084 [474, [revert_separator]]
2088 if __name__ == "__main__":