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