]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/parser_tools.py
Cleanup
[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_tokens(lines, tokens, start, end = 0):
37     if end == 0:
38         end = len(lines)
39     for i in xrange(start, end):
40         line = lines[i]
41         for token in tokens:
42             if line[:len(token)] == token:
43                 return i
44     return -1
45
46 def find_re(lines, rexp, start, end = 0):
47     if end == 0:
48         end = len(lines)
49     for i in xrange(start, end):
50         if rexp.match(lines[i]):
51                 return i
52     return -1
53
54 def find_token_backwards(lines, token, start):
55     m = len(token)
56     for i in xrange(start, -1, -1):
57         line = lines[i]
58         if line[:m] == token:
59             return i
60     return -1
61
62 def find_tokens_backwards(lines, tokens, start):
63     for i in xrange(start, -1, -1):
64         line = lines[i]
65         for token in tokens:
66             if line[:len(token)] == token:
67                 return i
68     return -1
69
70 def get_value(lines, token, start, end = 0):
71     i = find_token(lines, token, start, end)
72     if i == -1:
73         return ""
74     return string.split(lines[i])[1]
75
76 # Finds the paragraph that contains line i.
77 def get_paragraph(lines, i):
78     while 1:
79         i = find_tokens_backwards(lines, ["\\end_inset", "\\layout"], i)
80         if check_token(lines[i], "\\layout"):
81             return i
82         i = find_beginning_of_inset(lines, i)
83
84 # Finds the paragraph after the paragraph that contains line i.
85 def get_next_paragraph(lines, i):
86     while 1:
87         i = find_tokens(lines, ["\\begin_inset", "\\layout"], i)
88         if check_token(lines[i], "\\layout"):
89             return i
90         i = find_end_of_inset(lines, i)
91
92 # Finds the matching \end_inset
93 def find_end_of_inset(lines, i):
94     count = 1
95     while 1:
96         i = find_tokens(lines, ["\\end_inset", "\\begin_inset"], i+1)
97         if check_token(lines[i], "\\begin_inset"):
98             count = count+1
99         else:
100             count = count-1
101         if count == 0:
102             return i
103
104 # Finds the matching \end_inset
105 def find_beginning_of_inset(lines, i):
106     count = 1
107     while 1:
108         i = find_tokens_backwards(lines, ["\\end_inset", "\\begin_inset"], i-1)
109         if check_token(lines[i], "\\end_inset"):
110             count = count+1
111         else:
112             count = count-1
113         if count == 0:
114             return i
115
116 def is_nonempty_line(line):
117     return line != " "*len(line)
118
119 def find_nonempty_line(lines, start, end = 0):
120     if end == 0:
121         end = len(lines)
122     for i in xrange(start, end):
123         if is_nonempty_line(lines[i]):
124             return i
125     return -1
126
127 def set_comment(lines, number):
128     x = int(number)
129     if x < 216:
130         # It is not worth the trouble to handle this case
131         return
132     elif x < 220:
133         version = "1.1"
134     else:
135         version = "1.2"
136
137     lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
138     if lines[1][0] == '#':
139         del lines[1]
140
141 def set_format(lines, number):
142     if int(number) <= 217:
143         number = float(number)/100
144     i = find_token(lines, "\\lyxformat", 0)
145     lines[i] = "\\lyxformat %s" % number