]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
Update it.po
[lyx.git] / lib / lyx2lyx / lyx_0_12.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 """ Convert files to the file format generated by lyx 0.12"""
20
21 import re
22 from parser_tools import find_token, find_re, check_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         
97         if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
98                lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 \
99                and lines[i+1] != '':
100
101             lines[i] = lines[i] + lines[i+1]
102             del lines[i+1]
103         else:
104             i = i + 1
105
106
107 def update_inset_label(document):
108     " Update inset Label."
109     lines = document.body
110     i = 0
111     while True:
112         i = find_token(lines, '\\begin_inset Label', i)
113         if i == -1:
114             return
115         lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
116         i = i + 1
117
118
119 def update_latexdel(document):
120     " Update inset LatexDel."
121     lines = document.body
122     i = 0
123     while True:
124         i = find_token(lines, '\\begin_inset LatexDel', i)
125         if i == -1:
126             return
127         lines[i] = lines[i].replace('\\begin_inset LatexDel',
128                                     '\\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',
137                                     '\\added_space_top vfill')
138         lines[i] = lines[i].replace('\\fill_bottom',
139                                     '\\added_space_bottom vfill')
140
141
142 def update_space_units(document):
143     " Update space units."
144     lines = document.body
145     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
146     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
147     for i in range(len(lines)):
148         result = added_space_bottom.search(lines[i])
149         if result:
150             old = '\\added_space_bottom ' + result.group(1)
151             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
152             lines[i] = lines[i].replace(old, new)
153
154         result = added_space_top.search(lines[i])
155         if result:
156             old = '\\added_space_top ' + result.group(1)
157             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
158             lines[i] = lines[i].replace(old, new)
159
160
161 def remove_cursor(document):
162     " Remove cursor, it is not saved on the file anymore."
163     lines = document.body
164     i = 0
165     cursor_re = re.compile(r'.*(\\cursor \d*)')
166     while True:
167         i = find_re(lines, cursor_re, i)
168         if i == -1:
169             break
170         cursor = cursor_re.search(lines[i]).group(1)
171         lines[i] = lines[i].replace(cursor, '')
172         i = i + 1
173
174
175 def remove_empty_insets(document):
176     " Remove empty insets."
177     lines = document.body
178     i = 0
179     while True:
180         i = find_token(lines, '\\begin_inset ', i)
181         if i == -1:
182             break
183         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
184             del lines[i]
185             del lines[i]
186         i = i + 1
187
188
189 def remove_formula_latex(document):
190     " Remove formula latex."
191     lines = document.body
192     i = 0
193     while True:
194         i = find_token(lines, '\\latex formula_latex ', i)
195         if i == -1:
196             break
197         del lines[i]
198
199         i = find_token(lines, '\\latex default', i)
200         if i == -1:
201             break
202         del lines[i]
203
204
205 def add_end_document(document):
206     " Add \\the_end to the end of the document."
207     lines = document.body
208     i = find_token(lines, '\\the_end', 0)
209     if i == -1:
210         lines.append('\\the_end')
211
212
213 def header_update(document):
214     " Update document header."
215     lines = document.header
216     i = 0
217     l = len(lines)
218     while i < l:
219         if lines[i][-1:] == ' ':
220             lines[i] = lines[i][:-1]
221
222         if check_token(lines[i], '\\epsfig'):
223             lines[i] = lines[i].replace('\\epsfig', '\\graphics')
224             i = i + 1
225             continue
226
227         if check_token(lines[i], '\\papersize'):
228             size = lines[i].split()[1]
229             new_size = size
230             paperpackage = ""
231
232             if size == 'usletter':
233                 new_size = 'letterpaper'
234             if size == 'a4wide':
235                 new_size = 'Default'
236                 paperpackage = "widemarginsa4"
237
238             lines[i] = '\\papersize ' + new_size
239             i = i + 1
240             if paperpackage:
241                 lines.insert(i, '\\paperpackage ' + paperpackage)
242                 i = i + 1
243
244             lines.insert(i,'\\use_geometry 0')
245             lines.insert(i + 1,'\\use_amsmath 0')
246             i = i + 2
247             continue
248
249
250         if check_token(lines[i], '\\baselinestretch'):
251             size = lines[i].split()[1]
252             if size == '1.00':
253                 name = 'single'
254             elif size == '1.50':
255                 name = 'onehalf'
256             elif size == '2.00':
257                 name = 'double'
258             else:
259                 name = 'other ' + size
260             lines[i] = '\\spacing %s ' % name
261             i = i + 1
262             continue
263
264         i = i + 1
265
266
267 def update_latexaccents(document):
268     " Update latex accent insets."
269     body = document.body
270     i = 1
271     while True:
272         i = find_token(body, '\\i ', i)
273         if i == -1:
274             return
275
276         contents = body[i][2:].strip()
277
278         if contents.find('{') != -1 and contents.find('}') != -1:
279             i = i + 1
280             continue
281
282         if len(contents) == 2:
283             contents = contents + '{}'
284         elif len(contents) == 3:
285             contents = contents[:2] + '{' + contents[2] + '}'
286         elif len(contents) == 4:
287             if contents[2] == ' ':
288                 contents = contents[:2] + '{' + contents[3] + '}'
289             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
290                 contents = contents[:2] + '{' + contents[2:] + '}'
291
292         body[i] = '\\i ' + contents
293         i = i + 1
294
295
296 def obsolete_latex_title(document):
297     " Replace layout Latex_Title with Title."
298     body = document.body
299     i = 0
300     while True:
301         i = find_token(body, '\\layout', i)
302         if i == -1:
303             return
304
305         if body[i].lower().find('latex_title') != -1:
306             body[i] = '\\layout Title'
307
308         i = i + 1
309
310
311 def remove_inset_latex(document):
312     "Replace inset latex with layout LaTeX"
313     body = document.body
314
315     i = 0
316     while True:
317         i = find_token(body, '\\begin_inset Latex', i)
318         if i == -1:
319             return
320
321         body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX')
322         i = find_token(body, '\\end_inset', i)
323         if i == -1:
324             #this should not happen
325             return
326         del body[i]
327         
328     
329 supported_versions = ["0.12.0","0.12.1","0.12"]
330 convert = [[215, [header_update, add_end_document, remove_cursor,
331                   final_dot, update_inset_label, update_latexdel,
332                   update_space_units, space_before_layout,
333                   formula_inset_space_eat, update_tabular,
334                   update_vfill, remove_empty_insets,
335                   remove_formula_latex, update_latexaccents,
336                   obsolete_latex_title, remove_inset_latex]]]
337 revert  = []
338
339
340 if __name__ == "__main__":
341     pass