1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>, 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.
23 def check_token(line, token):
24 if line[:len(token)] == token:
29 # We need to check that the char after the token is space, but I think
31 def find_token(lines, token, start, end = 0):
35 for i in xrange(start, end):
36 if lines[i][:m] == token:
41 def find_token2(lines, token, start, end = 0):
44 for i in xrange(start, end):
45 x = string.split(lines[i])
46 if len(x) > 0 and x[0] == token:
51 def find_tokens(lines, tokens, start, end = 0):
54 for i in xrange(start, end):
57 if line[:len(token)] == token:
62 def find_re(lines, rexp, start, end = 0):
65 for i in xrange(start, end):
66 if rexp.match(lines[i]):
71 def find_token_backwards(lines, token, start):
73 for i in xrange(start, -1, -1):
80 def find_tokens_backwards(lines, tokens, start):
81 for i in xrange(start, -1, -1):
84 if line[:len(token)] == token:
89 def get_value(lines, token, start, end = 0):
90 i = find_token2(lines, token, start, end)
93 if len(string.split(lines[i])) > 1:
94 return string.split(lines[i])[1]
99 def del_token(lines, token, i, j):
100 k = find_token2(lines, token, i, j)
108 # Finds the paragraph that contains line i.
109 def get_paragraph(lines, i):
111 i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
112 if i == -1: return -1
113 if check_token(lines[i], "\\layout"):
115 i = find_beginning_of_inset(lines, i)
119 # Finds the paragraph after the paragraph that contains line i.
120 def get_next_paragraph(lines, i):
122 i = find_tokens(lines, ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"], i)
123 if not check_token(lines[i], "\\begin_inset"):
125 i = find_end_of_inset(lines, i)
129 def find_end_of(lines, i, start_token, end_token):
133 i = find_tokens(lines, [end_token, start_token], i+1)
134 if check_token(lines[i], start_token):
143 # Finds the matching \end_inset
144 def find_beginning_of(lines, i, start_token, end_token):
147 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
148 if check_token(lines[i], end_token):
157 # Finds the matching \end_inset
158 def find_end_of_inset(lines, i):
159 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
162 # Finds the matching \end_inset
163 def find_beginning_of_inset(lines, i):
164 return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
167 def find_end_of_tabular(lines, i):
168 return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
171 def get_tabular_lines(lines, i):
174 j = find_end_of_tabular(lines, i)
179 if check_token(lines[i], "\\begin_inset"):
180 i = find_end_of_inset(lines, i)+1
187 def is_nonempty_line(line):
188 return line != " "*len(line)
191 def find_nonempty_line(lines, start, end = 0):
194 for i in xrange(start, end):
195 if is_nonempty_line(lines[i]):
201 # Tools for file reading
203 def read_file(header, body, opt):
204 """Reads a file into the header and body parts"""
208 line = opt.input.readline()
210 opt.error("Invalid LyX file.")
213 if check_token(line, '\\begin_preamble'):
215 if check_token(line, '\\end_preamble'):
219 line = string.strip(line)
221 if not line and not preamble:
227 line = opt.input.readline()
230 body.append(line[:-1])
233 def write_file(header, body, opt):
235 opt.output.write(line+"\n")
236 opt.output.write("\n")
238 opt.output.write(line+"\n")
244 original_version = re.compile(r"\#LyX (\S*)")
246 def read_version(header):
251 result = original_version.match(line)
253 return result.group(1)
257 def set_version(lines, version):
258 lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
259 if lines[1][0] == '#':
263 # file format version
265 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
266 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
267 lst_ft = [210, 215, 216, 217, 218, 220, 221, 223, 224, 225, 226, 227, 228, 229,
268 230, 231, 232, 233, 234]
270 format_relation = [("0_10", [210], ["0.10.7","0.10"]),
271 ("0_12", [215], ["0.12","0.12.1","0.12"]),
272 ("1_0_0", [215], ["1.0.0","1.0"]),
273 ("1_0_1", [215], ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]),
274 ("1_1_4", [215], ["1.1.4","1.1"]),
275 ("1_1_5", [216], ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]),
276 ("1_1_6", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]),
277 ("1_1_6fix3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]),
278 ("1_2", [220], ["1.2.0","1.2.1","1.2.3","1.2.4","1.2"]),
279 ("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3"]),
280 ("1_4", [223,224,225,226,227,228,229,230,231,232,233,234], ["1.4.0cvs","1.4"])]
283 def lyxformat(format, opt):
284 result = format_re.match(format)
286 format = int(result.group(1) + result.group(2))
288 opt.error(str(format) + ": " + "Invalid LyX file.")
293 opt.error(str(format) + ": " + "Format not supported.")
297 def read_format(header, opt):
299 result = fileformat.match(line)
301 return lyxformat(result.group(1), opt)
303 opt.error("Invalid LyX File.")
307 def set_format(lines, number):
308 if int(number) <= 217:
309 number = float(number)/100
310 i = find_token(lines, "\\lyxformat", 0)
311 lines[i] = "\\lyxformat %s" % number
314 def get_end_format():
315 return format_relation[-1:][0][1][-1:][0]
318 def get_backend(textclass):
319 if textclass == "linuxdoc" or textclass == "manpage":
321 if textclass[:7] == "docbook":
326 def chain(opt, initial_version):
327 """ This is where all the decisions related with the convertion are taken"""
331 if opt.start != format:
332 opt.warning("%s: %s %s" % ("Proposed file format and input file formats do not match:", opt.start, format))
337 opt.end = get_end_format()
341 for rel in format_relation:
342 if initial_version in rel[2]:
344 initial_step = rel[0]
348 if not correct_version:
350 opt.warning("Version does not match file format, discarding it.")
351 for rel in format_relation:
353 initial_step = rel[0]
356 # This should not happen, really.
357 opt.error("Format not supported.")
359 # Find the final step
360 for rel in format_relation:
361 if opt.end in rel[1]:
365 opt.error("Format not supported.")
367 # Convertion mode, back or forth
369 if (initial_step, opt.start) < (final_step, opt.end):
372 for step in format_relation:
373 if initial_step <= step[0] <= final_step:
374 if first_step and len(step[1]) == 1:
377 steps.append(step[0])
380 relation_format = format_relation
381 relation_format.reverse()
384 for step in relation_format:
385 if final_step <= step[0] <= initial_step:
386 steps.append(step[0])
389 if last_step[1][-1] == opt.end: