1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 """ Convert files to the file format generated by lyx 0.12"""
22 from parser_tools import find_token, find_re, check_token
25 def space_before_layout(document):
26 " Remove empty line before \\layout. "
28 i = 2 # skip first layout
30 i = find_token(lines, '\\layout', i)
34 prot_space = lines[i-2].find('\\protected_separator')
35 if lines[i - 1] == '' and prot_space == -1:
40 def formula_inset_space_eat(document):
41 " Remove space after inset formula."
45 i = find_token(lines, "\\begin_inset Formula", i)
49 if len(lines[i]) > 22 and lines[i][21] == ' ':
50 lines[i] = lines[i][:20] + lines[i][21:]
54 def update_tabular(document):
55 " Update from tabular format 1 or 2 to 4."
57 lyxtable_re = re.compile(r".*\\LyXTable$")
60 i = find_re(lines, lyxtable_re, i)
66 lines[i] = 'multicol4'
68 rows = int(lines[i].split()[0])
69 columns = int(lines[i].split()[1])
71 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
75 lines[i] = lines[i] + ' 0 0'
78 for j in range(columns):
79 lines[i] = lines[i] + ' '
82 while lines[i].strip():
84 lines[i] = lines[i] + ' 1 1'
85 lines[i] = lines[i] + ' 0 0 0'
88 lines[i] = lines[i].strip()
91 def final_dot(document):
92 " Merge lines if the dot is the final character."
97 if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
98 lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 \
101 lines[i] = lines[i] + lines[i+1]
107 def update_inset_label(document):
108 " Update inset Label."
109 lines = document.body
112 i = find_token(lines, '\\begin_inset Label', i)
115 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
119 def update_latexdel(document):
120 " Update inset LatexDel."
121 lines = document.body
124 i = find_token(lines, '\\begin_inset LatexDel', i)
127 lines[i] = lines[i].replace('\\begin_inset LatexDel',
128 '\\begin_inset LatexCommand')
132 def update_vfill(document):
133 " Update fill_top and fill_bottom."
134 lines = document.body
135 for i in range(len(lines)):
136 lines[i] = lines[i].replace('\\fill_top',
137 '\\added_space_top vfill')
138 lines[i] = lines[i].replace('\\fill_bottom',
139 '\\added_space_bottom vfill')
142 def update_space_units(document):
143 " Update space units."
144 lines = document.body
145 added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
146 added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
147 for i in range(len(lines)):
148 result = added_space_bottom.search(lines[i])
150 old = '\\added_space_bottom ' + result.group(1)
151 new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
152 lines[i] = lines[i].replace(old, new)
154 result = added_space_top.search(lines[i])
156 old = '\\added_space_top ' + result.group(1)
157 new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
158 lines[i] = lines[i].replace(old, new)
161 def remove_cursor(document):
162 " Remove cursor, it is not saved on the file anymore."
163 lines = document.body
165 cursor_re = re.compile(r'.*(\\cursor \d*)')
167 i = find_re(lines, cursor_re, i)
170 cursor = cursor_re.search(lines[i]).group(1)
171 lines[i] = lines[i].replace(cursor, '')
175 def remove_empty_insets(document):
176 " Remove empty insets."
177 lines = document.body
180 i = find_token(lines, '\\begin_inset ', i)
183 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
189 def remove_formula_latex(document):
190 " Remove formula latex."
191 lines = document.body
194 i = find_token(lines, '\\latex formula_latex ', i)
199 i = find_token(lines, '\\latex default', i)
205 def add_end_document(document):
206 " Add \\the_end to the end of the document."
207 lines = document.body
208 i = find_token(lines, '\\the_end', 0)
210 lines.append('\\the_end')
213 def header_update(document):
214 " Update document header."
215 lines = document.header
219 if lines[i][-1:] == ' ':
220 lines[i] = lines[i][:-1]
222 if check_token(lines[i], '\\epsfig'):
223 lines[i] = lines[i].replace('\\epsfig', '\\graphics')
227 if check_token(lines[i], '\\papersize'):
228 size = lines[i].split()[1]
232 if size == 'usletter':
233 new_size = 'letterpaper'
236 paperpackage = "widemarginsa4"
238 lines[i] = '\\papersize ' + new_size
241 lines.insert(i, '\\paperpackage ' + paperpackage)
244 lines.insert(i,'\\use_geometry 0')
245 lines.insert(i + 1,'\\use_amsmath 0')
250 if check_token(lines[i], '\\baselinestretch'):
251 size = lines[i].split()[1]
259 name = 'other ' + size
260 lines[i] = '\\spacing %s ' % name
267 def update_latexaccents(document):
268 " Update latex accent insets."
272 i = find_token(body, '\\i ', i)
276 contents = body[i][2:].strip()
278 if contents.find('{') != -1 and contents.find('}') != -1:
282 if len(contents) == 2:
283 contents = contents + '{}'
284 elif len(contents) == 3:
285 contents = contents[:2] + '{' + contents[2] + '}'
286 elif len(contents) == 4:
287 if contents[2] == ' ':
288 contents = contents[:2] + '{' + contents[3] + '}'
289 elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
290 contents = contents[:2] + '{' + contents[2:] + '}'
292 body[i] = '\\i ' + contents
296 def obsolete_latex_title(document):
297 " Replace layout Latex_Title with Title."
301 i = find_token(body, '\\layout', i)
305 if body[i].lower().find('latex_title') != -1:
306 body[i] = '\\layout Title'
311 def remove_inset_latex(document):
312 "Replace inset latex with layout LaTeX"
317 i = find_token(body, '\\begin_inset Latex', i)
321 body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX')
322 i = find_token(body, '\\end_inset', i)
324 #this should not happen
329 supported_versions = ["0.12.0","0.12.1","0.12"]
330 convert = [[215, [header_update, add_end_document, remove_cursor,
331 final_dot, update_inset_label, update_latexdel,
332 update_space_units, space_before_layout,
333 formula_inset_space_eat, update_tabular,
334 update_vfill, remove_empty_insets,
335 remove_formula_latex, update_latexaccents,
336 obsolete_latex_title, remove_inset_latex]]]
340 if __name__ == "__main__":