]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/parser_tools.py
status tag patch from Georg
[lyx.git] / lib / lyx2lyx / parser_tools.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2003 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
21 def check_token(line, token):
22     if line[:len(token)] == token:
23         return 1
24     return 0
25
26 # We need to check that the char after the token is space, but I think
27 # we can ignore this
28 def find_token(lines, token, start, end = 0):
29     if end == 0:
30         end = len(lines)
31     m = len(token)
32     for i in xrange(start, end):
33         if lines[i][:m] == token:
34             return i
35     return -1
36
37 def find_token2(lines, token, start, end = 0):
38     if end == 0:
39         end = len(lines)
40     for i in xrange(start, end):
41         x = string.split(lines[i])
42         if len(x) > 0 and x[0] == token:
43             return i
44     return -1
45
46 def find_tokens(lines, tokens, start, end = 0):
47     if end == 0:
48         end = len(lines)
49     for i in xrange(start, end):
50         line = lines[i]
51         for token in tokens:
52             if line[:len(token)] == token:
53                 return i
54     return -1
55
56 def find_re(lines, rexp, start, end = 0):
57     if end == 0:
58         end = len(lines)
59     for i in xrange(start, end):
60         if rexp.match(lines[i]):
61                 return i
62     return -1
63
64 def find_token_backwards(lines, token, start):
65     m = len(token)
66     for i in xrange(start, -1, -1):
67         line = lines[i]
68         if line[:m] == token:
69             return i
70     return -1
71
72 def find_tokens_backwards(lines, tokens, start):
73     for i in xrange(start, -1, -1):
74         line = lines[i]
75         for token in tokens:
76             if line[:len(token)] == token:
77                 return i
78     return -1
79
80 def get_value(lines, token, start, end = 0):
81     i = find_token2(lines, token, start, end)
82     if i == -1:
83         return ""
84     if len(string.split(lines[i])) > 1:
85         return string.split(lines[i])[1]
86     else:
87         return ""
88
89 def del_token(lines, token, i, j):
90     k = find_token2(lines, token, i, j)
91     if k == -1:
92         return j
93     else:
94         del lines[k]
95         return j-1
96
97 # Finds the paragraph that contains line i.
98 def get_paragraph(lines, i):
99     while i != -1:
100         i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
101         if i == -1: return -1
102         if check_token(lines[i], "\\layout"):
103             return i
104         i = find_beginning_of_inset(lines, i)
105
106 # Finds the paragraph after the paragraph that contains line i.
107 def get_next_paragraph(lines, i):
108     while i != -1:
109         i = find_tokens(lines, ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"], i)
110         if not check_token(lines[i], "\\begin_inset"):
111             return i
112         i = find_end_of_inset(lines, i)
113
114 def find_end_of(lines, i, start_token, end_token):
115     count = 1
116     n = len(lines)
117     while i < n:
118         i = find_tokens(lines, [end_token, start_token], i+1)
119         if check_token(lines[i], start_token):
120             count = count+1
121         else:
122             count = count-1
123         if count == 0:
124             return i
125     return -1
126
127 # Finds the matching \end_inset
128 def find_beginning_of(lines, i, start_token, end_token):
129     count = 1
130     while i > 0:
131         i = find_tokens_backwards(lines, [start_token, end_token], i-1)
132         if check_token(lines[i], end_token):
133             count = count+1
134         else:
135             count = count-1
136         if count == 0:
137             return i
138     return -1
139
140 # Finds the matching \end_inset
141 def find_end_of_inset(lines, i):
142     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
143
144 # Finds the matching \end_inset
145 def find_beginning_of_inset(lines, i):
146     return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
147
148 def find_end_of_tabular(lines, i):
149     return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
150
151 def get_tabular_lines(lines, i):
152     result = []
153     i = i+1
154     j = find_end_of_tabular(lines, i)
155     if j == -1:
156         return []
157
158     while i <= j:
159         if check_token(lines[i], "\\begin_inset"):
160             i = find_end_of_inset(lines, i)+1
161         else:
162             result.append(i)
163             i = i+1
164     return result
165
166 def is_nonempty_line(line):
167     return line != " "*len(line)
168
169 def find_nonempty_line(lines, start, end = 0):
170     if end == 0:
171         end = len(lines)
172     for i in xrange(start, end):
173         if is_nonempty_line(lines[i]):
174             return i
175     return -1
176
177 def set_comment(lines, version):
178     lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
179     if lines[1][0] == '#':
180         del lines[1]
181
182 def set_format(lines, number):
183     if int(number) <= 217:
184         number = float(number)/100
185     i = find_token(lines, "\\lyxformat", 0)
186     lines[i] = "\\lyxformat %s" % number