1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 This modules offer several free functions to help parse lines.
23 More documentaton is below, but here is a quick guide to what
24 they do. Optional arguments are marked by brackets.
26 find_token(lines, token, start[, end[, ignorews]]):
27 Returns the first line i, start <= i < end, on which
28 token is found at the beginning. Returns -1 if not
30 If ignorews is (given and) True, then differences
31 in whitespace do not count, except that there must be no
32 extra whitespace following token itself.
34 find_token_exact(lines, token, start[, end]):
35 As find_token, but with ignorews True.
37 find_tokens(lines, tokens, start[, end[, ignorews]]):
38 Returns the first line i, start <= i < end, on which
39 oen of the tokens in tokens is found at the beginning.
40 Returns -1 if not found.
41 If ignorews is (given and) True, then differences
42 in whitespace do not count, except that there must be no
43 extra whitespace following token itself.
45 find_tokens_exact(lines, token, start[, end]):
46 As find_tokens, but with ignorews True.
48 find_token_backwards(lines, token, start):
49 find_tokens_backwards(lines, tokens, start):
50 As before, but look backwards.
52 find_re(lines, rexp, start[, end]):
53 As find_token, but rexp is a regular expression object,
54 so it has to be passed as e.g.: re.compile(r'...').
56 get_value(lines, token, start[, end[, default]):
57 Similar to find_token, but it returns what follows the
58 token on the found line. Example:
59 get_value(document.header, "\use_xetex", 0)
60 will find a line like:
62 and, in that case, return "true". (Note that whitespace
63 is stripped.) The final argument, default, defaults to "",
64 and is what is returned if we do not find anything. So you
65 can use that to set a default.
67 get_quoted_value(lines, token, start[, end[, default]):
68 Similar to get_value, but it will strip quotes off the
69 value, if they are present. So use this one for cases
70 where the value is normally quoted.
72 get_option_value(line, option):
73 This assumes we have a line with something like:
75 and returns value. Returns "" if not found.
77 del_token(lines, token, start[, end]):
78 Like find_token, but deletes the line if it finds one.
79 Returns True if a line got deleted, otherwise False.
81 find_beginning_of(lines, i, start_token, end_token):
82 Here, start_token and end_token are meant to be a matching
83 pair, like "\begin_layout" and "\end_layout". We look for
84 the start_token that pairs with the end_token that occurs
85 on or after line i. Returns -1 if not found.
86 So, in the layout case, this would find the \begin_layout
87 for the layout line i is in.
89 ec = find_token(document.body, "</cell", i)
90 bc = find_beginning_of(document.body, ec, \
92 Now, assuming no -1s, bc-ec wraps the cell for line i.
94 find_end_of(lines, i, start_token, end_token):
95 Like find_beginning_of, but looking for the matching
96 end_token. This might look like:
97 bc = find_token_(document.body, "<cell", i)
98 ec = find_end_of(document.body, bc, "<cell", "</cell")
99 Now, assuming no -1s, bc-ec wrap the next cell.
101 find_end_of_inset(lines, i):
102 Specialization of find_end_of for insets.
104 find_end_of_layout(lines, i):
105 Specialization of find_end_of for layouts.
107 find_end_of_sequence(lines, i):
108 Find the end of the sequence of layouts of the same kind.
109 Considers nesting. If the last paragraph in sequence is nested,
110 the position of the last \end_deeper is returned, else
111 the position of the last \end_layout.
113 is_in_inset(lines, i, inset):
114 Checks if line i is in an inset of the given type.
115 If so, returns starting and ending lines. Otherwise,
118 is_in_inset(document.body, i, "\\begin_inset Tabular")
119 returns False unless i is within a table. If it is, then
120 it returns the line on which the table begins and the one
121 on which it ends. Note that this pair will evaulate to
124 will do what you expect.
126 get_containing_inset(lines, i):
127 Finds out what kind of inset line i is within. Returns a
128 list containing what follows \begin_inset on the the line
129 on which the inset begins, plus the starting and ending line.
130 Returns False on any kind of error or if it isn't in an inset.
131 So get_containing_inset(document.body, i) might return:
132 ("CommandInset ref", 300, 306)
133 if i is within an InsetRef beginning on line 300 and ending
136 get_containing_layout(lines, i):
137 As get_containing_inset, but for layout. Additionally returns the
138 position of real paragraph start (after par params) as 4th value.
141 find_nonempty_line(lines, start[, end):
142 Finds the next non-empty line.
144 check_token(line, token):
145 Does line begin with token?
147 is_nonempty_line(line):
148 Does line contain something besides whitespace?
150 count_pars_in_inset(lines, i):
151 Counts the paragraphs inside an inset.
157 # Utilities for one line
158 def check_token(line, token):
159 """ check_token(line, token) -> bool
161 Return True if token is present in line and is the first element
162 else returns False."""
164 return line[:len(token)] == token
167 def is_nonempty_line(line):
168 """ is_nonempty_line(line) -> bool
170 Return False if line is either empty or it has only whitespaces,
172 return line != " "*len(line)
175 # Utilities for a list of lines
176 def find_token(lines, token, start, end = 0, ignorews = False):
177 """ find_token(lines, token, start[[, end], ignorews]) -> int
179 Return the lowest line where token is found, and is the first
180 element, in lines[start, end].
182 If ignorews is True (default is False), then differences in
183 whitespace are ignored, except that there must be no extra
184 whitespace following token itself.
186 Return -1 on failure."""
188 if end == 0 or end > len(lines):
191 for i in xrange(start, end):
200 if lines[i][:m] == token:
205 def find_token_exact(lines, token, start, end = 0):
206 return find_token(lines, token, start, end, True)
209 def find_tokens(lines, tokens, start, end = 0, ignorews = False):
210 """ find_tokens(lines, tokens, start[[, end], ignorews]) -> int
212 Return the lowest line where one token in tokens is found, and is
213 the first element, in lines[start, end].
215 Return -1 on failure."""
216 if end == 0 or end > len(lines):
219 for i in xrange(start, end):
229 if lines[i][:len(token)] == token:
234 def find_tokens_exact(lines, tokens, start, end = 0):
235 return find_tokens(lines, tokens, start, end, True)
238 def find_re(lines, rexp, start, end = 0):
239 """ find_token_re(lines, rexp, start[, end]) -> int
241 Return the lowest line where rexp, a regular expression, is found
242 in lines[start, end].
244 Return -1 on failure."""
246 if end == 0 or end > len(lines):
248 for i in xrange(start, end):
249 if rexp.match(lines[i]):
254 def find_token_backwards(lines, token, start):
255 """ find_token_backwards(lines, token, start) -> int
257 Return the highest line where token is found, and is the first
258 element, in lines[start, end].
260 Return -1 on failure."""
262 for i in xrange(start, -1, -1):
264 if line[:m] == token:
269 def find_tokens_backwards(lines, tokens, start):
270 """ find_tokens_backwards(lines, token, start) -> int
272 Return the highest line where token is found, and is the first
273 element, in lines[end, start].
275 Return -1 on failure."""
276 for i in xrange(start, -1, -1):
279 if line[:len(token)] == token:
284 def get_value(lines, token, start, end = 0, default = ""):
285 """ get_value(lines, token, start[[, end], default]) -> string
287 Find the next line that looks like:
288 token followed by other stuff
289 Returns "followed by other stuff" with leading and trailing
293 i = find_token_exact(lines, token, start, end)
296 l = lines[i].split(None, 1)
302 def get_quoted_value(lines, token, start, end = 0, default = ""):
303 """ get_quoted_value(lines, token, start[[, end], default]) -> string
305 Find the next line that looks like:
306 token "followed by other stuff"
307 Returns "followed by other stuff" with leading and trailing
308 whitespace and quotes removed. If there are no quotes, that is OK too.
309 So use get_value to preserve possible quotes, this one to remove them,
311 Note that we will NOT strip quotes from default!
313 val = get_value(lines, token, start, end, "")
316 return val.strip('"')
319 def get_option_value(line, option):
320 rx = option + '\s*=\s*"([^"]+)"'
328 def set_option_value(line, option, value):
329 rx = '(' + option + '\s*=\s*")[^"]+"'
334 return re.sub(rx, '\g<1>' + value + '"', line)
337 def del_token(lines, token, start, end = 0):
338 """ del_token(lines, token, start, end) -> int
340 Find the first line in lines where token is the first element
341 and delete that line. Returns True if we deleted a line, False
344 k = find_token_exact(lines, token, start, end)
351 def find_beginning_of(lines, i, start_token, end_token):
354 i = find_tokens_backwards(lines, [start_token, end_token], i-1)
357 if check_token(lines[i], end_token):
366 def find_end_of(lines, i, start_token, end_token):
370 i = find_tokens(lines, [end_token, start_token], i+1)
373 if check_token(lines[i], start_token):
382 def find_nonempty_line(lines, start, end = 0):
385 for i in xrange(start, end):
386 if is_nonempty_line(lines[i]):
391 def find_end_of_inset(lines, i):
392 " Find end of inset, where lines[i] is included."
393 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
396 def find_end_of_layout(lines, i):
397 " Find end of layout, where lines[i] is included."
398 return find_end_of(lines, i, "\\begin_layout", "\\end_layout")
401 def is_in_inset(lines, i, inset):
403 Checks if line i is in an inset of the given type.
404 If so, returns starting and ending lines.
405 Otherwise, returns False.
407 is_in_inset(document.body, i, "\\begin_inset Tabular")
408 returns False unless i is within a table. If it is, then
409 it returns the line on which the table begins and the one
410 on which it ends. Note that this pair will evaulate to
413 will do what you expect.
416 stins = find_token_backwards(lines, inset, i)
419 endins = find_end_of_inset(lines, stins)
420 # note that this includes the notfound case.
423 return (stins, endins)
426 def get_containing_inset(lines, i):
428 Finds out what kind of inset line i is within. Returns a
429 list containing (i) what follows \begin_inset on the the line
430 on which the inset begins, plus the starting and ending line.
431 Returns False on any kind of error or if it isn't in an inset.
435 stins = find_token_backwards(lines, "\\begin_inset", j)
438 endins = find_end_of_inset(lines, stins)
443 inset = get_value(lines, "\\begin_inset", stins)
447 return (inset, stins, endins)
450 def get_containing_layout(lines, i):
452 Finds out what kind of layout line i is within. Returns a
453 list containing (i) what follows \begin_layout on the the line
454 on which the layout begins, plus the starting and ending line
455 and the start of the apargraph (after all params).
456 Returns False on any kind of error.
460 stlay = find_token_backwards(lines, "\\begin_layout", j)
463 endlay = find_end_of_layout(lines, stlay)
468 lay = get_value(lines, "\\begin_layout", stlay)
472 par_params = ["\\noindent", "\\indent", "\\indent-toggle", "\\leftindent",
473 "\\start_of_appendix", "\\paragraph_spacing single",
474 "\\paragraph_spacing onehalf", "\\paragraph_spacing double",
475 "\\paragraph_spacing other", "\\align", "\\labelwidthstring"]
479 if lines[stpar] not in par_params:
481 return (lay, stlay, endlay, stpar)
484 def count_pars_in_inset(lines, i):
486 Counts the paragraphs within this inset
488 ins = get_containing_inset(lines, i)
492 for j in range(ins[1], ins[2]):
493 m = re.match(r'\\begin_layout (.*)', lines[j])
494 if m and get_containing_inset(lines, j)[0] == ins[0]:
500 def find_end_of_sequence(lines, i):
502 Returns the end of a sequence of identical layouts.
504 lay = get_containing_layout(lines, i)
511 m = re.match(r'\\begin_layout (.*)', lines[i])
512 if m and m.group(1) != layout:
514 elif lines[i] == "\\begin_deeper":
515 j = find_end_of(lines, i, "\\begin_deeper", "\\end_deeper")
520 if m and m.group(1) == layout:
521 endlay = find_end_of_layout(lines, i)
524 if i == len(lines) - 1: