1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 from parser_tools import *
22 def space_before_layout(lines):
23 i = 2 # skip first layout
25 i = find_token(lines, '\\layout', i)
29 if lines[i - 1] == '':
33 def formula_inset_space_eat(lines):
36 i = find_token(lines, "\\begin_inset Formula", i)
39 if len(lines[i]) > 22:
40 lines[i] = lines[i][:20] + lines[i][21:]
43 # Update from tabular format 2 to 4
44 def update_tabular(lines):
45 lyxtable_re = re.compile(r".*\\LyXTable$")
48 i = find_re(lines, lyxtable_re, i)
56 rows = int(string.split(lines[i])[0])
57 columns = int(string.split(lines[i])[1])
59 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
63 lines[i] = lines[i] + ' 0 0'
66 for j in range(columns):
67 lines[i] = lines[i] + ' '
71 lines[i] = lines[i] + ' 0 0 0'
77 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] != '':
78 lines[i] = lines[i] + lines[i+1]
83 def update_inset_label(lines):
86 i = find_token(lines, '\\begin_inset Label', i)
89 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
92 def update_latexdel(lines):
95 i = find_token(lines, '\\begin_inset LatexDel', i)
98 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
101 def update_vfill(lines):
102 for i in range(len(lines)):
103 lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
104 lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
106 def update_space_units(lines):
107 added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
108 added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
109 for i in range(len(lines)):
110 result = added_space_bottom.search(lines[i])
112 old = '\\added_space_bottom ' + result.group(1)
113 new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
114 lines[i] = string.replace(lines[i], old, new)
116 result = added_space_top.search(lines[i])
118 old = '\\added_space_top ' + result.group(1)
119 new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
120 lines[i] = string.replace(lines[i], old, new)
122 def update_inset_accent(lines):
125 def remove_cursor(lines):
128 i = find_token(lines, '\\cursor', i)
134 def add_end_document(lines):
135 lines.append('\\the_end')
137 def header_update(lines):
141 if check_token(lines[i], '\\begin_preamble'):
142 i = find_token(lines, '\\end_preamble', i)
144 sys.stderr.write('Unfinished preamble')
149 if lines[i][-1:] == ' ':
150 lines[i] = lines[i][:-1]
152 if check_token(lines[i], '\\epsfig'):
153 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
157 if check_token(lines[i], '\\papersize'):
158 size = string.split(lines[i])[1]
159 if size == 'usletter':
160 lines[i] = '\\papersize letterpaper'
164 if check_token(lines[i], '\\baselinestretch'):
165 size = string.split(lines[i])[1]
172 lines[i] = '\\spacing %s ' % name
178 lines.append('\\paperpackage a4')
179 lines.append('\\use_geometry 0')
180 lines.append('\\use_amsmath 0')
181 lines.append('\\paperorientation portrait')
184 def convert(header,body):
185 header_update(header)
186 add_end_document(body)
189 update_inset_label(body)
190 update_latexdel(body)
191 update_space_units(body)
192 update_inset_accent(body)
193 space_before_layout(body)
194 formula_inset_space_eat(body)
198 if __name__ == "__main__":