1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 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#, \
34 # insert_to_preamble, lyx2latex, latex_length, revert_flex_inset, \
35 # revert_font_attrs, hex2ratio, str2bool
37 from parser_tools import find_token, find_token_backwards, find_re, \
38 find_end_of_inset, find_end_of_layout, find_nonempty_line, \
39 get_containing_layout, get_value, check_token
41 ###############################################################################
43 ### Conversion and reversion routines
45 ###############################################################################
47 def convert_separator(document):
49 Convert layout separators to separator insets and add (LaTeX) paragraph
50 breaks in order to mimic previous LaTeX export.
53 parins = ["\\begin_inset Separator parbreak", "\\end_inset", ""]
54 parlay = ["\\begin_layout Standard", "\\begin_inset Separator parbreak",
55 "\\end_inset", "", "\\end_layout", ""]
67 i = find_token(document.body, "\\begin_deeper", i)
71 j = find_token_backwards(document.body, "\\end_layout", i-1)
73 # reset any text style before inserting the inset
74 lay = get_containing_layout(document.body, j-1)
76 content = "\n".join(document.body[lay[1]:lay[2]])
77 for val in sty_dict.keys():
78 if content.find("\\%s" % val) != -1:
79 document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])]
82 document.body[j:j] = parins
83 i = i + len(parins) + 1
89 i = find_token(document.body, "\\align", i)
93 lay = get_containing_layout(document.body, i)
94 if lay != False and lay[0] == "Plain Layout":
98 j = find_token_backwards(document.body, "\\end_layout", i-1)
100 lay = get_containing_layout(document.body, j-1)
101 if lay != False and lay[0] == "Standard" \
102 and find_token(document.body, "\\align", lay[1], lay[2]) == -1 \
103 and find_token(document.body, "\\begin_inset VSpace", lay[1], lay[2]) == -1:
104 # reset any text style before inserting the inset
105 content = "\n".join(document.body[lay[1]:lay[2]])
106 for val in sty_dict.keys():
107 if content.find("\\%s" % val) != -1:
108 document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])]
111 document.body[j:j] = parins
112 i = i + len(parins) + 1
118 regexp = re.compile(r'^\\begin_layout (?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE)
122 i = find_re(document.body, regexp, i)
126 j = find_end_of_layout(document.body, i)
128 document.warning("Malformed LyX document: Missing `\\end_layout'.")
131 lay = get_containing_layout(document.body, j-1)
133 lines = document.body[lay[3]:lay[2]]
137 document.body[i:j+1] = parlay
139 document.body[i+1:i+1] = lines
141 i = i + len(parlay) + len(lines) + 1
144 def revert_separator(document):
145 " Revert separator insets to layout separators "
147 beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
148 if document.textclass in beamer_classes:
149 beglaysep = "\\begin_layout Separator"
151 beglaysep = "\\begin_layout --Separator--"
153 parsep = [beglaysep, "", "\\end_layout", ""]
154 comert = ["\\begin_inset ERT", "status collapsed", "",
155 "\\begin_layout Plain Layout", "%", "\\end_layout",
156 "", "\\end_inset", ""]
157 empert = ["\\begin_inset ERT", "status collapsed", "",
158 "\\begin_layout Plain Layout", " ", "\\end_layout",
159 "", "\\end_inset", ""]
163 i = find_token(document.body, "\\begin_inset Separator", i)
167 lay = get_containing_layout(document.body, i)
169 document.warning("Malformed LyX document: Can't convert separator inset at line " + str(i))
176 kind = get_value(document.body, "\\begin_inset Separator", i, i+1, "plain").split()[1]
177 before = document.body[beg+1:i]
178 something_before = len(before) > 0 and len("".join(before)) > 0
179 j = find_end_of_inset(document.body, i)
180 after = document.body[j+1:end]
181 something_after = len(after) > 0 and len("".join(after)) > 0
183 beg = beg + len(before) + 1
184 elif something_before:
185 document.body[i:i] = ["\\end_layout", ""]
193 document.body[beg:j+1] = empert
196 document.body[beg:j+1] = comert
200 if layoutname == "Standard":
201 if not something_before:
202 document.body[beg:j+1] = parsep
204 document.body[i:i] = ["", "\\begin_layout Standard"]
207 document.body[beg:j+1] = ["\\begin_layout Standard"]
210 document.body[beg:j+1] = ["\\begin_deeper"]
212 end = end + 1 - (j + 1 - beg)
213 if not something_before:
214 document.body[i:i] = parsep
216 end = end + len(parsep)
217 document.body[i:i] = ["\\begin_layout Standard"]
218 document.body[end+2:end+2] = ["", "\\end_deeper", ""]
221 next_par_is_aligned = False
222 k = find_nonempty_line(document.body, end+1)
223 if k != -1 and check_token(document.body[k], "\\begin_layout"):
224 lay = get_containing_layout(document.body, k)
225 next_par_is_aligned = lay != False and \
226 find_token(document.body, "\\align", lay[1], lay[2]) != -1
227 if k != -1 and not next_par_is_aligned \
228 and not check_token(document.body[k], "\\end_deeper") \
229 and not check_token(document.body[k], "\\begin_deeper"):
230 if layoutname == "Standard":
231 document.body[beg:j+1] = [beglaysep]
234 document.body[beg:j+1] = ["\\begin_deeper", beglaysep]
235 end = end + 2 - (j + 1 - beg)
236 document.body[end+1:end+1] = ["", "\\end_deeper", ""]
240 del document.body[i:end+1]
242 del document.body[i:end-1]
247 def revert_smash(document):
248 " Set amsmath to on if smash commands are used "
250 commands = ["smash[t]", "smash[b]", "notag"]
251 i = find_token(document.header, "\\use_package amsmath", 0)
253 document.warning("Malformed LyX document: Can't find \\use_package amsmath.")
255 value = get_value(document.header, "\\use_package amsmath", i).split()[1]
257 # nothing to do if package is not auto but on or off
261 j = find_token(document.body, '\\begin_inset Formula', j)
264 k = find_end_of_inset(document.body, j)
266 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(j))
269 code = "\n".join(document.body[j:k])
271 if code.find("\\%s" % c) != -1:
272 # set amsmath to on, since it is loaded by the newer format
273 document.header[i] = "\\use_package amsmath 2"
278 def revert_swissgerman(document):
279 " Set language german-ch-old to german "
281 if document.language == "german-ch-old":
282 document.language = "german"
283 i = find_token(document.header, "\\language", 0)
285 document.header[i] = "\\language german"
288 j = find_token(document.body, "\\lang german-ch-old", j)
291 document.body[j] = document.body[j].replace("\\lang german-ch-old", "\\lang german")
295 def revert_use_package(document, pkg, commands, oldauto):
296 # oldauto defines how the version we are reverting to behaves:
297 # if it is true, the old version uses the package automatically.
298 # if it is false, the old version never uses the package.
299 regexp = re.compile(r'(\\use_package\s+%s)' % pkg)
300 i = find_re(document.header, regexp, 0)
301 value = "1" # default is auto
303 value = get_value(document.header, "\\use_package" , i).split()[1]
304 del document.header[i]
305 if value == "2": # on
306 add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
307 elif value == "1" and not oldauto: # auto
310 i = find_token(document.body, '\\begin_inset Formula', i)
313 j = find_end_of_inset(document.body, i)
315 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
318 code = "\n".join(document.body[i:j])
320 if code.find("\\%s" % c) != -1:
321 add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
326 mathtools_commands = ["xhookrightarrow", "xhookleftarrow", "xRightarrow", \
327 "xrightharpoondown", "xrightharpoonup", "xrightleftharpoons", \
328 "xLeftarrow", "xleftharpoondown", "xleftharpoonup", \
329 "xleftrightarrow", "xLeftrightarrow", "xleftrightharpoons", \
332 def revert_xarrow(document):
333 "remove use_package mathtools"
334 revert_use_package(document, "mathtools", mathtools_commands, False)
337 def revert_beamer_lemma(document):
338 " Reverts beamer lemma layout to ERT "
340 beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"]
341 if document.textclass not in beamer_classes:
347 i = find_token(document.body, "\\begin_layout Lemma", i)
350 j = find_end_of_layout(document.body, i)
352 document.warning("Malformed LyX document: Can't find end of Lemma layout")
355 arg1 = find_token(document.body, "\\begin_inset Argument 1", i, j)
356 endarg1 = find_end_of_inset(document.body, arg1)
357 arg2 = find_token(document.body, "\\begin_inset Argument 2", i, j)
358 endarg2 = find_end_of_inset(document.body, arg2)
362 beginPlain1 = find_token(document.body, "\\begin_layout Plain Layout", arg1, endarg1)
363 if beginPlain1 == -1:
364 document.warning("Malformed LyX document: Can't find arg1 plain Layout")
367 endPlain1 = find_end_of_inset(document.body, beginPlain1)
368 content1 = document.body[beginPlain1 + 1 : endPlain1 - 2]
369 subst1 = put_cmd_in_ert("<") + content1 + put_cmd_in_ert(">")
371 beginPlain2 = find_token(document.body, "\\begin_layout Plain Layout", arg2, endarg2)
372 if beginPlain2 == -1:
373 document.warning("Malformed LyX document: Can't find arg2 plain Layout")
376 endPlain2 = find_end_of_inset(document.body, beginPlain2)
377 content2 = document.body[beginPlain2 + 1 : endPlain2 - 2]
378 subst2 = put_cmd_in_ert("[") + content2 + put_cmd_in_ert("]")
382 del document.body[arg2 : endarg2 + 1]
384 del document.body[arg1 : endarg1 + 1]
386 del document.body[arg1 : endarg1 + 1]
388 del document.body[arg2 : endarg2 + 1]
390 # index of end layout has probably changed
391 j = find_end_of_layout(document.body, i)
393 document.warning("Malformed LyX document: Can't find end of Lemma layout")
399 # if this is not a consecutive env, add start command
401 begcmd = put_cmd_in_ert("\\begin{lemma}")
403 # has this a consecutive lemma?
404 consecutive = document.body[j + 2] == "\\begin_layout Lemma"
406 # if this is not followed by a consecutive env, add end command
408 document.body[j : j + 1] = put_cmd_in_ert("\\end{lemma}") + ["\\end_layout"]
410 document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd + subst1 + subst2
416 def revert_question_env(document):
418 Reverts question and question* environments of
419 theorems-ams-extended-bytype module to ERT
422 # Do we use theorems-ams-extended-bytype module?
424 mods = document.get_module_list()
426 if mod == "theorems-ams-extended-bytype":
436 i = find_token(document.body, "\\begin_layout Question", i)
440 starred = document.body[i] == "\\begin_layout Question*"
442 j = find_end_of_layout(document.body, i)
444 document.warning("Malformed LyX document: Can't find end of Question layout")
448 # if this is not a consecutive env, add start command
452 begcmd = put_cmd_in_ert("\\begin{question*}")
454 begcmd = put_cmd_in_ert("\\begin{question}")
456 # has this a consecutive theorem of same type?
459 consecutive = document.body[j + 2] == "\\begin_layout Question*"
461 consecutive = document.body[j + 2] == "\\begin_layout Question"
463 # if this is not followed by a consecutive env, add end command
466 document.body[j : j + 1] = put_cmd_in_ert("\\end{question*}") + ["\\end_layout"]
468 document.body[j : j + 1] = put_cmd_in_ert("\\end{question}") + ["\\end_layout"]
470 document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd
472 add_to_preamble(document, "\\providecommand{\questionname}{Question}")
475 add_to_preamble(document, "\\theoremstyle{plain}\n" \
476 "\\newtheorem*{question*}{\\protect\\questionname}")
478 add_to_preamble(document, "\\theoremstyle{plain}\n" \
479 "\\newtheorem{question}{\\protect\\questionname}")
484 def convert_dashes(document):
485 "convert -- and --- to \\twohyphens and \\threehyphens"
487 if document.backend != "latex":
491 while i < len(document.body):
492 words = document.body[i].split()
493 if len(words) > 1 and words[0] == "\\begin_inset" and \
494 words[1] in ["CommandInset", "ERT", "Formula", "IPA"]:
495 # must not replace anything in insets that store LaTeX contents in .lyx files
496 # (math and command insets withut overridden read() and write() methods
497 # filtering out IPA makes Text::readParToken() more simple
498 # skip ERT as well since it is not needed there
499 j = find_end_of_inset(document.body, i)
501 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
507 j = document.body[i].find("--")
510 front = document.body[i][:j]
511 back = document.body[i][j+2:]
512 # We can have an arbitrary number of consecutive hyphens.
513 # These must be split into the corresponding number of two and three hyphens
514 # We must match what LaTeX does: First try emdash, then endash, then single hyphen
515 if back.find("-") == 0:
518 document.body.insert(i+1, back)
519 document.body[i] = front + "\\threehyphens"
522 document.body.insert(i+1, back)
523 document.body[i] = front + "\\twohyphens"
527 def revert_dashes(document):
528 "convert \\twohyphens and \\threehyphens to -- and ---"
531 while i < len(document.body):
532 words = document.body[i].split()
533 if len(words) > 1 and words[0] == "\\begin_inset" and \
534 words[1] in ["CommandInset", "ERT", "Formula", "IPA"]:
536 j = find_end_of_inset(document.body, i)
538 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
544 if document.body[i].find("\\twohyphens") >= 0:
545 document.body[i] = document.body[i].replace("\\twohyphens", "--")
547 if document.body[i].find("\\threehyphens") >= 0:
548 document.body[i] = document.body[i].replace("\\threehyphens", "---")
550 if replaced and i+1 < len(document.body) and \
551 (document.body[i+1].find("\\") != 0 or \
552 document.body[i+1].find("\\twohyphens") == 0 or
553 document.body[i+1].find("\\threehyphens") == 0) and \
554 len(document.body[i]) + len(document.body[i+1]) <= 80:
555 document.body[i] = document.body[i] + document.body[i+1]
556 document.body[i+1:i+2] = []
565 supported_versions = ["2.2.0", "2.2"]
567 [475, [convert_separator]],
568 # nothing to do for 476: We consider it a bug that older versions
569 # did not load amsmath automatically for these commands, and do not
570 # want to hardcode amsmath off.
576 [481, [convert_dashes]]
580 [480, [revert_dashes]],
581 [479, [revert_question_env]],
582 [478, [revert_beamer_lemma]],
583 [477, [revert_xarrow]],
584 [476, [revert_swissgerman]],
585 [475, [revert_smash]],
586 [474, [revert_separator]]
590 if __name__ == "__main__":