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