1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2010 Dekel Tsur <dekel@lyx.org>,
4 # José Matos <jamatos@lyx.org>, Richard Heck <rgheck@comcast.net>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 " This modules offer several free functions to help parse lines. "
22 # Utilities for one line
23 def check_token(line, token):
24 """ check_token(line, token) -> bool
26 Return True if token is present in line and is the first element
27 else returns False."""
29 return line[:len(token)] == token
32 def is_nonempty_line(line):
33 """ is_nonempty_line(line) -> bool
35 Return False if line is either empty or it has only whitespaces,
37 return line != " "*len(line)
40 # Utilities for a list of lines
41 def find_token(lines, token, start, end = 0, exact = False):
42 """ find_token(lines, token, start[[, end], exact]) -> int
44 Return the lowest line where token is found, and is the first
45 element, in lines[start, end].
47 Return -1 on failure."""
49 if end == 0 or end > len(lines):
52 for i in xrange(start, end):
61 if lines[i][:m] == token:
66 def find_token_exact(lines, token, start, end = 0):
67 return find_token(lines, token, start, end, True)
70 def find_tokens(lines, tokens, start, end = 0, exact = False):
71 """ find_tokens(lines, tokens, start[[, end], exact]) -> int
73 Return the lowest line where one token in tokens is found, and is
74 the first element, in lines[start, end].
76 Return -1 on failure."""
80 for i in xrange(start, end):
90 if lines[i][:len(token)] == token:
95 def find_tokens_exact(lines, tokens, start, end = 0):
96 return find_tokens(lines, tokens, start, end, True)
99 def find_re(lines, rexp, start, end = 0):
100 """ find_token_re(lines, rexp, start[, end]) -> int
102 Return the lowest line where rexp, a regular expression, is found
103 in lines[start, end].
105 Return -1 on failure."""
109 for i in xrange(start, end):
110 if rexp.match(lines[i]):
115 def find_token_backwards(lines, token, start):
116 """ find_token_backwards(lines, token, start) -> int
118 Return the highest line where token is found, and is the first
119 element, in lines[start, end].
121 Return -1 on failure."""
123 for i in xrange(start, -1, -1):
125 if line[:m] == token:
130 def find_tokens_backwards(lines, tokens, start):
131 """ find_tokens_backwards(lines, token, start) -> int
133 Return the highest line where token is found, and is the first
134 element, in lines[end, start].
136 Return -1 on failure."""
137 for i in xrange(start, -1, -1):
140 if line[:len(token)] == token:
145 def get_value(lines, token, start, end = 0, default = ""):
146 """ get_value(lines, token, start[[, end], default]) -> string
148 Find the next line that looks like:
149 token followed by other stuff
150 Returns "followed by other stuff".
153 i = find_token_exact(lines, token, start, end)
156 l = lines[i].split(None, 1)
162 def get_value_string(lines, token, start, end = 0, trim = False, default = ""):
163 """ get_value_string(lines, token, start[[, end], trim, default]) -> string
165 Return tokens after token as string, in lines, where
166 token is the first element. When trim is used, the first and last character
167 of the string is trimmed."""
169 i = find_token_exact(lines, token, start, end)
172 if len(lines[i].split()) > 1:
173 for k in range (0, len(lines[i])):
174 if lines[i][k] == ' ':
176 return lines[i][k+1:len(lines[i])]
178 return lines[i][k+2:len(lines[i])-1]
183 def del_token(lines, token, start, end):
184 """ del_token(lines, token, start, end) -> int
186 Find the lower line in lines where token is the first element and
189 Returns the number of lines remaining."""
191 k = find_token_exact(lines, token, start, end)
199 def find_beginning_of(lines, i, start_token, end_token):
202 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
205 if check_token(lines[i], end_token):
214 def find_end_of(lines, i, start_token, end_token):
218 i = find_tokens(lines, [end_token, start_token], i+1)
221 if check_token(lines[i], start_token):
230 def find_nonempty_line(lines, start, end = 0):
233 for i in xrange(start, end):
234 if is_nonempty_line(lines[i]):
239 def find_end_of_inset(lines, i):
240 " Find end of inset, where lines[i] is included."
241 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
244 def find_end_of_layout(lines, i):
245 " Find end of layout, where lines[i] is included."
246 return find_end_of(lines, i, "\\begin_layout", "\\end_layout")
249 # checks if line i is in the inset e.g., "\\begin_inset CommandInset ref"
250 # if so, returns starting and ending lines
251 # otherwise, returns (-1, -1)
252 def get_containing_inset(lines, i, inset):
254 stins = find_token_backwards(lines, inset, i)
257 endins = find_end_of_inset(lines, stins)
258 # note that this includes the notfound case.
261 return (stins, endins)