]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/parser_tools.py
fix bug 2026 and bug 2088
[lyx.git] / lib / lyx2lyx / parser_tools.py
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>
4 #
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.
9 #
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.
14 #
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.
18
19 import string
20 import re
21
22 def check_token(line, token):
23     if line[:len(token)] == token:
24         return 1
25     return 0
26
27
28 # We need to check that the char after the token is space, but I think
29 # we can ignore this
30 def find_token(lines, token, start, end = 0):
31     if end == 0:
32         end = len(lines)
33     m = len(token)
34     for i in xrange(start, end):
35         if lines[i][:m] == token:
36             return i
37     return -1
38
39
40 def find_token2(lines, token, start, end = 0):
41     if end == 0:
42         end = len(lines)
43     for i in xrange(start, end):
44         x = string.split(lines[i])
45         if len(x) > 0 and x[0] == token:
46             return i
47     return -1
48
49
50 def find_tokens(lines, tokens, start, end = 0):
51     if end == 0:
52         end = len(lines)
53     for i in xrange(start, end):
54         line = lines[i]
55         for token in tokens:
56             if line[:len(token)] == token:
57                 return i
58     return -1
59
60
61 def find_re(lines, rexp, start, end = 0):
62     if end == 0:
63         end = len(lines)
64     for i in xrange(start, end):
65         if rexp.match(lines[i]):
66                 return i
67     return -1
68
69
70 def find_token_backwards(lines, token, start):
71     m = len(token)
72     for i in xrange(start, -1, -1):
73         line = lines[i]
74         if line[:m] == token:
75             return i
76     return -1
77
78
79 def find_tokens_backwards(lines, tokens, start):
80     for i in xrange(start, -1, -1):
81         line = lines[i]
82         for token in tokens:
83             if line[:len(token)] == token:
84                 return i
85     return -1
86
87
88 def get_value(lines, token, start, end = 0):
89     i = find_token2(lines, token, start, end)
90     if i == -1:
91         return ""
92     if len(string.split(lines[i])) > 1:
93         return string.split(lines[i])[1]
94     else:
95         return ""
96
97
98 def get_layout(line, default_layout):
99     tokens = string.split(line)
100     if len(tokens) > 1:
101         return tokens[1]
102     return default_layout
103
104
105 def del_token(lines, token, i, j):
106     k = find_token2(lines, token, i, j)
107     if k == -1:
108         return j
109     else:
110         del lines[k]
111         return j-1
112
113
114 # Finds the paragraph that contains line i.
115 def get_paragraph(lines, i, format):
116     if format < 225:
117         begin_layout = "\\layout"
118     else:
119         begin_layout = "\\begin_layout"
120     while i != -1:
121         i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
122         if i == -1: return -1
123         if check_token(lines[i], begin_layout):
124             return i
125         i = find_beginning_of_inset(lines, i)
126     return -1
127
128
129 # Finds the paragraph after the paragraph that contains line i.
130 def get_next_paragraph(lines, i, format):
131     if format < 225:
132         tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
133     elif format < 236:
134         tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_document"]
135     else:
136         tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_body", "\\end_document"]
137     while i != -1:
138         i = find_tokens(lines, tokens, i)
139         if not check_token(lines[i], "\\begin_inset"):
140             return i
141         i = find_end_of_inset(lines, i)
142     return -1
143
144
145 def find_end_of(lines, i, start_token, end_token):
146     count = 1
147     n = len(lines)
148     while i < n:
149         i = find_tokens(lines, [end_token, start_token], i+1)
150         if check_token(lines[i], start_token):
151             count = count+1
152         else:
153             count = count-1
154         if count == 0:
155             return i
156     return -1
157
158
159 # Finds the matching \end_inset
160 def find_beginning_of(lines, i, start_token, end_token):
161     count = 1
162     while i > 0:
163         i = find_tokens_backwards(lines, [start_token, end_token], i-1)
164         if check_token(lines[i], end_token):
165             count = count+1
166         else:
167             count = count-1
168         if count == 0:
169             return i
170     return -1
171
172
173 # Finds the matching \end_inset
174 def find_end_of_inset(lines, i):
175     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
176
177
178 # Finds the matching \end_inset
179 def find_beginning_of_inset(lines, i):
180     return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
181
182
183 def find_end_of_tabular(lines, i):
184     return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
185
186
187 def get_tabular_lines(lines, i):
188     result = []
189     i = i+1
190     j = find_end_of_tabular(lines, i)
191     if j == -1:
192         return []
193
194     while i <= j:
195         if check_token(lines[i], "\\begin_inset"):
196             i = find_end_of_inset(lines, i)+1
197         else:
198             result.append(i)
199             i = i+1
200     return result
201
202
203 def is_nonempty_line(line):
204     return line != " "*len(line)
205
206
207 def find_nonempty_line(lines, start, end = 0):
208     if end == 0:
209         end = len(lines)
210     for i in xrange(start, end):
211         if is_nonempty_line(lines[i]):
212             return i
213     return -1