1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2008 The LyX Team <lyx-devel@lists.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, get_value_string
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 def add_to_preamble(document, text):
40 """ Add text to the preamble if it is not already there.
41 Only the first line is checked!"""
43 if find_token(document.preamble, text[0], 0) != -1:
46 document.preamble.extend(text)
48 # Convert a LyX length into a LaTeX length
50 units = {"text%":"\\backslash\ntextwidth", "col%":"\\backslash\ncolumnwidth",
51 "page%":"\\backslash\npagewidth", "line%":"\\backslash\nlinewidth",
52 "theight%":"\\backslash\ntextheight", "pheight%":"\\backslash\npageheight"}
54 # Convert LyX units to LaTeX units
55 for unit in units.keys():
56 if len.find(unit) != -1:
57 len = '%f' % (len2value(len) / 100)
58 len = len.strip('0') + units[unit]
63 # Return the value of len without the unit in numerical form.
65 result = re.search('([+-]?[0-9.]+)', len)
67 return float(result.group(1))
71 # Unfortunately, this doesn't really work, since Standard isn't always default.
72 # But it's as good as we can do right now.
73 def find_default_layout(document, start, end):
74 l = find_token(document.body, "\\begin_layout Standard", start, end)
76 l = find_token(document.body, "\\begin_layout PlainLayout", start, end)
78 l = find_token(document.body, "\\begin_layout Plain Layout", start, end)
81 def get_option(document, m, option, default):
82 l = document.body[m].find(option)
85 val = document.body[m][l:].split('"')[1]
88 def remove_option(document, m, option):
89 l = document.body[m].find(option)
91 val = document.body[m][l:].split('"')[1]
92 document.body[m] = document.body[m][:l-1] + document.body[m][l+len(option + '="' + val + '"'):]
95 def set_option(document, m, option, value):
96 l = document.body[m].find(option)
98 oldval = document.body[m][l:].split('"')[1]
99 l = l + len(option + '="')
100 document.body[m] = document.body[m][:l] + value + document.body[m][l+len(oldval):]
102 document.body[m] = document.body[m][:-1] + ' ' + option + '="' + value + '">'
106 ####################################################################
108 def convert_ltcaption(document):
111 i = find_token(document.body, "\\begin_inset Tabular", i)
114 j = find_end_of_inset(document.body, i + 1)
116 document.warning("Malformed LyX document: Could not find end of tabular.")
119 nrows = int(document.body[i+1].split('"')[3])
120 ncols = int(document.body[i+1].split('"')[5])
123 for k in range(nrows):
124 m = find_token(document.body, "<row", m)
127 for k in range(ncols):
128 m = find_token(document.body, "<cell", m)
130 mend = find_token(document.body, "</cell>", m + 1)
131 # first look for caption insets
132 mcap = find_token(document.body, "\\begin_inset Caption", m + 1, mend)
133 # then look for ERT captions
135 mcap = find_token(document.body, "caption", m + 1, mend)
137 mcap = find_token(document.body, "\\backslash", mcap - 1, mcap)
140 if caption == 'true':
142 set_option(document, r, 'caption', 'true')
143 set_option(document, m, 'multicolumn', '1')
144 set_option(document, m, 'bottomline', 'false')
145 set_option(document, m, 'topline', 'false')
146 set_option(document, m, 'rightline', 'false')
147 set_option(document, m, 'leftline', 'false')
148 #j = find_end_of_inset(document.body, j + 1)
150 set_option(document, m, 'multicolumn', '2')
156 def revert_ltcaption(document):
159 i = find_token(document.body, "\\begin_inset Tabular", i)
162 j = find_end_of_inset(document.body, i + 1)
164 document.warning("Malformed LyX document: Could not find end of tabular.")
168 nrows = int(document.body[i+1].split('"')[3])
169 ncols = int(document.body[i+1].split('"')[5])
171 for k in range(nrows):
172 m = find_token(document.body, "<row", m)
173 caption = get_option(document, m, 'caption', 'false')
174 if caption == 'true':
175 remove_option(document, m, 'caption')
176 for k in range(ncols):
177 m = find_token(document.body, "<cell", m)
178 remove_option(document, m, 'multicolumn')
180 m = find_token(document.body, "\\begin_inset Caption", m)
183 m = find_end_of_inset(document.body, m + 1)
184 document.body[m] += wrap_into_ert("","","\\backslash\n\\backslash\n%")
190 def convert_tablines(document):
193 i = find_token(document.body, "\\begin_inset Tabular", i)
195 # LyX 1.3 inserted an extra space between \begin_inset
196 # and Tabular so let us try if this is the case and fix it.
197 i = find_token(document.body, "\\begin_inset Tabular", i)
201 document.body[i] = "\\begin_inset Tabular"
202 j = find_end_of_inset(document.body, i + 1)
204 document.warning("Malformed LyX document: Could not find end of tabular.")
208 nrows = int(document.body[i+1].split('"')[3])
209 ncols = int(document.body[i+1].split('"')[5])
212 for k in range(ncols):
213 m = find_token(document.body, "<column", m)
214 left = get_option(document, m, 'leftline', 'false')
215 right = get_option(document, m, 'rightline', 'false')
216 col_info.append([left, right])
217 remove_option(document, m, 'leftline')
218 remove_option(document, m, 'rightline')
222 for k in range(nrows):
223 m = find_token(document.body, "<row", m)
224 top = get_option(document, m, 'topline', 'false')
225 bottom = get_option(document, m, 'bottomline', 'false')
226 row_info.append([top, bottom])
227 remove_option(document, m, 'topline')
228 remove_option(document, m, 'bottomline')
233 for k in range(nrows*ncols):
234 m = find_token(document.body, "<cell", m)
235 mc_info.append(get_option(document, m, 'multicolumn', '0'))
238 for l in range(nrows):
239 for k in range(ncols):
240 m = find_token(document.body, '<cell', m)
241 if mc_info[l*ncols + k] == '0':
242 r = set_option(document, m, 'topline', row_info[l][0])
243 r = set_option(document, m, 'bottomline', row_info[l][1])
244 r = set_option(document, m, 'leftline', col_info[k][0])
245 r = set_option(document, m, 'rightline', col_info[k][1])
246 elif mc_info[l*ncols + k] == '1':
248 while s < ncols and mc_info[l*ncols + s] == '2':
250 if s < ncols and mc_info[l*ncols + s] != '1':
251 r = set_option(document, m, 'rightline', col_info[k][1])
252 if k > 0 and mc_info[l*ncols + k - 1] == '0':
253 r = set_option(document, m, 'leftline', col_info[k][0])
258 def revert_tablines(document):
261 i = find_token(document.body, "\\begin_inset Tabular", i)
264 j = find_end_of_inset(document.body, i + 1)
266 document.warning("Malformed LyX document: Could not find end of tabular.")
270 nrows = int(document.body[i+1].split('"')[3])
271 ncols = int(document.body[i+1].split('"')[5])
274 for k in range(nrows*ncols):
275 m = find_token(document.body, "<cell", m)
276 top = get_option(document, m, 'topline', 'false')
277 bottom = get_option(document, m, 'bottomline', 'false')
278 left = get_option(document, m, 'leftline', 'false')
279 right = get_option(document, m, 'rightline', 'false')
280 lines.append([top, bottom, left, right])
283 # we will want to ignore longtable captions
286 for k in range(nrows):
287 m = find_token(document.body, "<row", m)
288 caption = get_option(document, m, 'caption', 'false')
289 caption_info.append([caption])
294 for k in range(ncols):
295 m = find_token(document.body, "<column", m)
297 for l in range(nrows):
298 left = lines[l*ncols + k][2]
299 if left == 'false' and caption_info[l] == 'false':
301 set_option(document, m, 'leftline', left)
303 for l in range(nrows):
304 right = lines[l*ncols + k][3]
305 if right == 'false' and caption_info[l] == 'false':
307 set_option(document, m, 'rightline', right)
311 for k in range(nrows):
312 m = find_token(document.body, "<row", m)
314 for l in range(ncols):
315 top = lines[k*ncols + l][0]
318 if caption_info[k] == 'false':
320 set_option(document, m, 'topline', top)
322 for l in range(ncols):
323 bottom = lines[k*ncols + l][1]
324 if bottom == 'false':
326 if caption_info[k] == 'false':
328 set_option(document, m, 'bottomline', bottom)
334 def fix_wrong_tables(document):
337 i = find_token(document.body, "\\begin_inset Tabular", i)
340 j = find_end_of_inset(document.body, i + 1)
342 document.warning("Malformed LyX document: Could not find end of tabular.")
346 nrows = int(document.body[i+1].split('"')[3])
347 ncols = int(document.body[i+1].split('"')[5])
349 for l in range(nrows):
351 for k in range(ncols):
352 m = find_token(document.body, '<cell', m)
354 if document.body[m].find('multicolumn') != -1:
355 multicol_cont = int(document.body[m].split('"')[1])
357 if multicol_cont == 2 and (k == 0 or prev_multicolumn == 0):
358 document.body[m] = document.body[m][:5] + document.body[m][21:]
361 prev_multicolumn = multicol_cont
368 def close_begin_deeper(document):
372 i = find_tokens(document.body, ["\\begin_deeper", "\\end_deeper"], i)
377 if document.body[i][:13] == "\\begin_deeper":
384 document.body[-2:-2] = ['\\end_deeper' for i in range(depth)]
387 def long_charstyle_names(document):
390 i = find_token(document.body, "\\begin_inset CharStyle", i)
393 document.body[i] = document.body[i].replace("CharStyle ", "CharStyle CharStyle:")
396 def revert_long_charstyle_names(document):
399 i = find_token(document.body, "\\begin_inset CharStyle", i)
402 document.body[i] = document.body[i].replace("CharStyle CharStyle:", "CharStyle")
406 def axe_show_label(document):
409 i = find_token(document.body, "\\begin_inset CharStyle", i)
412 if document.body[i + 1].find("show_label") != -1:
413 if document.body[i + 1].find("true") != -1:
414 document.body[i + 1] = "status open"
415 del document.body[ i + 2]
417 if document.body[i + 1].find("false") != -1:
418 document.body[i + 1] = "status collapsed"
419 del document.body[ i + 2]
421 document.warning("Malformed LyX document: show_label neither false nor true.")
423 document.warning("Malformed LyX document: show_label missing in CharStyle.")
428 def revert_show_label(document):
431 i = find_token(document.body, "\\begin_inset CharStyle", i)
434 if document.body[i + 1].find("status open") != -1:
435 document.body.insert(i + 1, "show_label true")
437 if document.body[i + 1].find("status collapsed") != -1:
438 document.body.insert(i + 1, "show_label false")
440 document.warning("Malformed LyX document: no legal status line in CharStyle.")
443 def revert_begin_modules(document):
446 i = find_token(document.header, "\\begin_modules", i)
449 j = find_end_of(document.header, i, "\\begin_modules", "\\end_modules")
451 # this should not happen
453 document.header[i : j + 1] = []
455 def convert_flex(document):
456 "Convert CharStyle to Flex"
459 i = find_token(document.body, "\\begin_inset CharStyle", i)
462 document.body[i] = document.body[i].replace('\\begin_inset CharStyle', '\\begin_inset Flex')
464 def revert_flex(document):
465 "Convert Flex to CharStyle"
468 i = find_token(document.body, "\\begin_inset Flex", i)
471 document.body[i] = document.body[i].replace('\\begin_inset Flex', '\\begin_inset CharStyle')
474 # Discard PDF options for hyperref
475 def revert_pdf_options(document):
476 "Revert PDF options for hyperref."
477 # store the PDF options and delete the entries from the Lyx file
485 bookmarksnumbered = ""
487 bookmarksopenlevel = ""
495 i = find_token(document.header, "\\use_hyperref", i)
497 hyperref = get_value(document.header, "\\use_hyperref", i) == 'true'
498 del document.header[i]
499 i = find_token(document.header, "\\pdf_store_options", i)
501 del document.header[i]
502 i = find_token(document.header, "\\pdf_title", 0)
504 title = get_value_string(document.header, '\\pdf_title', 0, 0, True)
505 title = ' pdftitle={' + title + '}'
506 del document.header[i]
507 i = find_token(document.header, "\\pdf_author", 0)
509 author = get_value_string(document.header, '\\pdf_author', 0, 0, True)
511 author = ' pdfauthor={' + author + '}'
513 author = ',\n pdfauthor={' + author + '}'
514 del document.header[i]
515 i = find_token(document.header, "\\pdf_subject", 0)
517 subject = get_value_string(document.header, '\\pdf_subject', 0, 0, True)
518 if title == "" and author == "":
519 subject = ' pdfsubject={' + subject + '}'
521 subject = ',\n pdfsubject={' + subject + '}'
522 del document.header[i]
523 i = find_token(document.header, "\\pdf_keywords", 0)
525 keywords = get_value_string(document.header, '\\pdf_keywords', 0, 0, True)
526 if title == "" and author == "" and subject == "":
527 keywords = ' pdfkeywords={' + keywords + '}'
529 keywords = ',\n pdfkeywords={' + keywords + '}'
530 del document.header[i]
531 i = find_token(document.header, "\\pdf_bookmarks", 0)
533 bookmarks = get_value_string(document.header, '\\pdf_bookmarks', 0)
534 bookmarks = ',\n bookmarks=' + bookmarks
535 del document.header[i]
536 i = find_token(document.header, "\\pdf_bookmarksnumbered", i)
538 bookmarksnumbered = get_value_string(document.header, '\\pdf_bookmarksnumbered', 0)
539 bookmarksnumbered = ',\n bookmarksnumbered=' + bookmarksnumbered
540 del document.header[i]
541 i = find_token(document.header, "\\pdf_bookmarksopen", i)
543 bookmarksopen = get_value_string(document.header, '\\pdf_bookmarksopen', 0)
544 bookmarksopen = ',\n bookmarksopen=' + bookmarksopen
545 del document.header[i]
546 i = find_token(document.header, "\\pdf_bookmarksopenlevel", i)
548 bookmarksopenlevel = get_value_string(document.header, '\\pdf_bookmarksopenlevel', 0, 0, True)
549 bookmarksopenlevel = ',\n bookmarksopenlevel=' + bookmarksopenlevel
550 del document.header[i]
551 i = find_token(document.header, "\\pdf_breaklinks", i)
553 breaklinks = get_value_string(document.header, '\\pdf_breaklinks', 0)
554 breaklinks = ',\n breaklinks=' + breaklinks
555 del document.header[i]
556 i = find_token(document.header, "\\pdf_pdfborder", i)
558 pdfborder = get_value_string(document.header, '\\pdf_pdfborder', 0)
559 if pdfborder == 'true':
560 pdfborder = ',\n pdfborder={0 0 0}'
562 pdfborder = ',\n pdfborder={0 0 1}'
563 del document.header[i]
564 i = find_token(document.header, "\\pdf_colorlinks", i)
566 colorlinks = get_value_string(document.header, '\\pdf_colorlinks', 0)
567 colorlinks = ',\n colorlinks=' + colorlinks
568 del document.header[i]
569 i = find_token(document.header, "\\pdf_backref", i)
571 backref = get_value_string(document.header, '\\pdf_backref', 0)
572 backref = ',\n backref=' + backref
573 del document.header[i]
574 i = find_token(document.header, "\\pdf_pagebackref", i)
576 pagebackref = get_value_string(document.header, '\\pdf_pagebackref', 0)
577 pagebackref = ',\n pagebackref=' + pagebackref
578 del document.header[i]
579 i = find_token(document.header, "\\pdf_pagemode", 0)
581 pagemode = get_value_string(document.header, '\\pdf_pagemode', 0)
582 pagemode = ',\n pdfpagemode=' + pagemode
583 del document.header[i]
584 i = find_token(document.header, "\\pdf_quoted_options", 0)
586 otheroptions = get_value_string(document.header, '\\pdf_quoted_options', 0, 0, True)
587 if title == "" and author == "" and subject == "" and keywords == "":
588 otheroptions = ' ' + otheroptions
590 otheroptions = ',\n ' + otheroptions
591 del document.header[i]
593 # write to the preamble when hyperref was used
595 # preamble write preparations
596 # bookmark numbers are only output when they are turned on
597 if bookmarksopen == ',\n bookmarksopen=true':
598 bookmarksopen = bookmarksopen + bookmarksopenlevel
599 if bookmarks == ',\n bookmarks=true':
600 bookmarks = bookmarks + bookmarksnumbered + bookmarksopen
602 bookmarks = bookmarks
603 # hypersetup is only output when there are things to be set up
604 setupstart = '\\hypersetup{%\n'
606 if otheroptions == "" and title == "" and author == ""\
607 and subject == "" and keywords == "":
611 add_to_preamble(document,
612 ['% Commands inserted by lyx2lyx for PDF properties',
613 '\\usepackage[unicode=true'
632 def remove_inzip_options(document):
633 "Remove inzipName and embed options from the Graphics inset"
636 i = find_token(document.body, "\\begin_inset Graphics", i)
639 j = find_end_of_inset(document.body, i + 1)
642 document.warning("Malformed LyX document: Could not find end of graphics inset.")
643 # If there's a inzip param, just remove that
644 k = find_token(document.body, "\tinzipName", i + 1, j)
647 # embed option must follow the inzipName option
648 del document.body[k+1]
652 def convert_inset_command(document):
655 \begin_inset LatexCommand cmd
657 \begin_inset CommandInset InsetType
662 i = find_token(document.body, "\\begin_inset LatexCommand", i)
665 line = document.body[i]
666 r = re.compile(r'\\begin_inset LatexCommand (.*)$')
670 #this is adapted from factory.cpp
671 if cmdName[0:4].lower() == "cite":
672 insetName = "citation"
673 elif cmdName == "url" or cmdName == "htmlurl":
675 elif cmdName[-3:] == "ref":
677 elif cmdName == "tableofcontents":
679 elif cmdName == "printnomenclature":
680 insetName = "nomencl_print"
681 elif cmdName == "printindex":
682 insetName = "index_print"
685 insertion = ["\\begin_inset CommandInset " + insetName, "LatexCommand " + cmdName]
686 document.body[i : i+1] = insertion
689 def revert_inset_command(document):
692 \begin_inset CommandInset InsetType
695 \begin_inset LatexCommand cmd
696 Some insets may end up being converted to insets earlier versions of LyX
697 will not be able to recognize. Not sure what to do about that.
701 i = find_token(document.body, "\\begin_inset CommandInset", i)
704 nextline = document.body[i+1]
705 r = re.compile(r'LatexCommand\s+(.*)$')
706 m = r.match(nextline)
708 document.warning("Malformed LyX document: Missing LatexCommand in " + document.body[i] + ".")
711 insertion = ["\\begin_inset LatexCommand " + cmdName]
712 document.body[i : i+2] = insertion
715 def convert_wrapfig_options(document):
716 "Convert optional options for wrap floats (wrapfig)."
717 # adds the tokens "lines", "placement", and "overhang"
720 i = find_token(document.body, "\\begin_inset Wrap figure", i)
723 document.body.insert(i + 1, "lines 0")
724 j = find_token(document.body, "placement", i)
725 # placement can be already set or not; if not, set it
727 document.body.insert(i + 3, "overhang 0col%")
729 document.body.insert(i + 2, "placement o")
730 document.body.insert(i + 3, "overhang 0col%")
734 def revert_wrapfig_options(document):
735 "Revert optional options for wrap floats (wrapfig)."
738 i = find_token(document.body, "lines", i)
741 j = find_token(document.body, "overhang", i+1)
742 if j != i + 2 and j != -1:
743 document.warning("Malformed LyX document: Couldn't find overhang parameter of wrap float.")
747 del document.body[j-1]
751 def convert_latexcommand_index(document):
752 "Convert from LatexCommand form to collapsable form."
755 i = find_token(document.body, "\\begin_inset CommandInset index", i)
758 if document.body[i + 1] != "LatexCommand index": # Might also be index_print
760 fullcontent = document.body[i + 2][6:].strip('"')
761 document.body[i:i + 2] = ["\\begin_inset Index",
763 "\\begin_layout Standard"]
764 # Put here the conversions needed from LaTeX string to LyXText.
765 # Here we do a minimal conversion to prevent crashes and data loss.
766 # Manual patch-up may be needed.
767 # Umlauted characters (most common ones, can be extended):
768 fullcontent = fullcontent.replace(r'\\\"a', u'ä').replace(r'\\\"o', u'ö').replace(r'\\\"u', u'ü')
770 fullcontent = wrap_into_ert(fullcontent, r'\"', '"')
771 #fullcontent = fullcontent.replace(r'\"', '\n\\begin_inset ERT\nstatus collapsed\n\\begin_layout standard\n"\n\\end_layout\n\\end_inset\n')
773 r = re.compile('^(.*?)(\$.*?\$)(.*)')
778 f = m.group(2).replace('\\\\', '\\')
782 s = wrap_into_ert(s, r'\\', '\\backslash')
783 s = wrap_into_ert(s, '{', '{')
784 s = wrap_into_ert(s, '}', '}')
785 document.body.insert(i + 3, s)
787 document.body.insert(i + 3, "\\begin_inset Formula " + f)
788 document.body.insert(i + 4, "\\end_inset")
790 # Generic, \\ -> \backslash:
791 g = wrap_into_ert(g, r'\\', '\\backslash')
792 g = wrap_into_ert(g, '{', '{')
793 g = wrap_into_ert(g, '}', '}')
794 document.body.insert(i + 3, g)
795 document.body[i + 4] = "\\end_layout"
799 def revert_latexcommand_index(document):
800 "Revert from collapsable form to LatexCommand form."
803 i = find_token(document.body, "\\begin_inset Index", i)
806 j = find_end_of_inset(document.body, i + 1)
809 del document.body[j - 1]
810 del document.body[j - 2] # \end_layout
811 document.body[i] = "\\begin_inset CommandInset index"
812 document.body[i + 1] = "LatexCommand index"
813 # clean up multiline stuff
816 for k in range(i + 3, j - 2):
817 line = document.body[k]
818 if line.startswith("\\begin_inset ERT"):
819 ert_end = find_end_of_inset(document.body, k + 1)
821 if line.startswith("\\begin_inset Formula"):
823 if line.startswith("\\begin_layout Standard"):
825 if line.startswith("\\begin_layout Plain Layout"):
827 if line.startswith("\\end_layout"):
829 if line.startswith("\\end_inset"):
831 if line.startswith("status collapsed"):
833 if line.startswith("status open"):
835 # a lossless reversion is not possible
836 # try at least to handle some common insets and settings
837 # do not replace inside ERTs
839 line = line.replace(u'ä', r'\\\"a').replace(u'ö', r'\\\"o').replace(u'ü', r'\\\"u')
840 line = line.replace(r'\backslash', r'\textbackslash{}')
841 line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
842 line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
843 line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
844 line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
845 line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
846 line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
847 line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
848 line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
849 line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
851 line = line.replace(r'\backslash', r'\\')
852 content = content + line;
853 document.body[i + 3] = "name " + '"' + content + '"'
854 for k in range(i + 4, j - 2):
855 del document.body[i + 4]
856 document.body.insert(i + 4, "")
857 del document.body[i + 2] # \begin_layout standard
861 def revert_wraptable(document):
862 "Revert wrap table to wrap figure."
865 i = find_token(document.body, "\\begin_inset Wrap table", i)
868 document.body[i] = document.body[i].replace('\\begin_inset Wrap table', '\\begin_inset Wrap figure')
872 def revert_vietnamese(document):
873 "Set language Vietnamese to English"
874 # Set document language from Vietnamese to English
876 if document.language == "vietnamese":
877 document.language = "english"
878 i = find_token(document.header, "\\language", 0)
880 document.header[i] = "\\language english"
883 j = find_token(document.body, "\\lang vietnamese", j)
886 document.body[j] = document.body[j].replace("\\lang vietnamese", "\\lang english")
890 def revert_japanese(document):
891 "Set language japanese-plain to japanese"
892 # Set document language from japanese-plain to japanese
894 if document.language == "japanese-plain":
895 document.language = "japanese"
896 i = find_token(document.header, "\\language", 0)
898 document.header[i] = "\\language japanese"
901 j = find_token(document.body, "\\lang japanese-plain", j)
904 document.body[j] = document.body[j].replace("\\lang japanese-plain", "\\lang japanese")
908 def revert_japanese_encoding(document):
909 "Set input encoding form EUC-JP-plain to EUC-JP etc."
910 # Set input encoding form EUC-JP-plain to EUC-JP etc.
912 i = find_token(document.header, "\\inputencoding EUC-JP-plain", 0)
914 document.header[i] = "\\inputencoding EUC-JP"
916 j = find_token(document.header, "\\inputencoding JIS-plain", 0)
918 document.header[j] = "\\inputencoding JIS"
920 k = find_token(document.header, "\\inputencoding SJIS-plain", 0)
921 if k != -1: # convert to UTF8 since there is currently no SJIS encoding
922 document.header[k] = "\\inputencoding UTF8"
925 def revert_inset_info(document):
926 'Replace info inset with its content'
929 i = find_token(document.body, '\\begin_inset Info', i)
932 j = find_end_of_inset(document.body, i + 1)
935 document.warning("Malformed LyX document: Could not find end of Info inset.")
938 for k in range(i, j+1):
939 if document.body[k].startswith("arg"):
940 arg = document.body[k][3:].strip().strip('"')
941 if document.body[k].startswith("type"):
942 type = document.body[k][4:].strip().strip('"')
943 # I think there is a newline after \\end_inset, which should be removed.
944 if document.body[j + 1].strip() == "":
945 document.body[i : (j + 2)] = [type + ':' + arg]
947 document.body[i : (j + 1)] = [type + ':' + arg]
950 def convert_pdf_options(document):
951 # Set the pdfusetitle tag, delete the pdf_store_options,
952 # set quotes for bookmarksopenlevel"
953 has_hr = get_value(document.header, "\\use_hyperref", 0, default = "0")
955 k = find_token(document.header, "\\use_hyperref", 0)
956 document.header.insert(k + 1, "\\pdf_pdfusetitle true")
957 k = find_token(document.header, "\\pdf_store_options", 0)
959 del document.header[k]
960 i = find_token(document.header, "\\pdf_bookmarksopenlevel", k)
962 document.header[i] = document.header[i].replace('"', '')
965 def revert_pdf_options_2(document):
966 # reset the pdfusetitle tag, set quotes for bookmarksopenlevel"
967 k = find_token(document.header, "\\use_hyperref", 0)
968 i = find_token(document.header, "\\pdf_pdfusetitle", k)
970 del document.header[i]
971 i = find_token(document.header, "\\pdf_bookmarksopenlevel", k)
973 values = document.header[i].split()
974 values[1] = ' "' + values[1] + '"'
975 document.header[i] = ''.join(values)
978 def convert_htmlurl(document):
979 'Convert "htmlurl" to "href" insets for docbook'
980 if document.backend != "docbook":
984 i = find_token(document.body, "\\begin_inset CommandInset url", i)
987 document.body[i] = "\\begin_inset CommandInset href"
988 document.body[i + 1] = "LatexCommand href"
992 def convert_url(document):
993 'Convert url insets to url charstyles'
994 if document.backend == "docbook":
998 i = find_token(document.body, "\\begin_inset CommandInset url", i)
1001 n = find_token(document.body, "name", i)
1003 # place the URL name in typewriter before the new URL insert
1004 # grab the name 'bla' from the e.g. the line 'name "bla"',
1005 # therefore start with the 6th character
1006 name = document.body[n][6:-1]
1007 newname = [name + " "]
1008 document.body[i:i] = newname
1010 j = find_token(document.body, "target", i)
1012 document.warning("Malformed LyX document: Can't find target for url inset")
1015 target = document.body[j][8:-1]
1016 k = find_token(document.body, "\\end_inset", j)
1018 document.warning("Malformed LyX document: Can't find end of url inset")
1021 newstuff = ["\\begin_inset Flex URL",
1022 "status collapsed", "",
1023 "\\begin_layout Standard",
1028 document.body[i:k] = newstuff
1031 def convert_ams_classes(document):
1032 tc = document.textclass
1033 if (tc != "amsart" and tc != "amsart-plain" and
1034 tc != "amsart-seq" and tc != "amsbook"):
1036 if tc == "amsart-plain":
1037 document.textclass = "amsart"
1038 document.set_textclass()
1039 document.add_module("Theorems (Starred)")
1041 if tc == "amsart-seq":
1042 document.textclass = "amsart"
1043 document.set_textclass()
1044 document.add_module("Theorems (AMS)")
1046 #Now we want to see if any of the environments in the extended theorems
1047 #module were used in this document. If so, we'll add that module, too.
1048 layouts = ["Criterion", "Algorithm", "Axiom", "Condition", "Note", \
1049 "Notation", "Summary", "Acknowledgement", "Conclusion", "Fact", \
1052 r = re.compile(r'^\\begin_layout (.*?)\*?\s*$')
1055 i = find_token(document.body, "\\begin_layout", i)
1058 m = r.match(document.body[i])
1060 document.warning("Weirdly formed \\begin_layout at line %d of body!" % i)
1064 if layouts.count(m) != 0:
1065 document.add_module("Theorems (AMS-Extended)")
1069 def revert_href(document):
1070 'Reverts hyperlink insets (href) to url insets (url)'
1073 i = find_token(document.body, "\\begin_inset CommandInset href", i)
1076 document.body[i : i + 2] = \
1077 ["\\begin_inset CommandInset url", "LatexCommand url"]
1081 def convert_include(document):
1082 'Converts include insets to new format.'
1084 r = re.compile(r'\\begin_inset Include\s+\\([^{]+){([^}]*)}(?:\[(.*)\])?')
1086 i = find_token(document.body, "\\begin_inset Include", i)
1089 line = document.body[i]
1090 previewline = document.body[i + 1]
1093 document.warning("Unable to match line " + str(i) + " of body!")
1099 insertion = ["\\begin_inset CommandInset include",
1100 "LatexCommand " + cmd, previewline,
1101 "filename \"" + fn + "\""]
1104 insertion.append("lstparams " + '"' + opt + '"')
1106 document.body[i : i + 2] = insertion
1110 def revert_include(document):
1111 'Reverts include insets to old format.'
1113 r1 = re.compile('LatexCommand (.+)')
1114 r2 = re.compile('filename (.+)')
1115 r3 = re.compile('options (.*)')
1117 i = find_token(document.body, "\\begin_inset CommandInset include", i)
1120 previewline = document.body[i + 1]
1121 m = r1.match(document.body[i + 2])
1123 document.warning("Malformed LyX document: No LatexCommand line for `" +
1124 document.body[i] + "' on line " + str(i) + ".")
1128 m = r2.match(document.body[i + 3])
1130 document.warning("Malformed LyX document: No filename line for `" + \
1131 document.body[i] + "' on line " + str(i) + ".")
1137 if (cmd == "lstinputlisting"):
1138 m = r3.match(document.body[i + 4])
1140 options = m.group(1)
1142 newline = "\\begin_inset Include \\" + cmd + "{" + fn + "}"
1144 newline += ("[" + options + "]")
1145 insertion = [newline, previewline]
1146 document.body[i : i + numlines] = insertion
1150 def revert_albanian(document):
1151 "Set language Albanian to English"
1153 if document.language == "albanian":
1154 document.language = "english"
1155 i = find_token(document.header, "\\language", 0)
1157 document.header[i] = "\\language english"
1160 j = find_token(document.body, "\\lang albanian", j)
1163 document.body[j] = document.body[j].replace("\\lang albanian", "\\lang english")
1167 def revert_lowersorbian(document):
1168 "Set language lower Sorbian to English"
1170 if document.language == "lowersorbian":
1171 document.language = "english"
1172 i = find_token(document.header, "\\language", 0)
1174 document.header[i] = "\\language english"
1177 j = find_token(document.body, "\\lang lowersorbian", j)
1180 document.body[j] = document.body[j].replace("\\lang lowersorbian", "\\lang english")
1184 def revert_uppersorbian(document):
1185 "Set language uppersorbian to usorbian as this was used in LyX 1.5"
1187 if document.language == "uppersorbian":
1188 document.language = "usorbian"
1189 i = find_token(document.header, "\\language", 0)
1191 document.header[i] = "\\language usorbian"
1194 j = find_token(document.body, "\\lang uppersorbian", j)
1197 document.body[j] = document.body[j].replace("\\lang uppersorbian", "\\lang usorbian")
1201 def convert_usorbian(document):
1202 "Set language usorbian to uppersorbian"
1204 if document.language == "usorbian":
1205 document.language = "uppersorbian"
1206 i = find_token(document.header, "\\language", 0)
1208 document.header[i] = "\\language uppersorbian"
1211 j = find_token(document.body, "\\lang usorbian", j)
1214 document.body[j] = document.body[j].replace("\\lang usorbian", "\\lang uppersorbian")
1218 def revert_macro_optional_params(document):
1219 "Convert macro definitions with optional parameters into ERTs"
1220 # Stub to convert macro definitions with one or more optional parameters
1221 # into uninterpreted ERT insets
1224 def revert_hyperlinktype(document):
1225 'Reverts hyperlink type'
1229 i = find_token(document.body, "target", i)
1232 j = find_token(document.body, "type", i)
1236 del document.body[j]
1240 def revert_pagebreak(document):
1241 'Reverts pagebreak to ERT'
1244 i = find_token(document.body, "\\pagebreak", i)
1247 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
1248 '\\begin_layout Standard\n\n\n\\backslash\n' \
1249 'pagebreak{}\n\\end_layout\n\n\\end_inset\n\n'
1253 def revert_linebreak(document):
1254 'Reverts linebreak to ERT'
1257 i = find_token(document.body, "\\linebreak", i)
1260 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
1261 '\\begin_layout Standard\n\n\n\\backslash\n' \
1262 'linebreak{}\n\\end_layout\n\n\\end_inset\n\n'
1266 def revert_latin(document):
1267 "Set language Latin to English"
1269 if document.language == "latin":
1270 document.language = "english"
1271 i = find_token(document.header, "\\language", 0)
1273 document.header[i] = "\\language english"
1276 j = find_token(document.body, "\\lang latin", j)
1279 document.body[j] = document.body[j].replace("\\lang latin", "\\lang english")
1283 def revert_samin(document):
1284 "Set language North Sami to English"
1286 if document.language == "samin":
1287 document.language = "english"
1288 i = find_token(document.header, "\\language", 0)
1290 document.header[i] = "\\language english"
1293 j = find_token(document.body, "\\lang samin", j)
1296 document.body[j] = document.body[j].replace("\\lang samin", "\\lang english")
1300 def convert_serbocroatian(document):
1301 "Set language Serbocroatian to Croatian as this was really Croatian in LyX 1.5"
1303 if document.language == "serbocroatian":
1304 document.language = "croatian"
1305 i = find_token(document.header, "\\language", 0)
1307 document.header[i] = "\\language croatian"
1310 j = find_token(document.body, "\\lang serbocroatian", j)
1313 document.body[j] = document.body[j].replace("\\lang serbocroatian", "\\lang croatian")
1317 def convert_framed_notes(document):
1318 "Convert framed notes to boxes. "
1321 i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
1325 document.body[i] = document.body[i].replace("\\begin_inset Note", "\\begin_inset Box")
1326 document.body.insert(i + 1, 'position "t"\nhor_pos "c"\nhas_inner_box 0\ninner_pos "t"\n' \
1327 'use_parbox 0\nwidth "100col%"\nspecial "none"\nheight "1in"\n' \
1328 'height_special "totalheight"')
1332 def convert_module_names(document):
1333 modulemap = { 'Braille' : 'braille', 'Endnote' : 'endnotes', 'Foot to End' : 'foottoend',\
1334 'Hanging' : 'hanging', 'Linguistics' : 'linguistics', 'Logical Markup' : 'logicalmkup', \
1335 'Theorems (AMS-Extended)' : 'theorems-ams-extended', 'Theorems (AMS)' : 'theorems-ams', \
1336 'Theorems (Order By Chapter)' : 'theorems-chap', 'Theorems (Order By Section)' : 'theorems-sec', \
1337 'Theorems (Starred)' : 'theorems-starred', 'Theorems' : 'theorems-std' }
1338 modlist = document.get_module_list()
1339 if len(modlist) == 0:
1343 if modulemap.has_key(mod):
1344 newmodlist.append(modulemap[mod])
1346 document.warning("Can't find module %s in the module map!" % mod)
1347 newmodlist.append(mod)
1348 document.set_module_list(newmodlist)
1351 def revert_module_names(document):
1352 modulemap = { 'braille' : 'Braille', 'endnotes' : 'Endnote', 'foottoend' : 'Foot to End',\
1353 'hanging' : 'Hanging', 'linguistics' : 'Linguistics', 'logicalmkup' : 'Logical Markup', \
1354 'theorems-ams-extended' : 'Theorems (AMS-Extended)', 'theorems-ams' : 'Theorems (AMS)', \
1355 'theorems-chap' : 'Theorems (Order By Chapter)', 'theorems-sec' : 'Theorems (Order By Section)', \
1356 'theorems-starred' : 'Theorems (Starred)', 'theorems-std' : 'Theorems'}
1357 modlist = document.get_module_list()
1358 if len(modlist) == 0:
1362 if modulemap.has_key(mod):
1363 newmodlist.append(modulemap[mod])
1365 document.warning("Can't find module %s in the module map!" % mod)
1366 newmodlist.append(mod)
1367 document.set_module_list(newmodlist)
1370 def revert_colsep(document):
1371 i = find_token(document.header, "\\columnsep", 0)
1374 colsepline = document.header[i]
1375 r = re.compile(r'\\columnsep (.*)')
1376 m = r.match(colsepline)
1378 document.warning("Malformed column separation line!")
1381 del document.header[i]
1382 #it seems to be safe to add the package even if it is already used
1383 pretext = ["\\usepackage{geometry}", "\\geometry{columnsep=" + colsep + "}"]
1385 add_to_preamble(document, pretext)
1388 def revert_framed_notes(document):
1389 "Revert framed boxes to notes. "
1392 i = find_tokens(document.body, ["\\begin_inset Box Framed", "\\begin_inset Box Shaded"], i)
1396 j = find_end_of_inset(document.body, i + 1)
1399 document.warning("Malformed LyX document: Could not find end of Box inset.")
1400 k = find_token(document.body, "status", i + 1, j)
1402 document.warning("Malformed LyX document: Missing `status' tag in Box inset.")
1404 status = document.body[k]
1405 l = find_default_layout(document, i + 1, j)
1407 document.warning("Malformed LyX document: Missing `\\begin_layout' in Box inset.")
1409 m = find_token(document.body, "\\end_layout", i + 1, j)
1411 document.warning("Malformed LyX document: Missing `\\end_layout' in Box inset.")
1413 ibox = find_token(document.body, "has_inner_box 1", i + 1, k)
1414 pbox = find_token(document.body, "use_parbox 1", i + 1, k)
1415 if ibox == -1 and pbox == -1:
1416 document.body[i] = document.body[i].replace("\\begin_inset Box", "\\begin_inset Note")
1417 del document.body[i+1:k]
1419 document.body[i] = document.body[i].replace("\\begin_inset Box Shaded", "\\begin_inset Box Frameless")
1420 document.body.insert(l + 1, "\\begin_inset Note Shaded\n" + status + "\n\\begin_layout Standard\n")
1421 document.body.insert(m + 1, "\\end_layout\n\\end_inset")
1425 def revert_slash(document):
1426 'Revert \\SpecialChar \\slash{} to ERT'
1427 for i in range(len(document.body)):
1428 document.body[i] = document.body[i].replace('\\SpecialChar \\slash{}', \
1429 '\\begin_inset ERT\nstatus collapsed\n\n' \
1430 '\\begin_layout Standard\n\n\n\\backslash\n' \
1431 'slash{}\n\\end_layout\n\n\\end_inset\n\n')
1434 def revert_nobreakdash(document):
1435 'Revert \\SpecialChar \\nobreakdash- to ERT'
1437 for i in range(len(document.body)):
1438 line = document.body[i]
1439 r = re.compile(r'\\SpecialChar \\nobreakdash-')
1443 document.body[i] = document.body[i].replace('\\SpecialChar \\nobreakdash-', \
1444 '\\begin_inset ERT\nstatus collapsed\n\n' \
1445 '\\begin_layout Standard\n\n\n\\backslash\n' \
1446 'nobreakdash-\n\\end_layout\n\n\\end_inset\n\n')
1449 j = find_token(document.header, "\\use_amsmath", 0)
1451 document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
1453 document.header[j] = "\\use_amsmath 2"
1456 def revert_nocite_key(body, start, end):
1457 'key "..." -> \nocite{...}'
1458 for i in range(start, end):
1459 if (body[i][0:5] == 'key "'):
1460 body[i] = body[i].replace('key "', "\\backslash\nnocite{")
1461 body[i] = body[i].replace('"', "}")
1466 def revert_nocite(document):
1467 "Revert LatexCommand nocite to ERT"
1470 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1474 if (document.body[i] == "LatexCommand nocite"):
1475 j = find_end_of_inset(document.body, i + 1)
1477 #this should not happen
1478 document.warning("End of CommandInset citation not found in revert_nocite!")
1479 revert_nocite_key(document.body, i + 1, len(document.body))
1481 revert_nocite_key(document.body, i + 1, j)
1482 document.body[i-1] = "\\begin_inset ERT"
1483 document.body[i] = "status collapsed\n\n" \
1484 "\\begin_layout Standard"
1485 document.body.insert(j, "\\end_layout\n");
1489 def revert_btprintall(document):
1490 "Revert (non-bibtopic) btPrintAll option to ERT \nocite{*}"
1491 i = find_token(document.header, '\\use_bibtopic', 0)
1493 document.warning("Malformed lyx document: Missing '\\use_bibtopic'.")
1495 if get_value(document.header, '\\use_bibtopic', 0) == "false":
1497 while i < len(document.body):
1498 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1501 j = find_end_of_inset(document.body, i + 1)
1503 #this should not happen
1504 document.warning("End of CommandInset bibtex not found in revert_btprintall!")
1505 j = len(document.body)
1506 for k in range(i, j):
1507 if (document.body[k] == 'btprint "btPrintAll"'):
1508 del document.body[k]
1509 document.body.insert(i, "\\begin_inset ERT\n" \
1510 "status collapsed\n\n\\begin_layout Standard\n\n" \
1511 "\\backslash\nnocite{*}\n" \
1512 "\\end_layout\n\\end_inset\n")
1516 def revert_bahasam(document):
1517 "Set language Bahasa Malaysia to Bahasa Indonesia"
1519 if document.language == "bahasam":
1520 document.language = "bahasa"
1521 i = find_token(document.header, "\\language", 0)
1523 document.header[i] = "\\language bahasa"
1526 j = find_token(document.body, "\\lang bahasam", j)
1529 document.body[j] = document.body[j].replace("\\lang bahasam", "\\lang bahasa")
1533 def revert_interlingua(document):
1534 "Set language Interlingua to English"
1536 if document.language == "interlingua":
1537 document.language = "english"
1538 i = find_token(document.header, "\\language", 0)
1540 document.header[i] = "\\language english"
1543 j = find_token(document.body, "\\lang interlingua", j)
1546 document.body[j] = document.body[j].replace("\\lang interlingua", "\\lang english")
1550 def revert_serbianlatin(document):
1551 "Set language Serbian-Latin to Croatian"
1553 if document.language == "serbian-latin":
1554 document.language = "croatian"
1555 i = find_token(document.header, "\\language", 0)
1557 document.header[i] = "\\language croatian"
1560 j = find_token(document.body, "\\lang serbian-latin", j)
1563 document.body[j] = document.body[j].replace("\\lang serbian-latin", "\\lang croatian")
1567 def revert_rotfloat(document):
1568 " Revert sideways custom floats. "
1571 # whitespace intended (exclude \\begin_inset FloatList)
1572 i = find_token(document.body, "\\begin_inset Float ", i)
1575 line = document.body[i]
1576 r = re.compile(r'\\begin_inset Float (.*)$')
1579 document.warning("Unable to match line " + str(i) + " of body!")
1582 floattype = m.group(1)
1583 if floattype == "figure" or floattype == "table":
1586 j = find_end_of_inset(document.body, i)
1588 document.warning("Malformed lyx document: Missing '\\end_inset'.")
1591 if get_value(document.body, 'sideways', i, j) != "false":
1592 l = find_default_layout(document, i + 1, j)
1594 document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
1596 document.body[j] = '\\begin_layout Standard\n\\begin_inset ERT\nstatus collapsed\n\n' \
1597 '\\begin_layout Standard\n\n\n\\backslash\n' \
1598 'end{sideways' + floattype + '}\n\\end_layout\n\n\\end_inset\n'
1599 del document.body[i+1:l-1]
1600 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
1601 '\\begin_layout Standard\n\n\n\\backslash\n' \
1602 'begin{sideways' + floattype + '}\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n'
1603 if floattype == "algorithm":
1604 add_to_preamble(document,
1605 ['% Commands inserted by lyx2lyx for sideways algorithm float',
1606 '\\usepackage{rotfloat}\n'
1607 '\\floatstyle{ruled}\n'
1608 '\\newfloat{algorithm}{tbp}{loa}\n'
1609 '\\floatname{algorithm}{Algorithm}\n'])
1611 document.warning("Cannot create preamble definition for custom float" + floattype + ".")
1617 def revert_widesideways(document):
1618 " Revert wide sideways floats. "
1621 # whitespace intended (exclude \\begin_inset FloatList)
1622 i = find_token(document.body, '\\begin_inset Float ', i)
1625 line = document.body[i]
1626 r = re.compile(r'\\begin_inset Float (.*)$')
1629 document.warning("Unable to match line " + str(i) + " of body!")
1632 floattype = m.group(1)
1633 if floattype != "figure" and floattype != "table":
1636 j = find_end_of_inset(document.body, i)
1638 document.warning("Malformed lyx document: Missing '\\end_inset'.")
1641 if get_value(document.body, 'sideways', i, j) != "false":
1642 if get_value(document.body, 'wide', i, j) != "false":
1643 l = find_default_layout(document, i + 1, j)
1645 document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
1647 document.body[j] = '\\begin_layout Standard\n\\begin_inset ERT\nstatus collapsed\n\n' \
1648 '\\begin_layout Standard\n\n\n\\backslash\n' \
1649 'end{sideways' + floattype + '*}\n\\end_layout\n\n\\end_inset\n'
1650 del document.body[i+1:l-1]
1651 document.body[i] = '\\begin_inset ERT\nstatus collapsed\n\n' \
1652 '\\begin_layout Standard\n\n\n\\backslash\n' \
1653 'begin{sideways' + floattype + '*}\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n'
1654 add_to_preamble(document,
1655 ['\\usepackage{rotfloat}\n'])
1661 def revert_inset_embedding(document, type):
1662 ' Remove embed tag from certain type of insets'
1665 i = find_token(document.body, "\\begin_inset %s" % type, i)
1668 j = find_end_of_inset(document.body, i)
1670 document.warning("Malformed lyx document: Missing '\\end_inset'.")
1673 k = find_token(document.body, "\tembed", i, j)
1675 k = find_token(document.body, "embed", i, j)
1677 del document.body[k]
1681 def revert_external_embedding(document):
1682 ' Remove embed tag from external inset '
1683 revert_inset_embedding(document, 'External')
1686 def convert_subfig(document):
1687 " Convert subfigures to subfloats. "
1690 i = find_token(document.body, '\\begin_inset Graphics', i)
1693 j = find_end_of_inset(document.body, i)
1695 document.warning("Malformed lyx document: Missing '\\end_inset'.")
1698 k = find_token(document.body, '\tsubcaption', i, j)
1702 l = find_token(document.body, '\tsubcaptionText', i, j)
1703 caption = document.body[l][16:].strip('"')
1704 savestr = document.body[i]
1705 del document.body[l]
1706 del document.body[k]
1707 document.body[i] = '\\begin_inset Float figure\nwide false\nsideways false\n' \
1708 'status open\n\n\\begin_layout Plain Layout\n\\begin_inset Caption\n\n\\begin_layout Plain Layout\n' \
1709 + caption + '\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout Plain Layout\n' + savestr
1710 savestr = document.body[j]
1711 document.body[j] = '\n\\end_layout\n\n\\end_inset\n' + savestr
1714 def revert_subfig(document):
1715 " Revert subfloats. "
1718 # whitespace intended (exclude \\begin_inset FloatList)
1719 i = find_token(document.body, '\\begin_inset Float ', i)
1723 j = find_end_of_inset(document.body, i)
1725 document.warning("Malformed lyx document: Missing '\\end_inset' (float).")
1728 # look for embedded float (= subfloat)
1729 # whitespace intended (exclude \\begin_inset FloatList)
1730 k = find_token(document.body, '\\begin_inset Float ', i + 1, j)
1733 l = find_end_of_inset(document.body, k)
1735 document.warning("Malformed lyx document: Missing '\\end_inset' (embedded float).")
1738 m = find_default_layout(document, k + 1, l)
1740 cap = find_token(document.body, '\\begin_inset Caption', k + 1, l)
1744 capend = find_end_of_inset(document.body, cap)
1746 document.warning("Malformed lyx document: Missing '\\end_inset' (caption).")
1750 lbl = find_token(document.body, '\\begin_inset CommandInset label', cap, capend)
1752 lblend = find_end_of_inset(document.body, lbl + 1)
1754 document.warning("Malformed lyx document: Missing '\\end_inset' (label).")
1756 for line in document.body[lbl:lblend + 1]:
1757 if line.startswith('name '):
1758 label = line.split()[1].strip('"')
1765 opt = find_token(document.body, '\\begin_inset OptArg', cap, capend)
1767 optend = find_end_of_inset(document.body, opt)
1769 document.warning("Malformed lyx document: Missing '\\end_inset' (OptArg).")
1771 optc = find_default_layout(document, opt, optend)
1773 document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
1775 optcend = find_end_of(document.body, optc, "\\begin_layout", "\\end_layout")
1776 for line in document.body[optc:optcend]:
1777 if not line.startswith('\\'):
1778 shortcap += line.strip()
1782 for line in document.body[cap:capend]:
1783 if line in document.body[lbl:lblend]:
1785 elif line in document.body[opt:optend]:
1787 elif not line.startswith('\\'):
1788 caption += line.strip()
1790 caption += "\\backslash\nlabel{" + label + "}"
1791 document.body[l] = '\\begin_layout Plain Layout\n\\begin_inset ERT\nstatus collapsed\n\n' \
1792 '\\begin_layout Plain Layout\n\n}\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout Plain Layout\n'
1793 del document.body[cap:capend+1]
1794 del document.body[k+1:m-1]
1795 insertion = '\\begin_inset ERT\nstatus collapsed\n\n' \
1796 '\\begin_layout Plain Layout\n\n\\backslash\n' \
1798 if len(shortcap) > 0:
1799 insertion = insertion + "[" + shortcap + "]"
1800 if len(caption) > 0:
1801 insertion = insertion + "[" + caption + "]"
1802 insertion = insertion + '{%\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n'
1803 document.body[k] = insertion
1804 add_to_preamble(document,
1805 ['\\usepackage{subfig}\n'])
1809 def revert_wrapplacement(document):
1810 " Revert placement options wrap floats (wrapfig). "
1813 i = find_token(document.body, "lines", i)
1816 j = find_token(document.body, "placement", i+1)
1818 document.warning("Malformed LyX document: Couldn't find placement parameter of wrap float.")
1820 document.body[j] = document.body[j].replace("placement O", "placement o")
1821 document.body[j] = document.body[j].replace("placement I", "placement i")
1822 document.body[j] = document.body[j].replace("placement L", "placement l")
1823 document.body[j] = document.body[j].replace("placement R", "placement r")
1827 def remove_extra_embedded_files(document):
1828 " Remove \extra_embedded_files from buffer params "
1829 i = find_token(document.header, '\\extra_embedded_files', 0)
1832 document.header.pop(i)
1835 def convert_spaceinset(document):
1836 " Convert '\\InsetSpace foo' to '\\begin_inset Space foo\n\\end_inset' "
1837 for i in range(len(document.body)):
1838 m = re.match(r'(.*)\\InsetSpace (.*)', document.body[i])
1842 subst = [before, "\\begin_inset Space " + after, "\\end_inset"]
1843 document.body[i: i+1] = subst
1846 def revert_spaceinset(document):
1847 " Revert '\\begin_inset Space foo\n\\end_inset' to '\\InsetSpace foo' "
1850 i = find_token(document.body, "\\begin_inset Space", i)
1853 j = find_end_of_inset(document.body, i)
1855 document.warning("Malformed LyX document: Could not find end of space inset.")
1857 document.body[i] = document.body[i].replace('\\begin_inset Space', '\\InsetSpace')
1858 del document.body[j]
1861 def convert_hfill(document):
1862 " Convert hfill to space inset "
1865 i = find_token(document.body, "\\hfill", i)
1868 document.body[i] = document.body[i].replace('\\hfill', '\n\\begin_inset Space \\hfill{}\n\\end_inset')
1871 def revert_hfills(document):
1872 ' Revert \\hfill commands '
1873 for i in range(len(document.body)):
1874 document.body[i] = document.body[i].replace('\\InsetSpace \\hfill{}', '\\hfill')
1875 document.body[i] = document.body[i].replace('\\InsetSpace \\dotfill{}', \
1876 '\\begin_inset ERT\nstatus collapsed\n\n' \
1877 '\\begin_layout Standard\n\n\n\\backslash\n' \
1878 'dotfill{}\n\\end_layout\n\n\\end_inset\n\n')
1879 document.body[i] = document.body[i].replace('\\InsetSpace \\hrulefill{}', \
1880 '\\begin_inset ERT\nstatus collapsed\n\n' \
1881 '\\begin_layout Standard\n\n\n\\backslash\n' \
1882 'hrulefill{}\n\\end_layout\n\n\\end_inset\n\n')
1885 def revert_hspace(document):
1886 ' Revert \\InsetSpace \\hspace{} to ERT '
1889 i = find_token(document.body, "\\InsetSpace \\hspace", i)
1892 length = get_value(document.body, '\\length', i+1)
1894 document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1896 del document.body[i+1]
1897 document.body[i] = document.body[i].replace('\\InsetSpace \\hspace*{}', \
1898 '\\begin_inset ERT\nstatus collapsed\n\n' \
1899 '\\begin_layout Standard\n\n\n\\backslash\n' \
1900 'hspace*{' + length + '}\n\\end_layout\n\n\\end_inset\n\n')
1901 document.body[i] = document.body[i].replace('\\InsetSpace \\hspace{}', \
1902 '\\begin_inset ERT\nstatus collapsed\n\n' \
1903 '\\begin_layout Standard\n\n\n\\backslash\n' \
1904 'hspace{' + length + '}\n\\end_layout\n\n\\end_inset\n\n')
1907 def revert_protected_hfill(document):
1908 ' Revert \\begin_inset Space \\hspace*{\\fill} to ERT '
1911 i = find_token(document.body, '\\begin_inset Space \\hspace*{\\fill}', i)
1914 j = find_end_of_inset(document.body, i)
1916 document.warning("Malformed LyX document: Could not find end of space inset.")
1918 del document.body[j]
1919 document.body[i] = document.body[i].replace('\\begin_inset Space \\hspace*{\\fill}', \
1920 '\\begin_inset ERT\nstatus collapsed\n\n' \
1921 '\\begin_layout Standard\n\n\n\\backslash\n' \
1922 'hspace*{\n\\backslash\nfill}\n\\end_layout\n\n\\end_inset\n\n')
1925 def revert_leftarrowfill(document):
1926 ' Revert \\begin_inset Space \\leftarrowfill{} to ERT '
1929 i = find_token(document.body, '\\begin_inset Space \\leftarrowfill{}', i)
1932 j = find_end_of_inset(document.body, i)
1934 document.warning("Malformed LyX document: Could not find end of space inset.")
1936 del document.body[j]
1937 document.body[i] = document.body[i].replace('\\begin_inset Space \\leftarrowfill{}', \
1938 '\\begin_inset ERT\nstatus collapsed\n\n' \
1939 '\\begin_layout Standard\n\n\n\\backslash\n' \
1940 'leftarrowfill{}\n\\end_layout\n\n\\end_inset\n\n')
1943 def revert_rightarrowfill(document):
1944 ' Revert \\begin_inset Space \\rightarrowfill{} to ERT '
1947 i = find_token(document.body, '\\begin_inset Space \\rightarrowfill{}', i)
1950 j = find_end_of_inset(document.body, i)
1952 document.warning("Malformed LyX document: Could not find end of space inset.")
1954 del document.body[j]
1955 document.body[i] = document.body[i].replace('\\begin_inset Space \\rightarrowfill{}', \
1956 '\\begin_inset ERT\nstatus collapsed\n\n' \
1957 '\\begin_layout Standard\n\n\n\\backslash\n' \
1958 'rightarrowfill{}\n\\end_layout\n\n\\end_inset\n\n')
1961 def revert_upbracefill(document):
1962 ' Revert \\begin_inset Space \\upbracefill{} to ERT '
1965 i = find_token(document.body, '\\begin_inset Space \\upbracefill{}', i)
1968 j = find_end_of_inset(document.body, i)
1970 document.warning("Malformed LyX document: Could not find end of space inset.")
1972 del document.body[j]
1973 document.body[i] = document.body[i].replace('\\begin_inset Space \\upbracefill{}', \
1974 '\\begin_inset ERT\nstatus collapsed\n\n' \
1975 '\\begin_layout Standard\n\n\n\\backslash\n' \
1976 'upbracefill{}\n\\end_layout\n\n\\end_inset\n\n')
1979 def revert_downbracefill(document):
1980 ' Revert \\begin_inset Space \\downbracefill{} to ERT '
1983 i = find_token(document.body, '\\begin_inset Space \\downbracefill{}', i)
1986 j = find_end_of_inset(document.body, i)
1988 document.warning("Malformed LyX document: Could not find end of space inset.")
1990 del document.body[j]
1991 document.body[i] = document.body[i].replace('\\begin_inset Space \\downbracefill{}', \
1992 '\\begin_inset ERT\nstatus collapsed\n\n' \
1993 '\\begin_layout Standard\n\n\n\\backslash\n' \
1994 'downbracefill{}\n\\end_layout\n\n\\end_inset\n\n')
1997 def revert_local_layout(document):
1998 ' Revert local layout headers.'
2001 i = find_token(document.header, "\\begin_local_layout", i)
2004 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
2006 # this should not happen
2008 document.header[i : j + 1] = []
2011 def convert_pagebreaks(document):
2012 ' Convert inline Newpage insets to new format '
2015 i = find_token(document.body, '\\newpage', i)
2018 document.body[i:i+1] = ['\\begin_inset Newpage newpage',
2022 i = find_token(document.body, '\\pagebreak', i)
2025 document.body[i:i+1] = ['\\begin_inset Newpage pagebreak',
2029 i = find_token(document.body, '\\clearpage', i)
2032 document.body[i:i+1] = ['\\begin_inset Newpage clearpage',
2036 i = find_token(document.body, '\\cleardoublepage', i)
2039 document.body[i:i+1] = ['\\begin_inset Newpage cleardoublepage',
2043 def revert_pagebreaks(document):
2044 ' Revert \\begin_inset Newpage to previous inline format '
2047 i = find_token(document.body, '\\begin_inset Newpage', i)
2050 j = find_end_of_inset(document.body, i)
2052 document.warning("Malformed LyX document: Could not find end of Newpage inset.")
2054 del document.body[j]
2055 document.body[i] = document.body[i].replace('\\begin_inset Newpage newpage', '\\newpage')
2056 document.body[i] = document.body[i].replace('\\begin_inset Newpage pagebreak', '\\pagebreak')
2057 document.body[i] = document.body[i].replace('\\begin_inset Newpage clearpage', '\\clearpage')
2058 document.body[i] = document.body[i].replace('\\begin_inset Newpage cleardoublepage', '\\cleardoublepage')
2061 def convert_linebreaks(document):
2062 ' Convert inline Newline insets to new format '
2065 i = find_token(document.body, '\\newline', i)
2068 document.body[i:i+1] = ['\\begin_inset Newline newline',
2072 i = find_token(document.body, '\\linebreak', i)
2075 document.body[i:i+1] = ['\\begin_inset Newline linebreak',
2079 def revert_linebreaks(document):
2080 ' Revert \\begin_inset Newline to previous inline format '
2083 i = find_token(document.body, '\\begin_inset Newline', i)
2086 j = find_end_of_inset(document.body, i)
2088 document.warning("Malformed LyX document: Could not find end of Newline inset.")
2090 del document.body[j]
2091 document.body[i] = document.body[i].replace('\\begin_inset Newline newline', '\\newline')
2092 document.body[i] = document.body[i].replace('\\begin_inset Newline linebreak', '\\linebreak')
2095 def convert_japanese_plain(document):
2096 ' Set language japanese-plain to japanese '
2098 if document.language == "japanese-plain":
2099 document.language = "japanese"
2100 i = find_token(document.header, "\\language", 0)
2102 document.header[i] = "\\language japanese"
2105 j = find_token(document.body, "\\lang japanese-plain", j)
2108 document.body[j] = document.body[j].replace("\\lang japanese-plain", "\\lang japanese")
2112 def revert_pdfpages(document):
2113 ' Revert pdfpages external inset to ERT '
2116 i = find_token(document.body, "\\begin_inset External", i)
2119 j = find_end_of_inset(document.body, i)
2121 document.warning("Malformed lyx document: Missing '\\end_inset'.")
2124 if get_value(document.body, 'template', i, j) == "PDFPages":
2125 filename = get_value(document.body, 'filename', i, j)
2127 r = re.compile(r'\textra PDFLaTeX \"(.*)\"$')
2128 for k in range(i, j):
2129 m = r.match(document.body[k])
2132 angle = get_value(document.body, 'rotateAngle', i, j)
2133 width = get_value(document.body, 'width', i, j)
2134 height = get_value(document.body, 'height', i, j)
2135 scale = get_value(document.body, 'scale', i, j)
2136 keepAspectRatio = find_token(document.body, "\tkeepAspectRatio", i, j)
2140 options += ",angle=" + angle
2142 options += "angle=" + angle
2145 options += ",width=" + convert_len(width)
2147 options += "width=" + convert_len(width)
2150 options += ",height=" + convert_len(height)
2152 options += "height=" + convert_len(height)
2155 options += ",scale=" + scale
2157 options += "scale=" + scale
2158 if keepAspectRatio != '':
2160 options += ",keepaspectratio"
2162 options += "keepaspectratio"
2164 options = '[' + options + ']'
2165 del document.body[i+1:j+1]
2166 document.body[i:i+1] = ['\\begin_inset ERT',
2169 '\\begin_layout Standard',
2172 'includepdf' + options + '{' + filename + '}',
2176 add_to_preamble(document, ['\\usepackage{pdfpages}\n'])
2182 def revert_mexican(document):
2183 ' Set language Spanish(Mexico) to Spanish '
2185 if document.language == "spanish-mexico":
2186 document.language = "spanish"
2187 i = find_token(document.header, "\\language", 0)
2189 document.header[i] = "\\language spanish"
2192 j = find_token(document.body, "\\lang spanish-mexico", j)
2195 document.body[j] = document.body[j].replace("\\lang spanish-mexico", "\\lang spanish")
2199 def remove_embedding(document):
2200 ' Remove embed tag from all insets '
2201 revert_inset_embedding(document, 'Graphics')
2202 revert_inset_embedding(document, 'External')
2203 revert_inset_embedding(document, 'CommandInset include')
2204 revert_inset_embedding(document, 'CommandInset bibtex')
2207 def revert_master(document):
2208 ' Remove master param '
2209 i = find_token(document.header, "\\master", 0)
2211 del document.header[i]
2214 def revert_graphics_group(document):
2215 ' Revert group information from graphics insets '
2218 i = find_token(document.body, "\\begin_inset Graphics", i)
2221 j = find_end_of_inset(document.body, i)
2223 document.warning("Malformed lyx document: Missing '\\end_inset'.")
2226 k = find_token(document.body, " groupId", i, j)
2230 del document.body[k]
2234 def update_apa_styles(document):
2235 ' Replace obsolete styles '
2237 if document.textclass != "apa":
2240 obsoletedby = { "Acknowledgments": "Acknowledgements",
2241 "Section*": "Section",
2242 "Subsection*": "Subsection",
2243 "Subsubsection*": "Subsubsection",
2244 "Paragraph*": "Paragraph",
2245 "Subparagraph*": "Subparagraph"}
2248 i = find_token(document.body, "\\begin_layout", i)
2252 layout = document.body[i][14:]
2253 if layout in obsoletedby:
2254 document.body[i] = "\\begin_layout " + obsoletedby[layout]
2259 def convert_paper_sizes(document):
2260 ' exchange size options legalpaper and executivepaper to correct order '
2261 # routine is needed to fix http://bugzilla.lyx.org/show_bug.cgi?id=4868
2264 i = find_token(document.header, "\\papersize executivepaper", 0)
2266 document.header[i] = "\\papersize legalpaper"
2268 j = find_token(document.header, "\\papersize legalpaper", 0)
2270 document.header[j] = "\\papersize executivepaper"
2273 def revert_paper_sizes(document):
2274 ' exchange size options legalpaper and executivepaper to correct order '
2277 i = find_token(document.header, "\\papersize executivepaper", 0)
2279 document.header[i] = "\\papersize legalpaper"
2281 j = find_token(document.header, "\\papersize legalpaper", 0)
2283 document.header[j] = "\\papersize executivepaper"
2286 def convert_InsetSpace(document):
2287 " Convert '\\begin_inset Space foo' to '\\begin_inset space foo'"
2290 i = find_token(document.body, "\\begin_inset Space", i)
2293 document.body[i] = document.body[i].replace('\\begin_inset Space', '\\begin_inset space')
2296 def revert_InsetSpace(document):
2297 " Revert '\\begin_inset space foo' to '\\begin_inset Space foo'"
2300 i = find_token(document.body, "\\begin_inset space", i)
2303 document.body[i] = document.body[i].replace('\\begin_inset space', '\\begin_inset Space')
2310 supported_versions = ["1.6.0","1.6"]
2311 convert = [[277, [fix_wrong_tables]],
2312 [278, [close_begin_deeper]],
2313 [279, [long_charstyle_names]],
2314 [280, [axe_show_label]],
2317 [283, [convert_flex]],
2321 [287, [convert_wrapfig_options]],
2322 [288, [convert_inset_command]],
2323 [289, [convert_latexcommand_index]],
2328 [294, [convert_pdf_options]],
2329 [295, [convert_htmlurl, convert_url]],
2330 [296, [convert_include]],
2331 [297, [convert_usorbian]],
2337 [303, [convert_serbocroatian]],
2338 [304, [convert_framed_notes]],
2345 [311, [convert_ams_classes]],
2347 [313, [convert_module_names]],
2350 [316, [convert_subfig]],
2353 [319, [convert_spaceinset, convert_hfill]],
2355 [321, [convert_tablines]],
2357 [323, [convert_pagebreaks]],
2358 [324, [convert_linebreaks]],
2359 [325, [convert_japanese_plain]],
2362 [328, [remove_embedding, remove_extra_embedded_files, remove_inzip_options]],
2365 [331, [convert_ltcaption]],
2367 [333, [update_apa_styles]],
2368 [334, [convert_paper_sizes]],
2369 [335, [convert_InsetSpace]],
2372 revert = [[334, [revert_InsetSpace]],
2373 [333, [revert_paper_sizes]],
2375 [331, [revert_graphics_group]],
2376 [330, [revert_ltcaption]],
2377 [329, [revert_leftarrowfill, revert_rightarrowfill, revert_upbracefill, revert_downbracefill]],
2378 [328, [revert_master]],
2380 [326, [revert_mexican]],
2381 [325, [revert_pdfpages]],
2383 [323, [revert_linebreaks]],
2384 [322, [revert_pagebreaks]],
2385 [321, [revert_local_layout]],
2386 [320, [revert_tablines]],
2387 [319, [revert_protected_hfill]],
2388 [318, [revert_spaceinset, revert_hfills, revert_hspace]],
2389 [317, [remove_extra_embedded_files]],
2390 [316, [revert_wrapplacement]],
2391 [315, [revert_subfig]],
2392 [314, [revert_colsep]],
2394 [312, [revert_module_names]],
2395 [311, [revert_rotfloat, revert_widesideways]],
2396 [310, [revert_external_embedding]],
2397 [309, [revert_btprintall]],
2398 [308, [revert_nocite]],
2399 [307, [revert_serbianlatin]],
2400 [306, [revert_slash, revert_nobreakdash]],
2401 [305, [revert_interlingua]],
2402 [304, [revert_bahasam]],
2403 [303, [revert_framed_notes]],
2405 [301, [revert_latin, revert_samin]],
2406 [300, [revert_linebreak]],
2407 [299, [revert_pagebreak]],
2408 [298, [revert_hyperlinktype]],
2409 [297, [revert_macro_optional_params]],
2410 [296, [revert_albanian, revert_lowersorbian, revert_uppersorbian]],
2411 [295, [revert_include]],
2412 [294, [revert_href]],
2413 [293, [revert_pdf_options_2]],
2414 [292, [revert_inset_info]],
2415 [291, [revert_japanese, revert_japanese_encoding]],
2416 [290, [revert_vietnamese]],
2417 [289, [revert_wraptable]],
2418 [288, [revert_latexcommand_index]],
2419 [287, [revert_inset_command]],
2420 [286, [revert_wrapfig_options]],
2421 [285, [revert_pdf_options]],
2422 [284, [remove_inzip_options]],
2424 [282, [revert_flex]],
2426 [280, [revert_begin_modules]],
2427 [279, [revert_show_label]],
2428 [278, [revert_long_charstyle_names]],
2434 if __name__ == "__main__":