]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
7eedd02f1aab873818f719be1d062fe4be084cbf
[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 remove_cursor(file):
141     lines = file.body
142     i = 0
143     cursor_re = re.compile(r'.*(\\cursor \d*)')
144     while 1:
145         i = find_re(lines, cursor_re, i)
146         if i == -1:
147             break
148         cursor = cursor_re.search(lines[i]).group(1)
149         lines[i]= string.replace(lines[i], cursor, '')
150         i = i + 1
151
152
153 def remove_empty_insets(file):
154     lines = file.body
155     i = 0
156     while 1:
157         i = find_token(lines, '\\begin_inset ',i)
158         if i == -1:
159             break
160         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
161             del lines[i]
162             del lines[i]
163         i = i + 1
164
165
166 def remove_formula_latex(file):
167     lines = file.body
168     i = 0
169     while 1:
170         i = find_token(lines, '\\latex formula_latex ', i)
171         if i == -1:
172             break
173         del lines[i]
174
175         i = find_token(lines, '\\latex default', i)
176         if i == -1:
177             break
178         del lines[i]
179
180
181 def add_end_document(file):
182     lines = file.body
183     i = find_token(lines, '\\the_end', 0)
184     if i == -1:
185         lines.append('\\the_end')
186
187
188 def header_update(file):
189     lines = file.header
190     i = 0
191     l = len(lines)
192     while i < l:
193         if check_token(lines[i], '\\begin_preamble'):
194             i = find_token(lines, '\\end_preamble', i)
195             if i == -1:
196                 file.error('Unfinished preamble')
197             i = i + 1
198             continue
199
200         if lines[i][-1:] == ' ':
201             lines[i] = lines[i][:-1]
202
203         if check_token(lines[i], '\\epsfig'):
204             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
205             i = i + 1
206             continue
207
208         if check_token(lines[i], '\\papersize'):
209             size = string.split(lines[i])[1]
210             new_size = size
211             paperpackage = ""
212
213             if size == 'usletter':
214                 new_size = 'letterpaper'
215             if size == 'a4wide':
216                 new_size = 'Default'
217                 paperpackage = "widemarginsa4"
218
219             lines[i] = '\\papersize ' + new_size
220             i = i + 1
221             if paperpackage:
222                 lines.insert(i, '\\paperpackage ' + paperpackage)
223                 i = i + 1
224
225             lines.insert(i,'\\use_geometry 0')
226             lines.insert(i + 1,'\\use_amsmath 0')
227             i = i + 2
228             continue
229
230
231         if check_token(lines[i], '\\baselinestretch'):
232             size = string.split(lines[i])[1]
233             if size == '1.00':
234                 name = 'single'
235             elif size == '1.50':
236                 name = 'onehalf'
237             elif size == '2.00':
238                 name = 'double'
239             else:
240                 name = 'other ' + size
241             lines[i] = '\\spacing %s ' % name
242             i = i + 1
243             continue
244
245         i = i + 1
246
247
248 def update_latexaccents(file):
249     body = file.body
250     i = 1
251     while 1:
252         i = find_token(body, '\\i ', i)
253         if i == -1:
254             return
255
256         contents = string.strip(body[i][2:])
257
258         if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
259             i = i + 1
260             continue
261
262         if len(contents) == 2:
263             contents = contents + '{}'
264         elif len(contents) == 3:
265             contents = contents[:2] + '{' + contents[2] + '}'
266         elif len(contents) == 4:
267             if contents[2] == ' ':
268                 contents = contents[:2] + '{' + contents[3] + '}'
269             elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
270                 contents = contents[:2] + '{' + contents[2:] + '}'
271
272         body[i] = '\\i ' + contents
273         i = i + 1
274
275
276 convert = [[215, [header_update, add_end_document, remove_cursor,
277                   final_dot, update_inset_label, update_latexdel,
278                   update_space_units, space_before_layout,
279                   formula_inset_space_eat, update_tabular,
280                   update_vfill, remove_empty_insets,
281                   remove_formula_latex, update_latexaccents]]]
282 revert  = []
283
284
285 if __name__ == "__main__":
286     pass