1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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.
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.
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.
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
23 def add_end_layout(lines):
24 i = find_token(lines, '\\layout', 0)
30 struct_stack = ["\\layout"]
32 i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
33 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
35 token = split(lines[i])[0]
37 if token == "\\begin_inset":
38 struct_stack.append(token)
42 if token == "\\end_inset":
43 tail = struct_stack.pop()
44 if tail == "\\layout":
45 lines.insert(i,"\\end_layout")
47 #Check if it is the correct tag
52 if token == "\\layout":
53 tail = struct_stack.pop()
55 lines.insert(i,"\\end_layout")
58 struct_stack.append(tail)
60 struct_stack.append(token)
63 if token == "\\begin_deeper" or token == "\\end_deeper":
64 lines.insert(i,"\\end_layout")
69 lines.insert(i, "\\end_layout")
72 def layout2begin_layout(lines):
75 i = find_token(lines, '\\layout', i)
79 lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
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"')
87 def table_valignment_middle(lines):
90 i = find_token(lines, '\\begin_inset Tabular', i)
93 j = find_end_of_inset(lines, i + 1)
95 #this should not happen
96 valignment_middle(lines, i + 1, len(lines))
98 valignment_middle(lines, i + 1, j)
101 def end_document(lines):
102 i = find_token(lines, "\\the_end", 0)
104 lines.append("\\end_document")
106 lines[i] = "\\end_document"
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.
114 inner_pos = ["c","t","b","s"]
118 i = find_token(lines, "\\begin_inset Minipage", i)
122 lines[i] = "\\begin_inset Frameless"
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])]
129 lines.insert(i, 'position "%s"' % pos[0])
132 lines.insert(i, 'hor_pos "c"')
134 lines.insert(i, 'has_inner_box 1')
137 # convert the inner_position
138 if lines[i][:14] == "inner_position":
139 lines[i] = 'inner_pos "%s"' % inner_pos[int(lines[i][15])]
141 lines.insert('inner_pos "%s"' % inner_pos[0])
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"':
155 if lines[i][:5] == "width":
161 lines.insert(i, 'use_parbox 0')
163 lines.insert(i, 'width' + width)
165 lines.insert(i, 'special "none"')
167 lines.insert(i, 'height' + height)
169 lines.insert(i, 'height_special "height"')
173 # Convert line and page breaks
176 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom
180 #\begin_layout Standard
191 def convert_breaks(lines):
194 i = find_token(lines, "\\begin_layout", i)
198 line_top = find(lines[i],"\\line_top")
199 line_bot = find(lines[i],"\\line_bottom")
200 pb_top = find(lines[i],"\\pagebreak_top")
201 pb_bot = find(lines[i],"\\pagebreak_bottom")
203 if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1:
209 # Create an empty paragraph for line and page break that belong
210 # above the paragraph
211 # To simplify the code, and maintain the same insertion point,
212 # I inserted by reverse order. It looks funny. :-)
213 if pb_top !=-1 or line_top != -1:
216 lines.insert(k, '\\end_layout')
220 lines.insert(k, "\\lyxline ")
225 lines.insert(k, "\\newpage ")
230 lines.insert(k, '\\begin_layout Standard')
233 # Ensure that nested style are converted later.
234 k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
240 lines.insert(k, "\\lyxline ")
244 lines.insert(k, "\\newpage ")
247 def convert(header, body):
249 layout2begin_layout(body)
251 table_valignment_middle(body)
252 convert_minipage(body)
255 if __name__ == "__main__":