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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 """ Convert files to the file format generated by lyx 0.12"""
23 from parser_tools import find_token, find_re, check_token
26 def space_before_layout(document):
27 " Remove empty line before \\layout. "
29 i = 2 # skip first layout
31 i = find_token(lines, '\\layout', i)
35 prot_space = string.find(lines[i-2],'\\protected_separator')
36 if lines[i - 1] == '' and prot_space == -1:
41 def formula_inset_space_eat(document):
42 " Remove space after inset formula."
46 i = find_token(lines, "\\begin_inset Formula", i)
50 if len(lines[i]) > 22 and lines[i][21] == ' ':
51 lines[i] = lines[i][:20] + lines[i][21:]
55 def update_tabular(document):
56 " Update from tabular format 1 or 2 to 4."
58 lyxtable_re = re.compile(r".*\\LyXTable$")
61 i = find_re(lines, lyxtable_re, i)
67 lines[i] = 'multicol4'
69 rows = int(string.split(lines[i])[0])
70 columns = int(string.split(lines[i])[1])
72 lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
76 lines[i] = lines[i] + ' 0 0'
79 for j in range(columns):
80 lines[i] = lines[i] + ' '
83 while string.strip(lines[i]):
85 lines[i] = lines[i] + ' 1 1'
86 lines[i] = lines[i] + ' 0 0 0'
89 lines[i] = string.strip(lines[i])
92 def final_dot(document):
93 " Merge lines if the dot is the final character."
98 if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
99 lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 \
100 and lines[i+1] != '':
102 lines[i] = lines[i] + lines[i+1]
108 def update_inset_label(document):
109 " Update inset Label."
110 lines = document.body
113 i = find_token(lines, '\\begin_inset Label', i)
116 lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
120 def update_latexdel(document):
121 " Update inset LatexDel."
122 lines = document.body
125 i = find_token(lines, '\\begin_inset LatexDel', i)
128 lines[i] = string.replace(lines[i],
129 '\\begin_inset LatexDel',
130 '\\begin_inset LatexCommand')
134 def update_vfill(document):
135 " Update fill_top and fill_bottom."
136 lines = document.body
137 for i in range(len(lines)):
138 lines[i] = string.replace(lines[i],
140 '\\added_space_top vfill')
141 lines[i] = string.replace(lines[i],
143 '\\added_space_bottom vfill')
146 def update_space_units(document):
147 " Update space units."
148 lines = document.body
149 added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
150 added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
151 for i in range(len(lines)):
152 result = added_space_bottom.search(lines[i])
154 old = '\\added_space_bottom ' + result.group(1)
155 new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
156 lines[i] = string.replace(lines[i], old, new)
158 result = added_space_top.search(lines[i])
160 old = '\\added_space_top ' + result.group(1)
161 new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
162 lines[i] = string.replace(lines[i], old, new)
165 def remove_cursor(document):
166 " Remove cursor, it is not saved on the file anymore."
167 lines = document.body
169 cursor_re = re.compile(r'.*(\\cursor \d*)')
171 i = find_re(lines, cursor_re, i)
174 cursor = cursor_re.search(lines[i]).group(1)
175 lines[i] = string.replace(lines[i], cursor, '')
179 def remove_empty_insets(document):
180 " Remove empty insets."
181 lines = document.body
184 i = find_token(lines, '\\begin_inset ', i)
187 if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
193 def remove_formula_latex(document):
194 " Remove formula latex."
195 lines = document.body
198 i = find_token(lines, '\\latex formula_latex ', i)
203 i = find_token(lines, '\\latex default', i)
209 def add_end_document(document):
210 " Add \\the_end to the end of the document."
211 lines = document.body
212 i = find_token(lines, '\\the_end', 0)
214 lines.append('\\the_end')
217 def header_update(document):
218 " Update document header."
219 lines = document.header
223 if lines[i][-1:] == ' ':
224 lines[i] = lines[i][:-1]
226 if check_token(lines[i], '\\epsfig'):
227 lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
231 if check_token(lines[i], '\\papersize'):
232 size = string.split(lines[i])[1]
236 if size == 'usletter':
237 new_size = 'letterpaper'
240 paperpackage = "widemarginsa4"
242 lines[i] = '\\papersize ' + new_size
245 lines.insert(i, '\\paperpackage ' + paperpackage)
248 lines.insert(i,'\\use_geometry 0')
249 lines.insert(i + 1,'\\use_amsmath 0')
254 if check_token(lines[i], '\\baselinestretch'):
255 size = string.split(lines[i])[1]
263 name = 'other ' + size
264 lines[i] = '\\spacing %s ' % name
271 def update_latexaccents(document):
272 " Update latex accent insets."
276 i = find_token(body, '\\i ', i)
280 contents = string.strip(body[i][2:])
282 if string.find(contents, '{') != -1 and string.find(contents, '}') != -1:
286 if len(contents) == 2:
287 contents = contents + '{}'
288 elif len(contents) == 3:
289 contents = contents[:2] + '{' + contents[2] + '}'
290 elif len(contents) == 4:
291 if contents[2] == ' ':
292 contents = contents[:2] + '{' + contents[3] + '}'
293 elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
294 contents = contents[:2] + '{' + contents[2:] + '}'
296 body[i] = '\\i ' + contents
300 def obsolete_latex_title(document):
301 " Replace layout Latex_Title with Title."
305 i = find_token(body, '\\layout', i)
309 if string.find(string.lower(body[i]),'latex_title') != -1:
310 body[i] = '\\layout Title'
315 supported_versions = ["0.12.0","0.12.1","0.12"]
316 convert = [[215, [header_update, add_end_document, remove_cursor,
317 final_dot, update_inset_label, update_latexdel,
318 update_space_units, space_before_layout,
319 formula_inset_space_eat, update_tabular,
320 update_vfill, remove_empty_insets,
321 remove_formula_latex, update_latexaccents,
322 obsolete_latex_title]]]
326 if __name__ == "__main__":