1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
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.
20 from parser_tools import *
23 "footnote": ["\\begin_inset Foot",
25 "margin": ["\\begin_inset Marginal",
27 "fig": ["\\begin_inset Float figure",
31 "tab": ["\\begin_inset Float table",
35 "alg": ["\\begin_inset Float algorithm",
39 "wide-fig": ["\\begin_inset Float figure",
43 "wide-tab": ["\\begin_inset Float table",
49 def remove_oldfloat(lines, language):
52 i = find_token(lines, "\\begin_float", i)
55 j = find_token(lines, "\\end_float", i+1)
56 floattype = string.split(lines[i])[1]
57 if not floats.has_key(floattype):
58 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
61 # skip \end_deeper tokens
63 while check_token(lines[i2], "\\end_deeper"):
66 j2 = find_token(lines, "\\layout", j+1)
67 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
69 start = floats[floattype]+[""]
71 end = ["\\end_inset ",
77 # "\\numeric default ",
81 "\\lang %s " % language]
82 # It isn't nice to always put all the '\xxx default' statements,
84 lines[i:j+1]= start+mid+end
88 def remove_oldminipage(lines):
92 i = find_token(lines, "\\pextra_type 2", i)
96 line = string.split(lines[i])
97 if line[4] == "\\pextra_hfill":
100 if line[4] == "\\pextra_start_minipage":
101 # We just ignore this
106 if line[4] == "\\pextra_widthp":
107 width = line[5]+"col%"
110 start = ["\\begin_inset Minipage",
111 "position " + position,
114 'width "%s"' % width,
120 start = ["","\hfill",""]+start
122 start = ["\\layout Standard"] + start
124 j = find_token_backwards(lines,"\\layout", i-1)
128 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
129 # j can be -1, but this is still ok
130 mid = mid+lines[i+1:j]
134 # collect more paragraphs to the minipage
136 if j == -1 or not check_token(lines[j], "\\layout"):
138 i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
139 if i == -1 or not check_token(lines[i], "\\pextra_type"):
141 line = string.split(lines[i])
142 if line[4] == "\\pextra_hfill":
144 if line[4] == "\\pextra_start_minipage" and line[5] == "1":
147 j = find_token_backwards(lines,"\\layout", i-1)
149 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
150 mid = mid+lines[i+1:j]
152 end = ["\\end_inset "]
154 lines[j0:j] = start+mid+end
158 return filter(is_nonempty_line, lines) == []
160 font_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
161 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
162 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
163 ert_begin = ["\\begin_inset ERT",
168 def remove_oldert(lines):
171 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
174 j = find_tokens(lines, ["\\latex default", "\\layout", "\\end_float", "\\the_end"],
176 if check_token(lines[j], "\\layout"):
177 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
180 # We need to remove insets, special chars & font commands from ERT text
183 if check_token(lines[i], "\\layout LaTeX"):
184 new = ["\layout Standard", "", ""]
185 # We have a problem with classes in which Standard is not the default layout!
189 k2 = find_re(lines, ert_rexp, k, j)
190 inset = specialchar = 0
193 elif check_token(lines[k2], "\\begin_inset"):
197 mo = spchar_rexp.match(lines[k2])
198 lines[k2] = mo.group(1)
199 specialchar_str = mo.group(2)
203 for line in lines[k:k2]:
204 if font_rexp.match(line):
212 new = new+ert_begin+tmp+["\\end_inset ", ""]
215 k3 = find_token(lines, "\\end_inset", k2+1)
216 new = new+[""]+lines[k2:k3+1]+["", ""]
219 new = new+[specialchar_str]
225 if not check_token(lines[j], "\\latex default"):
226 new = new+[""]+[lines[j]]
230 def is_ert_paragraph(lines, i):
231 i = find_nonempty_line(lines, i+1)
232 if not check_token(lines[i], "\\begin_inset ERT"):
234 j = find_token(lines, "\\end_inset", i)
235 k = find_nonempty_line(lines, j+1)
236 return check_token(lines[k], "\\layout")
238 def combine_ert(lines):
241 i = find_token(lines, "\\begin_inset ERT", i)
244 j = find_token_backwards(lines,"\\layout", i-1)
247 while is_ert_paragraph(lines, j):
250 i2 = find_token(lines, "\\layout", j+1)
251 k = find_token(lines, "\\end_inset", i2+1)
252 text = text+lines[i2:k]
253 j = find_token(lines, "\\layout", k+1)
258 j = find_token(lines, "\\layout", i+1)
263 oldunits = ["pt", "cm", "in", "text%", "col%"]
265 def get_length(lines, name, start, end):
266 i = find_token(lines, name, start, end)
269 x = string.split(lines[i])
270 return x[2]+oldunits[int(x[1])]
272 def append(x, token, value):
274 x.append("\t"+token+" "+value)
276 def remove_figinset(lines):
279 i = find_token(lines, "\\begin_inset Figure", i)
282 j = find_token(lines, "\\end_inset", i+1)
284 lyxwidth = string.split(lines[i])[3]+"pt"
285 lyxheight = string.split(lines[i])[4]+"pt"
287 filename = get_value(lines, "file", i+1, j)
289 width = get_length(lines, "width", i+1, j)
290 # what does width=5 mean ?
291 height = get_length(lines, "height", i+1, j)
292 rotateAngle = get_value(lines, "angle", i+1, j)
293 if width == "" and height == "":
298 flags = get_value(lines, "flags", i+1, j)
301 display = "monochrome"
307 subcaptionText = get_value(lines, "subcaption", i+1, j)
308 if subcaptionText != "":
309 subcaptionText = '"'+subcaptionText+'"'
310 k = find_token(lines, "subfigure", i+1,j)
316 new = ["\\begin_inset Graphics FormatVersion 1"]
317 append(new, "filename", filename)
318 append(new, "display", display)
320 new.append("\tsubcaption")
321 append(new, "subcaptionText", subcaptionText)
322 append(new, "size_type", size_type)
323 append(new, "width", width)
324 append(new, "height", height)
325 if rotateAngle != "":
326 new.append("\trotate")
327 append(new, "rotateAngle", rotateAngle)
328 new.append("\trotateOrigin center")
329 new.append("\tlyxsize_type 1")
330 append(new, "lyxwidth", lyxwidth)
331 append(new, "lyxheight", lyxheight)
332 new = new + ["\end_inset"]
335 def change_preamble(lines):
336 i = find_token(lines, "\\use_amsmath", 0)
339 lines[i+1:i+1] = ["\\use_natbib 0",
340 "\use_numerical_citations 0"]
342 def convert(header, body):
343 language = get_value(header, "\\language", 0)
347 change_preamble(header)
350 remove_oldminipage(body)
351 remove_oldfloat(body, language)
352 remove_figinset(body)
354 if __name__ == "__main__":