1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2003 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 *
23 def space_before_layout(lines):
24 i = 2 # skip first layout
26 i = find_token(lines, '\\layout', i)
30 if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
34 def formula_inset_space_eat(lines):
37 i = find_token(lines, "\\begin_inset Formula", i)
40 if len(lines[i]) > 22 and lines[i][21] == ' ':
41 lines[i] = lines[i][:20] + lines[i][21:]
44 # Update from tabular format 2 to 4
45 def update_tabular(lines):
46 lyxtable_re = re.compile(r".*\\LyXTable$")
49 i = find_re(lines, lyxtable_re, i)
57 rows = int(string.split(lines[i])[0])
58 columns = int(string.split(lines[i])[1])
60 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
64 lines[i] = lines[i] + ' 0 0'
67 for j in range(columns):
68 lines[i] = lines[i] + ' '
72 lines[i] = lines[i] + ' 0 0 0'
78 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] != '':
79 lines[i] = lines[i] + lines[i+1]
84 def update_inset_label(lines):
87 i = find_token(lines, '\\begin_inset Label', i)
90 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
93 def update_latexdel(lines):
96 i = find_token(lines, '\\begin_inset LatexDel', i)
99 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
102 def update_vfill(lines):
103 for i in range(len(lines)):
104 lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
105 lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
107 def update_space_units(lines):
108 added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
109 added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
110 for i in range(len(lines)):
111 result = added_space_bottom.search(lines[i])
113 old = '\\added_space_bottom ' + result.group(1)
114 new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
115 lines[i] = string.replace(lines[i], old, new)
117 result = added_space_top.search(lines[i])
119 old = '\\added_space_top ' + result.group(1)
120 new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
121 lines[i] = string.replace(lines[i], old, new)
123 def update_inset_accent(lines):
126 def remove_cursor(lines):
128 cursor_re = re.compile(r'.*(\\cursor \d*)')
130 i = find_re(lines, cursor_re, i)
133 cursor = cursor_re.search(lines[i]).group(1)
134 lines[i]= string.replace(lines[i], cursor, '')
137 def remove_empty_insets(lines):
140 i = find_token(lines, '\\begin_inset ',i)
143 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
148 def remove_formula_latex(lines):
151 i = find_token(lines, '\\latex formula_latex ', i)
156 i = find_token(lines, '\\latex default', i)
161 def add_end_document(lines):
162 lines.append('\\the_end')
164 def header_update(lines):
168 if check_token(lines[i], '\\begin_preamble'):
169 i = find_token(lines, '\\end_preamble', i)
171 sys.stderr.write('Unfinished preamble')
176 if lines[i][-1:] == ' ':
177 lines[i] = lines[i][:-1]
179 if check_token(lines[i], '\\epsfig'):
180 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
184 if check_token(lines[i], '\\papersize'):
185 size = string.split(lines[i])[1]
189 if size == 'usletter':
190 new_size = 'letterpaper'
193 paperpackage = "widemarginsa4"
195 lines[i] = '\\papersize ' + new_size
198 lines.insert(i, '\\paperpackage ' + paperpackage)
201 lines.insert(i,'\\use_geometry 0')
202 lines.insert(i + 1,'\\use_amsmath 0')
207 if check_token(lines[i], '\\baselinestretch'):
208 size = string.split(lines[i])[1]
216 name = 'other ' + size
217 lines[i] = '\\spacing %s ' % name
223 def convert(header,body):
224 header_update(header)
225 add_end_document(body)
228 update_inset_label(body)
229 update_latexdel(body)
230 update_space_units(body)
231 update_inset_accent(body)
232 space_before_layout(body)
233 formula_inset_space_eat(body)
236 remove_empty_insets(body)
237 remove_formula_latex(body)
239 if __name__ == "__main__":