]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_224.py
db4148f372f90fd1b1f2d0bcf48e0be708cca642
[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 from parser_tools import find_token, find_tokens
19 from sys import stderr
20 from string import replace, split
21
22 def add_end_layout(lines):
23     i = find_token(lines, '\\layout', 0)
24
25     if i == -1:
26         return
27
28     i = i + 1
29     struct_stack = ["\\layout"]
30     while 1:
31         i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
32                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
33
34         token = split(lines[i])[0]
35
36         if token == "\\begin_inset":
37             struct_stack.append(token)
38             i = i + 1
39             continue
40
41         if token == "\\end_inset":
42             tail = struct_stack.pop()
43             if tail == "\\layout":
44                 lines.insert(i,"\\end_layout")
45                 i = i + 1
46                 #Check if it is the correct tag
47                 struct_stack.pop()
48             i = i + 1
49             continue
50
51         if token == "\\layout":
52             tail = struct_stack.pop()
53             if tail == token:
54                 lines.insert(i,"\\end_layout")
55                 i = i + 2
56             else:
57                 struct_stack.append(tail)
58                 i = i + 1
59             struct_stack.append(token)
60             continue
61
62         if token == "\\begin_deeper" or token == "\\end_deeper":
63             lines.insert(i,"\\end_layout")
64             i = i + 2
65             continue
66         
67         #case \end_document
68         lines.insert(i, "\\end_layout")
69         return
70
71 def layout2begin_layout(lines):
72     i = 0
73     while 1:
74         i = find_token(lines, '\\layout', i)
75         if i == -1:
76             return
77
78         lines[i]= replace(lines[i], '\\layout', '\\begin_layout')
79         i = i + 1
80
81 def end_document(lines):
82     i = find_token(lines, "\\the_end", 0)
83     if i == -1:
84         lines.append("\\end_document")
85         return
86     lines[i] = "\\end_document"
87     
88 def convert(header, body):
89     add_end_layout(body)
90     layout2begin_layout(body)
91     end_document(body)
92
93 if __name__ == "__main__":
94     pass