1 # This document is part of lyx2lyx
2 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 """Convert files to the file format generated by lyx 1.1.5"""
21 from parser_tools import find_token, find_token_backwards, find_re
23 ####################################################################
24 # Private helper functions
27 def get_layout(line, default_layout):
28 "Get the line layout, beware of the empty layout."
35 ####################################################################
37 math_env = ["\\[", "\\begin{eqnarray*}", "\\begin{eqnarray}", "\\begin{equation}"]
40 def replace_protected_separator(document):
41 "Replace protected separator."
45 i = find_token(lines, "\\protected_separator", i)
48 j = find_token_backwards(lines, "\\layout", i)
49 # if j == -1: print error
50 layout = get_layout(lines[j], document.default_layout)
52 if layout == "LyX-Code":
54 while lines[i] == "\\protected_separator ":
58 lines[i - 1] = lines[i - 1] + result + lines[i]
60 lines[i - 1] = lines[i - 1] + "\\SpecialChar ~"
65 def merge_formula_inset(document):
66 "Merge formula insets."
70 i = find_token(lines, "\\begin_inset Formula", i)
73 if lines[i + 1] in math_env:
74 lines[i] = lines[i] + lines[i + 1]
79 def update_tabular(document):
80 "Update from tabular format 4 to 5 if necessary."
82 lyxtable_re = re.compile(r".*\\LyXTable$")
85 i = find_re(lines, lyxtable_re, i)
93 lines[i] = "multicol5"
95 rows = int(lines[i].split()[0])
96 columns = int(lines[i].split()[1])
99 for j in range(columns):
100 col_info = lines[i].split()
101 if len(col_info) == 3:
102 lines[i] = lines[i] + '"" ""'
104 lines[i] = " ".join(col_info[:3]) + ' "%s" ""' % col_info[3]
108 lines[i] = lines[i] + ' "" ""'
112 def update_toc(document):
113 "Update table of contents."
114 lines = document.body
117 i = find_token(lines, "\\begin_inset LatexCommand \\tableofcontents", i)
120 lines[i] = lines[i] + "{}"
124 def remove_cursor(document):
126 lines = document.body
127 i = find_token(lines, "\\cursor", 0)
132 def remove_vcid(document):
133 "Remove \\lyxvcid and \\lyxrcsid."
134 lines = document.header
135 i = find_token(lines, "\\lyxvcid", 0)
138 i = find_token(lines, "\\lyxrcsid", 0)
143 def first_layout(document):
144 "Fix first layout, if empty use the default layout."
145 lines = document.body
146 while lines[0] == "":
148 if lines[0][:7] != "\\layout":
149 lines[:0] = ["\\layout %s" % document.default_layout, ""]
152 def remove_space_in_units(document):
153 "Remove space in units."
154 lines = document.header
155 margins = ["\\topmargin", "\\rightmargin", "\\leftmargin", "\\bottommargin"]
157 unit_rexp = re.compile(r"[^ ]* (.*) (.*)")
159 for margin in margins:
162 i = find_token(lines, margin, i)
166 result = unit_rexp.search(lines[i])
168 lines[i] = margin + " " + result.group(1) + result.group(2)
172 def latexdel_getargs(document, i):
173 "Get arguments from latexdel insets."
174 lines = document.body
176 # play safe, clean empty lines
182 j = find_token(lines, "\\end_inset", i)
187 document.warning("Unexpected end of inset.")
188 j = find_token(lines, "\\begin_inset LatexDel }{", i)
190 ref = " ".join(lines[i:j])
193 # play safe, clean empty lines
199 j = find_token(lines, "\\end_inset", i - 1)
203 document.warning("Unexpected end of inset.")
204 j = find_token(lines, "\\begin_inset LatexDel }", i)
205 label = " ".join(lines[i:j])
211 def update_ref(document):
212 "Update reference inset."
213 lines = document.body
216 i = find_token(lines, "\\begin_inset LatexCommand", i)
220 if lines[i].split()[-1] == "\\ref{":
222 ref, label = latexdel_getargs(document, i)
223 lines[i - 1] = f"{lines[i - 1][:-1]}[{ref}]{{{label}}}"
228 def update_latexdel(document):
229 "Remove latexdel insets."
230 lines = document.body
232 latexdel_re = re.compile(r".*\\begin_inset LatexDel")
234 i = find_re(lines, latexdel_re, i)
237 lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
239 j = lines[i].find("\\begin_inset")
240 lines.insert(i + 1, lines[i][j:])
241 lines[i] = lines[i][:j].strip()
244 if lines[i].split()[-1] in ("\\url{", "\\htmlurl{"):
247 ref, label = latexdel_getargs(document, i)
248 lines[i - 1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}"
253 supported_versions = ["1.1.5", "1.1.5fix1", "1.1.5fix2", "1.1"]
262 replace_protected_separator,
265 remove_space_in_units,
274 if __name__ == "__main__":