]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyxconvert_224.py
9b491cb179126d4513f0de880af07871c678460a
[features.git] / lib / lyx2lyx / lyxconvert_224.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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 re
20 from parser_tools import find_token, find_tokens, find_end_of_inset, find_end_of
21 from sys import stderr
22 from string import replace, split, find, replace, strip, join
23
24 def add_end_layout(lines):
25     i = find_token(lines, '\\layout', 0)
26
27     if i == -1:
28         return
29
30     i = i + 1
31     struct_stack = ["\\layout"]
32     while 1:
33         i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
34                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
35
36         token = split(lines[i])[0]
37
38         if token == "\\begin_inset":
39             struct_stack.append(token)
40             i = i + 1
41             continue
42
43         if token == "\\end_inset":
44             tail = struct_stack.pop()
45             if tail == "\\layout":
46                 lines.insert(i,"\\end_layout")
47                 i = i + 1
48                 #Check if it is the correct tag
49                 struct_stack.pop()
50             i = i + 1
51             continue
52
53         if token == "\\layout":
54             tail = struct_stack.pop()
55             if tail == token:
56                 lines.insert(i,"\\end_layout")
57                 i = i + 2
58             else:
59                 struct_stack.append(tail)
60                 i = i + 1
61             struct_stack.append(token)
62             continue
63
64         if token == "\\begin_deeper" or token == "\\end_deeper":
65             lines.insert(i,"\\end_layout")
66             i = i + 2
67             continue
68
69         #case \end_document
70         lines.insert(i, "\\end_layout")
71         return
72
73 def layout2begin_layout(lines):
74     i = 0
75     while 1:
76         i = find_token(lines, '\\layout', i)
77         if i == -1:
78             return
79
80         lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
81         i = i + 1
82
83 def valignment_middle(lines, start, end):
84     for i in range(start, end):
85         if re.search('^<(column|cell) .*valignment="center".*>$', lines[i]):
86             lines[i] = replace(lines[i], 'valignment="center"', 'valignment="middle"')
87
88 def table_valignment_middle(lines):
89     i = 0
90     while 1:
91         i = find_token(lines, '\\begin_inset  Tabular', i)
92         if i == -1:
93             return
94         j = find_end_of_inset(lines, i + 1)
95         if j == -1:
96             #this should not happen
97             valignment_middle(lines, i + 1, len(lines))
98             return
99         valignment_middle(lines, i + 1, j)
100         i = j + 1
101
102 def end_document(lines):
103     i = find_token(lines, "\\the_end", 0)
104     if i == -1:
105         lines.append("\\end_document")
106         return
107     lines[i] = "\\end_document"
108
109 ##
110 # Convert line and page breaks
111 # Old:
112 #\layout Standard
113 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
114 #0
115 #
116 # New:
117 #\begin layout Standard
118 #
119 #\newpage 
120 #
121 #\lyxline
122 #\begin_inset VSpace xxx
123 #\end_inset
124 #
125 #\end_layout
126 #\begin_layout Standard
127 #
128 #0
129 #\end_layout
130 #\begin_layout Standard
131 #
132 #\begin_inset VSpace xxx
133 #\end_inset
134 #\lyxline 
135 #
136 #\newpage
137 #
138 #\end_layout
139
140 def convert_breaks(lines):    
141     i = 0
142     while 1:
143         i = find_token(lines, "\\begin_layout", i)
144         if i == -1:
145             return
146         i = i + 1
147         line_top   = find(lines[i],"\\line_top")
148         line_bot   = find(lines[i],"\\line_bottom")
149         pb_top     = find(lines[i],"\\pagebreak_top")
150         pb_bot     = find(lines[i],"\\pagebreak_bottom")
151         vspace_top = find(lines[i],"\\added_space_top")
152         vspace_bot = find(lines[i],"\\added_space_bottom")
153
154         if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
155             continue
156
157         for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
158             lines[i] = replace(lines[i], tag, "")
159
160         if vspace_top != -1:
161             # the position could be change because of the removal of other
162             # paragraph properties above
163             vspace_top = find(lines[i],"\\added_space_top")
164             tmp_list = split(lines[i][vspace_top:])
165             vspace_top_value = tmp_list[1]
166             lines[i] = lines[i][:vspace_top] + join(tmp_list[2:])
167
168         if vspace_bot != -1:
169             # the position could be change because of the removal of other
170             # paragraph properties above
171             vspace_bot = find(lines[i],"\\added_space_bottom")
172             tmp_list = split(lines[i][vspace_bot:])
173             vspace_bot_value = tmp_list[1]
174             lines[i] = lines[i][:vspace_bot] + join(tmp_list[2:])
175
176         lines[i] = strip(lines[i])
177         i = i + 1
178
179         #  Create an empty paragraph for line and page break that belong
180         # above the paragraph
181         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
182             
183             paragraph_above = ['','\\begin_layout Standard','','']
184
185             if pb_top != -1:
186                 paragraph_above.extend(['\\newpage ',''])
187
188             if vspace_top != -1:
189                 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset ','',''])
190
191             if line_top != -1:
192                 paragraph_above.extend(['\\lyxline ',''])
193
194             paragraph_above.extend(['\\end_layout',''])
195
196             #inset new paragraph above the current paragraph
197             lines[i-2:i-2] = paragraph_above
198             i = i + len(paragraph_above)
199
200         # Ensure that nested style are converted later.
201         k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
202
203         if k == -1:
204             return
205
206         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
207             
208             paragraph_bellow = ['','\\begin_layout Standard','','']
209
210             if line_bot != -1:
211                 paragraph_bellow.extend(['\\lyxline ',''])
212
213             if vspace_bot != -1:
214                 paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset ','',''])
215
216             if pb_bot != -1:
217                 paragraph_bellow.extend(['\\newpage ',''])
218
219             paragraph_bellow.extend(['\\end_layout',''])
220
221             #inset new paragraph above the current paragraph
222             lines[k + 1: k + 1] = paragraph_bellow
223
224 def convert(header, body):
225     add_end_layout(body)
226     layout2begin_layout(body)
227     end_document(body)
228     table_valignment_middle(body)
229     convert_breaks(body)
230
231 if __name__ == "__main__":
232     pass