]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
59a0b8cbeff7da31fc4ed6f0ebb28492d4013b32
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 """ Convert files to the file format generated by lyx 0.12"""
20
21 import re
22 import string
23 from parser_tools import find_token, find_re, check_token
24
25
26 def space_before_layout(document):
27     " Remove empty line before \\layout. "
28     lines = document.body
29     i = 2 # skip first layout
30     while 1:
31         i = find_token(lines, '\\layout', i)
32         if i == -1:
33             break
34
35         prot_space = string.find(lines[i-2],'\\protected_separator')
36         if lines[i - 1] == '' and prot_space == -1:
37             del lines[i-1]
38         i = i + 1
39
40
41 def formula_inset_space_eat(document):
42     " Remove space after inset formula."
43     lines = document.body
44     i = 0
45     while 1:
46         i = find_token(lines, "\\begin_inset Formula", i)
47         if i == -1:
48             break
49
50         if len(lines[i]) > 22 and lines[i][21] == ' ':
51             lines[i] = lines[i][:20] + lines[i][21:]
52         i = i + 1
53
54
55 def update_tabular(document):
56     " Update from tabular format 1 or 2 to 4."
57     lines = document.body
58     lyxtable_re = re.compile(r".*\\LyXTable$")
59     i = 0
60     while 1:
61         i = find_re(lines, lyxtable_re, i)
62         if i == -1:
63             break
64         i = i + 1
65         format = lines[i][8:]
66
67         lines[i] = 'multicol4'
68         i = i + 1
69         rows = int(string.split(lines[i])[0])
70         columns = int(string.split(lines[i])[1])
71
72         lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
73         i = i + 1
74
75         for j in range(rows):
76             lines[i] = lines[i] + ' 0 0'
77             i = i + 1
78
79         for j in range(columns):
80             lines[i] = lines[i] + ' '
81             i = i + 1
82
83         while string.strip(lines[i]):
84             if not format:
85                 lines[i] = lines[i] + ' 1 1'
86             lines[i] = lines[i] + ' 0 0 0'
87             i = i + 1
88
89         lines[i] = string.strip(lines[i])
90
91
92 def final_dot(document):
93     " Merge lines if the dot is the final character."
94     lines = document.body
95     i = 0
96     while i < len(lines):
97         
98         if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
99                lines[i+1][:1] != ' ' 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 1:
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 1:
125         i = find_token(lines, '\\begin_inset LatexDel', i)
126         if i == -1:
127             return
128         lines[i] = string.replace(lines[i],
129                                   '\\begin_inset LatexDel',
130                                   '\\begin_inset LatexCommand')
131         i = i + 1
132
133
134 def update_vfill(document):
135     " Update fill_top and fill_bottom."
136     lines = document.body
137     for i in range(len(lines)):
138         lines[i] = string.replace(lines[i],
139                                   '\\fill_top',
140                                   '\\added_space_top vfill')
141         lines[i] = string.replace(lines[i],
142                                   '\\fill_bottom',
143                                   '\\added_space_bottom vfill')
144
145
146 def update_space_units(document):
147     " Update space units."
148     lines = document.body
149     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
150     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
151     for i in range(len(lines)):
152         result = added_space_bottom.search(lines[i])
153         if result:
154             old = '\\added_space_bottom ' + result.group(1)
155             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
156             lines[i] = string.replace(lines[i], old, new)
157
158         result = added_space_top.search(lines[i])
159         if result:
160             old = '\\added_space_top ' + result.group(1)
161             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
162             lines[i] = string.replace(lines[i], old, new)
163
164
165 def remove_cursor(document):
166     " Remove cursor, it is not saved on the file anymore."
167     lines = document.body
168     i = 0
169     cursor_re = re.compile(r'.*(\\cursor \d*)')
170     while 1:
171         i = find_re(lines, cursor_re, i)
172         if i == -1:
173             break
174         cursor = cursor_re.search(lines[i]).group(1)
175         lines[i] = string.replace(lines[i], cursor, '')
176         i = i + 1
177
178
179 def remove_empty_insets(document):
180     " Remove empty insets."
181     lines = document.body
182     i = 0
183     while 1:
184         i = find_token(lines, '\\begin_inset ', i)
185         if i == -1:
186             break
187         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
188             del lines[i]
189             del lines[i]
190         i = i + 1
191
192
193 def remove_formula_latex(document):
194     " Remove formula latex."
195     lines = document.body
196     i = 0
197     while 1:
198         i = find_token(lines, '\\latex formula_latex ', i)
199         if i == -1:
200             break
201         del lines[i]
202
203         i = find_token(lines, '\\latex default', i)
204         if i == -1:
205             break
206         del lines[i]
207
208
209 def add_end_document(document):
210     " Add \\the_end to the end of the document."
211     lines = document.body
212     i = find_token(lines, '\\the_end', 0)
213     if i == -1:
214         lines.append('\\the_end')
215
216
217 def header_update(document):
218     " Update document header."
219     lines = document.header
220     i = 0
221     l = len(lines)
222     while i < l:
223         if lines[i][-1:] == ' ':
224             lines[i] = lines[i][:-1]
225
226         if check_token(lines[i], '\\epsfig'):
227             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
228             i = i + 1
229             continue
230
231         if check_token(lines[i], '\\papersize'):
232             size = string.split(lines[i])[1]
233             new_size = size
234             paperpackage = ""
235
236             if size == 'usletter':
237                 new_size = 'letterpaper'
238             if size == 'a4wide':
239                 new_size = 'Default'
240                 paperpackage = "widemarginsa4"
241
242             lines[i] = '\\papersize ' + new_size
243             i = i + 1
244             if paperpackage:
245                 lines.insert(i, '\\paperpackage ' + paperpackage)
246                 i = i + 1
247
248             lines.insert(i,'\\use_geometry 0')
249             lines.insert(i + 1,'\\use_amsmath 0')
250             i = i + 2
251             continue
252
253
254         if check_token(lines[i], '\\baselinestretch'):
255             size = string.split(lines[i])[1]
256             if size == '1.00':
257                 name = 'single'
258             elif size == '1.50':
259                 name = 'onehalf'
260             elif size == '2.00':
261                 name = 'double'
262             else:
263                 name = 'other ' + size
264             lines[i] = '\\spacing %s ' % name
265             i = i + 1
266             continue
267
268         i = i + 1
269
270
271 def update_latexaccents(document):
272     " Update latex accent insets."
273     body = document.body
274     i = 1
275     while 1:
276         i = find_token(body, '\\i ', i)
277         if i == -1:
278             return
279
280         contents = string.strip(body[i][2:])
281
282         if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
283             i = i + 1
284             continue
285
286         if len(contents) == 2:
287             contents = contents + '{}'
288         elif len(contents) == 3:
289             contents = contents[:2] + '{' + contents[2] + '}'
290         elif len(contents) == 4:
291             if contents[2] == ' ':
292                 contents = contents[:2] + '{' + contents[3] + '}'
293             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
294                 contents = contents[:2] + '{' + contents[2:] + '}'
295
296         body[i] = '\\i ' + contents
297         i = i + 1
298
299
300 def obsolete_latex_title(document):
301     " Replace layout Latex_Title with Title."
302     body = document.body
303     i = 0
304     while 1:
305         i = find_token(body, '\\layout', i)
306         if i == -1:
307             return
308
309         if string.find(string.lower(body[i]),'latex_title') != -1:
310             body[i] = '\\layout Title'
311
312         i = i + 1
313
314
315 supported_versions = ["0.12.0","0.12.1","0.12"]
316 convert = [[215, [header_update, add_end_document, remove_cursor,
317                   final_dot, update_inset_label, update_latexdel,
318                   update_space_units, space_before_layout,
319                   formula_inset_space_eat, update_tabular,
320                   update_vfill, remove_empty_insets,
321                   remove_formula_latex, update_latexaccents,
322                   obsolete_latex_title]]]
323 revert  = []
324
325
326 if __name__ == "__main__":
327     pass