1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
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.
19 " This modules offer several free functions to help parse lines."
21 # Utilities for one line
22 def check_token(line, token):
23 """ check_token(line, token) -> bool
25 Return True if token is present in line and is the first element
26 else returns False."""
28 return line[:len(token)] == token
31 def is_nonempty_line(line):
32 """ is_nonempty_line(line) -> bool
34 Return False if line is either empty or it has only whitespaces,
36 return line != " "*len(line)
39 # Utilities for a list of lines
40 def find_token(lines, token, start, end = 0, exact = False):
41 """ find_token(lines, token, start[[, end], exact]) -> int
43 Return the lowest line where token is found, and is the first
44 element, in lines[start, end].
46 Return -1 on failure."""
51 for i in xrange(start, end):
60 if lines[i][:m] == token:
65 def find_token_exact(lines, token, start, end = 0):
66 return find_token(lines, token, start, end, True)
69 def find_tokens(lines, tokens, start, end = 0, exact = False):
70 """ find_tokens(lines, tokens, start[[, end], exact]) -> int
72 Return the lowest line where one token in tokens is found, and is
73 the first element, in lines[start, end].
75 Return -1 on failure."""
79 for i in xrange(start, end):
89 if lines[i][:len(token)] == token:
94 def find_tokens_exact(lines, tokens, start, end = 0):
95 return find_tokens(lines, tokens, start, end, True)
98 def find_re(lines, rexp, start, end = 0):
99 """ find_token_re(lines, rexp, start[, end]) -> int
101 Return the lowest line where rexp, a regular expression, is found
102 in lines[start, end].
104 Return -1 on failure."""
108 for i in xrange(start, end):
109 if rexp.match(lines[i]):
114 def find_token_backwards(lines, token, start):
115 """ find_token_backwards(lines, token, start) -> int
117 Return the highest line where token is found, and is the first
118 element, in lines[start, end].
120 Return -1 on failure."""
122 for i in xrange(start, -1, -1):
124 if line[:m] == token:
129 def find_tokens_backwards(lines, tokens, start):
130 """ find_tokens_backwards(lines, token, start) -> int
132 Return the highest line where token is found, and is the first
133 element, in lines[end, start].
135 Return -1 on failure."""
136 for i in xrange(start, -1, -1):
139 if line[:len(token)] == token:
144 def get_value(lines, token, start, end = 0):
145 """ get_value(lines, token, start[, end]) -> list of strings
147 Return tokens after token for the first line, in lines, where
148 token is the first element."""
150 i = find_token_exact(lines, token, start, end)
153 if len(lines[i].split()) > 1:
154 return lines[i].split()[1]
159 def del_token(lines, token, start, end):
160 """ del_token(lines, token, start, end) -> int
162 Find the lower line in lines where token is the first element and
165 Returns the number of lines remaining."""
167 k = find_token_exact(lines, token, start, end)
175 def find_beginning_of(lines, i, start_token, end_token):
178 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
179 if check_token(lines[i], end_token):
188 def find_end_of(lines, i, start_token, end_token):
192 i = find_tokens(lines, [end_token, start_token], i+1)
193 if check_token(lines[i], start_token):
202 def find_nonempty_line(lines, start, end = 0):
205 for i in xrange(start, end):
206 if is_nonempty_line(lines[i]):