1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2010 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.0"""
26 from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
28 ####################################################################
29 # Private helper functions
31 def remove_option(document, m, option):
32 l = document.body[m].find(option)
34 val = document.body[m][l:].split('"')[1]
35 document.body[m] = document.body[m][:l - 1] + document.body[m][l+len(option + '="' + val + '"'):]
38 def find_end_of_inset(lines, i):
39 " Find end of inset, where lines[i] is included."
40 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
43 # Note that text can be either a list of lines or a single line.
44 def add_to_preamble(document, text):
45 """ Add text to the preamble if it is not already there.
46 Only the first line is checked!"""
48 if not type(text) is list:
49 # split on \n just in case
50 # it'll give us the one element list we want
51 # if there's no \n, too
52 text = text.split('\n')
54 if find_token(document.preamble, text[0], 0) != -1:
57 document.preamble.extend(text)
60 def insert_to_preamble(index, document, text):
61 """ Insert text to the preamble at a given line"""
63 document.preamble.insert(index, text)
66 def read_unicodesymbols():
67 " Read the unicodesymbols list of unicode characters and corresponding commands."
68 pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
69 fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
71 # Two backslashes, followed by some non-word character, and then a character
72 # in brackets. The idea is to check for constructs like: \"{u}, which is how
73 # they are written in the unicodesymbols file; but they can also be written
74 # as: \"u or even \" u.
75 r = re.compile(r'\\\\(\W)\{(\w)\}')
76 for line in fp.readlines():
77 if line[0] != '#' and line.strip() != "":
78 line=line.replace(' "',' ') # remove all quotation marks with spaces before
79 line=line.replace('" ',' ') # remove all quotation marks with spaces after
80 line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
82 [ucs4,command,dead] = line.split(None,2)
83 if command[0:1] != "\\":
85 spec_chars.append([command, unichr(eval(ucs4))])
91 # If the character is a double-quote, then we need to escape it, too,
92 # since it is done that way in the LyX file.
93 if m.group(1) == "\"":
96 command += m.group(1) + m.group(2)
97 commandbl += m.group(1) + ' ' + m.group(2)
98 spec_chars.append([command, unichr(eval(ucs4))])
99 spec_chars.append([commandbl, unichr(eval(ucs4))])
104 unicode_reps = read_unicodesymbols()
107 # DO NOT USE THIS ROUTINE ANY MORE. Better yet, replace the uses that
108 # have been made of it with uses of put_cmd_in_ert.
109 def old_put_cmd_in_ert(string):
110 for rep in unicode_reps:
111 string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
112 string = string.replace('\\', "\\backslash\n")
113 string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Plain Layout\n" \
114 + string + "\n\\end_layout\n\\end_inset"
118 # This routine wraps some content in an ERT inset.
120 # NOTE: The function accepts either a single string or a LIST of strings as
121 # argument. But it returns a LIST of strings, split on \n, so that it does
122 # not have embedded newlines.
124 # This is how lyx2lyx represents a LyX document: as a list of strings,
125 # each representing a line of a LyX file. Embedded newlines confuse
128 # A call to this routine will often go something like this:
129 # i = find_token('\\begin_inset FunkyInset', ...)
131 # j = find_end_of_inset(document.body, i)
132 # content = ...extract content from insets
133 # # that could be as simple as:
134 # # content = lyx2latex(document[i:j + 1])
135 # ert = put_cmd_in_ert(content)
136 # document.body[i:j] = ert
137 # Now, before we continue, we need to reset i appropriately. Normally,
140 # That puts us right after the ERT we just inserted.
142 def put_cmd_in_ert(arg):
143 ret = ["\\begin_inset ERT", "status collapsed", "\\begin_layout Plain Layout", ""]
144 # Despite the warnings just given, it will be faster for us to work
145 # with a single string internally. That way, we only go through the
146 # unicode_reps loop once.
147 if type(arg) is list:
151 for rep in unicode_reps:
152 s = s.replace(rep[1], rep[0].replace('\\\\', '\\'))
153 s = s.replace('\\', "\\backslash\n")
154 ret += s.splitlines()
155 ret += ["\\end_layout", "\\end_inset"]
159 def lyx2latex(document, lines):
160 'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
161 # clean up multiline stuff
167 for curline in range(len(lines)):
168 line = lines[curline]
169 if line.startswith("\\begin_inset Note Note"):
170 # We want to skip LyX notes, so remember where the inset ends
171 note_end = find_end_of_inset(lines, curline + 1)
173 elif note_end >= curline:
176 elif line.startswith("\\begin_inset ERT"):
177 # We don't want to replace things inside ERT, so figure out
178 # where the end of the inset is.
179 ert_end = find_end_of_inset(lines, curline + 1)
181 elif line.startswith("\\begin_inset Formula"):
183 elif line.startswith("\\begin_inset Quotes"):
184 # For now, we do a very basic reversion. Someone who understands
185 # quotes is welcome to fix it up.
186 qtype = line[20:].strip()
200 elif line.startswith("\\begin_inset space"):
201 line = line[18:].strip()
202 if line.startswith("\\hspace"):
203 # Account for both \hspace and \hspace*
206 elif line == "\\space{}":
208 elif line == "\\thinspace{}":
211 # The LyX length is in line[8:], after the \length keyword
212 # latex_length returns "bool,length"
213 length = latex_length(line[8:]).split(",")[1]
214 line = hspace + "{" + length + "}"
216 elif line.isspace() or \
217 line.startswith("\\begin_layout") or \
218 line.startswith("\\end_layout") or \
219 line.startswith("\\begin_inset") or \
220 line.startswith("\\end_inset") or \
221 line.startswith("\\lang") or \
222 line.strip() == "status collapsed" or \
223 line.strip() == "status open":
227 # this needs to be added to the preamble because of cases like
228 # \textmu, \textbackslash, etc.
229 add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
230 '\\@ifundefined{textmu}',
231 ' {\\usepackage{textcomp}}{}'])
232 # a lossless reversion is not possible
233 # try at least to handle some common insets and settings
234 if ert_end >= curline:
235 line = line.replace(r'\backslash', '\\')
237 # No need to add "{}" after single-nonletter macros
238 line = line.replace('&', '\\&')
239 line = line.replace('#', '\\#')
240 line = line.replace('^', '\\textasciicircum{}')
241 line = line.replace('%', '\\%')
242 line = line.replace('_', '\\_')
243 line = line.replace('$', '\\$')
245 # Do the LyX text --> LaTeX conversion
246 for rep in unicode_reps:
247 line = line.replace(rep[1], rep[0] + "{}")
248 line = line.replace(r'\backslash', r'\textbackslash{}')
249 line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
250 line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
251 line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
252 line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
253 line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
254 line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
255 line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
256 line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
257 line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
262 def latex_length(string):
263 'Convert lengths to their LaTeX representation.'
266 # the string has the form
267 # ValueUnit+ValueUnit-ValueUnit or
268 # ValueUnit+-ValueUnit
269 # the + and - (glue lengths) are optional
270 # the + always precedes the -
272 # Convert relative lengths to LaTeX units
273 units = {"text%":"\\textwidth", "col%":"\\columnwidth",
274 "page%":"\\paperwidth", "line%":"\\linewidth",
275 "theight%":"\\textheight", "pheight%":"\\paperheight"}
276 for unit in units.keys():
277 i = string.find(unit)
280 minus = string.rfind("-", 1, i)
281 plus = string.rfind("+", 0, i)
282 latex_unit = units[unit]
283 if plus == -1 and minus == -1:
285 value = str(float(value)/100)
286 end = string[i + len(unit):]
287 string = value + latex_unit + end
289 value = string[plus + 1:i]
290 value = str(float(value)/100)
291 begin = string[:plus + 1]
292 end = string[i+len(unit):]
293 string = begin + value + latex_unit + end
295 value = string[minus + 1:i]
296 value = str(float(value)/100)
297 begin = string[:minus + 1]
298 string = begin + value + latex_unit
300 # replace + and -, but only if the - is not the first character
301 string = string[0] + string[1:].replace("+", " plus ").replace("-", " minus ")
302 # handle the case where "+-1mm" was used, because LaTeX only understands
303 # "plus 1mm minus 1mm"
304 if string.find("plus minus"):
305 lastvaluepos = string.rfind(" ")
306 lastvalue = string[lastvaluepos:]
307 string = string.replace(" ", lastvalue + " ")
309 return "False," + string
311 return "True," + string
314 def revert_flex_inset(document, name, LaTeXname, position):
315 " Convert flex insets to TeX code "
318 i = find_token(document.body, '\\begin_inset Flex ' + name, i)
321 z = find_end_of_inset(document.body, i)
323 document.warning("Malformed LyX document: Can't find end of Flex " + name + " inset.")
325 # remove the \end_inset
326 document.body[z - 2:z + 1] = put_cmd_in_ert("}")
327 # we need to reset character layouts if necessary
328 j = find_token(document.body, '\\emph on', i)
329 k = find_token(document.body, '\\noun on', i)
330 l = find_token(document.body, '\\series', i)
331 m = find_token(document.body, '\\family', i)
332 n = find_token(document.body, '\\shape', i)
333 o = find_token(document.body, '\\color', i)
334 p = find_token(document.body, '\\size', i)
335 q = find_token(document.body, '\\bar under', i)
336 r = find_token(document.body, '\\uuline on', i)
337 s = find_token(document.body, '\\uwave on', i)
338 t = find_token(document.body, '\\strikeout on', i)
339 if j != -1 and j < z:
340 document.body.insert(z - 2, "\\emph default")
341 if k != -1 and k < z:
342 document.body.insert(z - 2, "\\noun default")
343 if l != -1 and l < z:
344 document.body.insert(z - 2, "\\series default")
345 if m != -1 and m < z:
346 document.body.insert(z - 2, "\\family default")
347 if n != -1 and n < z:
348 document.body.insert(z - 2, "\\shape default")
349 if o != -1 and o < z:
350 document.body.insert(z - 2, "\\color inherit")
351 if p != -1 and p < z:
352 document.body.insert(z - 2, "\\size default")
353 if q != -1 and q < z:
354 document.body.insert(z - 2, "\\bar default")
355 if r != -1 and r < z:
356 document.body.insert(z - 2, "\\uuline default")
357 if s != -1 and s < z:
358 document.body.insert(z - 2, "\\uwave default")
359 if t != -1 and t < z:
360 document.body.insert(z - 2, "\\strikeout default")
361 document.body[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
365 def revert_charstyles(document, name, LaTeXname, changed):
366 " Reverts character styles to TeX code "
369 i = find_token(document.body, name + ' on', i)
372 j = find_token(document.body, name + ' default', i)
373 k = find_token(document.body, name + ' on', i + 1)
374 # if there is no default set, the style ends with the layout
375 # assure hereby that we found the correct layout end
376 if j != -1 and (j < k or k == -1):
377 document.body[j:j + 1] = put_cmd_in_ert("}")
379 j = find_token(document.body, '\\end_layout', i)
380 document.body[j:j] = put_cmd_in_ert("}")
381 document.body[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
386 def revert_layout_command(document, name, LaTeXname, position):
387 " Reverts a command from a layout to TeX code "
390 i = find_token(document.body, '\\begin_layout ' + name, i)
394 # find the next layout
397 j = find_token(document.body, '\\begin_layout', j)
398 l = len(document.body)
399 # if nothing was found it was the last layout of the document
401 document.body[l - 4:l - 4] = put_cmd_in_ert("}")
403 # exclude plain layout because this can be TeX code or another inset
404 elif document.body[j] != '\\begin_layout Plain Layout':
405 document.body[j - 2:j - 2] = put_cmd_in_ert("}")
409 document.body[i] = '\\begin_layout Standard'
410 document.body[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
414 ####################################################################
417 def revert_swiss(document):
418 " Set language german-ch to ngerman "
420 if document.language == "german-ch":
421 document.language = "ngerman"
422 i = find_token(document.header, "\\language", 0)
424 document.header[i] = "\\language ngerman"
427 j = find_token(document.body, "\\lang german-ch", j)
430 document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
434 def revert_tabularvalign(document):
435 " Revert the tabular valign option "
438 i = find_token(document.body, "\\begin_inset Tabular", i)
441 j = find_token(document.body, "</cell>", i)
443 document.warning("Malformed LyX document: Could not find end of tabular cell.")
446 # don't set a box for longtables, only delete tabularvalignment
447 # the alignment is 2 lines below \\begin_inset Tabular
448 p = document.body[i + 2].find("islongtable")
450 q = document.body[i + 2].find("tabularvalignment")
452 document.body[i + 2] = document.body[i + 2][:q - 1]
453 document.body[i + 2] = document.body[i + 2] + '>'
458 tabularvalignment = 'c'
459 # which valignment is specified?
460 m = document.body[i + 2].find('tabularvalignment="top"')
462 tabularvalignment = 't'
463 m = document.body[ i+ 2].find('tabularvalignment="bottom"')
465 tabularvalignment = 'b'
466 # delete tabularvalignment
467 q = document.body[i + 2].find("tabularvalignment")
469 document.body[i + 2] = document.body[i + 2][:q - 1]
470 document.body[i + 2] = document.body[i + 2] + '>'
472 # don't add a box when centered
473 if tabularvalignment == 'c':
476 subst = ['\\end_layout', '\\end_inset']
477 document.body[j:j] = subst # just inserts those lines
478 subst = ['\\begin_inset Box Frameless',
479 'position "' + tabularvalignment +'"',
484 # we don't know the width, assume 50%
488 'height_special "totalheight"',
491 '\\begin_layout Plain Layout']
492 document.body[i:i] = subst # this just inserts the array at i
493 i += len(subst) + 2 # adjust i to save a few cycles
496 def revert_phantom(document):
497 " Reverts phantom to ERT "
501 i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
504 substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
505 '\\begin_inset ERT\nstatus collapsed\n\n' \
506 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
507 'phantom{\n\\end_layout\n\n\\end_inset\n')
508 substi = substi.split('\n')
509 document.body[i:i + 4] = substi
511 j = find_token(document.body, "\\end_layout", i)
513 document.warning("Malformed LyX document: Could not find end of Phantom inset.")
515 substj = document.body[j].replace('\\end_layout', \
516 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
517 '\\begin_layout Plain Layout\n\n' \
518 '}\n\\end_layout\n\n\\end_inset\n')
519 substj = substj.split('\n')
520 document.body[j:j + 4] = substj
524 def revert_hphantom(document):
525 " Reverts hphantom to ERT "
529 i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
532 substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
533 '\\begin_inset ERT\nstatus collapsed\n\n' \
534 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
535 'hphantom{\n\\end_layout\n\n\\end_inset\n')
536 substi = substi.split('\n')
537 document.body[i:i + 4] = substi
539 j = find_token(document.body, "\\end_layout", i)
541 document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
543 substj = document.body[j].replace('\\end_layout', \
544 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
545 '\\begin_layout Plain Layout\n\n' \
546 '}\n\\end_layout\n\n\\end_inset\n')
547 substj = substj.split('\n')
548 document.body[j:j + 4] = substj
552 def revert_vphantom(document):
553 " Reverts vphantom to ERT "
557 i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
560 substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
561 '\\begin_inset ERT\nstatus collapsed\n\n' \
562 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
563 'vphantom{\n\\end_layout\n\n\\end_inset\n')
564 substi = substi.split('\n')
565 document.body[i:i + 4] = substi
567 j = find_token(document.body, "\\end_layout", i)
569 document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
571 substj = document.body[j].replace('\\end_layout', \
572 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
573 '\\begin_layout Plain Layout\n\n' \
574 '}\n\\end_layout\n\n\\end_inset\n')
575 substj = substj.split('\n')
576 document.body[j:j + 4] = substj
580 def revert_xetex(document):
581 " Reverts documents that use XeTeX "
582 i = find_token(document.header, '\\use_xetex', 0)
584 document.warning("Malformed LyX document: Missing \\use_xetex.")
586 if get_value(document.header, "\\use_xetex", i) == 'false':
587 del document.header[i]
589 del document.header[i]
590 # 1.) set doc encoding to utf8-plain
591 i = find_token(document.header, "\\inputencoding", 0)
593 document.warning("Malformed LyX document: Missing \\inputencoding.")
594 document.header[i] = "\\inputencoding utf8-plain"
595 # 2.) check font settings
596 l = find_token(document.header, "\\font_roman", 0)
598 document.warning("Malformed LyX document: Missing \\font_roman.")
599 line = document.header[l]
600 l = re.compile(r'\\font_roman (.*)$')
603 l = find_token(document.header, "\\font_sans", 0)
605 document.warning("Malformed LyX document: Missing \\font_sans.")
606 line = document.header[l]
607 l = re.compile(r'\\font_sans (.*)$')
610 l = find_token(document.header, "\\font_typewriter", 0)
612 document.warning("Malformed LyX document: Missing \\font_typewriter.")
613 line = document.header[l]
614 l = re.compile(r'\\font_typewriter (.*)$')
616 typewriter = m.group(1)
617 osf = get_value(document.header, '\\font_osf', 0) == "true"
618 sf_scale = float(get_value(document.header, '\\font_sf_scale', 0))
619 tt_scale = float(get_value(document.header, '\\font_tt_scale', 0))
620 # 3.) set preamble stuff
621 pretext = '%% This document must be processed with xelatex!\n'
622 pretext += '\\usepackage{fontspec}\n'
623 if roman != "default":
624 pretext += '\\setmainfont[Mapping=tex-text]{' + roman + '}\n'
625 if sans != "default":
626 pretext += '\\setsansfont['
628 pretext += 'Scale=' + str(sf_scale / 100) + ','
629 pretext += 'Mapping=tex-text]{' + sans + '}\n'
630 if typewriter != "default":
631 pretext += '\\setmonofont'
633 pretext += '[Scale=' + str(tt_scale / 100) + ']'
634 pretext += '{' + typewriter + '}\n'
636 pretext += '\\defaultfontfeatures{Numbers=OldStyle}\n'
637 pretext += '\usepackage{xunicode}\n'
638 pretext += '\usepackage{xltxtra}\n'
639 insert_to_preamble(0, document, pretext)
640 # 4.) reset font settings
641 i = find_token(document.header, "\\font_roman", 0)
643 document.warning("Malformed LyX document: Missing \\font_roman.")
644 document.header[i] = "\\font_roman default"
645 i = find_token(document.header, "\\font_sans", 0)
647 document.warning("Malformed LyX document: Missing \\font_sans.")
648 document.header[i] = "\\font_sans default"
649 i = find_token(document.header, "\\font_typewriter", 0)
651 document.warning("Malformed LyX document: Missing \\font_typewriter.")
652 document.header[i] = "\\font_typewriter default"
653 i = find_token(document.header, "\\font_osf", 0)
655 document.warning("Malformed LyX document: Missing \\font_osf.")
656 document.header[i] = "\\font_osf false"
657 i = find_token(document.header, "\\font_sc", 0)
659 document.warning("Malformed LyX document: Missing \\font_sc.")
660 document.header[i] = "\\font_sc false"
661 i = find_token(document.header, "\\font_sf_scale", 0)
663 document.warning("Malformed LyX document: Missing \\font_sf_scale.")
664 document.header[i] = "\\font_sf_scale 100"
665 i = find_token(document.header, "\\font_tt_scale", 0)
667 document.warning("Malformed LyX document: Missing \\font_tt_scale.")
668 document.header[i] = "\\font_tt_scale 100"
671 def revert_outputformat(document):
672 " Remove default output format param "
673 i = find_token(document.header, '\\default_output_format', 0)
675 document.warning("Malformed LyX document: Missing \\default_output_format.")
677 del document.header[i]
680 def revert_backgroundcolor(document):
681 " Reverts background color to preamble code "
685 i = find_token(document.header, "\\backgroundcolor", i)
688 colorcode = get_value(document.header, '\\backgroundcolor', 0)
689 del document.header[i]
690 # don't clutter the preamble if backgroundcolor is not set
691 if colorcode == "#ffffff":
693 # the color code is in the form #rrggbb where every character denotes a hex number
694 # convert the string to an int
695 red = string.atoi(colorcode[1:3],16)
696 # we want the output "0.5" for the value "127" therefore add here
699 redout = float(red) / 256
700 green = string.atoi(colorcode[3:5],16)
703 greenout = float(green) / 256
704 blue = string.atoi(colorcode[5:7],16)
707 blueout = float(blue) / 256
709 insert_to_preamble(0, document,
710 '% Commands inserted by lyx2lyx to set the background color\n'
711 + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
712 + '\\definecolor{page_backgroundcolor}{rgb}{'
713 + str(redout) + ', ' + str(greenout)
714 + ', ' + str(blueout) + '}\n'
715 + '\\pagecolor{page_backgroundcolor}\n')
718 def revert_splitindex(document):
719 " Reverts splitindex-aware documents "
720 i = find_token(document.header, '\\use_indices', 0)
722 document.warning("Malformed LyX document: Missing \\use_indices.")
724 indices = get_value(document.header, "\\use_indices", i)
726 if indices == "true":
727 preamble += "\\usepackage{splitidx}\n"
728 del document.header[i]
731 i = find_token(document.header, "\\index", i)
734 k = find_token(document.header, "\\end_index", i)
736 document.warning("Malformed LyX document: Missing \\end_index.")
738 line = document.header[i]
739 l = re.compile(r'\\index (.*)$')
742 ishortcut = get_value(document.header, '\\shortcut', i, k)
743 if ishortcut != "" and indices == "true":
744 preamble += "\\newindex[" + iname + "]{" + ishortcut + "}\n"
745 del document.header[i:k + 1]
748 insert_to_preamble(0, document, preamble)
751 i = find_token(document.body, "\\begin_inset Index", i)
754 line = document.body[i]
755 l = re.compile(r'\\begin_inset Index (.*)$')
758 if itype == "idx" or indices == "false":
759 document.body[i] = "\\begin_inset Index"
761 k = find_end_of_inset(document.body, i)
764 content = lyx2latex(document, document.body[i:k])
766 content = content.replace('"', r'\"')
767 subst = [old_put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")]
768 document.body[i:k + 1] = subst
772 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
775 k = find_end_of_inset(document.body, i)
776 ptype = get_value(document.body, 'type', i, k).strip('"')
778 j = find_token(document.body, "type", i, k)
780 elif indices == "false":
781 del document.body[i:k + 1]
783 subst = [old_put_cmd_in_ert("\\printindex[" + ptype + "]{}")]
784 document.body[i:k + 1] = subst
788 def convert_splitindex(document):
789 " Converts index and printindex insets to splitindex-aware format "
792 i = find_token(document.body, "\\begin_inset Index", i)
795 document.body[i] = document.body[i].replace("\\begin_inset Index",
796 "\\begin_inset Index idx")
800 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
803 if document.body[i + 1].find('LatexCommand printindex') == -1:
804 document.warning("Malformed LyX document: Incomplete printindex inset.")
806 subst = ["LatexCommand printindex",
808 document.body[i + 1:i + 2] = subst
812 def revert_subindex(document):
813 " Reverts \\printsubindex CommandInset types "
814 i = find_token(document.header, '\\use_indices', 0)
816 document.warning("Malformed LyX document: Missing \\use_indices.")
818 indices = get_value(document.header, "\\use_indices", i)
821 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
824 k = find_end_of_inset(document.body, i)
825 ctype = get_value(document.body, 'LatexCommand', i, k)
826 if ctype != "printsubindex":
829 ptype = get_value(document.body, 'type', i, k).strip('"')
830 if indices == "false":
831 del document.body[i:k + 1]
833 subst = [old_put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")]
834 document.body[i:k + 1] = subst
838 def revert_printindexall(document):
839 " Reverts \\print[sub]index* CommandInset types "
840 i = find_token(document.header, '\\use_indices', 0)
842 document.warning("Malformed LyX document: Missing \\use_indices.")
844 indices = get_value(document.header, "\\use_indices", i)
847 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
850 k = find_end_of_inset(document.body, i)
851 ctype = get_value(document.body, 'LatexCommand', i, k)
852 if ctype != "printindex*" and ctype != "printsubindex*":
855 if indices == "false":
856 del document.body[i:k + 1]
858 subst = [old_put_cmd_in_ert("\\" + ctype + "{}")]
859 document.body[i:k + 1] = subst
863 def revert_strikeout(document):
864 " Reverts \\strikeout character style "
866 changed = revert_charstyles(document, "\\uuline", "\\uuline", changed)
867 changed = revert_charstyles(document, "\\uwave", "\\uwave", changed)
868 changed = revert_charstyles(document, "\\strikeout", "\\sout", changed)
870 insert_to_preamble(0, document,
871 '% Commands inserted by lyx2lyx for proper underlining\n'
872 + '\\PassOptionsToPackage{normalem}{ulem}\n'
873 + '\\usepackage{ulem}\n')
876 def revert_ulinelatex(document):
877 " Reverts \\uline character style "
878 i = find_token(document.body, '\\bar under', 0)
881 insert_to_preamble(0, document,
882 '% Commands inserted by lyx2lyx for proper underlining\n'
883 + '\\PassOptionsToPackage{normalem}{ulem}\n'
884 + '\\usepackage{ulem}\n'
885 + '\\let\\cite@rig\\cite\n'
886 + '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}\n'
887 + ' \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}\n'
888 + '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}\n')
891 def revert_custom_processors(document):
892 " Remove bibtex_command and index_command params "
893 i = find_token(document.header, '\\bibtex_command', 0)
895 document.warning("Malformed LyX document: Missing \\bibtex_command.")
897 del document.header[i]
898 i = find_token(document.header, '\\index_command', 0)
900 document.warning("Malformed LyX document: Missing \\index_command.")
902 del document.header[i]
905 def convert_nomencl_width(document):
906 " Add set_width param to nomencl_print "
909 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
912 document.body.insert(i + 2, "set_width \"none\"")
916 def revert_nomencl_width(document):
917 " Remove set_width param from nomencl_print "
920 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
923 j = find_end_of_inset(document.body, i)
924 l = find_token(document.body, "set_width", i, j)
926 document.warning("Can't find set_width option for nomencl_print!")
933 def revert_nomencl_cwidth(document):
934 " Remove width param from nomencl_print "
937 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
940 j = find_end_of_inset(document.body, i)
941 l = find_token(document.body, "width", i, j)
943 #Can't find width option for nomencl_print
946 width = get_value(document.body, "width", i, j).strip('"')
948 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
949 add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
953 def revert_applemac(document):
954 " Revert applemac encoding to auto "
956 if document.encoding == "applemac":
957 document.encoding = "auto"
958 i = find_token(document.header, "\\encoding", 0)
960 document.header[i] = "\\encoding auto"
963 def revert_longtable_align(document):
964 " Remove longtable alignment setting "
968 i = find_token(document.body, "\\begin_inset Tabular", i)
971 # the alignment is 2 lines below \\begin_inset Tabular
972 j = document.body[i + 2].find("longtabularalignment")
975 document.body[i + 2] = document.body[i + 2][:j - 1]
976 document.body[i + 2] = document.body[i + 2] + '>'
980 def revert_branch_filename(document):
981 " Remove \\filename_suffix parameter from branches "
984 i = find_token(document.header, "\\filename_suffix", i)
987 del document.header[i]
990 def revert_paragraph_indentation(document):
991 " Revert custom paragraph indentation to preamble code "
994 i = find_token(document.header, "\\paragraph_indentation", i)
997 # only remove the preamble line if default
998 # otherwise also write the value to the preamble
999 length = get_value(document.header, "\\paragraph_indentation", i)
1000 if length == "default":
1001 del document.header[i]
1004 # handle percent lengths
1005 # latex_length returns "bool,length"
1006 length = latex_length(length).split(",")[1]
1007 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1008 add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
1009 del document.header[i]
1013 def revert_percent_skip_lengths(document):
1014 " Revert relative lengths for paragraph skip separation to preamble code "
1017 i = find_token(document.header, "\\defskip", i)
1020 length = get_value(document.header, "\\defskip", i)
1021 # only revert when a custom length was set and when
1022 # it used a percent length
1023 if length not in ('smallskip', 'medskip', 'bigskip'):
1024 # handle percent lengths
1025 length = latex_length(length)
1026 # latex_length returns "bool,length"
1027 percent = length.split(",")[0]
1028 length = length.split(",")[1]
1029 if percent == "True":
1030 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1031 add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
1032 # set defskip to medskip as default
1033 document.header[i] = "\\defskip medskip"
1037 def revert_percent_vspace_lengths(document):
1038 " Revert relative VSpace lengths to ERT "
1041 i = find_token(document.body, "\\begin_inset VSpace", i)
1044 # only revert if a custom length was set and if
1045 # it used a percent length
1046 line = document.body[i]
1047 r = re.compile(r'\\begin_inset VSpace (.*)$')
1050 if length not in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
1051 # check if the space has a star (protected space)
1052 protected = (document.body[i].rfind("*") != -1)
1054 length = length.rstrip('*')
1055 # handle percent lengths
1056 length = latex_length(length)
1057 # latex_length returns "bool,length"
1058 percent = length.split(",")[0]
1059 length = length.split(",")[1]
1060 # revert the VSpace inset to ERT
1061 if percent == "True":
1063 subst = [old_put_cmd_in_ert("\\vspace*{" + length + "}")]
1065 subst = [old_put_cmd_in_ert("\\vspace{" + length + "}")]
1066 document.body[i:i + 2] = subst
1070 def revert_percent_hspace_lengths(document):
1071 " Revert relative HSpace lengths to ERT "
1074 i = find_token(document.body, "\\begin_inset space \\hspace", i)
1077 protected = (document.body[i].find("\\hspace*{}") != -1)
1078 # only revert if a custom length was set and if
1079 # it used a percent length
1080 length = get_value(document.body, '\\length', i + 1)
1082 document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1084 # handle percent lengths
1085 length = latex_length(length)
1086 # latex_length returns "bool,length"
1087 percent = length.split(",")[0]
1088 length = length.split(",")[1]
1089 # revert the HSpace inset to ERT
1090 if percent == "True":
1092 subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
1094 subst = [old_put_cmd_in_ert("\\hspace{" + length + "}")]
1095 document.body[i:i + 3] = subst
1099 def revert_hspace_glue_lengths(document):
1100 " Revert HSpace glue lengths to ERT "
1103 i = find_token(document.body, "\\begin_inset space \\hspace", i)
1106 protected = (document.body[i].find("\\hspace*{}") != -1)
1107 length = get_value(document.body, '\\length', i + 1)
1109 document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1111 # only revert if the length contains a plus or minus at pos != 0
1112 glue = re.compile(r'.+[\+-]')
1113 if glue.search(length):
1114 # handle percent lengths
1115 # latex_length returns "bool,length"
1116 length = latex_length(length).split(",")[1]
1117 # revert the HSpace inset to ERT
1119 subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
1121 subst = [old_put_cmd_in_ert("\\hspace{" + length + "}")]
1122 document.body[i:i + 3] = subst
1125 def convert_author_id(document):
1126 " Add the author_id to the \\author definition and make sure 0 is not used"
1130 i = find_token(document.header, "\\author", i)
1134 r = re.compile(r'(\\author) (\".*\")\s?(.*)$')
1135 m = r.match(document.header[i])
1140 if m.lastindex == 3:
1142 document.header[i] = "\\author %i %s %s" % (j, name, email)
1148 k = find_token(document.body, "\\change_", k)
1152 change = document.body[k].split(' ');
1153 if len(change) == 3:
1155 author_id = int(change[1])
1157 document.body[k] = "%s %i %s" % (type, author_id + 1, time)
1160 def revert_author_id(document):
1161 " Remove the author_id from the \\author definition "
1166 i = find_token(document.header, "\\author", i)
1170 r = re.compile(r'(\\author) (\d+) (\".*\")\s?(.*)$')
1171 m = r.match(document.header[i])
1173 author_id = int(m.group(2))
1174 idmap[author_id] = j
1178 if m.lastindex == 4:
1180 document.header[i] = "\\author %s %s" % (name, email)
1186 k = find_token(document.body, "\\change_", k)
1190 change = document.body[k].split(' ');
1191 if len(change) == 3:
1193 author_id = int(change[1])
1195 document.body[k] = "%s %i %s" % (type, idmap[author_id], time)
1199 def revert_suppress_date(document):
1200 " Revert suppressing of default document date to preamble code "
1203 i = find_token(document.header, "\\suppress_date", i)
1206 # remove the preamble line and write to the preamble
1207 # when suppress_date was true
1208 date = get_value(document.header, "\\suppress_date", i)
1210 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1211 add_to_preamble(document, ["\\date{}"])
1212 del document.header[i]
1216 def revert_mhchem(document):
1217 "Revert mhchem loading to preamble code"
1222 i = find_token(document.header, "\\use_mhchem 1", 0)
1226 i = find_token(document.header, "\\use_mhchem 2", 0)
1229 if mhchem == "auto":
1230 j = find_token(document.body, "\\cf{", 0)
1234 j = find_token(document.body, "\\ce{", 0)
1238 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1239 add_to_preamble(document, ["\\PassOptionsToPackage{version=3}{mhchem}"])
1240 add_to_preamble(document, ["\\usepackage{mhchem}"])
1241 k = find_token(document.header, "\\use_mhchem", 0)
1243 document.warning("Malformed LyX document: Could not find mhchem setting.")
1245 del document.header[k]
1248 def revert_fontenc(document):
1249 " Remove fontencoding param "
1250 i = find_token(document.header, '\\fontencoding', 0)
1252 document.warning("Malformed LyX document: Missing \\fontencoding.")
1254 del document.header[i]
1257 def merge_gbrief(document):
1258 " Merge g-brief-en and g-brief-de to one class "
1260 if document.textclass != "g-brief-de":
1261 if document.textclass == "g-brief-en":
1262 document.textclass = "g-brief"
1263 document.set_textclass()
1266 obsoletedby = { "Brieftext": "Letter",
1267 "Unterschrift": "Signature",
1268 "Strasse": "Street",
1269 "Zusatz": "Addition",
1272 "RetourAdresse": "ReturnAddress",
1273 "MeinZeichen": "MyRef",
1274 "IhrZeichen": "YourRef",
1275 "IhrSchreiben": "YourMail",
1278 "Konto": "BankAccount",
1279 "Postvermerk": "PostalComment",
1280 "Adresse": "Address",
1282 "Betreff": "Reference",
1283 "Anrede": "Opening",
1289 i = find_token(document.body, "\\begin_layout", i)
1293 layout = document.body[i][14:]
1294 if layout in obsoletedby:
1295 document.body[i] = "\\begin_layout " + obsoletedby[layout]
1299 document.textclass = "g-brief"
1300 document.set_textclass()
1303 def revert_gbrief(document):
1304 " Revert g-brief to g-brief-en "
1305 if document.textclass == "g-brief":
1306 document.textclass = "g-brief-en"
1307 document.set_textclass()
1310 def revert_html_options(document):
1311 " Remove html options "
1312 i = find_token(document.header, '\\html_use_mathml', 0)
1314 del document.header[i]
1315 i = find_token(document.header, '\\html_be_strict', 0)
1317 del document.header[i]
1320 def revert_includeonly(document):
1323 i = find_token(document.header, "\\begin_includeonly", i)
1326 j = find_end_of(document.header, i, "\\begin_includeonly", "\\end_includeonly")
1328 # this should not happen
1330 document.header[i : j + 1] = []
1333 def revert_includeall(document):
1334 " Remove maintain_unincluded_children param "
1335 i = find_token(document.header, '\\maintain_unincluded_children', 0)
1337 del document.header[i]
1340 def revert_multirow(document):
1341 " Revert multirow cells in tables "
1345 # cell type 3 is multirow begin cell
1346 i = find_token(document.body, '<cell multirow="3"', i)
1349 # a multirow cell was found
1351 # remove the multirow tag, set the valignment to top
1352 # and remove the bottom line
1353 document.body[i] = document.body[i].replace(' multirow="3" ', ' ')
1354 document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
1355 document.body[i] = document.body[i].replace(' bottomline="true" ', ' ')
1356 # write ERT to create the multirow cell
1357 # use 2 rows and 2cm as default with because the multirow span
1358 # and the column width is only hardly accessible
1359 subst = [old_put_cmd_in_ert("\\multirow{2}{2cm}{")]
1360 document.body[i + 4:i + 4] = subst
1361 i = find_token(document.body, "</cell>", i)
1363 document.warning("Malformed LyX document: Could not find end of tabular cell.")
1365 subst = [old_put_cmd_in_ert("}")]
1366 document.body[i - 3:i - 3] = subst
1367 # cell type 4 is multirow part cell
1368 i = find_token(document.body, '<cell multirow="4"', i)
1371 # remove the multirow tag, set the valignment to top
1372 # and remove the top line
1373 document.body[i] = document.body[i].replace(' multirow="4" ', ' ')
1374 document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
1375 document.body[i] = document.body[i].replace(' topline="true" ', ' ')
1377 if multirow == True:
1378 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1379 add_to_preamble(document, ["\\usepackage{multirow}"])
1382 def convert_math_output(document):
1383 " Convert \html_use_mathml to \html_math_output "
1384 i = find_token(document.header, "\\html_use_mathml", 0)
1387 rgx = re.compile(r'\\html_use_mathml\s+(\w+)')
1388 m = rgx.match(document.header[i])
1390 newval = "0" # MathML
1393 newval = "2" # Images
1394 document.header[i] = "\\html_math_output " + newval
1397 def revert_math_output(document):
1398 " Revert \html_math_output to \html_use_mathml "
1399 i = find_token(document.header, "\\html_math_output", 0)
1402 rgx = re.compile(r'\\html_math_output\s+(\d)')
1403 m = rgx.match(document.header[i])
1407 if val == "1" or val == "2":
1410 document.warning("Unable to match " + document.header[i])
1411 document.header[i] = "\\html_use_mathml " + newval
1415 def revert_inset_preview(document):
1416 " Dissolves the preview inset "
1421 i = find_token(document.body, "\\begin_inset Preview", i)
1424 j = find_end_of_inset(document.body, i)
1426 document.warning("Malformed LyX document: Could not find end of Preview inset.")
1428 #If the layout is Standard we need to remove it, otherwise there
1429 #will be paragraph breaks that shouldn't be there.
1430 k = find_token(document.body, "\\begin_layout Standard", i)
1432 del document.body[i:i + 3]
1433 del document.body[j - 5:j - 2]
1436 del document.body[i]
1437 del document.body[j - 1]
1441 def revert_equalspacing_xymatrix(document):
1442 " Revert a Formula with xymatrix@! to an ERT inset "
1445 has_preamble = False
1446 has_equal_spacing = False
1449 i = find_token(document.body, "\\begin_inset Formula", i)
1452 j = find_end_of_inset(document.body, i)
1454 document.warning("Malformed LyX document: Could not find end of Formula inset.")
1457 for curline in range(i,j):
1458 found = document.body[curline].find("\\xymatrix@!")
1463 has_equal_spacing = True
1464 content = [document.body[i][21:]]
1465 content += document.body[i + 1:j]
1466 subst = put_cmd_in_ert(content)
1467 document.body[i:j + 1] = subst
1470 for curline in range(i,j):
1471 l = document.body[curline].find("\\xymatrix")
1473 has_preamble = True;
1476 if has_equal_spacing and not has_preamble:
1477 add_to_preamble(document, ['\\usepackage[all]{xy}'])
1480 def revert_notefontcolor(document):
1481 " Reverts greyed-out note font color to preamble code "
1485 i = find_token(document.header, "\\notefontcolor", i)
1488 colorcode = get_value(document.header, '\\notefontcolor', 0)
1489 del document.header[i]
1490 # the color code is in the form #rrggbb where every character denotes a hex number
1491 # convert the string to an int
1492 red = string.atoi(colorcode[1:3],16)
1493 # we want the output "0.5" for the value "127" therefore increment here
1496 redout = float(red) / 256
1497 green = string.atoi(colorcode[3:5],16)
1500 greenout = float(green) / 256
1501 blue = string.atoi(colorcode[5:7],16)
1504 blueout = float(blue) / 256
1505 # write the preamble
1506 insert_to_preamble(0, document,
1507 '% Commands inserted by lyx2lyx to set the font color\n'
1508 '% for greyed-out notes\n'
1509 + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1510 + '\\definecolor{note_fontcolor}{rgb}{'
1511 + str(redout) + ', ' + str(greenout)
1512 + ', ' + str(blueout) + '}\n'
1513 + '\\renewenvironment{lyxgreyedout}\n'
1514 + ' {\\textcolor{note_fontcolor}\\bgroup}{\\egroup}\n')
1517 def revert_turkmen(document):
1518 "Set language Turkmen to English"
1520 if document.language == "turkmen":
1521 document.language = "english"
1522 i = find_token(document.header, "\\language", 0)
1524 document.header[i] = "\\language english"
1527 j = find_token(document.body, "\\lang turkmen", j)
1530 document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english")
1534 def revert_fontcolor(document):
1535 " Reverts font color to preamble code "
1539 i = find_token(document.header, "\\fontcolor", i)
1542 colorcode = get_value(document.header, '\\fontcolor', 0)
1543 del document.header[i]
1544 # don't clutter the preamble if backgroundcolor is not set
1545 if colorcode == "#000000":
1547 # the color code is in the form #rrggbb where every character denotes a hex number
1548 # convert the string to an int
1549 red = string.atoi(colorcode[1:3],16)
1550 # we want the output "0.5" for the value "127" therefore add here
1553 redout = float(red) / 256
1554 green = string.atoi(colorcode[3:5],16)
1557 greenout = float(green) / 256
1558 blue = string.atoi(colorcode[5:7],16)
1561 blueout = float(blue) / 256
1562 # write the preamble
1563 insert_to_preamble(0, document,
1564 '% Commands inserted by lyx2lyx to set the font color\n'
1565 + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1566 + '\\definecolor{document_fontcolor}{rgb}{'
1567 + str(redout) + ', ' + str(greenout)
1568 + ', ' + str(blueout) + '}\n'
1569 + '\\color{document_fontcolor}\n')
1571 def revert_shadedboxcolor(document):
1572 " Reverts shaded box color to preamble code "
1576 i = find_token(document.header, "\\boxbgcolor", i)
1579 colorcode = get_value(document.header, '\\boxbgcolor', 0)
1580 del document.header[i]
1581 # the color code is in the form #rrggbb where every character denotes a hex number
1582 # convert the string to an int
1583 red = string.atoi(colorcode[1:3],16)
1584 # we want the output "0.5" for the value "127" therefore increment here
1587 redout = float(red) / 256
1588 green = string.atoi(colorcode[3:5],16)
1591 greenout = float(green) / 256
1592 blue = string.atoi(colorcode[5:7],16)
1595 blueout = float(blue) / 256
1596 # write the preamble
1597 insert_to_preamble(0, document,
1598 '% Commands inserted by lyx2lyx to set the color\n'
1599 '% of boxes with shaded background\n'
1600 + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1601 + '\\definecolor{shadecolor}{rgb}{'
1602 + str(redout) + ', ' + str(greenout)
1603 + ', ' + str(blueout) + '}\n')
1606 def revert_lyx_version(document):
1607 " Reverts LyX Version information from Inset Info "
1608 version = "LyX version"
1610 import lyx2lyx_version
1611 version = lyx2lyx_version.version
1617 i = find_token(document.body, '\\begin_inset Info', i)
1620 j = find_end_of_inset(document.body, i + 1)
1623 document.warning("Malformed LyX document: Could not find end of Info inset.")
1629 # but we shall try to be forgiving.
1631 for k in range(i, j):
1632 if document.body[k].startswith("arg"):
1633 arg = document.body[k][3:].strip().strip('"')
1634 if document.body[k].startswith("type"):
1635 typ = document.body[k][4:].strip().strip('"')
1636 if arg != "version" or typ != "lyxinfo":
1640 # We do not actually know the version of LyX used to produce the document.
1641 # But we can use our version, since we are reverting.
1643 # Now we want to check if the line after "\end_inset" is empty. It normally
1644 # is, so we want to remove it, too.
1646 if document.body[j + 1].strip() == "":
1648 document.body[i: lastline] = s
1652 def revert_math_scale(document):
1653 " Remove math scaling and LaTeX options "
1654 i = find_token(document.header, '\\html_math_img_scale', 0)
1656 del document.header[i]
1657 i = find_token(document.header, '\\html_latex_start', 0)
1659 del document.header[i]
1660 i = find_token(document.header, '\\html_latex_end', 0)
1662 del document.header[i]
1665 def revert_pagesizes(document):
1667 " Revert page sizes to default "
1668 i = find_token(document.header, '\\papersize', 0)
1670 size = document.header[i][11:]
1671 if size == "a0paper" or size == "a1paper" or size == "a2paper" \
1672 or size == "a6paper" or size == "b0paper" or size == "b1paper" \
1673 or size == "b2paper" or size == "b6paper" or size == "b0j" \
1674 or size == "b1j" or size == "b2j" or size == "b3j" or size == "b4j" \
1675 or size == "b5j" or size == "b6j":
1676 del document.header[i]
1679 def revert_DIN_C_pagesizes(document):
1681 " Revert DIN C page sizes to default "
1682 i = find_token(document.header, '\\papersize', 0)
1684 size = document.header[i][11:]
1685 if size == "c0paper" or size == "c1paper" or size == "c2paper" \
1686 or size == "c3paper" or size == "c4paper" or size == "c5paper" \
1687 or size == "c6paper":
1688 del document.header[i]
1691 def convert_html_quotes(document):
1692 " Remove quotes around html_latex_start and html_latex_end "
1694 i = find_token(document.header, '\\html_latex_start', 0)
1696 line = document.header[i]
1697 l = re.compile(r'\\html_latex_start\s+"(.*)"')
1700 document.header[i] = "\\html_latex_start " + m.group(1)
1702 i = find_token(document.header, '\\html_latex_end', 0)
1704 line = document.header[i]
1705 l = re.compile(r'\\html_latex_end\s+"(.*)"')
1708 document.header[i] = "\\html_latex_end " + m.group(1)
1711 def revert_html_quotes(document):
1712 " Remove quotes around html_latex_start and html_latex_end "
1714 i = find_token(document.header, '\\html_latex_start', 0)
1716 line = document.header[i]
1717 l = re.compile(r'\\html_latex_start\s+(.*)')
1719 document.header[i] = "\\html_latex_start \"" + m.group(1) + "\""
1721 i = find_token(document.header, '\\html_latex_end', 0)
1723 line = document.header[i]
1724 l = re.compile(r'\\html_latex_end\s+(.*)')
1726 document.header[i] = "\\html_latex_end \"" + m.group(1) + "\""
1729 def revert_output_sync(document):
1730 " Remove forward search options "
1731 i = find_token(document.header, '\\output_sync_macro', 0)
1733 del document.header[i]
1734 i = find_token(document.header, '\\output_sync', 0)
1736 del document.header[i]
1739 def convert_beamer_args(document):
1740 " Convert ERT arguments in Beamer to InsetArguments "
1742 if document.textclass != "beamer" and document.textclass != "article-beamer":
1745 layouts = ("Block", "ExampleBlock", "AlertBlock")
1746 for layout in layouts:
1749 blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1752 elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1754 document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1757 bert = find_token(document.body, '\\begin_inset ERT', blay)
1759 document.warning("Malformed Beamer LyX document: Can't find argument of " + layout + " layout.")
1762 eert = find_end_of_inset(document.body, bert)
1764 document.warning("Malformed LyX document: Can't find end of ERT.")
1768 # So the ERT inset begins at line k and goes to line l. We now wrap it in
1769 # an argument inset.
1770 # Do the end first, so as not to mess up the variables.
1771 document.body[eert + 1:eert + 1] = ['', '\\end_layout', '', '\\end_inset', '']
1772 document.body[bert:bert] = ['\\begin_inset OptArg', 'status open', '',
1773 '\\begin_layout Plain Layout']
1777 def revert_beamer_args(document):
1778 " Revert Beamer arguments to ERT "
1780 if document.textclass != "beamer" and document.textclass != "article-beamer":
1783 layouts = ("Block", "ExampleBlock", "AlertBlock")
1784 for layout in layouts:
1787 blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1790 elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1792 document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1795 bopt = find_token(document.body, '\\begin_inset OptArg', blay)
1797 # it is legal not to have one of these
1800 eopt = find_end_of_inset(document.body, bopt)
1802 document.warning("Malformed LyX document: Can't find end of argument.")
1805 bplay = find_token(document.body, '\\begin_layout Plain Layout', blay)
1807 document.warning("Malformed LyX document: Can't find plain layout.")
1810 eplay = find_end_of(document.body, bplay, '\\begin_layout', '\\end_layout')
1812 document.warning("Malformed LyX document: Can't find end of plain layout.")
1815 # So the content of the argument inset goes from bplay + 1 to eplay - 1
1819 document.warning(str(bcont) + " " + str(eplay))
1822 # we convert the content of the argument into pure LaTeX...
1823 content = lyx2latex(document, document.body[bcont:eplay])
1824 strlist = put_cmd_in_ert(["{" + content + "}"])
1826 # now replace the optional argument with the ERT
1827 document.body[bopt:eopt + 1] = strlist
1831 def revert_align_decimal(document):
1834 l = document.body[l].find('alignment=decimal')
1837 remove_option(document, l, 'decimal_point')
1838 document.body[l].replace('decimal', 'center')
1841 def convert_optarg(document):
1842 " Convert \\begin_inset OptArg to \\begin_inset Argument "
1845 i = find_token(document.body, '\\begin_inset OptArg', i)
1848 document.body[i] = "\\begin_inset Argument"
1852 def revert_argument(document):
1853 " Convert \\begin_inset Argument to \\begin_inset OptArg "
1856 i = find_token(document.body, '\\begin_inset Argument', i)
1859 document.body[i] = "\\begin_inset OptArg"
1863 def revert_makebox(document):
1864 " Convert \\makebox to TeX code "
1867 # only revert frameless boxes without an inner box
1868 i = find_token(document.body, '\\begin_inset Box Frameless', i)
1870 # remove the option use_makebox
1871 revert_use_makebox(document)
1873 z = find_end_of_inset(document.body, i)
1875 document.warning("Malformed LyX document: Can't find end of box inset.")
1877 j = find_token(document.body, 'use_makebox 1', i)
1878 # assure we found the makebox of the current box
1879 if j < z and j != -1:
1880 y = find_token(document.body, "\\begin_layout", i)
1881 if y > z or y == -1:
1882 document.warning("Malformed LyX document: Can't find layout in box.")
1884 # remove the \end_layout \end_inset pair
1885 document.body[z - 2:z + 1] = put_cmd_in_ert("}")
1886 # determine the alignment
1887 k = find_token(document.body, 'hor_pos', j - 4)
1888 align = document.body[k][9]
1889 # determine the width
1890 l = find_token(document.body, 'width "', j + 1)
1891 length = document.body[l][7:]
1892 # remove trailing '"'
1893 length = length[:-1]
1894 # latex_length returns "bool,length"
1895 length = latex_length(length).split(",")[1]
1896 subst = "\\makebox[" + length + "][" \
1898 document.body[i:y + 1] = put_cmd_in_ert(subst)
1902 def revert_use_makebox(document):
1903 " Deletes use_makebox option of boxes "
1906 # remove the option use_makebox
1907 h = find_token(document.body, 'use_makebox', 0)
1910 del document.body[h]
1914 def convert_use_makebox(document):
1915 " Adds use_makebox option for boxes "
1918 # remove the option use_makebox
1919 i = find_token(document.body, '\\begin_inset Box', i)
1922 k = find_token(document.body, 'use_parbox', i)
1924 document.warning("Malformed LyX document: Can't find use_parbox statement in box.")
1926 document.body.insert(k + 1, "use_makebox 0")
1930 def revert_IEEEtran(document):
1931 " Convert IEEEtran layouts and styles to TeX code "
1932 if document.textclass != "IEEEtran":
1934 revert_flex_inset(document, "IEEE membership", "\\IEEEmembership", 0)
1935 revert_flex_inset(document, "Lowercase", "\\MakeLowercase", 0)
1936 layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
1937 "Page headings", "Biography without photo")
1938 latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
1939 "After Title Text": "\\IEEEaftertitletext",
1940 "Publication ID": "\\IEEEpubid"}
1941 obsoletedby = {"Page headings": "MarkBoth",
1942 "Biography without photo": "BiographyNoPhoto"}
1943 for layout in layouts:
1946 i = find_token(document.body, '\\begin_layout ' + layout, i)
1949 j = find_end_of(document.body, i, '\\begin_layout', '\\end_layout')
1951 document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1954 if layout in obsoletedby:
1955 document.body[i] = "\\begin_layout " + obsoletedby[layout]
1958 content = lyx2latex(document, document.body[i:j + 1])
1959 add_to_preamble(document, [latexcmd[layout] + "{" + content + "}"])
1960 del document.body[i:j + 1]
1963 def revert_nameref(document):
1964 " Convert namerefs to regular references "
1965 cmds = ["Nameref", "nameref"]
1967 rx = re.compile(r'reference "(.*)"')
1970 oldcmd = "LatexCommand " + cmd
1972 # It seems better to look for this, as most of the reference
1973 # insets won't be ones we care about.
1974 i = find_token(document.body, oldcmd, i)
1979 # Make sure it is actually in an inset!
1980 # We could just check document.lines[i-1], but that relies
1981 # upon something that might easily change.
1982 # We'll look back a few lines.
1986 stins = find_token(document.body, "\\begin_inset CommandInset ref", stins)
1987 if stins == -1 or stins > cmdloc:
1989 endins = find_end_of_inset(document.body, stins)
1991 document.warning("Can't find end of inset at line " + stins + "!!")
1995 refline = find_token(document.body, "reference", stins)
1996 if refline == -1 or refline > endins:
1997 document.warning("Can't find reference for inset at line " + stinst + "!!")
1999 m = rx.match(document.body[refline])
2001 document.warning("Can't match reference line: " + document.body[ref])
2005 newcontent = ['\\begin_inset ERT', 'status collapsed', '', \
2006 '\\begin_layout Plain Layout', '', '\\backslash', \
2007 cmd + '{' + ref + '}', '\\end_layout', '', '\\end_inset']
2008 document.body[stins:endins + 1] = newcontent
2010 add_to_preamble(document, "\usepackage{nameref}")
2013 def remove_Nameref(document):
2014 " Convert Nameref commands to nameref commands "
2017 # It seems better to look for this, as most of the reference
2018 # insets won't be ones we care about.
2019 i = find_token(document.body, "LatexCommand Nameref" , i)
2025 # Make sure it is actually in an inset!
2026 # We could just check document.lines[i-1], but that relies
2027 # upon something that might easily change.
2028 # We'll look back a few lines.
2032 stins = find_token(document.body, "\\begin_inset CommandInset ref", stins)
2033 if stins == -1 or stins > cmdloc:
2035 endins = find_end_of_inset(document.body, stins)
2037 document.warning("Can't find end of inset at line " + stins + "!!")
2041 document.body[cmdloc] = "LatexCommand nameref"
2044 def revert_mathrsfs(document):
2045 " Load mathrsfs if \mathrsfs us use in the document "
2047 end = len(document.body) - 1
2049 j = document.body[i].find("\\mathscr{")
2051 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
2052 add_to_preamble(document, ["\\usepackage{mathrsfs}"])
2059 def convert_mathdots(document):
2060 " Load mathdots automatically "
2062 i = find_token(document.header, "\\use_esint" , 0)
2064 document.header.insert(i + 1, "\\use_mathdots 1")
2068 def revert_mathdots(document):
2069 " Load mathdots if used in the document "
2071 i = find_token(document.header, "\\use_mathdots" , 0)
2073 # use \@ifundefined to catch also the "auto" case
2074 add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
2075 add_to_preamble(document, ["\\@ifundefined{iddots}{\\usepackage{mathdots}}\n"])
2076 del document.header[i]
2080 def convert_rule(document):
2081 " Convert \\lyxline to CommandInset line "
2084 i = find_token(document.body, "\\lyxline" , i)
2086 j = find_token(document.body, "\\color" , i - 2)
2088 color = document.body[j] + '\n'
2091 k = find_token(document.body, "\\begin_layout Standard" , i - 4)
2092 # we need to handle the case that \lyxline is in a separate paragraph and that it is colored
2093 # the result is then an extra empty paragraph which we get by adding an empty ERT inset
2094 if k == i - 4 and j == i - 2 and document.body[i - 1] == '':
2095 layout = '\\begin_inset ERT\nstatus collapsed\n\n\\begin_layout Plain Layout\n\n\n\\end_layout\n\n\\end_inset\n' \
2096 + '\\end_layout\n\n' \
2097 + '\\begin_layout Standard\n'
2098 elif k == i - 2 and document.body[i - 1] == '':
2101 layout = '\\end_layout\n\n' \
2102 + '\\begin_layout Standard\n'
2103 l = find_token(document.body, "\\begin_layout Standard" , i + 4)
2104 if l == i + 4 and document.body[i + 1] == '':
2107 layout2 = '\\end_layout\n' \
2108 + '\n\\begin_layout Standard\n'
2110 + '\\noindent\n\n' \
2112 + '\\begin_inset CommandInset line\n' \
2113 + 'LatexCommand rule\n' \
2114 + 'offset "0.5ex"\n' \
2115 + 'width "100line%"\n' \
2116 + 'height "1pt"\n' \
2117 + '\n\\end_inset\n\n\n' \
2119 document.body[i] = subst
2125 def revert_rule(document):
2126 " Revert line insets to Tex code "
2129 i = find_token(document.body, "\\begin_inset CommandInset line" , i)
2132 j = find_token(document.body, "\\end_inset" , i)
2133 # assure we found the end_inset of the current inset
2134 if j > i + 6 or j == -1:
2135 document.warning("Malformed LyX document: Can't find end of line inset.")
2137 # determine the optional offset
2138 k = find_token(document.body, 'offset', i)
2139 if k != -1 and k < j:
2140 offset = document.body[k][8:]
2143 # determine the width
2144 l = find_token(document.body, 'width', k + 1)
2145 width = document.body[l][7:]
2146 # determine the height
2147 m = find_token(document.body, 'height', l + 1)
2148 height = document.body[m][8:]
2149 # remove trailing '"'
2150 offset = offset[:-1]
2152 height = height[:-1]
2153 # output the \rule command
2155 subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
2157 subst = "\\rule{" + width + "}{" + height + "}"
2158 document.body[i:j + 1] = put_cmd_in_ert(subst)
2163 def revert_diagram(document):
2165 re_diagram = re.compile(r'\\begin_inset Formula .*\\Diagram', re.DOTALL)
2167 i = find_token(document.body, '\\begin_inset Formula', i)
2170 j = find_end_of_inset(document.body, i)
2172 document.warning("Malformed LyX document: Can't find end of line inset.")
2174 m = re_diagram.search("\n".join(document.body[i:j]))
2178 add_to_preamble(document, "\\use_package{feyn}")
2179 # only need to do it once!
2188 supported_versions = ["2.0.0","2.0"]
2189 convert = [[346, []],
2195 [352, [convert_splitindex]],
2202 [359, [convert_nomencl_width]],
2212 [369, [convert_author_id]],
2216 [373, [merge_gbrief]],
2222 [379, [convert_math_output]],
2232 [389, [convert_html_quotes]],
2235 [392, [convert_beamer_args]],
2236 [393, [convert_optarg]],
2237 [394, [convert_use_makebox]],
2240 [397, [remove_Nameref]],
2242 [399, [convert_mathdots]],
2243 [400, [convert_rule]],
2247 revert = [[400, [revert_diagram]],
2248 [399, [revert_rule]],
2249 [398, [revert_mathdots]],
2250 [397, [revert_mathrsfs]],
2252 [395, [revert_nameref]],
2253 [394, [revert_DIN_C_pagesizes]],
2254 [393, [revert_makebox]],
2255 [392, [revert_argument]],
2256 [391, [revert_beamer_args]],
2257 [390, [revert_align_decimal, revert_IEEEtran]],
2258 [389, [revert_output_sync]],
2259 [388, [revert_html_quotes]],
2260 [387, [revert_pagesizes]],
2261 [386, [revert_math_scale]],
2262 [385, [revert_lyx_version]],
2263 [384, [revert_shadedboxcolor]],
2264 [383, [revert_fontcolor]],
2265 [382, [revert_turkmen]],
2266 [381, [revert_notefontcolor]],
2267 [380, [revert_equalspacing_xymatrix]],
2268 [379, [revert_inset_preview]],
2269 [378, [revert_math_output]],
2271 [376, [revert_multirow]],
2272 [375, [revert_includeall]],
2273 [374, [revert_includeonly]],
2274 [373, [revert_html_options]],
2275 [372, [revert_gbrief]],
2276 [371, [revert_fontenc]],
2277 [370, [revert_mhchem]],
2278 [369, [revert_suppress_date]],
2279 [368, [revert_author_id]],
2280 [367, [revert_hspace_glue_lengths]],
2281 [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
2282 [365, [revert_percent_skip_lengths]],
2283 [364, [revert_paragraph_indentation]],
2284 [363, [revert_branch_filename]],
2285 [362, [revert_longtable_align]],
2286 [361, [revert_applemac]],
2288 [359, [revert_nomencl_cwidth]],
2289 [358, [revert_nomencl_width]],
2290 [357, [revert_custom_processors]],
2291 [356, [revert_ulinelatex]],
2293 [354, [revert_strikeout]],
2294 [353, [revert_printindexall]],
2295 [352, [revert_subindex]],
2296 [351, [revert_splitindex]],
2297 [350, [revert_backgroundcolor]],
2298 [349, [revert_outputformat]],
2299 [348, [revert_xetex]],
2300 [347, [revert_phantom, revert_hphantom, revert_vphantom]],
2301 [346, [revert_tabularvalign]],
2302 [345, [revert_swiss]]
2306 if __name__ == "__main__":