1 # This file is part of lyx2lyx
2 # Copyright (C) 2003-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 0.12"""
22 from parser_tools import check_token, find_re, find_token
25 def space_before_layout(document):
26 "Remove empty line before \\layout."
28 i = 2 # skip first layout
30 i = find_token(lines, "\\layout", i)
34 prot_space = lines[i - 2].find("\\protected_separator")
35 if lines[i - 1] == "" and prot_space == -1:
40 def formula_inset_space_eat(document):
41 "Remove space after inset formula."
45 i = find_token(lines, "\\begin_inset Formula", i)
49 if len(lines[i]) > 22 and lines[i][21] == " ":
50 lines[i] = lines[i][:20] + lines[i][21:]
54 def update_tabular(document):
55 "Update from tabular format 1 or 2 to 4."
57 lyxtable_re = re.compile(r".*\\LyXTable$")
60 i = find_re(lines, lyxtable_re, i)
66 lines[i] = "multicol4"
68 rows = int(lines[i].split()[0])
69 columns = int(lines[i].split()[1])
71 lines[i] = lines[i] + " 0 0 -1 -1 -1 -1"
75 lines[i] = lines[i] + " 0 0"
78 for j in range(columns):
79 lines[i] = lines[i] + " "
82 while lines[i].strip():
84 lines[i] = lines[i] + " 1 1"
85 lines[i] = lines[i] + " 0 0 0"
88 lines[i] = lines[i].strip()
91 def final_dot(document):
92 "Merge lines if the dot is the final character."
98 and lines[i + 1][:1] != "\\"
99 and lines[i + 1][:1] != " "
100 and len(lines[i]) + len(lines[i + 1]) <= 72
101 and lines[i + 1] != ""
103 lines[i] = lines[i] + lines[i + 1]
109 def update_inset_label(document):
110 "Update inset Label."
111 lines = document.body
114 i = find_token(lines, "\\begin_inset Label", i)
117 lines[i] = "\\begin_inset LatexCommand \\label{" + lines[i][19:] + "}"
121 def update_latexdel(document):
122 "Update inset LatexDel."
123 lines = document.body
126 i = find_token(lines, "\\begin_inset LatexDel", i)
129 lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
133 def update_vfill(document):
134 "Update fill_top and fill_bottom."
135 lines = document.body
136 for i in range(len(lines)):
137 lines[i] = lines[i].replace("\\fill_top", "\\added_space_top vfill")
138 lines[i] = lines[i].replace("\\fill_bottom", "\\added_space_bottom vfill")
141 def update_space_units(document):
142 "Update space units."
143 lines = document.body
144 added_space_bottom = re.compile(r"\\added_space_bottom ([^ ]*)")
145 added_space_top = re.compile(r"\\added_space_top ([^ ]*)")
146 for i in range(len(lines)):
147 result = added_space_bottom.search(lines[i])
149 old = "\\added_space_bottom " + result.group(1)
150 new = "\\added_space_bottom " + str(float(result.group(1))) + "cm"
151 lines[i] = lines[i].replace(old, new)
153 result = added_space_top.search(lines[i])
155 old = "\\added_space_top " + result.group(1)
156 new = "\\added_space_top " + str(float(result.group(1))) + "cm"
157 lines[i] = lines[i].replace(old, new)
160 def remove_cursor(document):
161 "Remove cursor, it is not saved on the file anymore."
162 lines = document.body
164 cursor_re = re.compile(r".*(\\cursor \d*)")
166 i = find_re(lines, cursor_re, i)
169 cursor = cursor_re.search(lines[i]).group(1)
170 lines[i] = lines[i].replace(cursor, "")
174 def remove_empty_insets(document):
175 "Remove empty insets."
176 lines = document.body
179 i = find_token(lines, "\\begin_inset ", i)
182 if lines[i] == "\\begin_inset " and lines[i + 1] == "\\end_inset ":
188 def remove_formula_latex(document):
189 "Remove formula latex."
190 lines = document.body
193 i = find_token(lines, "\\latex formula_latex ", i)
198 i = find_token(lines, "\\latex default", i)
204 def add_end_document(document):
205 "Add \\the_end to the end of the document."
206 lines = document.body
207 i = find_token(lines, "\\the_end", 0)
209 lines.append("\\the_end")
212 def header_update(document):
213 "Update document header."
214 lines = document.header
218 if lines[i][-1:] == " ":
219 lines[i] = lines[i][:-1]
221 if check_token(lines[i], "\\epsfig"):
222 lines[i] = lines[i].replace("\\epsfig", "\\graphics")
226 if check_token(lines[i], "\\papersize"):
227 size = lines[i].split()[1]
231 if size == "usletter":
232 new_size = "letterpaper"
235 paperpackage = "widemarginsa4"
237 lines[i] = "\\papersize " + new_size
240 lines.insert(i, "\\paperpackage " + paperpackage)
243 lines.insert(i, "\\use_geometry 0")
244 lines.insert(i + 1, "\\use_amsmath 0")
248 if check_token(lines[i], "\\baselinestretch"):
249 size = lines[i].split()[1]
257 name = "other " + size
258 lines[i] = "\\spacing %s " % name
265 def update_latexaccents(document):
266 "Update latex accent insets."
270 i = find_token(body, "\\i ", i)
274 contents = body[i][2:].strip()
276 if contents.find("{") != -1 and contents.find("}") != -1:
280 if len(contents) == 2:
281 contents = contents + "{}"
282 elif len(contents) == 3:
283 contents = contents[:2] + "{" + contents[2] + "}"
284 elif len(contents) == 4:
285 if contents[2] == " ":
286 contents = contents[:2] + "{" + contents[3] + "}"
287 elif contents[2:4] == "\\i" or contents[2:4] == "\\j":
288 contents = contents[:2] + "{" + contents[2:] + "}"
290 body[i] = "\\i " + contents
294 def obsolete_latex_title(document):
295 "Replace layout Latex_Title with Title."
299 i = find_token(body, "\\layout", i)
303 if body[i].lower().find("latex_title") != -1:
304 body[i] = "\\layout Title"
309 def remove_inset_latex(document):
310 "Replace inset latex with layout LaTeX"
315 i = find_token(body, "\\begin_inset Latex", i)
319 body[i] = body[i].replace("\\begin_inset Latex", "\\layout LaTeX")
320 i = find_token(body, "\\end_inset", i)
322 # this should not happen
327 supported_versions = ["0.12.0", "0.12.1", "0.12"]
340 formula_inset_space_eat,
344 remove_formula_latex,
346 obsolete_latex_title,
354 if __name__ == "__main__":