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(lines):
25 i = 2 # skip first layout
27 i = find_token(lines, '\\layout', i)
31 if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
36 def formula_inset_space_eat(lines):
39 i = find_token(lines, "\\begin_inset Formula", i)
42 if len(lines[i]) > 22 and lines[i][21] == ' ':
43 lines[i] = lines[i][:20] + lines[i][21:]
47 # Update from tabular format 2 to 4
48 def update_tabular(lines):
49 lyxtable_re = re.compile(r".*\\LyXTable$")
52 i = find_re(lines, lyxtable_re, i)
60 rows = int(string.split(lines[i])[0])
61 columns = int(string.split(lines[i])[1])
63 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
67 lines[i] = lines[i] + ' 0 0'
70 for j in range(columns):
71 lines[i] = lines[i] + ' '
75 lines[i] = lines[i] + ' 0 0 0'
82 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] != '':
83 lines[i] = lines[i] + lines[i+1]
89 def update_inset_label(lines):
92 i = find_token(lines, '\\begin_inset Label', i)
95 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
99 def update_latexdel(lines):
102 i = find_token(lines, '\\begin_inset LatexDel', i)
105 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
109 def update_vfill(lines):
110 for i in range(len(lines)):
111 lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
112 lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
115 def update_space_units(lines):
116 added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
117 added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
118 for i in range(len(lines)):
119 result = added_space_bottom.search(lines[i])
121 old = '\\added_space_bottom ' + result.group(1)
122 new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
123 lines[i] = string.replace(lines[i], old, new)
125 result = added_space_top.search(lines[i])
127 old = '\\added_space_top ' + result.group(1)
128 new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
129 lines[i] = string.replace(lines[i], old, new)
132 def update_inset_accent(lines):
136 def remove_cursor(lines):
138 cursor_re = re.compile(r'.*(\\cursor \d*)')
140 i = find_re(lines, cursor_re, i)
143 cursor = cursor_re.search(lines[i]).group(1)
144 lines[i]= string.replace(lines[i], cursor, '')
148 def remove_empty_insets(lines):
151 i = find_token(lines, '\\begin_inset ',i)
154 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
160 def remove_formula_latex(lines):
163 i = find_token(lines, '\\latex formula_latex ', i)
168 i = find_token(lines, '\\latex default', i)
174 def add_end_document(lines):
175 i = find_token(lines, '\\the_end', 0)
177 lines.append('\\the_end')
180 def header_update(lines, file):
184 if check_token(lines[i], '\\begin_preamble'):
185 i = find_token(lines, '\\end_preamble', i)
187 file.error('Unfinished preamble')
191 if lines[i][-1:] == ' ':
192 lines[i] = lines[i][:-1]
194 if check_token(lines[i], '\\epsfig'):
195 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
199 if check_token(lines[i], '\\papersize'):
200 size = string.split(lines[i])[1]
204 if size == 'usletter':
205 new_size = 'letterpaper'
208 paperpackage = "widemarginsa4"
210 lines[i] = '\\papersize ' + new_size
213 lines.insert(i, '\\paperpackage ' + paperpackage)
216 lines.insert(i,'\\use_geometry 0')
217 lines.insert(i + 1,'\\use_amsmath 0')
222 if check_token(lines[i], '\\baselinestretch'):
223 size = string.split(lines[i])[1]
231 name = 'other ' + size
232 lines[i] = '\\spacing %s ' % name
239 def update_latexaccents(body):
242 i = find_token(body, '\\i ', i)
246 contents = string.strip(body[i][2:])
248 if len(contents) == 2:
249 contents = contents + '{}'
250 elif len(contents) == 3:
251 contents = contents[:2] + '{' + contents[2] + '}'
252 elif len(contents) == 4:
253 if contents[2] == ' ':
254 contents = contents[:2] + '{' + contents[3] + '}'
255 elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
256 contents = contents[:2] + '{' + contents[2:] + '}'
258 body[i] = '\\i ' + contents
263 header_update(file.header, file)
264 add_end_document(file.body)
265 remove_cursor(file.body)
267 update_inset_label(file.body)
268 update_latexdel(file.body)
269 update_space_units(file.body)
270 update_inset_accent(file.body)
271 space_before_layout(file.body)
272 formula_inset_space_eat(file.body)
273 update_tabular(file.body)
274 update_vfill(file.body)
275 remove_empty_insets(file.body)
276 remove_formula_latex(file.body)
277 update_latexaccents(file.body)
282 file.error("The convertion to an older format (%s) is not implemented." % file.format)
285 if __name__ == "__main__":