]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/parser_tools.py
e24d5bd36cb375f6b24bc9c917fa8402da80451e
[lyx.git] / lib / lyx2lyx / parser_tools.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 import string
19
20 def check_token(line, token):
21     if line[:len(token)] == token:
22         return 1
23     return 0
24
25 # We need to check that the char after the token is space, but I think
26 # we can ignore this
27 def find_token(lines, token, start, end = 0):
28     if end == 0:
29         end = len(lines)
30     m = len(token)
31     for i in xrange(start, end):
32         if lines[i][:m] == token:
33             return i
34     return -1
35
36 def find_token2(lines, token, start, end = 0):
37     if end == 0:
38         end = len(lines)
39     for i in xrange(start, end):
40         x = string.split(lines[i])
41         if len(x) > 0 and x[0] == token:
42             return i
43     return -1
44
45 def find_tokens(lines, tokens, start, end = 0):
46     if end == 0:
47         end = len(lines)
48     for i in xrange(start, end):
49         line = lines[i]
50         for token in tokens:
51             if line[:len(token)] == token:
52                 return i
53     return -1
54
55 def find_re(lines, rexp, start, end = 0):
56     if end == 0:
57         end = len(lines)
58     for i in xrange(start, end):
59         if rexp.match(lines[i]):
60                 return i
61     return -1
62
63 def find_token_backwards(lines, token, start):
64     m = len(token)
65     for i in xrange(start, -1, -1):
66         line = lines[i]
67         if line[:m] == token:
68             return i
69     return -1
70
71 def find_tokens_backwards(lines, tokens, start):
72     for i in xrange(start, -1, -1):
73         line = lines[i]
74         for token in tokens:
75             if line[:len(token)] == token:
76                 return i
77     return -1
78
79 def get_value(lines, token, start, end = 0):
80     i = find_token2(lines, token, start, end)
81     if i == -1:
82         return ""
83     return string.split(lines[i])[1]
84
85 def del_token(lines, token, i, j):
86     k = find_token2(lines, token, i, j)
87     if k == -1:
88         return j
89     else:
90         del lines[k]
91         return j-1
92
93 # Finds the paragraph that contains line i.
94 def get_paragraph(lines, i):
95     while i != -1:
96         i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
97         if i == -1: return -1
98         if check_token(lines[i], "\\layout"):
99             return i
100         i = find_beginning_of_inset(lines, i)
101
102 # Finds the paragraph after the paragraph that contains line i.
103 def get_next_paragraph(lines, i):
104     while i != -1:
105         i = find_tokens(lines, ["\\begin_inset", "\\layout"], i)
106         if i == -1: return -1
107         if check_token(lines[i], "\\layout"):
108             return i
109         i = find_end_of_inset(lines, i)
110
111 def find_end_of(lines, i, start_token, end_token):
112     count = 1
113     n = len(lines)
114     while i < n:
115         i = find_tokens(lines, [end_token, start_token], i+1)
116         if check_token(lines[i], start_token):
117             count = count+1
118         else:
119             count = count-1
120         if count == 0:
121             return i
122     return -1
123
124 # Finds the matching \end_inset
125 def find_beginning_of(lines, i, start_token, end_token):
126     count = 1
127     while i > 0:
128         i = find_tokens_backwards(lines, [start_token, end_token], i-1)
129         if check_token(lines[i], end_token):
130             count = count+1
131         else:
132             count = count-1
133         if count == 0:
134             return i
135     return -1
136
137 # Finds the matching \end_inset
138 def find_end_of_inset(lines, i):
139     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
140
141 # Finds the matching \end_inset
142 def find_beginning_of_inset(lines, i):
143     return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
144
145 def find_end_of_tabular(lines, i):
146     return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
147
148 def get_tabular_lines(lines, i):
149     result = []
150     i = i+1
151     j = find_end_of_tabular(lines, i)
152     if j == -1:
153         return []
154
155     while i <= j:
156         if check_token(lines[i], "\\begin_inset"):
157             i = find_end_of_inset(lines, i)+1
158         else:
159             result.append(i)
160             i = i+1
161     return result
162
163 def is_nonempty_line(line):
164     return line != " "*len(line)
165
166 def find_nonempty_line(lines, start, end = 0):
167     if end == 0:
168         end = len(lines)
169     for i in xrange(start, end):
170         if is_nonempty_line(lines[i]):
171             return i
172     return -1
173
174 def set_comment(lines, number):
175     x = int(number)
176     if x < 216:
177         # It is not worth the trouble to handle this case
178         return
179     elif x < 220:
180         version = "1.1"
181     else:
182         version = str((x-220)/10.0+1.2)
183
184     lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
185     if lines[1][0] == '#':
186         del lines[1]
187
188 def set_format(lines, number):
189     if int(number) <= 217:
190         number = float(number)/100
191     i = find_token(lines, "\\lyxformat", 0)
192     lines[i] = "\\lyxformat %s" % number