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 1 or 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] + ' '
77 while string.strip(lines[i]):
79 lines[i] = lines[i] + ' 1 1'
80 lines[i] = lines[i] + ' 0 0 0'
83 lines[i] = string.strip(lines[i])
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]
96 def update_inset_label(file):
100 i = find_token(lines, '\\begin_inset Label', i)
103 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
107 def update_latexdel(file):
111 i = find_token(lines, '\\begin_inset LatexDel', i)
114 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
118 def update_vfill(file):
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')
125 def update_space_units(file):
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])
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)
136 result = added_space_top.search(lines[i])
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)
143 def remove_cursor(file):
146 cursor_re = re.compile(r'.*(\\cursor \d*)')
148 i = find_re(lines, cursor_re, i)
151 cursor = cursor_re.search(lines[i]).group(1)
152 lines[i]= string.replace(lines[i], cursor, '')
156 def remove_empty_insets(file):
160 i = find_token(lines, '\\begin_inset ',i)
163 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
169 def remove_formula_latex(file):
173 i = find_token(lines, '\\latex formula_latex ', i)
178 i = find_token(lines, '\\latex default', i)
184 def add_end_document(file):
186 i = find_token(lines, '\\the_end', 0)
188 lines.append('\\the_end')
191 def header_update(file):
196 if check_token(lines[i], '\\begin_preamble'):
197 i = find_token(lines, '\\end_preamble', i)
199 file.error('Unfinished preamble')
203 if lines[i][-1:] == ' ':
204 lines[i] = lines[i][:-1]
206 if check_token(lines[i], '\\epsfig'):
207 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
211 if check_token(lines[i], '\\papersize'):
212 size = string.split(lines[i])[1]
216 if size == 'usletter':
217 new_size = 'letterpaper'
220 paperpackage = "widemarginsa4"
222 lines[i] = '\\papersize ' + new_size
225 lines.insert(i, '\\paperpackage ' + paperpackage)
228 lines.insert(i,'\\use_geometry 0')
229 lines.insert(i + 1,'\\use_amsmath 0')
234 if check_token(lines[i], '\\baselinestretch'):
235 size = string.split(lines[i])[1]
243 name = 'other ' + size
244 lines[i] = '\\spacing %s ' % name
251 def update_latexaccents(file):
255 i = find_token(body, '\\i ', i)
259 contents = string.strip(body[i][2:])
261 if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
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:] + '}'
275 body[i] = '\\i ' + contents
279 def obsolete_latex_title(file):
283 i = find_token(body, '\\layout', i)
287 if string.find(string.lower(body[i]),'latex_title') != -1:
288 body[i] = '\\layout Title'
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]]]
302 if __name__ == "__main__":