1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
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.
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.
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.
21 from parser_tools import find_token, find_re, check_token
24 def space_before_layout(file):
26 i = 2 # skip first layout
28 i = find_token(lines, '\\layout', i)
32 if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
37 def formula_inset_space_eat(file):
41 i = find_token(lines, "\\begin_inset Formula", i)
44 if len(lines[i]) > 22 and lines[i][21] == ' ':
45 lines[i] = lines[i][:20] + lines[i][21:]
49 # Update from tabular format 2 to 4
50 def update_tabular(file):
52 lyxtable_re = re.compile(r".*\\LyXTable$")
55 i = find_re(lines, lyxtable_re, i)
63 rows = int(string.split(lines[i])[0])
64 columns = int(string.split(lines[i])[1])
66 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
70 lines[i] = lines[i] + ' 0 0'
73 for j in range(columns):
74 lines[i] = lines[i] + ' '
78 lines[i] = lines[i] + ' 0 0 0'
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]
93 def update_inset_label(file):
97 i = find_token(lines, '\\begin_inset Label', i)
100 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
104 def update_latexdel(file):
108 i = find_token(lines, '\\begin_inset LatexDel', i)
111 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
115 def update_vfill(file):
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')
122 def update_space_units(file):
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])
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)
133 result = added_space_top.search(lines[i])
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)
140 def remove_cursor(file):
143 cursor_re = re.compile(r'.*(\\cursor \d*)')
145 i = find_re(lines, cursor_re, i)
148 cursor = cursor_re.search(lines[i]).group(1)
149 lines[i]= string.replace(lines[i], cursor, '')
153 def remove_empty_insets(file):
157 i = find_token(lines, '\\begin_inset ',i)
160 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
166 def remove_formula_latex(file):
170 i = find_token(lines, '\\latex formula_latex ', i)
175 i = find_token(lines, '\\latex default', i)
181 def add_end_document(file):
183 i = find_token(lines, '\\the_end', 0)
185 lines.append('\\the_end')
188 def header_update(file):
193 if check_token(lines[i], '\\begin_preamble'):
194 i = find_token(lines, '\\end_preamble', i)
196 file.error('Unfinished preamble')
200 if lines[i][-1:] == ' ':
201 lines[i] = lines[i][:-1]
203 if check_token(lines[i], '\\epsfig'):
204 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
208 if check_token(lines[i], '\\papersize'):
209 size = string.split(lines[i])[1]
213 if size == 'usletter':
214 new_size = 'letterpaper'
217 paperpackage = "widemarginsa4"
219 lines[i] = '\\papersize ' + new_size
222 lines.insert(i, '\\paperpackage ' + paperpackage)
225 lines.insert(i,'\\use_geometry 0')
226 lines.insert(i + 1,'\\use_amsmath 0')
231 if check_token(lines[i], '\\baselinestretch'):
232 size = string.split(lines[i])[1]
240 name = 'other ' + size
241 lines[i] = '\\spacing %s ' % name
248 def update_latexaccents(file):
252 i = find_token(body, '\\i ', i)
256 contents = string.strip(body[i][2:])
258 if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
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:] + '}'
272 body[i] = '\\i ' + contents
276 def obsolete_latex_title(file):
280 i = find_token(body, '\\layout', i)
284 if string.find(string.lower(body[i]),'latex_title') != -1:
285 body[i] = '\\layout Title'
290 convert = [[215, [header_update, add_end_document, remove_cursor,
291 final_dot, update_inset_label, update_latexdel,
292 update_space_units, space_before_layout,
293 formula_inset_space_eat, update_tabular,
294 update_vfill, remove_empty_insets,
295 remove_formula_latex, update_latexaccents, obsolete_latex_title]]]
299 if __name__ == "__main__":