]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
merge booktabs branch
[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 lines[i][-1:] == ' ':
197             lines[i] = lines[i][:-1]
198
199         if check_token(lines[i], '\\epsfig'):
200             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
201             i = i + 1
202             continue
203
204         if check_token(lines[i], '\\papersize'):
205             size = string.split(lines[i])[1]
206             new_size = size
207             paperpackage = ""
208
209             if size == 'usletter':
210                 new_size = 'letterpaper'
211             if size == 'a4wide':
212                 new_size = 'Default'
213                 paperpackage = "widemarginsa4"
214
215             lines[i] = '\\papersize ' + new_size
216             i = i + 1
217             if paperpackage:
218                 lines.insert(i, '\\paperpackage ' + paperpackage)
219                 i = i + 1
220
221             lines.insert(i,'\\use_geometry 0')
222             lines.insert(i + 1,'\\use_amsmath 0')
223             i = i + 2
224             continue
225
226
227         if check_token(lines[i], '\\baselinestretch'):
228             size = string.split(lines[i])[1]
229             if size == '1.00':
230                 name = 'single'
231             elif size == '1.50':
232                 name = 'onehalf'
233             elif size == '2.00':
234                 name = 'double'
235             else:
236                 name = 'other ' + size
237             lines[i] = '\\spacing %s ' % name
238             i = i + 1
239             continue
240
241         i = i + 1
242
243
244 def update_latexaccents(file):
245     body = file.body
246     i = 1
247     while 1:
248         i = find_token(body, '\\i ', i)
249         if i == -1:
250             return
251
252         contents = string.strip(body[i][2:])
253
254         if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
255             i = i + 1
256             continue
257
258         if len(contents) == 2:
259             contents = contents + '{}'
260         elif len(contents) == 3:
261             contents = contents[:2] + '{' + contents[2] + '}'
262         elif len(contents) == 4:
263             if contents[2] == ' ':
264                 contents = contents[:2] + '{' + contents[3] + '}'
265             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
266                 contents = contents[:2] + '{' + contents[2:] + '}'
267
268         body[i] = '\\i ' + contents
269         i = i + 1
270
271
272 def obsolete_latex_title(file):
273     body = file.body
274     i = 0
275     while 1:
276         i = find_token(body, '\\layout', i)
277         if i == -1:
278             return
279
280         if string.find(string.lower(body[i]),'latex_title') != -1:
281             body[i] = '\\layout Title'
282
283         i = i + 1
284
285
286 convert = [[215, [header_update, add_end_document, remove_cursor,
287                   final_dot, update_inset_label, update_latexdel,
288                   update_space_units, space_before_layout,
289                   formula_inset_space_eat, update_tabular,
290                   update_vfill, remove_empty_insets,
291                   remove_formula_latex, update_latexaccents, obsolete_latex_title]]]
292 revert  = []
293
294
295 if __name__ == "__main__":
296     pass