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.0"""
26 from parser_tools import find_token, find_end_of, find_tokens, \
27 find_token_exact, find_end_of_inset, find_end_of_layout, \
28 find_token_backwards, is_in_inset, get_value, get_quoted_value, \
29 del_token, check_token, get_option_value
31 from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
32 put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
33 revert_font_attrs, hex2ratio, str2bool
35 ####################################################################
36 # Private helper functions
38 def remove_option(lines, m, option):
39 ''' removes option from line m. returns whether we did anything '''
40 l = lines[m].find(option)
43 val = lines[m][l:].split('"')[1]
44 lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
48 # DO NOT USE THIS ROUTINE ANY MORE. Better yet, replace the uses that
49 # have been made of it with uses of put_cmd_in_ert.
50 def old_put_cmd_in_ert(string):
51 for rep in unicode_reps:
52 string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
53 string = string.replace('\\', "\\backslash\n")
54 string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Plain Layout\n" \
55 + string + "\n\\end_layout\n\\end_inset"
59 ###############################################################################
61 ### Conversion and reversion routines
63 ###############################################################################
65 def revert_swiss(document):
66 " Set language german-ch to ngerman "
68 if document.language == "german-ch":
69 document.language = "ngerman"
70 i = find_token(document.header, "\\language", 0)
72 document.header[i] = "\\language ngerman"
75 j = find_token(document.body, "\\lang german-ch", j)
78 document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
82 def revert_tabularvalign(document):
83 " Revert the tabular valign option "
86 i = find_token(document.body, "\\begin_inset Tabular", i)
89 end = find_end_of_inset(document.body, i)
91 document.warning("Can't find end of inset at line " + str(i))
94 fline = find_token(document.body, "<features", i, end)
96 document.warning("Can't find features for inset at line " + str(i))
99 p = document.body[fline].find("islongtable")
101 q = document.body[fline].find("tabularvalignment")
104 # This seems wrong: It removes everything after
105 # tabularvalignment, too.
106 document.body[fline] = document.body[fline][:q - 1] + '>'
111 tabularvalignment = 'c'
112 # which valignment is specified?
113 m = document.body[fline].find('tabularvalignment="top"')
115 tabularvalignment = 't'
116 m = document.body[fline].find('tabularvalignment="bottom"')
118 tabularvalignment = 'b'
119 # delete tabularvalignment
120 q = document.body[fline].find("tabularvalignment")
123 # This seems wrong: It removes everything after
124 # tabularvalignment, too.
125 document.body[fline] = document.body[fline][:q - 1] + '>'
127 # don't add a box when centered
128 if tabularvalignment == 'c':
131 subst = ['\\end_layout', '\\end_inset']
132 document.body[end:end] = subst # just inserts those lines
133 subst = ['\\begin_inset Box Frameless',
134 'position "' + tabularvalignment +'"',
139 # we don't know the width, assume 50%
143 'height_special "totalheight"',
146 '\\begin_layout Plain Layout']
147 document.body[i:i] = subst # this just inserts the array at i
148 # since there could be a tabular inside a tabular, we cannot
153 def revert_phantom_types(document, ptype, cmd):
154 " Reverts phantom to ERT "
157 i = find_token(document.body, "\\begin_inset Phantom " + ptype, i)
160 end = find_end_of_inset(document.body, i)
162 document.warning("Can't find end of inset at line " + str(i))
165 blay = find_token(document.body, "\\begin_layout Plain Layout", i, end)
167 document.warning("Can't find layout for inset at line " + str(i))
170 bend = find_end_of_layout(document.body, blay)
172 document.warning("Malformed LyX document: Could not find end of Phantom inset's layout.")
175 substi = ["\\begin_inset ERT", "status collapsed", "",
176 "\\begin_layout Plain Layout", "", "", "\\backslash",
177 cmd + "{", "\\end_layout", "", "\\end_inset"]
178 substj = ["\\size default", "", "\\begin_inset ERT", "status collapsed", "",
179 "\\begin_layout Plain Layout", "", "}", "\\end_layout", "", "\\end_inset"]
180 # do the later one first so as not to mess up the numbering
181 document.body[bend:end + 1] = substj
182 document.body[i:blay + 1] = substi
183 i = end + len(substi) + len(substj) - (end - bend) - (blay - i) - 2
186 def revert_phantom(document):
187 revert_phantom_types(document, "Phantom", "phantom")
189 def revert_hphantom(document):
190 revert_phantom_types(document, "HPhantom", "hphantom")
192 def revert_vphantom(document):
193 revert_phantom_types(document, "VPhantom", "vphantom")
196 def revert_xetex(document):
197 " Reverts documents that use XeTeX "
199 i = find_token(document.header, '\\use_xetex', 0)
201 document.warning("Malformed LyX document: Missing \\use_xetex.")
203 if not str2bool(get_value(document.header, "\\use_xetex", i)):
204 del document.header[i]
206 del document.header[i]
208 # 1.) set doc encoding to utf8-plain
209 i = find_token(document.header, "\\inputencoding", 0)
211 document.warning("Malformed LyX document: Missing \\inputencoding.")
213 document.header[i] = "\\inputencoding utf8-plain"
215 # 2.) check font settings
217 roman = sans = typew = default
219 sf_scale = tt_scale = 100.0
221 i = find_token(document.header, "\\font_roman", 0)
223 document.warning("Malformed LyX document: Missing \\font_roman.")
225 roman = get_value(document.header, "\\font_roman", i)
226 document.header[i] = "\\font_roman default"
228 i = find_token(document.header, "\\font_sans", 0)
230 document.warning("Malformed LyX document: Missing \\font_sans.")
232 sans = get_value(document.header, "\\font_sans", i)
233 document.header[i] = "\\font_sans default"
235 i = find_token(document.header, "\\font_typewriter", 0)
237 document.warning("Malformed LyX document: Missing \\font_typewriter.")
239 typew = get_value(document.header, "\\font_typewriter", i)
240 document.header[i] = "\\font_typewriter default"
242 i = find_token(document.header, "\\font_osf", 0)
244 document.warning("Malformed LyX document: Missing \\font_osf.")
246 osf = str2bool(get_value(document.header, "\\font_osf", i))
247 document.header[i] = "\\font_osf false"
249 i = find_token(document.header, "\\font_sc", 0)
251 document.warning("Malformed LyX document: Missing \\font_sc.")
253 # we do not need this value.
254 document.header[i] = "\\font_sc false"
256 i = find_token(document.header, "\\font_sf_scale", 0)
258 document.warning("Malformed LyX document: Missing \\font_sf_scale.")
260 val = get_value(document.header, '\\font_sf_scale', i)
263 sf_scale = float(val)
265 document.warning("Invalid font_sf_scale value: " + val)
266 document.header[i] = "\\font_sf_scale 100"
268 i = find_token(document.header, "\\font_tt_scale", 0)
270 document.warning("Malformed LyX document: Missing \\font_tt_scale.")
272 val = get_value(document.header, '\\font_tt_scale', i)
275 tt_scale = float(val)
277 document.warning("Invalid font_tt_scale value: " + val)
278 document.header[i] = "\\font_tt_scale 100"
280 # 3.) set preamble stuff
281 pretext = ['%% This document must be processed with xelatex!']
282 pretext.append('\\usepackage{fontspec}')
283 if roman != "default":
284 pretext.append('\\setmainfont[Mapping=tex-text]{' + roman + '}')
285 if sans != "default":
286 sf = '\\setsansfont['
287 if sf_scale != 100.0:
288 sf += 'Scale=' + str(sf_scale / 100.0) + ','
289 sf += 'Mapping=tex-text]{' + sans + '}'
291 if typewriter != "default":
293 if tt_scale != 100.0:
294 tw += '[Scale=' + str(tt_scale / 100.0) + ']'
295 tw += '{' + typewriter + '}'
298 pretext.append('\\defaultfontfeatures{Numbers=OldStyle}')
299 pretext.append('\usepackage{xunicode}')
300 pretext.append('\usepackage{xltxtra}')
301 insert_to_preamble(document, pretext)
304 def revert_outputformat(document):
305 " Remove default output format param "
307 if not del_token(document.header, '\\default_output_format', 0):
308 document.warning("Malformed LyX document: Missing \\default_output_format.")
311 def revert_backgroundcolor(document):
312 " Reverts background color to preamble code "
313 i = find_token(document.header, "\\backgroundcolor", 0)
316 colorcode = get_value(document.header, '\\backgroundcolor', i)
317 del document.header[i]
318 # don't clutter the preamble if backgroundcolor is not set
319 if colorcode == "#ffffff":
321 red = hex2ratio(colorcode[1:3])
322 green = hex2ratio(colorcode[3:5])
323 blue = hex2ratio(colorcode[5:7])
324 insert_to_preamble(document, \
325 ['% To set the background color',
326 '\\@ifundefined{definecolor}{\\usepackage{color}}{}',
327 '\\definecolor{page_backgroundcolor}{rgb}{' + red + ',' + green + ',' + blue + '}',
328 '\\pagecolor{page_backgroundcolor}'])
331 def revert_splitindex(document):
332 " Reverts splitindex-aware documents "
333 i = find_token(document.header, '\\use_indices', 0)
335 document.warning("Malformed LyX document: Missing \\use_indices.")
337 useindices = str2bool(get_value(document.header, "\\use_indices", i))
338 del document.header[i]
341 preamble.append("\\usepackage{splitidx})")
343 # deal with index declarations in the preamble
346 i = find_token(document.header, "\\index", i)
349 k = find_token(document.header, "\\end_index", i)
351 document.warning("Malformed LyX document: Missing \\end_index.")
354 line = document.header[i]
355 l = re.compile(r'\\index (.*)$')
358 ishortcut = get_value(document.header, '\\shortcut', i, k)
360 preamble.append("\\newindex[" + iname + "]{" + ishortcut + "}")
361 del document.header[i:k + 1]
363 insert_to_preamble(document, preamble)
365 # deal with index insets
366 # these need to have the argument removed
369 i = find_token(document.body, "\\begin_inset Index", i)
372 line = document.body[i]
373 l = re.compile(r'\\begin_inset Index (.*)$')
376 if itype == "idx" or indices == "false":
377 document.body[i] = "\\begin_inset Index"
379 k = find_end_of_inset(document.body, i)
381 document.warning("Can't find end of index inset!")
384 content = lyx2latex(document, document.body[i:k])
386 content = content.replace('"', r'\"')
387 subst = put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")
388 document.body[i:k + 1] = subst
391 # deal with index_print insets
394 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
397 k = find_end_of_inset(document.body, i)
398 ptype = get_quoted_value(document.body, 'type', i, k)
400 j = find_token(document.body, "type", i, k)
403 del document.body[i:k + 1]
405 subst = put_cmd_in_ert("\\printindex[" + ptype + "]{}")
406 document.body[i:k + 1] = subst
410 def convert_splitindex(document):
411 " Converts index and printindex insets to splitindex-aware format "
414 i = find_token(document.body, "\\begin_inset Index", i)
417 document.body[i] = document.body[i].replace("\\begin_inset Index",
418 "\\begin_inset Index idx")
422 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
425 if document.body[i + 1].find('LatexCommand printindex') == -1:
426 document.warning("Malformed LyX document: Incomplete printindex inset.")
428 subst = ["LatexCommand printindex",
430 document.body[i + 1:i + 2] = subst
434 def revert_subindex(document):
435 " Reverts \\printsubindex CommandInset types "
436 i = find_token(document.header, '\\use_indices', 0)
438 document.warning("Malformed LyX document: Missing \\use_indices.")
440 useindices = str2bool(get_value(document.header, "\\use_indices", i))
443 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
446 k = find_end_of_inset(document.body, i)
447 ctype = get_value(document.body, 'LatexCommand', i, k)
448 if ctype != "printsubindex":
451 ptype = get_quoted_value(document.body, 'type', i, k)
453 del document.body[i:k + 1]
455 subst = put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")
456 document.body[i:k + 1] = subst
460 def revert_printindexall(document):
461 " Reverts \\print[sub]index* CommandInset types "
462 i = find_token(document.header, '\\use_indices', 0)
464 document.warning("Malformed LyX document: Missing \\use_indices.")
466 useindices = str2bool(get_value(document.header, "\\use_indices", i))
469 i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
472 k = find_end_of_inset(document.body, i)
473 ctype = get_value(document.body, 'LatexCommand', i, k)
474 if ctype != "printindex*" and ctype != "printsubindex*":
478 del document.body[i:k + 1]
480 subst = put_cmd_in_ert("\\" + ctype + "{}")
481 document.body[i:k + 1] = subst
485 def revert_strikeout(document):
486 " Reverts \\strikeout font attribute "
487 changed = revert_font_attrs(document.body, "\\uuline", "\\uuline")
488 changed = revert_font_attrs(document.body, "\\uwave", "\\uwave") or changed
489 changed = revert_font_attrs(document.body, "\\strikeout", "\\sout") or changed
491 insert_to_preamble(document, \
492 ['% for proper underlining',
493 '\\PassOptionsToPackage{normalem}{ulem}',
494 '\\usepackage{ulem}'])
497 def revert_ulinelatex(document):
498 " Reverts \\uline font attribute "
499 i = find_token(document.body, '\\bar under', 0)
502 insert_to_preamble(document,\
503 ['% for proper underlining',
504 '\\PassOptionsToPackage{normalem}{ulem}',
505 '\\usepackage{ulem}',
506 '\\let\\cite@rig\\cite',
507 '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}',
508 ' \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}',
509 '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}'])
512 def revert_custom_processors(document):
513 " Remove bibtex_command and index_command params "
515 if not del_token(document.header, '\\bibtex_command', 0):
516 document.warning("Malformed LyX document: Missing \\bibtex_command.")
518 if not del_token(document.header, '\\index_command', 0):
519 document.warning("Malformed LyX document: Missing \\index_command.")
522 def convert_nomencl_width(document):
523 " Add set_width param to nomencl_print "
526 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
529 document.body.insert(i + 2, "set_width \"none\"")
533 def revert_nomencl_width(document):
534 " Remove set_width param from nomencl_print "
537 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
540 j = find_end_of_inset(document.body, i)
541 if not del_token(document.body, "set_width", i, j):
542 document.warning("Can't find set_width option for nomencl_print!")
546 def revert_nomencl_cwidth(document):
547 " Remove width param from nomencl_print "
550 i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
553 j = find_end_of_inset(document.body, i)
554 l = find_token(document.body, "width", i, j)
556 document.warning("Can't find width option for nomencl_print!")
559 width = get_quoted_value(document.body, "width", i, j)
561 insert_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
565 def revert_applemac(document):
566 " Revert applemac encoding to auto "
567 if document.encoding != "applemac":
569 document.encoding = "auto"
570 i = find_token(document.header, "\\encoding", 0)
572 document.header[i] = "\\encoding auto"
575 def revert_longtable_align(document):
576 " Remove longtable alignment setting "
579 i = find_token(document.body, "\\begin_inset Tabular", i)
582 end = find_end_of_inset(document.body, i)
584 document.warning("Can't find end of inset at line " + str(i))
587 fline = find_token(document.body, "<features", i, end)
589 document.warning("Can't find features for inset at line " + str(i))
592 j = document.body[fline].find("longtabularalignment")
596 # FIXME Is this correct? It wipes out everything after the
598 document.body[fline] = document.body[fline][:j - 1] + '>'
599 # since there could be a tabular inside this one, we
600 # cannot jump to end.
604 def revert_branch_filename(document):
605 " Remove \\filename_suffix parameter from branches "
608 i = find_token(document.header, "\\filename_suffix", i)
611 del document.header[i]
614 def revert_paragraph_indentation(document):
615 " Revert custom paragraph indentation to preamble code "
616 i = find_token(document.header, "\\paragraph_indentation", 0)
619 length = get_value(document.header, "\\paragraph_indentation", i)
620 # we need only remove the line if indentation is default
621 if length != "default":
622 # handle percent lengths
623 length = latex_length(length)[1]
624 insert_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
625 del document.header[i]
628 def revert_percent_skip_lengths(document):
629 " Revert relative lengths for paragraph skip separation to preamble code "
630 i = find_token(document.header, "\\defskip", 0)
633 length = get_value(document.header, "\\defskip", i)
634 # only revert when a custom length was set and when
635 # it used a percent length
636 if length in ('smallskip', 'medskip', 'bigskip'):
638 # handle percent lengths
639 percent, length = latex_length(length)
641 insert_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
642 # set defskip to medskip as default
643 document.header[i] = "\\defskip medskip"
646 def revert_percent_vspace_lengths(document):
647 " Revert relative VSpace lengths to ERT "
650 i = find_token(document.body, "\\begin_inset VSpace", i)
653 # only revert if a custom length was set and if
654 # it used a percent length
655 r = re.compile(r'\\begin_inset VSpace (.*)$')
656 m = r.match(document.body[i])
658 if length in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
661 # check if the space has a star (protected space)
662 protected = (document.body[i].rfind("*") != -1)
664 length = length.rstrip('*')
665 # handle percent lengths
666 percent, length = latex_length(length)
667 # revert the VSpace inset to ERT
670 subst = put_cmd_in_ert("\\vspace*{" + length + "}")
672 subst = put_cmd_in_ert("\\vspace{" + length + "}")
673 document.body[i:i + 2] = subst
677 def revert_percent_hspace_lengths(document):
678 " Revert relative HSpace lengths to ERT "
681 i = find_token_exact(document.body, "\\begin_inset space \\hspace", i)
684 j = find_end_of_inset(document.body, i)
686 document.warning("Can't find end of inset at line " + str(i))
689 # only revert if a custom length was set...
690 length = get_value(document.body, '\\length', i + 1, j)
692 document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
696 if document.body[i].find("\\hspace*{}") != -1:
698 # ...and if it used a percent length
699 percent, length = latex_length(length)
700 # revert the HSpace inset to ERT
702 subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
703 document.body[i:j + 1] = subst
704 # if we did a substitution, this will still be ok
708 def revert_hspace_glue_lengths(document):
709 " Revert HSpace glue lengths to ERT "
712 i = find_token_exact(document.body, "\\begin_inset space \\hspace", i)
715 j = find_end_of_inset(document.body, i)
717 document.warning("Can't find end of inset at line " + str(i))
720 length = get_value(document.body, '\\length', i + 1, j)
722 document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
726 if document.body[i].find("\\hspace*{}") != -1:
728 # only revert if the length contains a plus or minus at pos != 0
729 if length.find('-',1) != -1 or length.find('+',1) != -1:
730 # handle percent lengths
731 length = latex_length(length)[1]
732 # revert the HSpace inset to ERT
733 subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
734 document.body[i:j+1] = subst
738 def convert_author_id(document):
739 " Add the author_id to the \\author definition and make sure 0 is not used"
742 re_author = re.compile(r'(\\author) (\".*\")\s*(.*)$')
745 i = find_token(document.header, "\\author", i)
748 m = re_author.match(document.header[i])
752 document.header[i] = "\\author %i %s %s" % (anum, name, email)
758 i = find_token(document.body, "\\change_", i)
761 change = document.body[i].split(' ');
764 author_id = int(change[1])
766 document.body[i] = "%s %i %s" % (type, author_id + 1, time)
770 def revert_author_id(document):
771 " Remove the author_id from the \\author definition "
774 rx = re.compile(r'(\\author)\s+(\d+)\s+(\".*\")\s*(.*)$')
778 i = find_token(document.header, "\\author", i)
781 m = rx.match(document.header[i])
783 author_id = int(m.group(2))
784 idmap[author_id] = anum
787 document.header[i] = "\\author %s %s" % (name, email)
789 # FIXME Should this be incremented if we didn't match?
794 i = find_token(document.body, "\\change_", i)
797 change = document.body[i].split(' ');
800 author_id = int(change[1])
802 document.body[i] = "%s %i %s" % (type, idmap[author_id], time)
806 def revert_suppress_date(document):
807 " Revert suppressing of default document date to preamble code "
808 i = find_token(document.header, "\\suppress_date", 0)
811 # remove the preamble line and write to the preamble
812 # when suppress_date was true
813 date = str2bool(get_value(document.header, "\\suppress_date", i))
815 add_to_preamble(document, ["\\date{}"])
816 del document.header[i]
819 def convert_mhchem(document):
820 "Set mhchem to off for versions older than 1.6.x"
821 if document.start < 277:
822 # LyX 1.5.x and older did never load mhchem.
823 # Therefore we must switch it off: Documents that use mhchem have
824 # a manual \usepackage anyway, and documents not using mhchem but
825 # custom macros with the same names as mhchem commands might get
826 # corrupted if mhchem is automatically loaded.
829 # LyX 1.6.x did always load mhchem automatically.
831 i = find_token(document.header, "\\use_esint", 0)
834 i = find_token(document.header, "\\use_amsmath", 0)
836 document.warning("Malformed LyX document: Could not find amsmath os esint setting.")
838 document.header.insert(i + 1, "\\use_mhchem %d" % mhchem)
841 def revert_mhchem(document):
842 "Revert mhchem loading to preamble code"
845 i = find_token(document.header, "\\use_mhchem", 0)
847 document.warning("Malformed LyX document: Could not find mhchem setting.")
850 val = get_value(document.header, "\\use_mhchem", i)
855 del document.header[i]
864 i = find_token(document.body, "\\begin_inset Formula", i)
867 line = document.body[i]
868 if line.find("\\ce{") != -1 or line.find("\\cf{") != -1:
874 pre = ["\\PassOptionsToPackage{version=3}{mhchem}",
875 "\\usepackage{mhchem}"]
876 insert_to_preamble(document, pre)
879 def revert_fontenc(document):
880 " Remove fontencoding param "
881 if not del_token(document.header, '\\fontencoding', 0):
882 document.warning("Malformed LyX document: Missing \\fontencoding.")
885 def merge_gbrief(document):
886 " Merge g-brief-en and g-brief-de to one class "
888 if document.textclass != "g-brief-de":
889 if document.textclass == "g-brief-en":
890 document.textclass = "g-brief"
891 document.set_textclass()
894 obsoletedby = { "Brieftext": "Letter",
895 "Unterschrift": "Signature",
897 "Zusatz": "Addition",
900 "RetourAdresse": "ReturnAddress",
901 "MeinZeichen": "MyRef",
902 "IhrZeichen": "YourRef",
903 "IhrSchreiben": "YourMail",
906 "Konto": "BankAccount",
907 "Postvermerk": "PostalComment",
908 "Adresse": "Address",
910 "Betreff": "Reference",
917 i = find_token(document.body, "\\begin_layout", i)
921 layout = document.body[i][14:]
922 if layout in obsoletedby:
923 document.body[i] = "\\begin_layout " + obsoletedby[layout]
927 document.textclass = "g-brief"
928 document.set_textclass()
931 def revert_gbrief(document):
932 " Revert g-brief to g-brief-en "
933 if document.textclass == "g-brief":
934 document.textclass = "g-brief-en"
935 document.set_textclass()
938 def revert_html_options(document):
939 " Remove html options "
940 del_token(document.header, '\\html_use_mathml', 0)
941 del_token(document.header, '\\html_be_strict', 0)
944 def revert_includeonly(document):
947 i = find_token(document.header, "\\begin_includeonly", i)
950 j = find_end_of(document.header, i, "\\begin_includeonly", "\\end_includeonly")
952 document.warning("Unable to find end of includeonly section!!")
954 document.header[i : j + 1] = []
957 def revert_includeall(document):
958 " Remove maintain_unincluded_children param "
959 del_token(document.header, '\\maintain_unincluded_children', 0)
962 def revert_multirow(document):
963 " Revert multirow cells in tables to TeX-code"
965 # first, let's find out if we need to do anything
966 # cell type 3 is multirow begin cell
967 i = find_token(document.body, '<cell multirow="3"', 0)
971 add_to_preamble(document, ["\\usepackage{multirow}"])
975 # find begin/end of table
976 begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
977 if begin_table == -1:
979 end_table = find_end_of(document.body, begin_table, '<lyxtabular', '</lyxtabular>')
981 document.warning("Malformed LyX document: Could not find end of table.")
984 # does this table have multirow?
985 i = find_token(document.body, '<cell multirow="3"', begin_table, end_table)
987 begin_table = end_table
990 # store the number of rows and columns
991 numrows = get_option_value(document.body[begin_table], "rows")
992 numcols = get_option_value(document.body[begin_table], "columns")
994 numrows = int(numrows)
995 numcols = int(numcols)
997 document.warning(numrows)
998 document.warning("Unable to determine rows and columns!")
999 begin_table = end_table
1004 # collect info on rows and columns of this table.
1005 begin_row = begin_table
1006 for row in range(numrows):
1007 begin_row = find_token(document.body, '<row>', begin_row, end_table)
1009 document.warning("Can't find row " + str(row + 1))
1011 end_row = find_end_of(document.body, begin_row, '<row>', '</row>')
1013 document.warning("Can't find end of row " + str(row + 1))
1015 begin_cell = begin_row
1016 multirows.append([])
1017 for column in range(numcols):
1018 begin_cell = find_token(document.body, '<cell ', begin_cell, end_row)
1019 if begin_cell == -1:
1020 document.warning("Can't find column " + str(column + 1) + \
1021 "in row " + str(row + 1))
1024 # this will fail if someone puts "</cell>" in a cell, but
1025 # that seems fairly unlikely.
1026 end_cell = find_end_of(document.body, begin_cell, '<cell', '</cell>')
1028 document.warning("Can't find end of column " + str(column + 1) + \
1029 "in row " + str(row + 1))
1031 multirows[row].append([begin_cell, end_cell, 0])
1032 if document.body[begin_cell].find('multirow="3"') != -1:
1033 multirows[row][column][2] = 3 # begin multirow
1034 mrstarts.append([row, column])
1035 elif document.body[begin_cell].find('multirow="4"') != -1:
1036 multirows[row][column][2] = 4 # in multirow
1037 begin_cell = end_cell
1039 # end of table info collection
1041 # work from the back to avoid messing up numbering
1047 col_width = get_option_value(document.body[begin_table + 2 + col], "width")
1048 # "0pt" means that no width is specified
1049 if not col_width or col_width == "0pt":
1051 # determine the number of cells that are part of the multirow
1053 for r in range(row + 1, numrows):
1054 if multirows[r][col][2] != 4:
1057 # take the opportunity to revert this line
1058 lineno = multirows[r][col][0]
1059 document.body[lineno] = document.body[lineno].\
1060 replace(' multirow="4" ', ' ').\
1061 replace('valignment="middle"', 'valignment="top"').\
1062 replace(' topline="true" ', ' ')
1063 # remove bottom line of previous multirow-part cell
1064 lineno = multirows[r-1][col][0]
1065 document.body[lineno] = document.body[lineno].replace(' bottomline="true" ', ' ')
1066 # revert beginning cell
1067 bcell = multirows[row][col][0]
1068 ecell = multirows[row][col][1]
1069 document.body[bcell] = document.body[bcell].\
1070 replace(' multirow="3" ', ' ').\
1071 replace('valignment="middle"', 'valignment="top"')
1072 blay = find_token(document.body, "\\begin_layout", bcell, ecell)
1074 document.warning("Can't find layout for cell!")
1076 bend = find_end_of_layout(document.body, blay)
1078 document.warning("Can't find end of layout for cell!")
1080 # do the later one first, so as not to mess up the numbering
1081 # we are wrapping the whole cell in this ert
1082 # so before the end of the layout...
1083 document.body[bend:bend] = put_cmd_in_ert("}")
1084 # ...and after the beginning
1085 document.body[blay + 1:blay + 1] = \
1086 put_cmd_in_ert("\\multirow{" + str(nummrs) + "}{" + col_width + "}{")
1088 begin_table = end_table
1091 def convert_math_output(document):
1092 " Convert \html_use_mathml to \html_math_output "
1093 i = find_token(document.header, "\\html_use_mathml", 0)
1096 rgx = re.compile(r'\\html_use_mathml\s+(\w+)')
1097 m = rgx.match(document.header[i])
1098 newval = "0" # MathML
1100 val = str2bool(m.group(1))
1102 newval = "2" # Images
1104 document.warning("Can't match " + document.header[i])
1105 document.header[i] = "\\html_math_output " + newval
1108 def revert_math_output(document):
1109 " Revert \html_math_output to \html_use_mathml "
1110 i = find_token(document.header, "\\html_math_output", 0)
1113 rgx = re.compile(r'\\html_math_output\s+(\d)')
1114 m = rgx.match(document.header[i])
1118 if val == "1" or val == "2":
1121 document.warning("Unable to match " + document.header[i])
1122 document.header[i] = "\\html_use_mathml " + newval
1126 def revert_inset_preview(document):
1127 " Dissolves the preview inset "
1130 i = find_token(document.body, "\\begin_inset Preview", i)
1133 iend = find_end_of_inset(document.body, i)
1135 document.warning("Malformed LyX document: Could not find end of Preview inset.")
1139 # This has several issues.
1140 # We need to do something about the layouts inside InsetPreview.
1141 # If we just leave the first one, then we have something like:
1142 # \begin_layout Standard
1144 # \begin_layout Standard
1145 # and we get a "no \end_layout" error. So something has to be done.
1146 # Ideally, we would check if it is the same as the layout we are in.
1147 # If so, we just remove it; if not, we end the active one. But it is
1148 # not easy to know what layout we are in, due to depth changes, etc,
1149 # and it is not clear to me how much work it is worth doing. In most
1150 # cases, the layout will probably be the same.
1152 # For the same reason, we have to remove the \end_layout tag at the
1153 # end of the last layout in the inset. Again, that will sometimes be
1154 # wrong, but it will usually be right. To know what to do, we would
1155 # again have to know what layout the inset is in.
1157 blay = find_token(document.body, "\\begin_layout", i, iend)
1159 document.warning("Can't find layout for preview inset!")
1160 # always do the later one first...
1161 del document.body[iend]
1162 del document.body[i]
1163 # deletions mean we do not need to reset i
1166 # This is where we would check what layout we are in.
1167 # The check for Standard is definitely wrong.
1169 # lay = document.body[blay].split(None, 1)[1]
1170 # if lay != oldlayout:
1171 # # record a boolean to tell us what to do later....
1172 # # better to do it later, since (a) it won't mess up
1173 # # the numbering and (b) we only modify at the end.
1175 # we want to delete the last \\end_layout in this inset, too.
1176 # note that this may not be the \\end_layout that goes with blay!!
1177 bend = find_end_of_layout(document.body, blay)
1179 tmp = find_token(document.body, "\\end_layout", bend + 1, iend)
1184 document.warning("Unable to find last layout in preview inset!")
1185 del document.body[iend]
1186 del document.body[i]
1187 # deletions mean we do not need to reset i
1189 # always do the later one first...
1190 del document.body[iend]
1191 del document.body[bend]
1192 del document.body[i:blay + 1]
1193 # we do not need to reset i
1196 def revert_equalspacing_xymatrix(document):
1197 " Revert a Formula with xymatrix@! to an ERT inset "
1199 has_preamble = False
1200 has_equal_spacing = False
1203 i = find_token(document.body, "\\begin_inset Formula", i)
1206 j = find_end_of_inset(document.body, i)
1208 document.warning("Malformed LyX document: Could not find end of Formula inset.")
1212 for curline in range(i,j):
1213 found = document.body[curline].find("\\xymatrix@!")
1218 has_equal_spacing = True
1219 content = [document.body[i][21:]]
1220 content += document.body[i + 1:j]
1221 subst = put_cmd_in_ert(content)
1222 document.body[i:j + 1] = subst
1223 i += len(subst) - (j - i) + 1
1225 for curline in range(i,j):
1226 l = document.body[curline].find("\\xymatrix")
1228 has_preamble = True;
1232 if has_equal_spacing and not has_preamble:
1233 add_to_preamble(document, ['\\usepackage[all]{xy}'])
1236 def revert_notefontcolor(document):
1237 " Reverts greyed-out note font color to preamble code "
1239 i = find_token(document.header, "\\notefontcolor", 0)
1243 colorcode = get_value(document.header, '\\notefontcolor', i)
1244 del document.header[i]
1246 # are there any grey notes?
1247 if find_token(document.body, "\\begin_inset Note Greyedout", 0) == -1:
1248 # no need to do anything else, and \renewcommand will throw
1249 # an error since lyxgreyedout will not exist.
1252 # the color code is in the form #rrggbb where every character denotes a hex number
1253 red = hex2ratio(colorcode[1:3])
1254 green = hex2ratio(colorcode[3:5])
1255 blue = hex2ratio(colorcode[5:7])
1256 # write the preamble
1257 insert_to_preamble(document,
1258 [ '% for greyed-out notes',
1259 '\\@ifundefined{definecolor}{\\usepackage{color}}{}'
1260 '\\definecolor{note_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
1261 '\\renewenvironment{lyxgreyedout}',
1262 ' {\\textcolor{note_fontcolor}\\bgroup}{\\egroup}'])
1265 def revert_turkmen(document):
1266 "Set language Turkmen to English"
1268 if document.language == "turkmen":
1269 document.language = "english"
1270 i = find_token(document.header, "\\language", 0)
1272 document.header[i] = "\\language english"
1276 j = find_token(document.body, "\\lang turkmen", j)
1279 document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english")
1283 def revert_fontcolor(document):
1284 " Reverts font color to preamble code "
1285 i = find_token(document.header, "\\fontcolor", 0)
1288 colorcode = get_value(document.header, '\\fontcolor', i)
1289 del document.header[i]
1290 # don't clutter the preamble if font color is not set
1291 if colorcode == "#000000":
1293 # the color code is in the form #rrggbb where every character denotes a hex number
1294 red = hex2ratio(colorcode[1:3])
1295 green = hex2ratio(colorcode[3:5])
1296 blue = hex2ratio(colorcode[5:7])
1297 # write the preamble
1298 insert_to_preamble(document,
1299 ['% Set the font color',
1300 '\\@ifundefined{definecolor}{\\usepackage{color}}{}',
1301 '\\definecolor{document_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
1302 '\\color{document_fontcolor}'])
1305 def revert_shadedboxcolor(document):
1306 " Reverts shaded box color to preamble code "
1307 i = find_token(document.header, "\\boxbgcolor", 0)
1310 colorcode = get_value(document.header, '\\boxbgcolor', i)
1311 del document.header[i]
1312 # the color code is in the form #rrggbb
1313 red = hex2ratio(colorcode[1:3])
1314 green = hex2ratio(colorcode[3:5])
1315 blue = hex2ratio(colorcode[5:7])
1316 # write the preamble
1317 insert_to_preamble(document,
1318 ['% Set the color of boxes with shaded background',
1319 '\\@ifundefined{definecolor}{\\usepackage{color}}{}',
1320 "\\definecolor{shadecolor}{rgb}{%s,%s,%s}" % (red, green, blue)])
1323 def revert_lyx_version(document):
1324 " Reverts LyX Version information from Inset Info "
1325 version = "LyX version"
1327 import lyx2lyx_version
1328 version = lyx2lyx_version.version
1334 i = find_token(document.body, '\\begin_inset Info', i)
1337 j = find_end_of_inset(document.body, i + 1)
1339 document.warning("Malformed LyX document: Could not find end of Info inset.")
1348 typ = get_quoted_value(document.body, "type", i, j)
1349 arg = get_quoted_value(document.body, "arg", i, j)
1350 if arg != "version" or typ != "lyxinfo":
1354 # We do not actually know the version of LyX used to produce the document.
1355 # But we can use our version, since we are reverting.
1357 # Now we want to check if the line after "\end_inset" is empty. It normally
1358 # is, so we want to remove it, too.
1360 if document.body[j + 1].strip() == "":
1362 document.body[i: lastline] = s
1366 def revert_math_scale(document):
1367 " Remove math scaling and LaTeX options "
1368 del_token(document.header, '\\html_math_img_scale', 0)
1369 del_token(document.header, '\\html_latex_start', 0)
1370 del_token(document.header, '\\html_latex_end', 0)
1373 def revert_pagesizes(document):
1374 " Revert page sizes to default "
1375 i = find_token(document.header, '\\papersize', 0)
1377 size = document.header[i][11:]
1378 if size == "a0paper" or size == "a1paper" or size == "a2paper" \
1379 or size == "a6paper" or size == "b0paper" or size == "b1paper" \
1380 or size == "b2paper" or size == "b6paper" or size == "b0j" \
1381 or size == "b1j" or size == "b2j" or size == "b3j" or size == "b4j" \
1382 or size == "b5j" or size == "b6j":
1383 del document.header[i]
1386 def revert_DIN_C_pagesizes(document):
1387 " Revert DIN C page sizes to default "
1388 i = find_token(document.header, '\\papersize', 0)
1390 size = document.header[i][11:]
1391 if size == "c0paper" or size == "c1paper" or size == "c2paper" \
1392 or size == "c3paper" or size == "c4paper" or size == "c5paper" \
1393 or size == "c6paper":
1394 del document.header[i]
1397 def convert_html_quotes(document):
1398 " Remove quotes around html_latex_start and html_latex_end "
1400 i = find_token(document.header, '\\html_latex_start', 0)
1402 line = document.header[i]
1403 l = re.compile(r'\\html_latex_start\s+"(.*)"')
1406 document.header[i] = "\\html_latex_start " + m.group(1)
1408 i = find_token(document.header, '\\html_latex_end', 0)
1410 line = document.header[i]
1411 l = re.compile(r'\\html_latex_end\s+"(.*)"')
1414 document.header[i] = "\\html_latex_end " + m.group(1)
1417 def revert_html_quotes(document):
1418 " Remove quotes around html_latex_start and html_latex_end "
1420 i = find_token(document.header, '\\html_latex_start', 0)
1422 line = document.header[i]
1423 l = re.compile(r'\\html_latex_start\s+(.*)')
1426 document.warning("Weird html_latex_start line: " + line)
1427 del document.header[i]
1429 document.header[i] = "\\html_latex_start \"" + m.group(1) + "\""
1431 i = find_token(document.header, '\\html_latex_end', 0)
1433 line = document.header[i]
1434 l = re.compile(r'\\html_latex_end\s+(.*)')
1437 document.warning("Weird html_latex_end line: " + line)
1438 del document.header[i]
1440 document.header[i] = "\\html_latex_end \"" + m.group(1) + "\""
1443 def revert_output_sync(document):
1444 " Remove forward search options "
1445 del_token(document.header, '\\output_sync_macro', 0)
1446 del_token(document.header, '\\output_sync', 0)
1449 def revert_align_decimal(document):
1452 i = find_token(document.body, "\\begin_inset Tabular", i)
1455 j = find_end_of_inset(document.body, i)
1457 document.warning("Unable to find end of Tabular inset at line " + str(i))
1460 cell = find_token(document.body, "<cell", i, j)
1462 document.warning("Can't find any cells in Tabular inset at line " + str(i))
1467 k = find_token(document.body, "<column", k, cell)
1470 if document.body[k].find('alignment="decimal"') == -1:
1473 remove_option(document.body, k, 'decimal_point')
1474 document.body[k] = \
1475 document.body[k].replace('alignment="decimal"', 'alignment="center"')
1479 def convert_optarg(document):
1480 " Convert \\begin_inset OptArg to \\begin_inset Argument "
1483 i = find_token(document.body, '\\begin_inset OptArg', i)
1486 document.body[i] = "\\begin_inset Argument"
1490 def revert_argument(document):
1491 " Convert \\begin_inset Argument to \\begin_inset OptArg "
1494 i = find_token(document.body, '\\begin_inset Argument', i)
1497 document.body[i] = "\\begin_inset OptArg"
1501 def revert_makebox(document):
1502 " Convert \\makebox to TeX code "
1505 i = find_token(document.body, '\\begin_inset Box', i)
1508 z = find_end_of_inset(document.body, i)
1510 document.warning("Malformed LyX document: Can't find end of box inset.")
1513 blay = find_token(document.body, "\\begin_layout", i, z)
1515 document.warning("Malformed LyX document: Can't find layout in box.")
1518 # by looking before the layout we make sure we're actually finding
1519 # an option, not text.
1520 j = find_token(document.body, 'use_makebox', i, blay)
1525 if not check_token(document.body[i], "\\begin_inset Box Frameless") \
1526 or get_value(document.body, 'use_makebox', j) != 1:
1527 del document.body[j]
1530 bend = find_end_of_layout(document.body, blay)
1531 if bend == -1 or bend > z:
1532 document.warning("Malformed LyX document: Can't find end of layout in box.")
1535 # determine the alignment
1536 align = get_quoted_value(document.body, 'hor_pos', i, blay, "c")
1537 # determine the width
1538 length = get_quoted_value(document.body, 'width', i, blay, "50col%")
1539 length = latex_length(length)[1]
1540 # remove the \end_layout \end_inset pair
1541 document.body[bend:z + 1] = put_cmd_in_ert("}")
1542 subst = "\\makebox[" + length + "][" \
1544 document.body[i:blay + 1] = put_cmd_in_ert(subst)
1548 def convert_use_makebox(document):
1549 " Adds use_makebox option for boxes "
1552 i = find_token(document.body, '\\begin_inset Box', i)
1555 # all of this is to make sure we actually find the use_parbox
1556 # that is an option for this box, not some text elsewhere.
1557 z = find_end_of_inset(document.body, i)
1559 document.warning("Can't find end of box inset!!")
1562 blay = find_token(document.body, "\\begin_layout", i, z)
1564 document.warning("Can't find layout in box inset!!")
1567 # so now we are looking for use_parbox before the box's layout
1568 k = find_token(document.body, 'use_parbox', i, blay)
1570 document.warning("Malformed LyX document: Can't find use_parbox statement in box.")
1573 document.body.insert(k + 1, "use_makebox 0")
1574 i = blay + 1 # not z + 1 (box insets may be nested)
1577 def revert_IEEEtran(document):
1578 " Convert IEEEtran layouts and styles to TeX code "
1579 if document.textclass != "IEEEtran":
1581 revert_flex_inset(document.body, "IEEE membership", "\\IEEEmembership")
1582 revert_flex_inset(document.body, "Lowercase", "\\MakeLowercase")
1583 layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
1584 "Page headings", "Biography without photo")
1585 latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
1586 "After Title Text": "\\IEEEaftertitletext",
1587 "Publication ID": "\\IEEEpubid"}
1588 obsoletedby = {"Page headings": "MarkBoth",
1589 "Biography without photo": "BiographyNoPhoto"}
1590 for layout in layouts:
1593 i = find_token(document.body, '\\begin_layout ' + layout, i)
1596 j = find_end_of_layout(document.body, i)
1598 document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1601 if layout in obsoletedby:
1602 document.body[i] = "\\begin_layout " + obsoletedby[layout]
1605 content = lyx2latex(document, document.body[i:j + 1])
1606 add_to_preamble(document, [latexcmd[layout] + "{" + content + "}"])
1607 del document.body[i:j + 1]
1608 # no need to reset i
1611 def convert_prettyref(document):
1612 " Converts prettyref references to neutral formatted refs "
1613 re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
1614 nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
1618 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1621 j = find_end_of_inset(document.body, i)
1623 document.warning("Malformed LyX document: No end of InsetRef!")
1626 k = find_token(document.body, "LatexCommand prettyref", i, j)
1628 document.body[k] = "LatexCommand formatted"
1630 document.header.insert(-1, "\\use_refstyle 0")
1633 def revert_refstyle(document):
1634 " Reverts neutral formatted refs to prettyref "
1635 re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
1636 nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
1640 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1643 j = find_end_of_inset(document.body, i)
1645 document.warning("Malformed LyX document: No end of InsetRef")
1648 k = find_token(document.body, "LatexCommand formatted", i, j)
1650 document.body[k] = "LatexCommand prettyref"
1652 i = find_token(document.header, "\\use_refstyle", 0)
1654 document.header.pop(i)
1657 def revert_nameref(document):
1658 " Convert namerefs to regular references "
1659 cmds = ["Nameref", "nameref"]
1661 rx = re.compile(r'reference "(.*)"')
1664 oldcmd = "LatexCommand " + cmd
1666 # It seems better to look for this, as most of the reference
1667 # insets won't be ones we care about.
1668 i = find_token(document.body, oldcmd, i)
1673 # Make sure it is actually in an inset!
1674 # A normal line could begin with "LatexCommand nameref"!
1675 val = is_in_inset(document.body, cmdloc, \
1676 "\\begin_inset CommandInset ref")
1681 # ok, so it is in an InsetRef
1682 refline = find_token(document.body, "reference", stins, endins)
1684 document.warning("Can't find reference for inset at line " + stinst + "!!")
1686 m = rx.match(document.body[refline])
1688 document.warning("Can't match reference line: " + document.body[ref])
1692 newcontent = put_cmd_in_ert('\\' + cmd + '{' + ref + '}')
1693 document.body[stins:endins + 1] = newcontent
1696 add_to_preamble(document, ["\usepackage{nameref}"])
1699 def remove_Nameref(document):
1700 " Convert Nameref commands to nameref commands "
1703 # It seems better to look for this, as most of the reference
1704 # insets won't be ones we care about.
1705 i = find_token(document.body, "LatexCommand Nameref" , i)
1711 # Make sure it is actually in an inset!
1712 val = is_in_inset(document.body, cmdloc, \
1713 "\\begin_inset CommandInset ref")
1716 document.body[cmdloc] = "LatexCommand nameref"
1719 def revert_mathrsfs(document):
1720 " Load mathrsfs if \mathrsfs us use in the document "
1722 for line in document.body:
1723 if line.find("\\mathscr{") != -1:
1724 add_to_preamble(document, ["\\usepackage{mathrsfs}"])
1728 def convert_flexnames(document):
1729 "Convert \\begin_inset Flex Custom:Style to \\begin_inset Flex Style and similarly for CharStyle and Element."
1732 rx = re.compile(r'^\\begin_inset Flex (?:Custom|CharStyle|Element):(.+)$')
1734 i = find_token(document.body, "\\begin_inset Flex", i)
1737 m = rx.match(document.body[i])
1739 document.body[i] = "\\begin_inset Flex " + m.group(1)
1744 "Alert" : "CharStyle:Alert",
1745 "Code" : "CharStyle:Code",
1746 "Concepts" : "CharStyle:Concepts",
1747 "E-Mail" : "CharStyle:E-Mail",
1748 "Emph" : "CharStyle:Emph",
1749 "Expression" : "CharStyle:Expression",
1750 "Initial" : "CharStyle:Initial",
1751 "Institute" : "CharStyle:Institute",
1752 "Meaning" : "CharStyle:Meaning",
1753 "Noun" : "CharStyle:Noun",
1754 "Strong" : "CharStyle:Strong",
1755 "Structure" : "CharStyle:Structure",
1756 "ArticleMode" : "Custom:ArticleMode",
1757 "Endnote" : "Custom:Endnote",
1758 "Glosse" : "Custom:Glosse",
1759 "PresentationMode" : "Custom:PresentationMode",
1760 "Tri-Glosse" : "Custom:Tri-Glosse"
1764 "Abbrev" : "Element:Abbrev",
1765 "CCC-Code" : "Element:CCC-Code",
1766 "Citation-number" : "Element:Citation-number",
1767 "City" : "Element:City",
1768 "Code" : "Element:Code",
1769 "CODEN" : "Element:CODEN",
1770 "Country" : "Element:Country",
1771 "Day" : "Element:Day",
1772 "Directory" : "Element:Directory",
1773 "Dscr" : "Element:Dscr",
1774 "Email" : "Element:Email",
1775 "Emph" : "Element:Emph",
1776 "Filename" : "Element:Filename",
1777 "Firstname" : "Element:Firstname",
1778 "Fname" : "Element:Fname",
1779 "GuiButton" : "Element:GuiButton",
1780 "GuiMenu" : "Element:GuiMenu",
1781 "GuiMenuItem" : "Element:GuiMenuItem",
1782 "ISSN" : "Element:ISSN",
1783 "Issue-day" : "Element:Issue-day",
1784 "Issue-months" : "Element:Issue-months",
1785 "Issue-number" : "Element:Issue-number",
1786 "KeyCap" : "Element:KeyCap",
1787 "KeyCombo" : "Element:KeyCombo",
1788 "Keyword" : "Element:Keyword",
1789 "Literal" : "Element:Literal",
1790 "MenuChoice" : "Element:MenuChoice",
1791 "Month" : "Element:Month",
1792 "Orgdiv" : "Element:Orgdiv",
1793 "Orgname" : "Element:Orgname",
1794 "Postcode" : "Element:Postcode",
1795 "SS-Code" : "Element:SS-Code",
1796 "SS-Title" : "Element:SS-Title",
1797 "State" : "Element:State",
1798 "Street" : "Element:Street",
1799 "Surname" : "Element:Surname",
1800 "Volume" : "Element:Volume",
1801 "Year" : "Element:Year"
1805 def revert_flexnames(document):
1806 if document.backend == "latex":
1807 flexlist = flex_insets
1809 flexlist = flex_elements
1811 rx = re.compile(r'^\\begin_inset Flex\s+(.+)$')
1814 i = find_token(document.body, "\\begin_inset Flex", i)
1817 m = rx.match(document.body[i])
1819 document.warning("Illegal flex inset: " + document.body[i])
1823 if style in flexlist:
1824 document.body[i] = "\\begin_inset Flex " + flexlist[style]
1828 def convert_mathdots(document):
1829 " Load mathdots automatically "
1830 i = find_token(document.header, "\\use_mhchem" , 0)
1832 i = find_token(document.header, "\\use_esint" , 0)
1834 document.header.insert(i + 1, "\\use_mathdots 1")
1837 def revert_mathdots(document):
1838 " Load mathdots if used in the document "
1840 mathdots = find_token(document.header, "\\use_mathdots" , 0)
1842 document.warning("No \\usemathdots line. Assuming auto.")
1844 val = get_value(document.header, "\\use_mathdots", mathdots)
1845 del document.header[mathdots]
1849 document.warning("Invalid \\use_mathdots value: " + val + ". Assuming auto.")
1850 # probably usedots has not been changed, but be safe.
1858 add_to_preamble(["\\usepackage{mathdots}"])
1861 # so we are in the auto case. we want to load mathdots if \iddots is used.
1864 i = find_token(document.body, '\\begin_inset Formula', i)
1867 j = find_end_of_inset(document.body, i)
1869 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
1872 code = "\n".join(document.body[i:j])
1873 if code.find("\\iddots") != -1:
1874 add_to_preamble(document, ["\\@ifundefined{iddots}{\\usepackage{mathdots}}"])
1879 def convert_rule(document):
1880 " Convert \\lyxline to CommandInset line. "
1883 inset = ['\\begin_inset CommandInset line',
1884 'LatexCommand rule',
1888 '\\end_inset', '', '']
1890 # if paragraphs are indented, we may have to unindent to get the
1891 # line to be full-width.
1892 indent = get_value(document.header, "\\paragraph_separation", 0)
1893 have_indent = (indent == "indent")
1896 i = find_token(document.body, "\\lyxline" , i)
1900 # we need to find out if this line follows other content
1901 # in its paragraph. find its layout....
1902 lastlay = find_token_backwards(document.body, "\\begin_layout", i)
1904 document.warning("Can't find layout for line at " + str(i))
1905 # do the best we can.
1906 document.body[i:i+1] = inset
1910 # ...and look for other content before it.
1912 for line in document.body[lastlay + 1:i]:
1913 # is it empty or a paragraph option?
1914 if not line or line[0] == '\\':
1920 document.body[i:i+1] = inset
1922 # we need to unindent, lest the line be too long
1923 document.body.insert(lastlay + 1, "\\noindent")
1926 # so our line is in the middle of a paragraph
1927 # we need to add a new line, lest this line follow the
1928 # other content on that line and run off the side of the page
1929 document.body[i:i+1] = inset
1930 document.body[i:i] = ["\\begin_inset Newline newline", "\\end_inset", ""]
1934 def revert_rule(document):
1935 " Revert line insets to Tex code "
1938 i = find_token(document.body, "\\begin_inset CommandInset line" , i)
1942 j = find_token(document.body, "\\end_inset" , i)
1944 document.warning("Malformed LyX document: Can't find end of line inset.")
1946 # determine the optional offset
1947 offset = get_quoted_value(document.body, 'offset', i, j)
1949 offset = '[' + offset + ']'
1950 # determine the width
1951 width = get_quoted_value(document.body, 'width', i, j, "100col%")
1952 width = latex_length(width)[1]
1953 # determine the height
1954 height = get_quoted_value(document.body, 'height', i, j, "1pt")
1955 height = latex_length(height)[1]
1956 # output the \rule command
1957 subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
1958 document.body[i:j + 1] = put_cmd_in_ert(subst)
1959 i += len(subst) - (j - i)
1962 def revert_diagram(document):
1963 " Add the feyn package if \\Diagram is used in math "
1966 i = find_token(document.body, '\\begin_inset Formula', i)
1969 j = find_end_of_inset(document.body, i)
1971 document.warning("Malformed LyX document: Can't find end of Formula inset.")
1973 lines = "\n".join(document.body[i:j])
1974 if lines.find("\\Diagram") == -1:
1977 add_to_preamble(document, ["\\usepackage{feyn}"])
1978 # only need to do it once!
1981 chapters = ("amsbook", "book", "docbook-book", "elsart", "extbook", "extreport",
1982 "jbook", "jreport", "jsbook", "literate-book", "literate-report", "memoir",
1983 "mwbk", "mwrep", "recipebook", "report", "scrbook", "scrreprt", "svmono",
1984 "svmult", "tbook", "treport", "tufte-book")
1986 def convert_bibtex_clearpage(document):
1987 " insert a clear(double)page bibliographystyle if bibtotoc option is used "
1989 if document.textclass not in chapters:
1992 i = find_token(document.header, '\\papersides', 0)
1995 document.warning("Malformed LyX document: Can't find papersides definition.")
1996 document.warning("Assuming single sided.")
1999 val = get_value(document.header, "\\papersides", i)
2004 if sides != 1 and sides != 2:
2005 document.warning("Invalid papersides value: " + val)
2006 document.warning("Assuming single sided.")
2011 j = find_token(document.body, "\\begin_inset CommandInset bibtex", j)
2015 k = find_end_of_inset(document.body, j)
2017 document.warning("Can't find end of Bibliography inset at line " + str(j))
2021 # only act if there is the option "bibtotoc"
2022 val = get_value(document.body, 'options', j, k)
2024 document.warning("Can't find options for bibliography inset at line " + str(j))
2028 if val.find("bibtotoc") == -1:
2032 # so we want to insert a new page right before the paragraph that
2033 # this bibliography thing is in.
2034 lay = find_token_backwards(document.body, "\\begin_layout", j)
2036 document.warning("Can't find layout containing bibliography inset at line " + str(j))
2043 cmd = "cleardoublepage"
2044 subst = ['\\begin_layout Standard',
2045 '\\begin_inset Newpage ' + cmd,
2046 '\\end_inset', '', '',
2048 document.body[lay:lay] = subst
2052 def check_passthru(document):
2053 tc = document.textclass
2054 ok = (tc == "literate-article" or tc == "literate-book" or tc == "literate-report")
2056 mods = document.get_module_list()
2058 if mod == "sweave" or mod == "noweb":
2064 def convert_passthru(document):
2065 " http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg161298.html "
2066 if not check_passthru:
2069 rx = re.compile("\\\\begin_layout \s*(\w+)")
2071 for lay in ["Chunk", "Scrap"]:
2073 beg = find_token(document.body, "\\begin_layout " + lay, beg)
2076 end = find_end_of_layout(document.body, beg)
2078 document.warning("Can't find end of layout at line " + str(beg))
2081 document.warning(str(end))
2083 # we are now going to replace newline insets within this layout
2084 # by new instances of this layout. so we have repeated layouts
2085 # instead of newlines.
2087 # first, though, we need to find out if the paragraph has any
2088 # customization, so those can be propogated.
2091 while document.body[i].startswith("\\"):
2092 custom.append(document.body[i])
2097 ns = find_token(document.body, "\\begin_inset Newline newline", ns, end)
2100 ne = find_end_of_inset(document.body, ns)
2101 if ne == -1 or ne > end:
2102 document.warning("Can't find end of inset at line " + str(nb))
2105 if document.body[ne + 1] == "":
2107 subst = ["\\end_layout", "", "\\begin_layout " + lay] + custom
2108 document.body[ns:ne + 1] = subst
2109 # now we need to adjust end, in particular, but might as well
2110 # do ns properly, too
2111 newlines = (ne - ns) - len(subst) + len(custom)
2115 # ok, we now want to find out if the next layout is the
2116 # same as this one. if so, we will insert an extra copy of it
2118 next = find_token(document.body, "\\begin_layout", end)
2120 m = rx.match(document.body[next])
2122 nextlay = m.group(1)
2124 subst = ["\\begin_layout " + lay, "", "\\end_layout", ""]
2125 document.body[next:next] = subst
2129 beg += 4 # for the extra layout
2132 def revert_passthru(document):
2133 " http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg161298.html "
2134 if not check_passthru:
2136 rx = re.compile("\\\\begin_layout \s*(\w+)")
2138 for lay in ["Chunk", "Scrap"]:
2140 beg = find_token(document.body, "\\begin_layout " + lay, beg)
2143 end = find_end_of_layout(document.body, beg)
2145 document.warning("Can't find end of layout at line " + str(beg))
2149 # we now want to find out if the next layout is the
2150 # same as this one. but we will need to do this over and
2153 next = find_token(document.body, "\\begin_layout", end)
2156 m = rx.match(document.body[next])
2159 nextlay = m.group(1)
2162 # so it is the same layout again. we now want to know if it is empty.
2163 # but first let's check and make sure there is no content between the
2164 # two layouts. i'm not sure if that can happen or not.
2165 for l in range(end + 1, next):
2166 document.warning("c'" + document.body[l] + "'")
2167 if document.body[l] != "":
2168 document.warning("Found content between adjacent " + lay + " layouts!")
2170 nextend = find_end_of_layout(document.body, next)
2172 document.warning("Can't find end of layout at line " + str(next))
2175 for l in range(next + 1, nextend):
2176 document.warning("e'" + document.body[l] + "'")
2177 if document.body[l] != "":
2181 # empty layouts just get removed
2182 # should we check if it's before yet another such layout?
2183 del document.body[next : nextend + 1]
2184 # and we do not want to check again. we know the next layout
2185 # should be another Chunk and should be left as is.
2188 # if it's not empty, then we want to insert a newline in place
2189 # of the layout switch
2190 subst = ["\\begin_inset Newline newline", "\\end_inset", ""]
2191 document.body[end : next + 1] = subst
2192 # and now we have to find the end of the new, larger layout
2193 newend = find_end_of_layout(document.body, beg)
2195 document.warning("Can't find end of new layout at line " + str(beg))
2201 def revert_multirowOffset(document):
2202 " Revert multirow cells with offset in tables to TeX-code"
2203 # this routine is the same as the revert_multirow routine except that
2204 # it checks additionally for the offset
2206 # first, let's find out if we need to do anything
2207 i = find_token(document.body, '<cell multirow="3" mroffset=', 0)
2211 add_to_preamble(document, ["\\usepackage{multirow}"])
2213 rgx = re.compile(r'mroffset="[^"]+?"')
2217 # find begin/end of table
2218 begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
2219 if begin_table == -1:
2221 end_table = find_end_of(document.body, begin_table, '<lyxtabular', '</lyxtabular>')
2223 document.warning("Malformed LyX document: Could not find end of table.")
2226 # does this table have multirow?
2227 i = find_token(document.body, '<cell multirow="3"', begin_table, end_table)
2229 begin_table = end_table
2232 # store the number of rows and columns
2233 numrows = get_option_value(document.body[begin_table], "rows")
2234 numcols = get_option_value(document.body[begin_table], "columns")
2236 numrows = int(numrows)
2237 numcols = int(numcols)
2239 document.warning(numrows)
2240 document.warning("Unable to determine rows and columns!")
2241 begin_table = end_table
2246 # collect info on rows and columns of this table.
2247 begin_row = begin_table
2248 for row in range(numrows):
2249 begin_row = find_token(document.body, '<row>', begin_row, end_table)
2251 document.warning("Can't find row " + str(row + 1))
2253 end_row = find_end_of(document.body, begin_row, '<row>', '</row>')
2255 document.warning("Can't find end of row " + str(row + 1))
2257 begin_cell = begin_row
2258 multirows.append([])
2259 for column in range(numcols):
2260 begin_cell = find_token(document.body, '<cell ', begin_cell, end_row)
2261 if begin_cell == -1:
2262 document.warning("Can't find column " + str(column + 1) + \
2263 "in row " + str(row + 1))
2266 # this will fail if someone puts "</cell>" in a cell, but
2267 # that seems fairly unlikely.
2268 end_cell = find_end_of(document.body, begin_cell, '<cell', '</cell>')
2270 document.warning("Can't find end of column " + str(column + 1) + \
2271 "in row " + str(row + 1))
2273 multirows[row].append([begin_cell, end_cell, 0])
2274 if document.body[begin_cell].find('multirow="3" mroffset=') != -1:
2275 multirows[row][column][2] = 3 # begin multirow
2276 mrstarts.append([row, column])
2277 elif document.body[begin_cell].find('multirow="4"') != -1:
2278 multirows[row][column][2] = 4 # in multirow
2279 begin_cell = end_cell
2281 # end of table info collection
2283 # work from the back to avoid messing up numbering
2289 col_width = get_option_value(document.body[begin_table + 2 + col], "width")
2290 # "0pt" means that no width is specified
2291 if not col_width or col_width == "0pt":
2293 # determine the number of cells that are part of the multirow
2295 for r in range(row + 1, numrows):
2296 if multirows[r][col][2] != 4:
2299 # take the opportunity to revert this line
2300 lineno = multirows[r][col][0]
2301 document.body[lineno] = document.body[lineno].\
2302 replace(' multirow="4" ', ' ').\
2303 replace('valignment="middle"', 'valignment="top"').\
2304 replace(' topline="true" ', ' ')
2305 # remove bottom line of previous multirow-part cell
2306 lineno = multirows[r-1][col][0]
2307 document.body[lineno] = document.body[lineno].replace(' bottomline="true" ', ' ')
2308 # revert beginning cell
2309 bcell = multirows[row][col][0]
2310 ecell = multirows[row][col][1]
2311 offset = get_option_value(document.body[bcell], "mroffset")
2312 document.body[bcell] = document.body[bcell].\
2313 replace(' multirow="3" ', ' ').\
2314 replace('valignment="middle"', 'valignment="top"')
2315 # remove mroffset option
2316 document.body[bcell] = rgx.sub('', document.body[bcell])
2318 blay = find_token(document.body, "\\begin_layout", bcell, ecell)
2320 document.warning("Can't find layout for cell!")
2322 bend = find_end_of_layout(document.body, blay)
2324 document.warning("Can't find end of layout for cell!")
2326 # do the later one first, so as not to mess up the numbering
2327 # we are wrapping the whole cell in this ert
2328 # so before the end of the layout...
2329 document.body[bend:bend] = put_cmd_in_ert("}")
2330 # ...and after the beginning
2331 document.body[blay + 1:blay + 1] = \
2332 put_cmd_in_ert("\\multirow{" + str(nummrs) + "}{" + col_width + "}[" \
2335 # on to the next table
2336 begin_table = end_table
2339 def revert_script(document):
2340 " Convert subscript/superscript inset to TeX code "
2342 foundsubscript = False
2344 i = find_token(document.body, '\\begin_inset script', i)
2347 z = find_end_of_inset(document.body, i)
2349 document.warning("Malformed LyX document: Can't find end of script inset.")
2352 blay = find_token(document.body, "\\begin_layout", i, z)
2354 document.warning("Malformed LyX document: Can't find layout in script inset.")
2358 if check_token(document.body[i], "\\begin_inset script subscript"):
2359 subst = '\\textsubscript{'
2360 foundsubscript = True
2361 elif check_token(document.body[i], "\\begin_inset script superscript"):
2362 subst = '\\textsuperscript{'
2364 document.warning("Malformed LyX document: Unknown type of script inset.")
2367 bend = find_end_of_layout(document.body, blay)
2368 if bend == -1 or bend > z:
2369 document.warning("Malformed LyX document: Can't find end of layout in script inset.")
2372 # remove the \end_layout \end_inset pair
2373 document.body[bend:z + 1] = put_cmd_in_ert("}")
2374 document.body[i:blay + 1] = put_cmd_in_ert(subst)
2376 # these classes provide a \textsubscript command:
2377 # FIXME: Would be nice if we could use the information of the .layout file here
2378 classes = ["memoir", "scrartcl", "scrbook", "scrlttr2", "scrreprt"]
2379 if foundsubscript and find_token_exact(classes, document.textclass, 0) == -1:
2380 add_to_preamble(document, ['\\usepackage{subscript}'])
2383 def convert_use_xetex(document):
2384 " convert \\use_xetex to \\use_non_tex_fonts "
2386 i = find_token(document.header, "\\use_xetex", 0)
2390 val = get_value(document.header, "\\use_xetex", 0)
2391 document.header[i] = "\\use_non_tex_fonts " + val
2394 def revert_use_xetex(document):
2395 " revert \\use_non_tex_fonts to \\use_xetex "
2397 i = find_token(document.header, "\\use_non_tex_fonts", 0)
2399 document.warning("Malformed document. No \\use_non_tex_fonts param!")
2402 val = get_value(document.header, "\\use_non_tex_fonts", 0)
2403 document.header[i] = "\\use_xetex " + val
2406 def revert_labeling(document):
2407 koma = ("scrartcl", "scrarticle-beamer", "scrbook", "scrlettr",
2408 "scrlttr2", "scrreprt")
2409 if document.textclass in koma:
2413 i = find_token_exact(document.body, "\\begin_layout Labeling", i)
2416 document.body[i] = "\\begin_layout List"
2419 def revert_langpack(document):
2420 " revert \\language_package parameter "
2422 i = find_token(document.header, "\\language_package", 0)
2424 document.warning("Malformed document. No \\language_package param!")
2427 del document.header[i]
2430 def convert_langpack(document):
2431 " Add \\language_package parameter "
2432 i = find_token(document.header, "\language" , 0)
2434 document.warning("Malformed document. No \\language defined!")
2437 document.header.insert(i + 1, "\\language_package default")
2443 supported_versions = ["2.0.0","2.0"]
2444 convert = [[346, []],
2450 [352, [convert_splitindex]],
2457 [359, [convert_nomencl_width]],
2467 [369, [convert_author_id]],
2469 [371, [convert_mhchem]],
2471 [373, [merge_gbrief]],
2477 [379, [convert_math_output]],
2487 [389, [convert_html_quotes]],
2491 [393, [convert_optarg]],
2492 [394, [convert_use_makebox]],
2495 [397, [remove_Nameref]],
2497 [399, [convert_mathdots]],
2498 [400, [convert_rule]],
2500 [402, [convert_bibtex_clearpage]],
2501 [403, [convert_flexnames]],
2502 [404, [convert_prettyref]],
2504 [406, [convert_passthru]],
2507 [409, [convert_use_xetex]],
2509 [411, [convert_langpack]]
2512 revert = [[410, [revert_langpack]],
2513 [409, [revert_labeling]],
2514 [408, [revert_use_xetex]],
2515 [407, [revert_script]],
2516 [406, [revert_multirowOffset]],
2517 [405, [revert_passthru]],
2519 [403, [revert_refstyle]],
2520 [402, [revert_flexnames]],
2522 [400, [revert_diagram]],
2523 [399, [revert_rule]],
2524 [398, [revert_mathdots]],
2525 [397, [revert_mathrsfs]],
2527 [395, [revert_nameref]],
2528 [394, [revert_DIN_C_pagesizes]],
2529 [393, [revert_makebox]],
2530 [392, [revert_argument]],
2532 [390, [revert_align_decimal, revert_IEEEtran]],
2533 [389, [revert_output_sync]],
2534 [388, [revert_html_quotes]],
2535 [387, [revert_pagesizes]],
2536 [386, [revert_math_scale]],
2537 [385, [revert_lyx_version]],
2538 [384, [revert_shadedboxcolor]],
2539 [383, [revert_fontcolor]],
2540 [382, [revert_turkmen]],
2541 [381, [revert_notefontcolor]],
2542 [380, [revert_equalspacing_xymatrix]],
2543 [379, [revert_inset_preview]],
2544 [378, [revert_math_output]],
2546 [376, [revert_multirow]],
2547 [375, [revert_includeall]],
2548 [374, [revert_includeonly]],
2549 [373, [revert_html_options]],
2550 [372, [revert_gbrief]],
2551 [371, [revert_fontenc]],
2552 [370, [revert_mhchem]],
2553 [369, [revert_suppress_date]],
2554 [368, [revert_author_id]],
2555 [367, [revert_hspace_glue_lengths]],
2556 [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
2557 [365, [revert_percent_skip_lengths]],
2558 [364, [revert_paragraph_indentation]],
2559 [363, [revert_branch_filename]],
2560 [362, [revert_longtable_align]],
2561 [361, [revert_applemac]],
2563 [359, [revert_nomencl_cwidth]],
2564 [358, [revert_nomencl_width]],
2565 [357, [revert_custom_processors]],
2566 [356, [revert_ulinelatex]],
2568 [354, [revert_strikeout]],
2569 [353, [revert_printindexall]],
2570 [352, [revert_subindex]],
2571 [351, [revert_splitindex]],
2572 [350, [revert_backgroundcolor]],
2573 [349, [revert_outputformat]],
2574 [348, [revert_xetex]],
2575 [347, [revert_phantom, revert_hphantom, revert_vphantom]],
2576 [346, [revert_tabularvalign]],
2577 [345, [revert_swiss]]
2581 if __name__ == "__main__":