]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
Move DrawStrategy enum to update_flags.h.
[lyx.git] / lib / lyx2lyx / lyx_0_12.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
3 #
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.
8 #
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.
13 #
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
17
18 """Convert files to the file format generated by lyx 0.12"""
19
20 import re
21
22 from parser_tools import check_token, find_re, find_token
23
24
25 def space_before_layout(document):
26     "Remove empty line before \\layout."
27     lines = document.body
28     i = 2  # skip first layout
29     while True:
30         i = find_token(lines, "\\layout", i)
31         if i == -1:
32             break
33
34         prot_space = lines[i - 2].find("\\protected_separator")
35         if lines[i - 1] == "" and prot_space == -1:
36             del lines[i - 1]
37         i = i + 1
38
39
40 def formula_inset_space_eat(document):
41     "Remove space after inset formula."
42     lines = document.body
43     i = 0
44     while True:
45         i = find_token(lines, "\\begin_inset Formula", i)
46         if i == -1:
47             break
48
49         if len(lines[i]) > 22 and lines[i][21] == " ":
50             lines[i] = lines[i][:20] + lines[i][21:]
51         i = i + 1
52
53
54 def update_tabular(document):
55     "Update from tabular format 1 or 2 to 4."
56     lines = document.body
57     lyxtable_re = re.compile(r".*\\LyXTable$")
58     i = 0
59     while True:
60         i = find_re(lines, lyxtable_re, i)
61         if i == -1:
62             break
63         i = i + 1
64         format = lines[i][8:]
65
66         lines[i] = "multicol4"
67         i = i + 1
68         rows = int(lines[i].split()[0])
69         columns = int(lines[i].split()[1])
70
71         lines[i] = lines[i] + " 0 0 -1 -1 -1 -1"
72         i = i + 1
73
74         for j in range(rows):
75             lines[i] = lines[i] + " 0 0"
76             i = i + 1
77
78         for j in range(columns):
79             lines[i] = lines[i] + " "
80             i = i + 1
81
82         while lines[i].strip():
83             if not format:
84                 lines[i] = lines[i] + " 1 1"
85             lines[i] = lines[i] + " 0 0 0"
86             i = i + 1
87
88         lines[i] = lines[i].strip()
89
90
91 def final_dot(document):
92     "Merge lines if the dot is the final character."
93     lines = document.body
94     i = 0
95     while i < len(lines):
96         if (
97             lines[i][-1:] == "."
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] != ""
102         ):
103             lines[i] = lines[i] + lines[i + 1]
104             del lines[i + 1]
105         else:
106             i = i + 1
107
108
109 def update_inset_label(document):
110     "Update inset Label."
111     lines = document.body
112     i = 0
113     while True:
114         i = find_token(lines, "\\begin_inset Label", i)
115         if i == -1:
116             return
117         lines[i] = "\\begin_inset LatexCommand \\label{" + lines[i][19:] + "}"
118         i = i + 1
119
120
121 def update_latexdel(document):
122     "Update inset LatexDel."
123     lines = document.body
124     i = 0
125     while True:
126         i = find_token(lines, "\\begin_inset LatexDel", i)
127         if i == -1:
128             return
129         lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
130         i = i + 1
131
132
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")
139
140
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])
148         if result:
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)
152
153         result = added_space_top.search(lines[i])
154         if result:
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)
158
159
160 def remove_cursor(document):
161     "Remove cursor, it is not saved on the file anymore."
162     lines = document.body
163     i = 0
164     cursor_re = re.compile(r".*(\\cursor \d*)")
165     while True:
166         i = find_re(lines, cursor_re, i)
167         if i == -1:
168             break
169         cursor = cursor_re.search(lines[i]).group(1)
170         lines[i] = lines[i].replace(cursor, "")
171         i = i + 1
172
173
174 def remove_empty_insets(document):
175     "Remove empty insets."
176     lines = document.body
177     i = 0
178     while True:
179         i = find_token(lines, "\\begin_inset ", i)
180         if i == -1:
181             break
182         if lines[i] == "\\begin_inset " and lines[i + 1] == "\\end_inset ":
183             del lines[i]
184             del lines[i]
185         i = i + 1
186
187
188 def remove_formula_latex(document):
189     "Remove formula latex."
190     lines = document.body
191     i = 0
192     while True:
193         i = find_token(lines, "\\latex formula_latex ", i)
194         if i == -1:
195             break
196         del lines[i]
197
198         i = find_token(lines, "\\latex default", i)
199         if i == -1:
200             break
201         del lines[i]
202
203
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)
208     if i == -1:
209         lines.append("\\the_end")
210
211
212 def header_update(document):
213     "Update document header."
214     lines = document.header
215     i = 0
216     l = len(lines)
217     while i < l:
218         if lines[i][-1:] == " ":
219             lines[i] = lines[i][:-1]
220
221         if check_token(lines[i], "\\epsfig"):
222             lines[i] = lines[i].replace("\\epsfig", "\\graphics")
223             i = i + 1
224             continue
225
226         if check_token(lines[i], "\\papersize"):
227             size = lines[i].split()[1]
228             new_size = size
229             paperpackage = ""
230
231             if size == "usletter":
232                 new_size = "letterpaper"
233             if size == "a4wide":
234                 new_size = "Default"
235                 paperpackage = "widemarginsa4"
236
237             lines[i] = "\\papersize " + new_size
238             i = i + 1
239             if paperpackage:
240                 lines.insert(i, "\\paperpackage " + paperpackage)
241                 i = i + 1
242
243             lines.insert(i, "\\use_geometry 0")
244             lines.insert(i + 1, "\\use_amsmath 0")
245             i = i + 2
246             continue
247
248         if check_token(lines[i], "\\baselinestretch"):
249             size = lines[i].split()[1]
250             if size == "1.00":
251                 name = "single"
252             elif size == "1.50":
253                 name = "onehalf"
254             elif size == "2.00":
255                 name = "double"
256             else:
257                 name = "other " + size
258             lines[i] = "\\spacing %s " % name
259             i = i + 1
260             continue
261
262         i = i + 1
263
264
265 def update_latexaccents(document):
266     "Update latex accent insets."
267     body = document.body
268     i = 1
269     while True:
270         i = find_token(body, "\\i ", i)
271         if i == -1:
272             return
273
274         contents = body[i][2:].strip()
275
276         if contents.find("{") != -1 and contents.find("}") != -1:
277             i = i + 1
278             continue
279
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:] + "}"
289
290         body[i] = "\\i " + contents
291         i = i + 1
292
293
294 def obsolete_latex_title(document):
295     "Replace layout Latex_Title with Title."
296     body = document.body
297     i = 0
298     while True:
299         i = find_token(body, "\\layout", i)
300         if i == -1:
301             return
302
303         if body[i].lower().find("latex_title") != -1:
304             body[i] = "\\layout Title"
305
306         i = i + 1
307
308
309 def remove_inset_latex(document):
310     "Replace inset latex with layout LaTeX"
311     body = document.body
312
313     i = 0
314     while True:
315         i = find_token(body, "\\begin_inset Latex", i)
316         if i == -1:
317             return
318
319         body[i] = body[i].replace("\\begin_inset Latex", "\\layout LaTeX")
320         i = find_token(body, "\\end_inset", i)
321         if i == -1:
322             # this should not happen
323             return
324         del body[i]
325
326
327 supported_versions = ["0.12.0", "0.12.1", "0.12"]
328 convert = [
329     [
330         215,
331         [
332             header_update,
333             add_end_document,
334             remove_cursor,
335             final_dot,
336             update_inset_label,
337             update_latexdel,
338             update_space_units,
339             space_before_layout,
340             formula_inset_space_eat,
341             update_tabular,
342             update_vfill,
343             remove_empty_insets,
344             remove_formula_latex,
345             update_latexaccents,
346             obsolete_latex_title,
347             remove_inset_latex,
348         ],
349     ]
350 ]
351 revert = []
352
353
354 if __name__ == "__main__":
355     pass