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 # remove '\r' from line's end, if present
214 if line[-1:] == '\r':
217 if check_token(line, '\\begin_preamble'):
219 if check_token(line, '\\end_preamble'):
223 line = string.strip(line)
225 if not line and not preamble:
231 line = opt.input.readline()
234 # remove '\r' from line's end, if present
235 if line[-2:-1] == '\r':
236 body.append(line[:-2])
238 body.append(line[:-1])
241 def write_file(header, body, opt):
243 opt.output.write(line+"\n")
244 opt.output.write("\n")
246 opt.output.write(line+"\n")
252 original_version = re.compile(r"\#LyX (\S*)")
254 def read_version(header):
259 result = original_version.match(line)
261 return result.group(1)
265 def set_version(lines, version):
266 lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
267 if lines[1][0] == '#':
271 # file format version
273 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
274 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
275 lst_ft = [210, 215, 216, 217, 218, 220, 221, 223, 224, 225, 226, 227, 228, 229,
276 230, 231, 232, 233, 234, 235, 236]
278 format_relation = [("0_10", [210], ["0.10.7","0.10"]),
279 ("0_12", [215], ["0.12","0.12.1","0.12"]),
280 ("1_0_0", [215], ["1.0.0","1.0"]),
281 ("1_0_1", [215], ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]),
282 ("1_1_4", [215], ["1.1.4","1.1"]),
283 ("1_1_5", [216], ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]),
284 ("1_1_6", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]),
285 ("1_1_6fix3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]),
286 ("1_2", [220], ["1.2.0","1.2.1","1.2.3","1.2.4","1.2"]),
287 ("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3"]),
288 ("1_4", [223,224,225,226,227,228,229,230,231,232,233,234,235, 236], ["1.4.0cvs","1.4"])]
291 def lyxformat(format, opt):
292 result = format_re.match(format)
294 format = int(result.group(1) + result.group(2))
296 opt.error(str(format) + ": " + "Invalid LyX file.")
301 opt.error(str(format) + ": " + "Format not supported.")
305 def read_format(header, opt):
307 result = fileformat.match(line)
309 return lyxformat(result.group(1), opt)
311 opt.error("Invalid LyX File.")
315 def set_format(lines, number):
316 if int(number) <= 217:
317 number = float(number)/100
318 i = find_token(lines, "\\lyxformat", 0)
319 lines[i] = "\\lyxformat %s" % number
322 def get_end_format():
323 return format_relation[-1:][0][1][-1:][0]
326 def get_backend(textclass):
327 if textclass == "linuxdoc" or textclass == "manpage":
329 if textclass[:7] == "docbook":
334 def chain(opt, initial_version):
335 """ This is where all the decisions related with the convertion are taken"""
339 if opt.start != format:
340 opt.warning("%s: %s %s" % ("Proposed file format and input file formats do not match:", opt.start, format))
345 opt.end = get_end_format()
349 for rel in format_relation:
350 if initial_version in rel[2]:
352 initial_step = rel[0]
356 if not correct_version:
358 opt.warning("Version does not match file format, discarding it.")
359 for rel in format_relation:
361 initial_step = rel[0]
364 # This should not happen, really.
365 opt.error("Format not supported.")
367 # Find the final step
368 for rel in format_relation:
369 if opt.end in rel[1]:
373 opt.error("Format not supported.")
375 # Convertion mode, back or forth
377 if (initial_step, opt.start) < (final_step, opt.end):
380 for step in format_relation:
381 if initial_step <= step[0] <= final_step:
382 if first_step and len(step[1]) == 1:
385 steps.append(step[0])
388 relation_format = format_relation
389 relation_format.reverse()
392 for step in relation_format:
393 if final_step <= step[0] <= initial_step:
394 steps.append(step[0])
397 if last_step[1][-1] == opt.end: