]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_5.py
Reformat lyx2lyx code using ruff
[lyx.git] / lib / lyx2lyx / lyx_1_1_5.py
1 # This document is part of lyx2lyx
2 # Copyright (C) 2002-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 1.1.5"""
19
20 import re
21 from parser_tools import find_token, find_token_backwards, find_re
22
23 ####################################################################
24 # Private helper functions
25
26
27 def get_layout(line, default_layout):
28     "Get the line layout, beware of the empty layout."
29     tokens = line.split()
30     if len(tokens) > 1:
31         return tokens[1]
32     return default_layout
33
34
35 ####################################################################
36
37 math_env = ["\\[", "\\begin{eqnarray*}", "\\begin{eqnarray}", "\\begin{equation}"]
38
39
40 def replace_protected_separator(document):
41     "Replace protected separator."
42     lines = document.body
43     i = 0
44     while True:
45         i = find_token(lines, "\\protected_separator", i)
46         if i == -1:
47             break
48         j = find_token_backwards(lines, "\\layout", i)
49         # if j == -1: print error
50         layout = get_layout(lines[j], document.default_layout)
51
52         if layout == "LyX-Code":
53             result = ""
54             while lines[i] == "\\protected_separator ":
55                 result = result + " "
56                 del lines[i]
57
58             lines[i - 1] = lines[i - 1] + result + lines[i]
59         else:
60             lines[i - 1] = lines[i - 1] + "\\SpecialChar ~"
61
62         del lines[i]
63
64
65 def merge_formula_inset(document):
66     "Merge formula insets."
67     lines = document.body
68     i = 0
69     while True:
70         i = find_token(lines, "\\begin_inset Formula", i)
71         if i == -1:
72             break
73         if lines[i + 1] in math_env:
74             lines[i] = lines[i] + lines[i + 1]
75             del lines[i + 1]
76         i = i + 1
77
78
79 def update_tabular(document):
80     "Update from tabular format 4 to 5 if necessary."
81     lines = document.body
82     lyxtable_re = re.compile(r".*\\LyXTable$")
83     i = 0
84     while True:
85         i = find_re(lines, lyxtable_re, i)
86         if i == -1:
87             break
88         i = i + 1
89         format = lines[i][8]
90         if format != "4":
91             continue
92
93         lines[i] = "multicol5"
94         i = i + 1
95         rows = int(lines[i].split()[0])
96         columns = int(lines[i].split()[1])
97
98         i = i + rows + 1
99         for j in range(columns):
100             col_info = lines[i].split()
101             if len(col_info) == 3:
102                 lines[i] = lines[i] + '"" ""'
103             else:
104                 lines[i] = " ".join(col_info[:3]) + ' "%s" ""' % col_info[3]
105             i = i + 1
106
107         while lines[i]:
108             lines[i] = lines[i] + ' "" ""'
109             i = i + 1
110
111
112 def update_toc(document):
113     "Update table of contents."
114     lines = document.body
115     i = 0
116     while True:
117         i = find_token(lines, "\\begin_inset LatexCommand \\tableofcontents", i)
118         if i == -1:
119             break
120         lines[i] = lines[i] + "{}"
121         i = i + 1
122
123
124 def remove_cursor(document):
125     "Remove cursor."
126     lines = document.body
127     i = find_token(lines, "\\cursor", 0)
128     if i != -1:
129         del lines[i]
130
131
132 def remove_vcid(document):
133     "Remove \\lyxvcid and \\lyxrcsid."
134     lines = document.header
135     i = find_token(lines, "\\lyxvcid", 0)
136     if i != -1:
137         del lines[i]
138     i = find_token(lines, "\\lyxrcsid", 0)
139     if i != -1:
140         del lines[i]
141
142
143 def first_layout(document):
144     "Fix first layout, if empty use the default layout."
145     lines = document.body
146     while lines[0] == "":
147         del lines[0]
148     if lines[0][:7] != "\\layout":
149         lines[:0] = ["\\layout %s" % document.default_layout, ""]
150
151
152 def remove_space_in_units(document):
153     "Remove space in units."
154     lines = document.header
155     margins = ["\\topmargin", "\\rightmargin", "\\leftmargin", "\\bottommargin"]
156
157     unit_rexp = re.compile(r"[^ ]* (.*) (.*)")
158
159     for margin in margins:
160         i = 0
161         while True:
162             i = find_token(lines, margin, i)
163             if i == -1:
164                 break
165
166             result = unit_rexp.search(lines[i])
167             if result:
168                 lines[i] = margin + " " + result.group(1) + result.group(2)
169             i = i + 1
170
171
172 def latexdel_getargs(document, i):
173     "Get arguments from latexdel insets."
174     lines = document.body
175
176     # play safe, clean empty lines
177     while True:
178         if lines[i]:
179             break
180         del lines[i]
181
182     j = find_token(lines, "\\end_inset", i)
183
184     if i == j:
185         del lines[i]
186     else:
187         document.warning("Unexpected end of inset.")
188     j = find_token(lines, "\\begin_inset LatexDel }{", i)
189
190     ref = " ".join(lines[i:j])
191     del lines[i : j + 1]
192
193     # play safe, clean empty lines
194     while True:
195         if lines[i]:
196             break
197         del lines[i]
198
199     j = find_token(lines, "\\end_inset", i - 1)
200     if i == j:
201         del lines[i]
202     else:
203         document.warning("Unexpected end of inset.")
204     j = find_token(lines, "\\begin_inset LatexDel }", i)
205     label = " ".join(lines[i:j])
206     del lines[i : j + 1]
207
208     return ref, label
209
210
211 def update_ref(document):
212     "Update reference inset."
213     lines = document.body
214     i = 0
215     while True:
216         i = find_token(lines, "\\begin_inset LatexCommand", i)
217         if i == -1:
218             return
219
220         if lines[i].split()[-1] == "\\ref{":
221             i = i + 1
222             ref, label = latexdel_getargs(document, i)
223             lines[i - 1] = f"{lines[i - 1][:-1]}[{ref}]{{{label}}}"
224
225         i = i + 1
226
227
228 def update_latexdel(document):
229     "Remove latexdel insets."
230     lines = document.body
231     i = 0
232     latexdel_re = re.compile(r".*\\begin_inset LatexDel")
233     while True:
234         i = find_re(lines, latexdel_re, i)
235         if i == -1:
236             return
237         lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
238
239         j = lines[i].find("\\begin_inset")
240         lines.insert(i + 1, lines[i][j:])
241         lines[i] = lines[i][:j].strip()
242         i = i + 1
243
244         if lines[i].split()[-1] in ("\\url{", "\\htmlurl{"):
245             i = i + 1
246
247             ref, label = latexdel_getargs(document, i)
248             lines[i - 1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}"
249
250         i = i + 1
251
252
253 supported_versions = ["1.1.5", "1.1.5fix1", "1.1.5fix2", "1.1"]
254 convert = [
255     [
256         216,
257         [
258             first_layout,
259             remove_vcid,
260             remove_cursor,
261             update_toc,
262             replace_protected_separator,
263             merge_formula_inset,
264             update_tabular,
265             remove_space_in_units,
266             update_ref,
267             update_latexdel,
268         ],
269     ]
270 ]
271
272 revert = []
273
274 if __name__ == "__main__":
275     pass