1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2003 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.
21 def check_token(line, token):
22 if line[:len(token)] == token:
26 # We need to check that the char after the token is space, but I think
28 def find_token(lines, token, start, end = 0):
32 for i in xrange(start, end):
33 if lines[i][:m] == token:
37 def find_token2(lines, token, start, end = 0):
40 for i in xrange(start, end):
41 x = string.split(lines[i])
42 if len(x) > 0 and x[0] == token:
46 def find_tokens(lines, tokens, start, end = 0):
49 for i in xrange(start, end):
52 if line[:len(token)] == token:
56 def find_re(lines, rexp, start, end = 0):
59 for i in xrange(start, end):
60 if rexp.match(lines[i]):
64 def find_token_backwards(lines, token, start):
66 for i in xrange(start, -1, -1):
72 def find_tokens_backwards(lines, tokens, start):
73 for i in xrange(start, -1, -1):
76 if line[:len(token)] == token:
80 def get_value(lines, token, start, end = 0):
81 i = find_token2(lines, token, start, end)
84 if len(string.split(lines[i])) > 1:
85 return string.split(lines[i])[1]
89 def del_token(lines, token, i, j):
90 k = find_token2(lines, token, i, j)
97 # Finds the paragraph that contains line i.
98 def get_paragraph(lines, i):
100 i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
101 if i == -1: return -1
102 if check_token(lines[i], "\\layout"):
104 i = find_beginning_of_inset(lines, i)
106 # Finds the paragraph after the paragraph that contains line i.
107 def get_next_paragraph(lines, i):
109 i = find_tokens(lines, ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"], i)
110 if not check_token(lines[i], "\\begin_inset"):
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, version):
178 lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
179 if lines[1][0] == '#':
182 def set_format(lines, number):
183 if int(number) <= 217:
184 number = float(number)/100
185 i = find_token(lines, "\\lyxformat", 0)
186 lines[i] = "\\lyxformat %s" % number