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.
22 def check_token(line, token):
23 if line[:len(token)] == token:
28 # We need to check that the char after the token is space, but I think
30 def find_token(lines, token, start, end = 0):
34 for i in xrange(start, end):
35 if lines[i][:m] == token:
40 def find_token_exact(lines, token, start, end = 0):
43 for i in xrange(start, end):
44 x = string.split(lines[i])
45 y = string.split(token)
53 def find_tokens(lines, tokens, start, end = 0):
56 for i in xrange(start, end):
58 if lines[i][:len(token)] == token:
63 def find_tokens_exact(lines, tokens, start, end = 0):
66 for i in xrange(start, end):
68 x = string.split(lines[i])
69 y = string.split(token)
77 def find_re(lines, rexp, start, end = 0):
80 for i in xrange(start, end):
81 if rexp.match(lines[i]):
86 def find_token_backwards(lines, token, start):
88 for i in xrange(start, -1, -1):
95 def find_tokens_backwards(lines, tokens, start):
96 for i in xrange(start, -1, -1):
99 if line[:len(token)] == token:
104 def get_value(lines, token, start, end = 0):
105 i = find_token_exact(lines, token, start, end)
108 if len(string.split(lines[i])) > 1:
109 return string.split(lines[i])[1]
114 def get_layout(line, default_layout):
115 tokens = string.split(line)
118 return default_layout
121 def del_token(lines, token, i, j):
122 k = find_token_exact(lines, token, i, j)
130 # Finds the paragraph that contains line i.
131 def get_paragraph(lines, i, format):
133 begin_layout = "\\layout"
135 begin_layout = "\\begin_layout"
137 i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
138 if i == -1: return -1
139 if check_token(lines[i], begin_layout):
141 i = find_beginning_of_inset(lines, i)
145 # Finds the paragraph after the paragraph that contains line i.
146 def get_next_paragraph(lines, i, format):
148 tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
150 tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_document"]
152 tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_body", "\\end_document"]
154 i = find_tokens(lines, tokens, i)
155 if not check_token(lines[i], "\\begin_inset"):
157 i = find_end_of_inset(lines, i)
161 def find_end_of(lines, i, start_token, end_token):
165 i = find_tokens(lines, [end_token, start_token], i+1)
166 if check_token(lines[i], start_token):
175 # Finds the matching \end_inset
176 def find_beginning_of(lines, i, start_token, end_token):
179 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
180 if check_token(lines[i], end_token):
189 # Finds the matching \end_inset
190 def find_end_of_inset(lines, i):
191 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
194 # Finds the matching \end_inset
195 def find_beginning_of_inset(lines, i):
196 return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
199 def find_end_of_tabular(lines, i):
200 return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
203 def get_tabular_lines(lines, i):
206 j = find_end_of_tabular(lines, i)
211 if check_token(lines[i], "\\begin_inset"):
212 i = find_end_of_inset(lines, i)+1
219 def is_nonempty_line(line):
220 return line != " "*len(line)
223 def find_nonempty_line(lines, start, end = 0):
226 for i in xrange(start, end):
227 if is_nonempty_line(lines[i]):