1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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.
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.
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.
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
24 def add_end_layout(lines):
25 i = find_token(lines, '\\layout', 0)
31 struct_stack = ["\\layout"]
33 i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
34 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
36 token = split(lines[i])[0]
38 if token == "\\begin_inset":
39 struct_stack.append(token)
43 if token == "\\end_inset":
44 tail = struct_stack.pop()
45 if tail == "\\layout":
46 lines.insert(i,"\\end_layout")
48 #Check if it is the correct tag
53 if token == "\\layout":
54 tail = struct_stack.pop()
56 lines.insert(i,"\\end_layout")
59 struct_stack.append(tail)
61 struct_stack.append(token)
64 if token == "\\begin_deeper" or token == "\\end_deeper":
65 lines.insert(i,"\\end_layout")
70 lines.insert(i, "\\end_layout")
73 def layout2begin_layout(lines):
76 i = find_token(lines, '\\layout', i)
80 lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
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"')
88 def table_valignment_middle(lines):
91 i = find_token(lines, '\\begin_inset Tabular', i)
94 j = find_end_of_inset(lines, i + 1)
96 #this should not happen
97 valignment_middle(lines, i + 1, len(lines))
99 valignment_middle(lines, i + 1, j)
102 def end_document(lines):
103 i = find_token(lines, "\\the_end", 0)
105 lines.append("\\end_document")
107 lines[i] = "\\end_document"
110 # Convert line and page breaks
113 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
117 #\begin layout Standard
122 #\begin_inset VSpace xxx
126 #\begin_layout Standard
130 #\begin_layout Standard
132 #\begin_inset VSpace xxx
140 def convert_breaks(lines):
143 i = find_token(lines, "\\begin_layout", i)
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")
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:
157 for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
158 lines[i] = replace(lines[i], tag, "")
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:])
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:])
176 lines[i] = strip(lines[i])
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:
183 paragraph_above = ['','\\begin_layout Standard','','']
186 paragraph_above.extend(['\\newpage ',''])
189 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset ','',''])
192 paragraph_above.extend(['\\lyxline ',''])
194 paragraph_above.extend(['\\end_layout',''])
196 #inset new paragraph above the current paragraph
197 lines[i-2:i-2] = paragraph_above
198 i = i + len(paragraph_above)
200 # Ensure that nested style are converted later.
201 k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
206 if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
208 paragraph_bellow = ['','\\begin_layout Standard','','']
211 paragraph_bellow.extend(['\\lyxline ',''])
214 paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset ','',''])
217 paragraph_bellow.extend(['\\newpage ',''])
219 paragraph_bellow.extend(['\\end_layout',''])
221 #inset new paragraph above the current paragraph
222 lines[k + 1: k + 1] = paragraph_bellow
224 def convert(header, body):
226 layout2begin_layout(body)
228 table_valignment_middle(body)
231 if __name__ == "__main__":