]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_224.py
Revert, revert, revert. Sorry about that, y'all.
[lyx.git] / lib / lyx2lyx / lyxconvert_224.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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 re
19 from parser_tools import find_token, find_tokens, find_end_of_inset
20 from sys import stderr
21 from string import replace, split
22
23 def add_end_layout(lines):
24     i = find_token(lines, '\\layout', 0)
25
26     if i == -1:
27         return
28
29     i = i + 1
30     struct_stack = ["\\layout"]
31     while 1:
32         i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
33                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
34
35         token = split(lines[i])[0]
36
37         if token == "\\begin_inset":
38             struct_stack.append(token)
39             i = i + 1
40             continue
41
42         if token == "\\end_inset":
43             tail = struct_stack.pop()
44             if tail == "\\layout":
45                 lines.insert(i,"\\end_layout")
46                 i = i + 1
47                 #Check if it is the correct tag
48                 struct_stack.pop()
49             i = i + 1
50             continue
51
52         if token == "\\layout":
53             tail = struct_stack.pop()
54             if tail == token:
55                 lines.insert(i,"\\end_layout")
56                 i = i + 2
57             else:
58                 struct_stack.append(tail)
59                 i = i + 1
60             struct_stack.append(token)
61             continue
62
63         if token == "\\begin_deeper" or token == "\\end_deeper":
64             lines.insert(i,"\\end_layout")
65             i = i + 2
66             continue
67         
68         #case \end_document
69         lines.insert(i, "\\end_layout")
70         return
71
72 def layout2begin_layout(lines):
73     i = 0
74     while 1:
75         i = find_token(lines, '\\layout', i)
76         if i == -1:
77             return
78
79         lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
80         i = i + 1
81
82 def valignment_middle(lines, start, end):
83     for i in range(start, end):
84         if re.search('^<(column|cell) .*valignment="center".*>$', lines[i]):
85             lines[i] = replace(lines[i], 'valignment="center"', 'valignment="middle"')
86
87 def table_valignment_middle(lines):
88     i = 0
89     while 1:
90         i = find_token(lines, '\\begin_inset  Tabular', i)
91         if i == -1:
92             return
93         j = find_end_of_inset(lines, i + 1)
94         if j == -1:
95             #this should not happen
96             valignment_middle(lines, i + 1, len(lines))
97             return
98         valignment_middle(lines, i + 1, j)
99         i = j + 1
100
101 def end_document(lines):
102     i = find_token(lines, "\\the_end", 0)
103     if i == -1:
104         lines.append("\\end_document")
105         return
106     lines[i] = "\\end_document"
107     
108 def convert(header, body):
109     add_end_layout(body)
110     layout2begin_layout(body)
111     end_document(body)
112     table_valignment_middle(body)
113
114 if __name__ == "__main__":
115     pass