]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
bb89fb57bd6da6707cc88fe80c13dcea9400d103
[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 1 or 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 string.strip(lines[i]):
78             if not format:
79                 lines[i] = lines[i] + ' 1 1'
80             lines[i] = lines[i] + ' 0 0 0'
81             i = i + 1
82
83         lines[i] = string.strip(lines[i])
84
85 def final_dot(file):
86     lines = file.body
87     i = 0
88     while i < len(lines):
89         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] != '':
90             lines[i] = lines[i] + lines[i+1]
91             del lines[i+1]
92         else:
93             i = i + 1
94
95
96 def update_inset_label(file):
97     lines = file.body
98     i = 0
99     while 1:
100         i = find_token(lines, '\\begin_inset Label', i)
101         if i == -1:
102             return
103         lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
104         i = i + 1
105
106
107 def update_latexdel(file):
108     lines = file.body
109     i = 0
110     while 1:
111         i = find_token(lines, '\\begin_inset LatexDel', i)
112         if i == -1:
113             return
114         lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
115         i = i + 1
116
117
118 def update_vfill(file):
119     lines = file.body
120     for i in range(len(lines)):
121         lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
122         lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
123
124
125 def update_space_units(file):
126     lines = file.body
127     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
128     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
129     for i in range(len(lines)):
130         result = added_space_bottom.search(lines[i])
131         if result:
132             old = '\\added_space_bottom ' + result.group(1)
133             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
134             lines[i] = string.replace(lines[i], old, new)
135
136         result = added_space_top.search(lines[i])
137         if result:
138             old = '\\added_space_top ' + result.group(1)
139             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
140             lines[i] = string.replace(lines[i], old, new)
141
142
143 def remove_cursor(file):
144     lines = file.body
145     i = 0
146     cursor_re = re.compile(r'.*(\\cursor \d*)')
147     while 1:
148         i = find_re(lines, cursor_re, i)
149         if i == -1:
150             break
151         cursor = cursor_re.search(lines[i]).group(1)
152         lines[i]= string.replace(lines[i], cursor, '')
153         i = i + 1
154
155
156 def remove_empty_insets(file):
157     lines = file.body
158     i = 0
159     while 1:
160         i = find_token(lines, '\\begin_inset ',i)
161         if i == -1:
162             break
163         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
164             del lines[i]
165             del lines[i]
166         i = i + 1
167
168
169 def remove_formula_latex(file):
170     lines = file.body
171     i = 0
172     while 1:
173         i = find_token(lines, '\\latex formula_latex ', i)
174         if i == -1:
175             break
176         del lines[i]
177
178         i = find_token(lines, '\\latex default', i)
179         if i == -1:
180             break
181         del lines[i]
182
183
184 def add_end_document(file):
185     lines = file.body
186     i = find_token(lines, '\\the_end', 0)
187     if i == -1:
188         lines.append('\\the_end')
189
190
191 def header_update(file):
192     lines = file.header
193     i = 0
194     l = len(lines)
195     while i < l:
196         if check_token(lines[i], '\\begin_preamble'):
197             i = find_token(lines, '\\end_preamble', i)
198             if i == -1:
199                 file.error('Unfinished preamble')
200             i = i + 1
201             continue
202
203         if lines[i][-1:] == ' ':
204             lines[i] = lines[i][:-1]
205
206         if check_token(lines[i], '\\epsfig'):
207             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
208             i = i + 1
209             continue
210
211         if check_token(lines[i], '\\papersize'):
212             size = string.split(lines[i])[1]
213             new_size = size
214             paperpackage = ""
215
216             if size == 'usletter':
217                 new_size = 'letterpaper'
218             if size == 'a4wide':
219                 new_size = 'Default'
220                 paperpackage = "widemarginsa4"
221
222             lines[i] = '\\papersize ' + new_size
223             i = i + 1
224             if paperpackage:
225                 lines.insert(i, '\\paperpackage ' + paperpackage)
226                 i = i + 1
227
228             lines.insert(i,'\\use_geometry 0')
229             lines.insert(i + 1,'\\use_amsmath 0')
230             i = i + 2
231             continue
232
233
234         if check_token(lines[i], '\\baselinestretch'):
235             size = string.split(lines[i])[1]
236             if size == '1.00':
237                 name = 'single'
238             elif size == '1.50':
239                 name = 'onehalf'
240             elif size == '2.00':
241                 name = 'double'
242             else:
243                 name = 'other ' + size
244             lines[i] = '\\spacing %s ' % name
245             i = i + 1
246             continue
247
248         i = i + 1
249
250
251 def update_latexaccents(file):
252     body = file.body
253     i = 1
254     while 1:
255         i = find_token(body, '\\i ', i)
256         if i == -1:
257             return
258
259         contents = string.strip(body[i][2:])
260
261         if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
262             i = i + 1
263             continue
264
265         if len(contents) == 2:
266             contents = contents + '{}'
267         elif len(contents) == 3:
268             contents = contents[:2] + '{' + contents[2] + '}'
269         elif len(contents) == 4:
270             if contents[2] == ' ':
271                 contents = contents[:2] + '{' + contents[3] + '}'
272             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
273                 contents = contents[:2] + '{' + contents[2:] + '}'
274
275         body[i] = '\\i ' + contents
276         i = i + 1
277
278
279 def obsolete_latex_title(file):
280     body = file.body
281     i = 0
282     while 1:
283         i = find_token(body, '\\layout', i)
284         if i == -1:
285             return
286
287         if string.find(string.lower(body[i]),'latex_title') != -1:
288             body[i] = '\\layout Title'
289
290         i = i + 1
291
292
293 convert = [[215, [header_update, add_end_document, remove_cursor,
294                   final_dot, update_inset_label, update_latexdel,
295                   update_space_units, space_before_layout,
296                   formula_inset_space_eat, update_tabular,
297                   update_vfill, remove_empty_insets,
298                   remove_formula_latex, update_latexaccents, obsolete_latex_title]]]
299 revert  = []
300
301
302 if __name__ == "__main__":
303     pass