1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>, 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 def check_token(line, token):
21 if line[:len(token)] == token:
25 # We need to check that the char after the token is space, but I think
27 def find_token(lines, token, start, end = 0):
31 for i in xrange(start, end):
32 if lines[i][:m] == token:
36 def find_tokens(lines, tokens, start, end = 0):
39 for i in xrange(start, end):
42 if line[:len(token)] == token:
46 def find_re(lines, rexp, start, end = 0):
49 for i in xrange(start, end):
50 if rexp.match(lines[i]):
54 def find_token_backwards(lines, token, start):
56 for i in xrange(start, -1, -1):
62 def find_tokens_backwards(lines, tokens, start):
63 for i in xrange(start, -1, -1):
66 if line[:len(token)] == token:
70 def get_value(lines, token, start, end = 0):
71 i = find_token(lines, token, start, end)
74 return string.split(lines[i])[1]
76 # Finds the paragraph that contains line i.
77 def get_paragraph(lines, i):
79 i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
80 if check_token(lines[i], "\\layout"):
84 i = find_tokens_backwards(lines, ["\\end_inset", "\\begin_inset"], i)
85 if check_token(lines[i], "\\end_inset"):
91 # Finds the matching \end_inset
92 def skip_inset(lines, i):
96 i = find_tokens(lines, ["\\end_inset", "\\begin_inset"], i)
97 if check_token(lines[i], "\\begin_inset"):
104 def is_nonempty_line(line):
105 return line != " "*len(line)
107 def find_nonempty_line(lines, start, end = 0):
110 for i in xrange(start, end):
111 if is_nonempty_line(lines[i]):
116 def set_comment(lines, number):
119 # It is not worth the trouble to handle this case
126 lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
127 if lines[1][0] == '#':
130 def set_format(lines, number):
131 if int(number) <= 217:
132 number = float(number)/100
133 i = find_token(lines, "\\lyxformat", 0)
134 lines[i] = "\\lyxformat %s" % number