]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_224.py
Handle Michael's changes to InsetCollapsable.
[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, find_end_of
20 from sys import stderr
21 from string import replace, split, find, replace, strip, join
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
109 def convert_minipage(lines):
110     """ Convert minipages to the box inset.
111     We try to use the same order of arguments as lyx does.
112     """
113     pos = ["t","c","b"]
114     inner_pos = ["c","t","b","s"]
115
116     i = 0
117     while 1:
118         i = find_token(lines, "\\begin_inset Minipage", i)
119         if i == -1:
120             return
121
122         lines[i] = "\\begin_inset Frameless"
123         i = i + 1
124
125         # convert old to new position using the pos list
126         if lines[i][:8] == "position":
127             lines[i] = 'position "%s"' % pos[int(lines[i][9])]
128         else:
129             lines.insert(i, 'position "%s"' % pos[0])
130         i = i + 1
131
132         lines.insert(i, 'hor_pos "c"')
133         i = i + 1
134         lines.insert(i, 'has_inner_box 1')
135         i = i + 1
136
137         # convert the inner_position
138         if lines[i][:14] == "inner_position":
139             lines[i] = 'inner_pos "%s"' %  inner_pos[int(lines[i][15])]
140         else:
141             lines.insert('inner_pos "%s"' % inner_pos[0])
142         i = i + 1
143
144         # We need this since the new file format has a height and width
145         # in a different order.
146         if lines[i][:6] == "height":
147             height = lines[i][6:]
148             # test for default value of 221 and convert it accordingly
149             if height == ' "0pt"':
150                 height = ' "1pt"'
151             del lines[i]
152         else:
153             height = ' "1pt"'
154
155         if lines[i][:5] == "width":
156             width = lines[i][5:]
157             del lines[i]
158         else:
159             width = ' "0"'
160
161         lines.insert(i, 'use_parbox 0')
162         i = i + 1
163         lines.insert(i, 'width' + width)
164         i = i + 1
165         lines.insert(i, 'special "none"')
166         i = i + 1
167         lines.insert(i, 'height' + height)
168         i = i + 1
169         lines.insert(i, 'height_special "totalheight"')
170         i = i + 1
171
172 ##
173 # Convert line and page breaks
174 # Old:
175 #\layout Standard
176 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
177 #0
178 #
179 # New:
180 #\begin layout Standard
181 #
182 #\newpage 
183 #
184 #\lyxline
185 #\begin_inset VSpace xxx
186 #\end_inset
187 #
188 #\end_layout
189 #\begin_layout Standard
190 #
191 #0
192 #\end_layout
193 #\begin_layout Standard
194 #
195 #\begin_inset VSpace xxx
196 #\end_inset
197 #\lyxline 
198 #
199 #\newpage
200 #
201 #\end_layout
202
203 def convert_breaks(lines):    
204     i = 0
205     while 1:
206         i = find_token(lines, "\\begin_layout", i)
207         if i == -1:
208             return
209         i = i + 1
210         line_top   = find(lines[i],"\\line_top")
211         line_bot   = find(lines[i],"\\line_bottom")
212         pb_top     = find(lines[i],"\\pagebreak_top")
213         pb_bot     = find(lines[i],"\\pagebreak_bottom")
214         vspace_top = find(lines[i],"\\added_space_top")
215         vspace_bot = find(lines[i],"\\added_space_bottom")
216
217         if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
218             continue
219
220         for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
221             lines[i] = replace(lines[i], tag, "")
222
223         if vspace_top != -1:
224             # the position could be change because of the removal of other
225             # paragraph properties above
226             vspace_top = find(lines[i],"\\added_space_top")
227             tmp_list = split(lines[i][vspace_top:])
228             vspace_top_value = tmp_list[1]
229             lines[i] = lines[i][:vspace_top] + join(tmp_list[2:])
230
231         if vspace_bot != -1:
232             # the position could be change because of the removal of other
233             # paragraph properties above
234             vspace_bot = find(lines[i],"\\added_space_bottom")
235             tmp_list = split(lines[i][vspace_bot:])
236             vspace_bot_value = tmp_list[1]
237             lines[i] = lines[i][:vspace_bot] + join(tmp_list[2:])
238
239         lines[i] = strip(lines[i])
240         i = i + 1
241
242         #  Create an empty paragraph for line and page break that belong
243         # above the paragraph
244         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
245             
246             paragraph_above = ['','\\begin_layout Standard','','']
247
248             if pb_top != -1:
249                 paragraph_above.extend(['\\newpage ',''])
250
251             if vspace_top != -1:
252                 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset ','',''])
253
254             if line_top != -1:
255                 paragraph_above.extend(['\\lyxline ',''])
256
257             paragraph_above.extend(['\\end_layout',''])
258
259             #inset new paragraph above the current paragraph
260             lines[i-2:i-2] = paragraph_above
261             i = i + len(paragraph_above)
262
263         # Ensure that nested style are converted later.
264         k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
265
266         if k == -1:
267             return
268
269         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
270             
271             paragraph_bellow = ['','\\begin_layout Standard','','']
272
273             if line_bot != -1:
274                 paragraph_bellow.extend(['\\lyxline ',''])
275
276             if vspace_bot != -1:
277                 paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset ','',''])
278
279             if pb_bot != -1:
280                 paragraph_bellow.extend(['\\newpage ',''])
281
282             paragraph_bellow.extend(['\\end_layout',''])
283
284             #inset new paragraph above the current paragraph
285             lines[k + 1: k + 1] = paragraph_bellow
286
287 def convert(header, body):
288     add_end_layout(body)
289     layout2begin_layout(body)
290     end_document(body)
291     table_valignment_middle(body)
292     convert_minipage(body)
293     convert_breaks(body)
294
295 if __name__ == "__main__":
296     pass