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, replace, strip, join
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 # Convert line and page breaks
112 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
116 #\begin layout Standard
121 #\begin_inset VSpace xxx
125 #\begin_layout Standard
129 #\begin_layout Standard
131 #\begin_inset VSpace xxx
139 def convert_breaks(lines):
142 i = find_token(lines, "\\begin_layout", i)
146 line_top = find(lines[i],"\\line_top")
147 line_bot = find(lines[i],"\\line_bottom")
148 pb_top = find(lines[i],"\\pagebreak_top")
149 pb_bot = find(lines[i],"\\pagebreak_bottom")
150 vspace_top = find(lines[i],"\\added_space_top")
151 vspace_bot = find(lines[i],"\\added_space_bottom")
153 if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
156 for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
157 lines[i] = replace(lines[i], tag, "")
160 # the position could be change because of the removal of other
161 # paragraph properties above
162 vspace_top = find(lines[i],"\\added_space_top")
163 tmp_list = split(lines[i][vspace_top:])
164 vspace_top_value = tmp_list[1]
165 lines[i] = lines[i][:vspace_top] + join(tmp_list[2:])
168 # the position could be change because of the removal of other
169 # paragraph properties above
170 vspace_bot = find(lines[i],"\\added_space_bottom")
171 tmp_list = split(lines[i][vspace_bot:])
172 vspace_bot_value = tmp_list[1]
173 lines[i] = lines[i][:vspace_bot] + join(tmp_list[2:])
175 lines[i] = strip(lines[i])
178 # Create an empty paragraph for line and page break that belong
179 # above the paragraph
180 if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
182 paragraph_above = ['','\\begin_layout Standard','','']
185 paragraph_above.extend(['\\newpage ',''])
188 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset ','',''])
191 paragraph_above.extend(['\\lyxline ',''])
193 paragraph_above.extend(['\\end_layout',''])
195 #inset new paragraph above the current paragraph
196 lines[i-2:i-2] = paragraph_above
197 i = i + len(paragraph_above)
199 # Ensure that nested style are converted later.
200 k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
205 if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
207 paragraph_bellow = ['','\\begin_layout Standard','','']
210 paragraph_bellow.extend(['\\lyxline ',''])
213 paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset ','',''])
216 paragraph_bellow.extend(['\\newpage ',''])
218 paragraph_bellow.extend(['\\end_layout',''])
220 #inset new paragraph above the current paragraph
221 lines[k + 1: k + 1] = paragraph_bellow
223 def convert(header, body):
225 layout2begin_layout(body)
227 table_valignment_middle(body)
230 if __name__ == "__main__":