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"""
21 from parser_tools import find_token, find_re, check_token
24 def space_before_layout(document):
25 "Remove empty line before \\layout."
27 i = 2 # skip first layout
29 i = find_token(lines, "\\layout", i)
33 prot_space = lines[i - 2].find("\\protected_separator")
34 if lines[i - 1] == "" and prot_space == -1:
39 def formula_inset_space_eat(document):
40 "Remove space after inset formula."
44 i = find_token(lines, "\\begin_inset Formula", i)
48 if len(lines[i]) > 22 and lines[i][21] == " ":
49 lines[i] = lines[i][:20] + lines[i][21:]
53 def update_tabular(document):
54 "Update from tabular format 1 or 2 to 4."
56 lyxtable_re = re.compile(r".*\\LyXTable$")
59 i = find_re(lines, lyxtable_re, i)
65 lines[i] = "multicol4"
67 rows = int(lines[i].split()[0])
68 columns = int(lines[i].split()[1])
70 lines[i] = lines[i] + " 0 0 -1 -1 -1 -1"
74 lines[i] = lines[i] + " 0 0"
77 for j in range(columns):
78 lines[i] = lines[i] + " "
81 while lines[i].strip():
83 lines[i] = lines[i] + " 1 1"
84 lines[i] = lines[i] + " 0 0 0"
87 lines[i] = lines[i].strip()
90 def final_dot(document):
91 "Merge lines if the dot is the final character."
97 and lines[i + 1][:1] != "\\"
98 and lines[i + 1][:1] != " "
99 and len(lines[i]) + len(lines[i + 1]) <= 72
100 and lines[i + 1] != ""
102 lines[i] = lines[i] + lines[i + 1]
108 def update_inset_label(document):
109 "Update inset Label."
110 lines = document.body
113 i = find_token(lines, "\\begin_inset Label", i)
116 lines[i] = "\\begin_inset LatexCommand \\label{" + lines[i][19:] + "}"
120 def update_latexdel(document):
121 "Update inset LatexDel."
122 lines = document.body
125 i = find_token(lines, "\\begin_inset LatexDel", i)
128 lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
132 def update_vfill(document):
133 "Update fill_top and fill_bottom."
134 lines = document.body
135 for i in range(len(lines)):
136 lines[i] = lines[i].replace("\\fill_top", "\\added_space_top vfill")
137 lines[i] = lines[i].replace("\\fill_bottom", "\\added_space_bottom vfill")
140 def update_space_units(document):
141 "Update space units."
142 lines = document.body
143 added_space_bottom = re.compile(r"\\added_space_bottom ([^ ]*)")
144 added_space_top = re.compile(r"\\added_space_top ([^ ]*)")
145 for i in range(len(lines)):
146 result = added_space_bottom.search(lines[i])
148 old = "\\added_space_bottom " + result.group(1)
149 new = "\\added_space_bottom " + str(float(result.group(1))) + "cm"
150 lines[i] = lines[i].replace(old, new)
152 result = added_space_top.search(lines[i])
154 old = "\\added_space_top " + result.group(1)
155 new = "\\added_space_top " + str(float(result.group(1))) + "cm"
156 lines[i] = lines[i].replace(old, new)
159 def remove_cursor(document):
160 "Remove cursor, it is not saved on the file anymore."
161 lines = document.body
163 cursor_re = re.compile(r".*(\\cursor \d*)")
165 i = find_re(lines, cursor_re, i)
168 cursor = cursor_re.search(lines[i]).group(1)
169 lines[i] = lines[i].replace(cursor, "")
173 def remove_empty_insets(document):
174 "Remove empty insets."
175 lines = document.body
178 i = find_token(lines, "\\begin_inset ", i)
181 if lines[i] == "\\begin_inset " and lines[i + 1] == "\\end_inset ":
187 def remove_formula_latex(document):
188 "Remove formula latex."
189 lines = document.body
192 i = find_token(lines, "\\latex formula_latex ", i)
197 i = find_token(lines, "\\latex default", i)
203 def add_end_document(document):
204 "Add \\the_end to the end of the document."
205 lines = document.body
206 i = find_token(lines, "\\the_end", 0)
208 lines.append("\\the_end")
211 def header_update(document):
212 "Update document header."
213 lines = document.header
217 if lines[i][-1:] == " ":
218 lines[i] = lines[i][:-1]
220 if check_token(lines[i], "\\epsfig"):
221 lines[i] = lines[i].replace("\\epsfig", "\\graphics")
225 if check_token(lines[i], "\\papersize"):
226 size = lines[i].split()[1]
230 if size == "usletter":
231 new_size = "letterpaper"
234 paperpackage = "widemarginsa4"
236 lines[i] = "\\papersize " + new_size
239 lines.insert(i, "\\paperpackage " + paperpackage)
242 lines.insert(i, "\\use_geometry 0")
243 lines.insert(i + 1, "\\use_amsmath 0")
247 if check_token(lines[i], "\\baselinestretch"):
248 size = lines[i].split()[1]
256 name = "other " + size
257 lines[i] = "\\spacing %s " % name
264 def update_latexaccents(document):
265 "Update latex accent insets."
269 i = find_token(body, "\\i ", i)
273 contents = body[i][2:].strip()
275 if contents.find("{") != -1 and contents.find("}") != -1:
279 if len(contents) == 2:
280 contents = contents + "{}"
281 elif len(contents) == 3:
282 contents = contents[:2] + "{" + contents[2] + "}"
283 elif len(contents) == 4:
284 if contents[2] == " ":
285 contents = contents[:2] + "{" + contents[3] + "}"
286 elif contents[2:4] == "\\i" or contents[2:4] == "\\j":
287 contents = contents[:2] + "{" + contents[2:] + "}"
289 body[i] = "\\i " + contents
293 def obsolete_latex_title(document):
294 "Replace layout Latex_Title with Title."
298 i = find_token(body, "\\layout", i)
302 if body[i].lower().find("latex_title") != -1:
303 body[i] = "\\layout Title"
308 def remove_inset_latex(document):
309 "Replace inset latex with layout LaTeX"
314 i = find_token(body, "\\begin_inset Latex", i)
318 body[i] = body[i].replace("\\begin_inset Latex", "\\layout LaTeX")
319 i = find_token(body, "\\end_inset", i)
321 # this should not happen
326 supported_versions = ["0.12.0", "0.12.1", "0.12"]
339 formula_inset_space_eat,
343 remove_formula_latex,
345 obsolete_latex_title,
353 if __name__ == "__main__":