1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007 José Matos <jamatos@lyx.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 """ Convert files to the file format generated by lyx 1.6"""
25 from parser_tools import find_token, find_end_of, find_tokens, get_value
27 ####################################################################
28 # Private helper functions
30 def find_end_of_inset(lines, i):
31 " Find end of inset, where lines[i] is included."
32 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
34 def wrap_into_ert(string, src, dst):
35 " Wrap a something into an ERT"
36 return string.replace(src, '\n\\begin_inset ERT\nstatus collapsed\n\\begin_layout Standard\n'
37 + dst + '\n\\end_layout\n\\end_inset\n')
39 ####################################################################
41 def fix_wrong_tables(document):
44 i = find_token(document.body, "\\begin_inset Tabular", i)
47 j = find_end_of_inset(document.body, i + 1)
49 document.warning("Malformed LyX document: Could not find end of tabular.")
53 nrows = int(document.body[i+1].split('"')[3])
54 ncols = int(document.body[i+1].split('"')[5])
56 for l in range(nrows):
58 for k in range(ncols):
59 m = find_token(document.body, '<cell', m)
61 if document.body[m].find('multicolumn') != -1:
62 multicol_cont = int(document.body[m].split('"')[1])
64 if multicol_cont == 2 and (k == 0 or prev_multicolumn == 0):
65 document.body[m] = document.body[m][:5] + document.body[m][21:]
68 prev_multicolumn = multicol_cont
75 def close_begin_deeper(document):
79 i = find_tokens(document.body, ["\\begin_deeper", "\\end_deeper"], i)
84 if document.body[i][:13] == "\\begin_deeper":
91 document.body[-2:-2] = ['\\end_deeper' for i in range(depth)]
94 def long_charstyle_names(document):
97 i = find_token(document.body, "\\begin_inset CharStyle", i)
100 document.body[i] = document.body[i].replace("CharStyle ", "CharStyle CharStyle:")
103 def revert_long_charstyle_names(document):
106 i = find_token(document.body, "\\begin_inset CharStyle", i)
109 document.body[i] = document.body[i].replace("CharStyle CharStyle:", "CharStyle")
113 def axe_show_label(document):
116 i = find_token(document.body, "\\begin_inset CharStyle", i)
119 if document.body[i + 1].find("show_label") != -1:
120 if document.body[i + 1].find("true") != -1:
121 document.body[i + 1] = "status open"
122 del document.body[ i + 2]
124 if document.body[i + 1].find("false") != -1:
125 document.body[i + 1] = "status collapsed"
126 del document.body[ i + 2]
128 document.warning("Malformed LyX document: show_label neither false nor true.")
130 document.warning("Malformed LyX document: show_label missing in CharStyle.")
135 def revert_show_label(document):
138 i = find_token(document.body, "\\begin_inset CharStyle", i)
141 if document.body[i + 1].find("status open") != -1:
142 document.body.insert(i + 1, "show_label true")
144 if document.body[i + 1].find("status collapsed") != -1:
145 document.body.insert(i + 1, "show_label false")
147 document.warning("Malformed LyX document: no legal status line in CharStyle.")
150 def revert_begin_modules(document):
153 i = find_token(document.header, "\\begin_modules", i)
156 j = find_end_of(document.header, i, "\\begin_modules", "\\end_modules")
158 # this should not happen
160 document.header[i : j + 1] = []
162 def convert_flex(document):
163 "Convert CharStyle to Flex"
166 i = find_token(document.body, "\\begin_inset CharStyle", i)
169 document.body[i] = document.body[i].replace('\\begin_inset CharStyle', '\\begin_inset Flex')
171 def revert_flex(document):
172 "Convert Flex to CharStyle"
175 i = find_token(document.body, "\\begin_inset Flex", i)
178 document.body[i] = document.body[i].replace('\\begin_inset Flex', '\\begin_inset CharStyle')
181 # Discard PDF options for hyperref
182 def revert_pdf_options(document):
183 "Revert PDF options for hyperref."
185 i = find_token(document.header, "\\use_hyperref", i)
187 del document.header[i]
188 i = find_token(document.header, "\\pdf_store_options", i)
190 del document.header[i]
191 i = find_token(document.header, "\\pdf_title", 0)
193 del document.header[i]
194 i = find_token(document.header, "\\pdf_author", 0)
196 del document.header[i]
197 i = find_token(document.header, "\\pdf_subject", 0)
199 del document.header[i]
200 i = find_token(document.header, "\\pdf_keywords", 0)
202 del document.header[i]
203 i = find_token(document.header, "\\pdf_bookmarks", 0)
205 del document.header[i]
206 i = find_token(document.header, "\\pdf_bookmarksnumbered", i)
208 del document.header[i]
209 i = find_token(document.header, "\\pdf_bookmarksopen", i)
211 del document.header[i]
212 i = find_token(document.header, "\\pdf_bookmarksopenlevel", i)
214 del document.header[i]
215 i = find_token(document.header, "\\pdf_breaklinks", i)
217 del document.header[i]
218 i = find_token(document.header, "\\pdf_pdfborder", i)
220 del document.header[i]
221 i = find_token(document.header, "\\pdf_colorlinks", i)
223 del document.header[i]
224 i = find_token(document.header, "\\pdf_backref", i)
226 del document.header[i]
227 i = find_token(document.header, "\\pdf_pagebackref", i)
229 del document.header[i]
230 i = find_token(document.header, "\\pdf_pagemode", 0)
232 del document.header[i]
233 i = find_token(document.header, "\\pdf_quoted_options", 0)
235 del document.header[i]
238 def remove_inzip_options(document):
239 "Remove inzipName and embed options from the Graphics inset"
242 i = find_token(document.body, "\\begin_inset Graphics", i)
245 j = find_end_of_inset(document.body, i + 1)
248 document.warning("Malformed LyX document: Could not find end of graphics inset.")
249 # If there's a inzip param, just remove that
250 k = find_token(document.body, "\tinzipName", i + 1, j)
253 # embed option must follow the inzipName option
254 del document.body[k+1]
258 def convert_inset_command(document):
261 \begin_inset LatexCommand cmd
263 \begin_inset CommandInset InsetType
268 i = find_token(document.body, "\\begin_inset LatexCommand", i)
271 line = document.body[i]
272 r = re.compile(r'\\begin_inset LatexCommand (.*)$')
276 #this is adapted from factory.cpp
277 if cmdName[0:4].lower() == "cite":
278 insetName = "citation"
279 elif cmdName == "url" or cmdName == "htmlurl":
281 elif cmdName[-3:] == "ref":
283 elif cmdName == "tableofcontents":
285 elif cmdName == "printnomenclature":
286 insetName = "nomencl_print"
287 elif cmdName == "printindex":
288 insetName = "index_print"
291 insertion = ["\\begin_inset CommandInset " + insetName, "LatexCommand " + cmdName]
292 document.body[i : i+1] = insertion
295 def revert_inset_command(document):
298 \begin_inset CommandInset InsetType
301 \begin_inset LatexCommand cmd
302 Some insets may end up being converted to insets earlier versions of LyX
303 will not be able to recognize. Not sure what to do about that.
307 i = find_token(document.body, "\\begin_inset CommandInset", i)
310 nextline = document.body[i+1]
311 r = re.compile(r'LatexCommand\s+(.*)$')
312 m = r.match(nextline)
314 document.warning("Malformed LyX document: Missing LatexCommand in " + document.body[i] + ".")
317 insertion = ["\\begin_inset LatexCommand " + cmdName]
318 document.body[i : i+2] = insertion
321 def convert_wrapfig_options(document):
322 "Convert optional options for wrap floats (wrapfig)."
323 # adds the tokens "lines", "placement", and "overhang"
326 i = find_token(document.body, "\\begin_inset Wrap figure", i)
329 document.body.insert(i + 1, "lines 0")
330 j = find_token(document.body, "placement", i)
331 # placement can be already set or not; if not, set it
333 document.body.insert(i + 3, "overhang 0col%")
335 document.body.insert(i + 2, "placement o")
336 document.body.insert(i + 3, "overhang 0col%")
340 def revert_wrapfig_options(document):
341 "Revert optional options for wrap floats (wrapfig)."
344 i = find_token(document.body, "lines", i)
347 j = find_token(document.body, "overhang", i+1)
348 if j != i + 2 and j != -1:
349 document.warning("Malformed LyX document: Couldn't find overhang parameter of wrap float.")
353 del document.body[j-1]
357 def convert_latexcommand_index(document):
358 "Convert from LatexCommand form to collapsable form."
361 i = find_token(document.body, "\\begin_inset CommandInset index", i)
364 if document.body[i + 1] != "LatexCommand index": # Might also be index_print
366 fullcontent = document.body[i + 2][6:].strip('"')
367 document.body[i:i + 2] = ["\\begin_inset Index",
369 "\\begin_layout Standard"]
370 # Put here the conversions needed from LaTeX string to LyXText.
371 # Here we do a minimal conversion to prevent crashes and data loss.
372 # Manual patch-up may be needed.
373 # Umlauted characters (most common ones, can be extended):
374 fullcontent = fullcontent.replace(r'\\\"a', u'ä').replace(r'\\\"o', u'ö').replace(r'\\\"u', u'ü')
376 fullcontent = wrap_into_ert(fullcontent, r'\"', '"')
377 #fullcontent = fullcontent.replace(r'\"', '\n\\begin_inset ERT\nstatus collapsed\n\\begin_layout standard\n"\n\\end_layout\n\\end_inset\n')
379 r = re.compile('^(.*?)(\$.*?\$)(.*)')
384 f = m.group(2).replace('\\\\', '\\')
388 s = wrap_into_ert(s, r'\\', '\\backslash')
389 s = wrap_into_ert(s, '{', '{')
390 s = wrap_into_ert(s, '}', '}')
391 document.body.insert(i + 3, s)
393 document.body.insert(i + 3, "\\begin_inset Formula " + f)
394 document.body.insert(i + 4, "\\end_inset")
396 # Generic, \\ -> \backslash:
397 g = wrap_into_ert(g, r'\\', '\\backslash{}')
398 g = wrap_into_ert(g, '{', '{')
399 g = wrap_into_ert(g, '}', '}')
400 document.body.insert(i + 3, g)
401 document.body[i + 4] = "\\end_layout"
405 def revert_latexcommand_index(document):
406 "Revert from collapsable form to LatexCommand form."
409 i = find_token(document.body, "\\begin_inset Index", i)
412 j = find_end_of_inset(document.body, i + 1)
415 del document.body[j - 1]
416 del document.body[j - 2] # \end_layout
417 document.body[i] = "\\begin_inset CommandInset index"
418 document.body[i + 1] = "LatexCommand index"
419 # clean up multiline stuff
421 for k in range(i + 3, j - 2):
422 line = document.body[k]
423 if line.startswith("\\begin_inset ERT"):
425 if line.startswith("\\begin_inset Formula"):
427 if line.startswith("\\begin_layout Standard"):
429 if line.startswith("\\end_layout"):
431 if line.startswith("\\end_inset"):
433 if line.startswith("status collapsed"):
435 line = line.replace(u'ä', r'\\\"a').replace(u'ö', r'\\\"o').replace(u'ü', r'\\\"u')
436 content = content + line;
437 document.body[i + 3] = "name " + '"' + content + '"'
438 for k in range(i + 4, j - 2):
439 del document.body[i + 4]
440 document.body.insert(i + 4, "")
441 del document.body[i + 2] # \begin_layout standard
445 def revert_wraptable(document):
446 "Revert wrap table to wrap figure."
449 i = find_token(document.body, "\\begin_inset Wrap table", i)
452 document.body[i] = document.body[i].replace('\\begin_inset Wrap table', '\\begin_inset Wrap figure')
456 def revert_vietnamese(document):
457 "Set language Vietnamese to English"
458 # Set document language from Vietnamese to English
460 if document.language == "vietnamese":
461 document.language = "english"
462 i = find_token(document.header, "\\language", 0)
464 document.header[i] = "\\language english"
467 j = find_token(document.body, "\\lang vietnamese", j)
470 document.body[j] = document.body[j].replace("\\lang vietnamese", "\\lang english")
474 def revert_japanese(document):
475 "Set language japanese-plain to japanese"
476 # Set document language from japanese-plain to japanese
478 if document.language == "japanese-plain":
479 document.language = "japanese"
480 i = find_token(document.header, "\\language", 0)
482 document.header[i] = "\\language japanese"
485 j = find_token(document.body, "\\lang japanese-plain", j)
488 document.body[j] = document.body[j].replace("\\lang japanese-plain", "\\lang japanese")
492 def revert_japanese_encoding(document):
493 "Set input encoding form EUC-JP-plain to EUC-JP etc."
494 # Set input encoding form EUC-JP-plain to EUC-JP etc.
496 i = find_token(document.header, "\\inputencoding EUC-JP-plain", 0)
498 document.header[i] = "\\inputencoding EUC-JP"
500 j = find_token(document.header, "\\inputencoding JIS-plain", 0)
502 document.header[j] = "\\inputencoding JIS"
504 k = find_token(document.header, "\\inputencoding SJIS-plain", 0)
505 if k != -1: # convert to UTF8 since there is currently no SJIS encoding
506 document.header[k] = "\\inputencoding UTF8"
509 def revert_inset_info(document):
510 'Replace info inset with its content'
513 i = find_token(document.body, '\\begin_inset Info', i)
516 j = find_end_of_inset(document.body, i + 1)
519 document.warning("Malformed LyX document: Could not find end of Info inset.")
522 for k in range(i, j+1):
523 if document.body[k].startswith("arg"):
524 arg = document.body[k][3:].strip().strip('"')
525 if document.body[k].startswith("type"):
526 type = document.body[k][4:].strip().strip('"')
527 # I think there is a newline after \\end_inset, which should be removed.
528 if document.body[j + 1].strip() == "":
529 document.body[i : (j + 2)] = [type + ':' + arg]
531 document.body[i : (j + 1)] = [type + ':' + arg]
534 def convert_pdf_options(document):
535 # Set the pdfusetitle tag, delete the pdf_store_options,
536 # set quotes for bookmarksopenlevel"
537 has_hr = get_value(document.header, "\\use_hyperref", 0, default = "0")
539 k = find_token(document.header, "\\use_hyperref", 0)
540 document.header.insert(k + 1, "\\pdf_pdfusetitle true")
541 k = find_token(document.header, "\\pdf_store_options", 0)
543 del document.header[k]
544 i = find_token(document.header, "\\pdf_bookmarksopenlevel", k)
546 document.header[i] = document.header[i].replace('"', '')
549 def revert_pdf_options_2(document):
550 # reset the pdfusetitle tag, set quotes for bookmarksopenlevel"
551 k = find_token(document.header, "\\use_hyperref", 0)
552 i = find_token(document.header, "\\pdf_pdfusetitle", k)
554 del document.header[i]
555 i = find_token(document.header, "\\pdf_bookmarksopenlevel", k)
557 values = document.header[i].split()
558 values[1] = ' "' + values[1] + '"'
559 document.header[i] = ''.join(values)
562 def convert_htmlurl(document):
563 'Convert "htmlurl" to "href" insets for docbook'
564 if document.backend != "docbook":
568 i = find_token(document.body, "\\begin_inset CommandInset url", i)
571 document.body[i] = "\\begin_inset CommandInset href"
572 document.body[i + 1] = "LatexCommand href"
576 def convert_url(document):
577 'Convert url insets to url charstyles'
578 if document.backend == "docbook":
582 i = find_token(document.body, "\\begin_inset CommandInset url", i)
585 n = find_token(document.body, "name", i)
587 # place the URL name in typewriter before the new URL insert
588 # grab the name 'bla' from the e.g. the line 'name "bla"',
589 # therefore start with the 6th character
590 name = document.body[n][6:-1]
591 newname = [name + " "]
592 document.body[i:i] = newname
594 j = find_token(document.body, "target", i)
596 document.warning("Malformed LyX document: Can't find target for url inset")
599 target = document.body[j][8:-1]
600 k = find_token(document.body, "\\end_inset", j)
602 document.warning("Malformed LyX document: Can't find end of url inset")
605 newstuff = ["\\begin_inset Flex URL",
606 "status collapsed", "",
607 "\\begin_layout Standard",
612 document.body[i:k] = newstuff
615 def convert_ams_classes(document):
616 tc = document.textclass
617 if (tc != "amsart" and tc != "amsart-plain" and
618 tc == "amsart-seq" and tc == "amsbook"):
620 if tc == "amsart-plain":
621 document.textclass = "amsart"
622 document.set_textclass()
623 document.add_module("Theorems (Starred)")
625 if tc == "amsart-seq":
626 document.textclass = "amsart"
627 document.set_textclass()
628 document.add_module("Theorems (AMS)")
630 #Now we want to see if any of the environments in the extended theorems
631 #module were used in this document. If so, we'll add that module, too.
632 layouts = ["Criterion", "Algorithm", "Axiom", "Condition", "Note", \
633 "Notation", "Summary", "Acknowledgement", "Conclusion", "Fact", \
636 r = re.compile(r'^\\begin_layout (.*?)\*?\s*$')
639 i = find_token(document.body, "\\begin_layout", i)
642 m = r.match(document.body[i])
644 document.warning("Weirdly formed \\begin_layout at line " + i + " of body!")
648 if layouts.count(m) != 0:
649 document.add_module("Theorems (AMS-Extended)")
653 def revert_href(document):
654 'Reverts hyperlink insets (href) to url insets (url)'
657 i = find_token(document.body, "\\begin_inset CommandInset href", i)
660 document.body[i : i + 2] = \
661 ["\\begin_inset CommandInset url", "LatexCommand url"]
665 def convert_include(document):
666 'Converts include insets to new format.'
668 r = re.compile(r'\\begin_inset Include\s+\\([^{]+){([^}]*)}(?:\[(.*)\])?')
670 i = find_token(document.body, "\\begin_inset Include", i)
673 line = document.body[i]
674 previewline = document.body[i + 1]
677 document.warning("Unable to match line " + str(i) + " of body!")
683 insertion = ["\\begin_inset CommandInset include",
684 "LatexCommand " + cmd, previewline,
685 "filename \"" + fn + "\""]
688 insertion.append("lstparams " + '"' + opt + '"')
690 document.body[i : i + 2] = insertion
694 def revert_include(document):
695 'Reverts include insets to old format.'
697 r1 = re.compile('LatexCommand (.+)')
698 r2 = re.compile('filename (.+)')
699 r3 = re.compile('options (.*)')
701 i = find_token(document.body, "\\begin_inset CommandInset include", i)
704 previewline = document.body[i + 1]
705 m = r1.match(document.body[i + 2])
707 document.warning("Malformed LyX document: No LatexCommand line for `" +
708 document.body[i] + "' on line " + str(i) + ".")
712 m = r2.match(document.body[i + 3])
714 document.warning("Malformed LyX document: No filename line for `" + \
715 document.body[i] + "' on line " + str(i) + ".")
721 if (cmd == "lstinputlisting"):
722 m = r3.match(document.body[i + 4])
726 newline = "\\begin_inset Include \\" + cmd + "{" + fn + "}"
728 newline += ("[" + options + "]")
729 insertion = [newline, previewline]
730 document.body[i : i + numlines] = insertion
734 def revert_albanian(document):
735 "Set language Albanian to English"
737 if document.language == "albanian":
738 document.language = "english"
739 i = find_token(document.header, "\\language", 0)
741 document.header[i] = "\\language english"
744 j = find_token(document.body, "\\lang albanian", j)
747 document.body[j] = document.body[j].replace("\\lang albanian", "\\lang english")
751 def revert_lowersorbian(document):
752 "Set language lower Sorbian to English"
754 if document.language == "lowersorbian":
755 document.language = "english"
756 i = find_token(document.header, "\\language", 0)
758 document.header[i] = "\\language english"
761 j = find_token(document.body, "\\lang lowersorbian", j)
764 document.body[j] = document.body[j].replace("\\lang lowersorbian", "\\lang english")
768 def revert_uppersorbian(document):
769 "Set language uppersorbian to usorbian as this was used in LyX 1.5"
771 if document.language == "uppersorbian":
772 document.language = "usorbian"
773 i = find_token(document.header, "\\language", 0)
775 document.header[i] = "\\language usorbian"
778 j = find_token(document.body, "\\lang uppersorbian", j)
781 document.body[j] = document.body[j].replace("\\lang uppersorbian", "\\lang usorbian")
785 def convert_usorbian(document):
786 "Set language usorbian to uppersorbian"
788 if document.language == "usorbian":
789 document.language = "uppersorbian"
790 i = find_token(document.header, "\\language", 0)
792 document.header[i] = "\\language uppersorbian"
795 j = find_token(document.body, "\\lang usorbian", j)
798 document.body[j] = document.body[j].replace("\\lang usorbian", "\\lang uppersorbian")
802 def revert_macro_optional_params(document):
803 "Convert macro definitions with optional parameters into ERTs"
804 # Stub to convert macro definitions with one or more optional parameters
805 # into uninterpreted ERT insets
808 def revert_hyperlinktype(document):
809 'Reverts hyperlink type'
813 i = find_token(document.body, "target", i)
816 j = find_token(document.body, "type", i)
824 def revert_pagebreak(document):
825 'Reverts pagebreak to ERT'
828 i = find_token(document.body, "\\pagebreak", i)
831 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
832 '\\begin_layout Standard\n\n\n\\backslash\n' \
833 'pagebreak{}\n\\end_layout\n\n\\end_inset\n\n'
837 def revert_linebreak(document):
838 'Reverts linebreak to ERT'
841 i = find_token(document.body, "\\linebreak", i)
844 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
845 '\\begin_layout Standard\n\n\n\\backslash\n' \
846 'linebreak{}\n\\end_layout\n\n\\end_inset\n\n'
850 def revert_latin(document):
851 "Set language Latin to English"
853 if document.language == "latin":
854 document.language = "english"
855 i = find_token(document.header, "\\language", 0)
857 document.header[i] = "\\language english"
860 j = find_token(document.body, "\\lang latin", j)
863 document.body[j] = document.body[j].replace("\\lang latin", "\\lang english")
867 def revert_samin(document):
868 "Set language North Sami to English"
870 if document.language == "samin":
871 document.language = "english"
872 i = find_token(document.header, "\\language", 0)
874 document.header[i] = "\\language english"
877 j = find_token(document.body, "\\lang samin", j)
880 document.body[j] = document.body[j].replace("\\lang samin", "\\lang english")
884 def convert_serbocroatian(document):
885 "Set language Serbocroatian to Croatian as this was really Croatian in LyX 1.5"
887 if document.language == "serbocroatian":
888 document.language = "croatian"
889 i = find_token(document.header, "\\language", 0)
891 document.header[i] = "\\language croatian"
894 j = find_token(document.body, "\\lang serbocroatian", j)
897 document.body[j] = document.body[j].replace("\\lang serbocroatian", "\\lang croatian")
901 def convert_framed_notes(document):
902 "Convert framed notes to boxes. "
905 i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
909 document.body[i] = document.body[i].replace("\\begin_inset Note", "\\begin_inset Box")
910 document.body.insert(i + 1, 'position "t"\nhor_pos "c"\nhas_inner_box 0\ninner_pos "t"\n' \
911 'use_parbox 0\nwidth "100col%"\nspecial "none"\nheight "1in"\n' \
912 'height_special "totalheight"')
916 def revert_framed_notes(document):
917 "Revert framed boxes to notes. "
920 i = find_tokens(document.body, ["\\begin_inset Box Framed", "\\begin_inset Box Shaded"], i)
924 j = find_end_of_inset(document.body, i + 1)
927 document.warning("Malformed LyX document: Could not find end of Box inset.")
928 k = find_token(document.body, "status", i + 1, j)
930 document.warning("Malformed LyX document: Missing `status' tag in Box inset.")
932 status = document.body[k]
933 l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
935 document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Box inset.")
937 m = find_token(document.body, "\\end_layout", i + 1, j)
939 document.warning("Malformed LyX document: Missing `\\end_layout' in Box inset.")
941 ibox = find_token(document.body, "has_inner_box 1", i + 1, k)
942 pbox = find_token(document.body, "use_parbox 1", i + 1, k)
943 if ibox == -1 and pbox == -1:
944 document.body[i] = document.body[i].replace("\\begin_inset Box", "\\begin_inset Note")
945 del document.body[i+1:k]
947 document.body[i] = document.body[i].replace("\\begin_inset Box Shaded", "\\begin_inset Box Frameless")
948 document.body.insert(l + 1, "\\begin_inset Note Shaded\n" + status + "\n\\begin_layout Standard\n")
949 document.body.insert(m + 1, "\\end_layout\n\\end_inset")
953 def revert_slash(document):
954 'Revert \\SpecialChar \\slash{} to ERT'
955 for i in range(len(document.body)):
956 document.body[i] = document.body[i].replace('\\SpecialChar \\slash{}', \
957 '\\begin_inset ERT\nstatus collapsed\n\n' \
958 '\\begin_layout Standard\n\n\n\\backslash\n' \
959 'slash{}\n\\end_layout\n\n\\end_inset\n\n')
962 def revert_nobreakdash(document):
963 'Revert \\SpecialChar \\nobreakdash- to ERT'
965 for i in range(len(document.body)):
966 line = document.body[i]
967 r = re.compile(r'\\SpecialChar \\nobreakdash-')
971 document.body[i] = document.body[i].replace('\\SpecialChar \\nobreakdash-', \
972 '\\begin_inset ERT\nstatus collapsed\n\n' \
973 '\\begin_layout Standard\n\n\n\\backslash\n' \
974 'nobreakdash-\n\\end_layout\n\n\\end_inset\n\n')
977 j = find_token(document.header, "\\use_amsmath", 0)
979 document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
981 document.header[j] = "\\use_amsmath 2"
984 def revert_nocite_key(body, start, end):
985 'key "..." -> \nocite{...}'
986 for i in range(start, end):
987 if (body[i][0:5] == 'key "'):
988 body[i] = body[i].replace('key "', "\\backslash\nnocite{")
989 body[i] = body[i].replace('"', "}")
994 def revert_nocite(document):
995 "Revert LatexCommand nocite to ERT"
998 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1002 if (document.body[i] == "LatexCommand nocite"):
1003 j = find_end_of_inset(document.body, i + 1)
1005 #this should not happen
1006 document.warning("End of CommandInset citation not found in revert_nocite!")
1007 revert_nocite_key(document.body, i + 1, len(document.body))
1009 revert_nocite_key(document.body, i + 1, j)
1010 document.body[i-1] = "\\begin_inset ERT"
1011 document.body[i] = "status collapsed\n\n" \
1012 "\\begin_layout Standard"
1013 document.body.insert(j, "\\end_layout\n");
1017 def revert_btprintall(document):
1018 "Revert (non-bibtopic) btPrintAll option to ERT \nocite{*}"
1019 i = find_token(document.header, '\\use_bibtopic', 0)
1021 document.warning("Malformed lyx document: Missing '\\use_bibtopic'.")
1023 if get_value(document.header, '\\use_bibtopic', 0) == "false":
1025 while i < len(document.body):
1026 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1029 j = find_end_of_inset(document.body, i + 1)
1031 #this should not happen
1032 document.warning("End of CommandInset bibtex not found in revert_btprintall!")
1033 j = len(document.body)
1034 for k in range(i, j):
1035 if (document.body[k] == 'btprint "btPrintAll"'):
1036 del document.body[k]
1037 document.body.insert(i, "\\begin_inset ERT\n" \
1038 "status collapsed\n\n\\begin_layout Standard\n\n" \
1039 "\\backslash\nnocite{*}\n" \
1040 "\\end_layout\n\\end_inset\n")
1044 def revert_bahasam(document):
1045 "Set language Bahasa Malaysia to Bahasa Indonesia"
1047 if document.language == "bahasam":
1048 document.language = "bahasa"
1049 i = find_token(document.header, "\\language", 0)
1051 document.header[i] = "\\language bahasa"
1054 j = find_token(document.body, "\\lang bahasam", j)
1057 document.body[j] = document.body[j].replace("\\lang bahasam", "\\lang bahasa")
1061 def revert_interlingua(document):
1062 "Set language Interlingua to English"
1064 if document.language == "interlingua":
1065 document.language = "english"
1066 i = find_token(document.header, "\\language", 0)
1068 document.header[i] = "\\language english"
1071 j = find_token(document.body, "\\lang interlingua", j)
1074 document.body[j] = document.body[j].replace("\\lang interlingua", "\\lang english")
1078 def revert_serbianlatin(document):
1079 "Set language Serbian-Latin to Croatian"
1081 if document.language == "serbian-latin":
1082 document.language = "croatian"
1083 i = find_token(document.header, "\\language", 0)
1085 document.header[i] = "\\language croatian"
1088 j = find_token(document.body, "\\lang serbian-latin", j)
1091 document.body[j] = document.body[j].replace("\\lang serbian-latin", "\\lang croatian")
1099 supported_versions = ["1.6.0","1.6"]
1100 convert = [[277, [fix_wrong_tables]],
1101 [278, [close_begin_deeper]],
1102 [279, [long_charstyle_names]],
1103 [280, [axe_show_label]],
1106 [283, [convert_flex]],
1110 [287, [convert_wrapfig_options]],
1111 [288, [convert_inset_command]],
1112 [289, [convert_latexcommand_index]],
1117 [294, [convert_pdf_options]],
1118 [295, [convert_htmlurl, convert_url]],
1119 [296, [convert_include]],
1120 [297, [convert_usorbian]],
1126 [303, [convert_serbocroatian]],
1127 [304, [convert_framed_notes]],
1134 [311, [convert_ams_classes]]
1137 revert = [[310, []],
1138 [309, [revert_btprintall]],
1139 [308, [revert_nocite]],
1140 [307, [revert_serbianlatin]],
1141 [306, [revert_slash, revert_nobreakdash]],
1142 [305, [revert_interlingua]],
1143 [304, [revert_bahasam]],
1144 [303, [revert_framed_notes]],
1146 [301, [revert_latin, revert_samin]],
1147 [300, [revert_linebreak]],
1148 [299, [revert_pagebreak]],
1149 [298, [revert_hyperlinktype]],
1150 [297, [revert_macro_optional_params]],
1151 [296, [revert_albanian, revert_lowersorbian, revert_uppersorbian]],
1152 [295, [revert_include]],
1153 [294, [revert_href]],
1154 [293, [revert_pdf_options_2]],
1155 [292, [revert_inset_info]],
1156 [291, [revert_japanese, revert_japanese_encoding]],
1157 [290, [revert_vietnamese]],
1158 [289, [revert_wraptable]],
1159 [288, [revert_latexcommand_index]],
1160 [287, [revert_inset_command]],
1161 [286, [revert_wrapfig_options]],
1162 [285, [revert_pdf_options]],
1163 [284, [remove_inzip_options]],
1165 [282, [revert_flex]],
1167 [280, [revert_begin_modules]],
1168 [279, [revert_show_label]],
1169 [278, [revert_long_charstyle_names]],
1175 if __name__ == "__main__":