]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
31d38b66e4506d1b1a0dd70b981b40803c6f03cd
[lyx.git] / lib / lyx2lyx / lyx_0_12.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
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 import re
20 import string
21 from parser_tools import find_token, find_re, check_token
22
23
24 def space_before_layout(file):
25     lines = file.body
26     i = 2 # skip first layout
27     while 1:
28         i = find_token(lines, '\\layout', i)
29         if i == -1:
30             break
31
32         if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
33             del lines[i-1]
34         i = i + 1
35
36
37 def formula_inset_space_eat(file):
38     lines = file.body
39     i=0
40     while 1:
41         i = find_token(lines, "\\begin_inset Formula", i)
42         if i == -1: break
43
44         if len(lines[i]) > 22 and lines[i][21] == ' ':
45             lines[i] = lines[i][:20] + lines[i][21:]
46         i = i + 1
47
48
49 # Update from tabular format 2 to 4
50 def update_tabular(file):
51     lines = file.body
52     lyxtable_re = re.compile(r".*\\LyXTable$")
53     i=0
54     while 1:
55         i = find_re(lines, lyxtable_re, i)
56         if i == -1:
57             break
58         i = i + 1
59         format = lines[i][8]
60
61         lines[i]='multicol4'
62         i = i + 1
63         rows = int(string.split(lines[i])[0])
64         columns = int(string.split(lines[i])[1])
65
66         lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
67         i = i + 1
68
69         for j in range(rows):
70             lines[i] = lines[i] + ' 0 0'
71             i = i + 1
72
73         for j in range(columns):
74             lines[i] = lines[i] + ' '
75             i = i + 1
76
77         while lines[i]:
78             lines[i] = lines[i] + ' 0 0 0'
79             i = i + 1
80
81
82 def final_dot(file):
83     lines = file.body
84     i = 0
85     while i < len(lines):
86         if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and  lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 and lines[i+1] != '':
87             lines[i] = lines[i] + lines[i+1]
88             del lines[i+1]
89         else:
90             i = i + 1
91
92
93 def update_inset_label(file):
94     lines = file.body
95     i = 0
96     while 1:
97         i = find_token(lines, '\\begin_inset Label', i)
98         if i == -1:
99             return
100         lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
101         i = i + 1
102
103
104 def update_latexdel(file):
105     lines = file.body
106     i = 0
107     while 1:
108         i = find_token(lines, '\\begin_inset LatexDel', i)
109         if i == -1:
110             return
111         lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
112         i = i + 1
113
114
115 def update_vfill(file):
116     lines = file.body
117     for i in range(len(lines)):
118         lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
119         lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
120
121
122 def update_space_units(file):
123     lines = file.body
124     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
125     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
126     for i in range(len(lines)):
127         result = added_space_bottom.search(lines[i])
128         if result:
129             old = '\\added_space_bottom ' + result.group(1)
130             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
131             lines[i] = string.replace(lines[i], old, new)
132
133         result = added_space_top.search(lines[i])
134         if result:
135             old = '\\added_space_top ' + result.group(1)
136             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
137             lines[i] = string.replace(lines[i], old, new)
138
139
140 def update_inset_accent(file):
141     lines = file.body
142     pass
143
144
145 def remove_cursor(file):
146     lines = file.body
147     i = 0
148     cursor_re = re.compile(r'.*(\\cursor \d*)')
149     while 1:
150         i = find_re(lines, cursor_re, i)
151         if i == -1:
152             break
153         cursor = cursor_re.search(lines[i]).group(1)
154         lines[i]= string.replace(lines[i], cursor, '')
155         i = i + 1
156
157
158 def remove_empty_insets(file):
159     lines = file.body
160     i = 0
161     while 1:
162         i = find_token(lines, '\\begin_inset ',i)
163         if i == -1:
164             break
165         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
166             del lines[i]
167             del lines[i]
168         i = i + 1
169
170
171 def remove_formula_latex(file):
172     lines = file.body
173     i = 0
174     while 1:
175         i = find_token(lines, '\\latex formula_latex ', i)
176         if i == -1:
177             break
178         del lines[i]
179
180         i = find_token(lines, '\\latex default', i)
181         if i == -1:
182             break
183         del lines[i]
184
185
186 def add_end_document(file):
187     lines = file.body
188     i = find_token(lines, '\\the_end', 0)
189     if i == -1:
190         lines.append('\\the_end')
191
192
193 def header_update(file):
194     lines = file.header
195     i = 0
196     l = len(lines)
197     while i < l:
198         if check_token(lines[i], '\\begin_preamble'):
199             i = find_token(lines, '\\end_preamble', i)
200             if i == -1:
201                 file.error('Unfinished preamble')
202             i = i + 1
203             continue
204
205         if lines[i][-1:] == ' ':
206             lines[i] = lines[i][:-1]
207
208         if check_token(lines[i], '\\epsfig'):
209             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
210             i = i + 1
211             continue
212
213         if check_token(lines[i], '\\papersize'):
214             size = string.split(lines[i])[1]
215             new_size = size
216             paperpackage = ""
217
218             if size == 'usletter':
219                 new_size = 'letterpaper'
220             if size == 'a4wide':
221                 new_size = 'Default'
222                 paperpackage = "widemarginsa4"
223
224             lines[i] = '\\papersize ' + new_size
225             i = i + 1
226             if paperpackage:
227                 lines.insert(i, '\\paperpackage ' + paperpackage)
228                 i = i + 1
229
230             lines.insert(i,'\\use_geometry 0')
231             lines.insert(i + 1,'\\use_amsmath 0')
232             i = i + 2
233             continue
234
235
236         if check_token(lines[i], '\\baselinestretch'):
237             size = string.split(lines[i])[1]
238             if size == '1.00':
239                 name = 'single'
240             elif size == '1.50':
241                 name = 'onehalf'
242             elif size == '2.00':
243                 name = 'double'
244             else:
245                 name = 'other ' + size
246             lines[i] = '\\spacing %s ' % name
247             i = i + 1
248             continue
249
250         i = i + 1
251
252
253 def update_latexaccents(file):
254     body = file.body
255     i = 1
256     while 1:
257         i = find_token(body, '\\i ', i)
258         if i == -1:
259             return
260
261         contents = string.strip(body[i][2:])
262
263         if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
264             i = i + 1
265             continue
266
267         if len(contents) == 2:
268             contents = contents + '{}'
269         elif len(contents) == 3:
270             contents = contents[:2] + '{' + contents[2] + '}'
271         elif len(contents) == 4:
272             if contents[2] == ' ':
273                 contents = contents[:2] + '{' + contents[3] + '}'
274             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
275                 contents = contents[:2] + '{' + contents[2:] + '}'
276
277         body[i] = '\\i ' + contents
278         i = i + 1
279
280
281 def convert(file):
282     table = [header_update, add_end_document, remove_cursor,
283              final_dot, update_inset_label, update_latexdel,
284              update_space_units, update_inset_accent,
285              space_before_layout, formula_inset_space_eat,
286              update_tabular, update_vfill, remove_empty_insets,
287              remove_formula_latex, update_latexaccents]
288
289     for conv in table:
290         conv(file)
291
292     file.format = 215
293
294
295 def revert(file):
296     file.error("The convertion to an older format (%s) is not implemented." % file.format)
297
298
299 if __name__ == "__main__":
300     pass