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_token2(lines, token, start, end = 0):
39 for i in xrange(start, end):
40 x = string.split(lines[i])
41 if len(x) > 0 and x[0] == token:
45 def find_tokens(lines, tokens, start, end = 0):
48 for i in xrange(start, end):
51 if line[:len(token)] == token:
55 def find_re(lines, rexp, start, end = 0):
58 for i in xrange(start, end):
59 if rexp.match(lines[i]):
63 def find_token_backwards(lines, token, start):
65 for i in xrange(start, -1, -1):
71 def find_tokens_backwards(lines, tokens, start):
72 for i in xrange(start, -1, -1):
75 if line[:len(token)] == token:
79 def get_value(lines, token, start, end = 0):
80 i = find_token2(lines, token, start, end)
83 if len(string.split(lines[i])) > 1:
84 return string.split(lines[i])[1]
88 def del_token(lines, token, i, j):
89 k = find_token2(lines, token, i, j)
96 # Finds the paragraph that contains line i.
97 def get_paragraph(lines, i):
99 i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
100 if i == -1: return -1
101 if check_token(lines[i], "\\layout"):
103 i = find_beginning_of_inset(lines, i)
105 # Finds the paragraph after the paragraph that contains line i.
106 def get_next_paragraph(lines, i):
108 i = find_tokens(lines, ["\\begin_inset", "\\layout"], i)
109 if i == -1: return -1
110 if check_token(lines[i], "\\layout"):
112 i = find_end_of_inset(lines, i)
114 def find_end_of(lines, i, start_token, end_token):
118 i = find_tokens(lines, [end_token, start_token], i+1)
119 if check_token(lines[i], start_token):
127 # Finds the matching \end_inset
128 def find_beginning_of(lines, i, start_token, end_token):
131 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
132 if check_token(lines[i], end_token):
140 # Finds the matching \end_inset
141 def find_end_of_inset(lines, i):
142 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
144 # Finds the matching \end_inset
145 def find_beginning_of_inset(lines, i):
146 return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
148 def find_end_of_tabular(lines, i):
149 return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
151 def get_tabular_lines(lines, i):
154 j = find_end_of_tabular(lines, i)
159 if check_token(lines[i], "\\begin_inset"):
160 i = find_end_of_inset(lines, i)+1
166 def is_nonempty_line(line):
167 return line != " "*len(line)
169 def find_nonempty_line(lines, start, end = 0):
172 for i in xrange(start, end):
173 if is_nonempty_line(lines[i]):
177 def set_comment(lines, number):
180 # It is not worth the trouble to handle this case
185 version = str((x-220)/10.0+1.2)
187 lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
188 if lines[1][0] == '#':
191 def set_format(lines, number):
192 if int(number) <= 217:
193 number = float(number)/100
194 i = find_token(lines, "\\lyxformat", 0)
195 lines[i] = "\\lyxformat %s" % number