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",
30 "tab": ["\\begin_inset Float table",
33 "alg": ["\\begin_inset Float algorithm",
36 "wide-fig": ["\\begin_inset Float figure",
39 "wide-tab": ["\\begin_inset Float table",
44 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
45 "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
48 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
51 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
52 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
53 r"(\s+\\pextra_alignment\s+(\S+))?"+\
54 r"(\s+\\pextra_hfill\s+(\S+))?"+\
55 r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
56 r"(\s+(\\pextra_widthp?)\s+(\S*))?")
59 if mo.group(9) == "\\pextra_widthp":
60 return mo.group(10)+"col%"
66 def remove_oldfloat(lines, language):
69 i = find_token(lines, "\\begin_float", i)
72 # There are no nested floats, so finding the end of the float is simple
73 j = find_token(lines, "\\end_float", i+1)
75 floattype = string.split(lines[i])[1]
76 if not floats.has_key(floattype):
77 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
80 # skip \end_deeper tokens
82 while check_token(lines[i2], "\\end_deeper"):
85 j2 = get_next_paragraph(lines, j+1)
86 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
88 new = floats[floattype]+[""]
90 # Check if the float is floatingfigure
91 k = find_re(lines, pextra_type3_rexp, i, j)
93 mo = pextra_rexp.search(lines[k])
95 lines[k] = re.sub(pextra_rexp, "", lines[k])
96 new = ["\\begin_inset Wrap figure",
101 new = new+lines[i2:j]+["\\end_inset ", ""]
103 # After a float, all font attributes are reseted.
104 # We need to output '\foo default' for every attribute foo
105 # whose value is not default before the float.
106 # The check here is not accurate, but it doesn't matter
107 # as extra '\foo default' commands are ignored.
108 # In fact, it might be safer to output '\foo default' for all
110 k = get_paragraph(lines, i)
112 for token in font_tokens:
113 if find_token(lines, token, k, i) != -1:
115 # This is not necessary, but we want the output to be
116 # as similar as posible to the lyx format
119 if token == "\\lang":
120 new.append(token+" "+language)
122 new.append(token+" default ")
127 pextra_type2_rexp = re.compile(r".*\\pextra_type\s+2")
128 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
130 def remove_oldminipage(lines):
134 i = find_re(lines, pextra_type2_rexp, i)
138 mo = pextra_rexp.search(lines[i])
139 position = mo.group(3)
141 width = get_width(mo)
142 lines[i] = re.sub(pextra_rexp, "", lines[i])
144 start = ["\\begin_inset Minipage",
145 "position " + position,
148 'width "%s"' % width,
154 start = ["","\hfill",""]+start
156 start = ["\\layout Standard"] + start
158 j = find_token_backwards(lines,"\\layout", i-1)
162 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
163 # j can be -1, but this is still ok
164 mid = mid+lines[i+1:j]
168 # collect more paragraphs to the minipage
170 if j == -1 or not check_token(lines[j], "\\layout"):
172 i = find_re(lines, pextra_type2_rexp2, j+1)
175 mo = pextra_rexp.search(lines[i])
178 if mo.group(7) == "1":
181 j = find_token_backwards(lines,"\\layout", i-1)
183 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
184 mid = mid+lines[i+1:j]
186 end = ["\\end_inset "]
188 lines[j0:j] = start+mid+end
192 return filter(is_nonempty_line, lines) == []
194 font_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
195 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
196 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
197 ert_begin = ["\\begin_inset ERT",
202 def remove_oldert(lines):
205 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
210 j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_float", "\\the_end"],
212 if check_token(lines[j], "\\begin_inset"):
213 j = find_end_of_inset(lines, j)
217 if check_token(lines[j], "\\layout"):
218 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
221 # We need to remove insets, special chars & font commands from ERT text
224 if check_token(lines[i], "\\layout LaTeX"):
225 new = ["\layout Standard", "", ""]
226 # We have a problem with classes in which Standard is not the default layout!
230 k2 = find_re(lines, ert_rexp, k, j)
231 inset = specialchar = 0
234 elif check_token(lines[k2], "\\begin_inset"):
238 mo = spchar_rexp.match(lines[k2])
239 lines[k2] = mo.group(1)
240 specialchar_str = mo.group(2)
244 for line in lines[k:k2]:
245 if font_rexp.match(line):
247 # This is not necessary, but we want the output to be
248 # as similar as posible to the lyx format
251 elif not check_token(line, "\\latex"):
255 if filter(lambda x:x != "", tmp) != []:
257 # This is not necessary, but we want the output to be
258 # as similar as posible to the lyx format
259 lines[i-1] = lines[i-1]+" "
263 new = new+ert_begin+tmp+["\\end_inset ", ""]
266 k3 = find_end_of_inset(lines, k2)
267 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
269 # Skip the empty line after \end_inset
270 if not is_nonempty_line(lines[k]):
275 # This is not necessary, but we want the output to be
276 # as similar as posible to the lyx format
277 lines[i-1] = lines[i-1]+specialchar_str
280 new = new+[specialchar_str, ""]
286 if not check_token(lines[j], "\\latex "):
287 new = new+[""]+[lines[j]]
293 i = find_token(lines, "\\latex ", i)
299 def remove_oldertinset(lines):
302 i = find_token(lines, "\\begin_inset ERT", i)
305 j = find_end_of_inset(lines, i)
306 k = find_token(lines, "\\layout", i+1)
307 l = get_paragraph(lines, i)
308 if lines[k] == lines[l]: # same layout
314 def is_ert_paragraph(lines, i):
315 i = find_nonempty_line(lines, i+1)
316 if not check_token(lines[i], "\\begin_inset ERT"):
318 j = find_end_of_inset(lines, i)
319 k = find_nonempty_line(lines, j+1)
320 return check_token(lines[k], "\\layout")
322 def combine_ert(lines):
325 i = find_token(lines, "\\begin_inset ERT", i)
328 j = find_token_backwards(lines,"\\layout", i-1)
331 while is_ert_paragraph(lines, j):
334 i2 = find_token(lines, "\\layout", j+1)
335 k = find_token(lines, "\\end_inset", i2+1)
336 text = text+lines[i2:k]
337 j = find_token(lines, "\\layout", k+1)
342 j = find_token(lines, "\\layout", i+1)
347 oldunits = ["pt", "cm", "in", "text%", "col%"]
349 def get_length(lines, name, start, end):
350 i = find_token(lines, name, start, end)
353 x = string.split(lines[i])
354 return x[2]+oldunits[int(x[1])]
356 def write_attribute(x, token, value):
358 x.append("\t"+token+" "+value)
360 def remove_figinset(lines):
363 i = find_token(lines, "\\begin_inset Figure", i)
366 j = find_end_of_inset(lines, i)
368 lyxwidth = string.split(lines[i])[3]+"pt"
369 lyxheight = string.split(lines[i])[4]+"pt"
371 filename = get_value(lines, "file", i+1, j)
373 width = get_length(lines, "width", i+1, j)
374 # what does width=5 mean ?
375 height = get_length(lines, "height", i+1, j)
376 rotateAngle = get_value(lines, "angle", i+1, j)
377 if width == "" and height == "":
382 flags = get_value(lines, "flags", i+1, j)
385 display = "monochrome"
391 subcaptionText = get_value(lines, "subcaption", i+1, j)
392 if subcaptionText != "":
393 subcaptionText = '"'+subcaptionText+'"'
394 k = find_token(lines, "subfigure", i+1,j)
400 new = ["\\begin_inset Graphics FormatVersion 1"]
401 write_attribute(new, "filename", filename)
402 write_attribute(new, "display", display)
404 new.append("\tsubcaption")
405 write_attribute(new, "subcaptionText", subcaptionText)
406 write_attribute(new, "size_type", size_type)
407 write_attribute(new, "width", width)
408 write_attribute(new, "height", height)
409 if rotateAngle != "":
410 new.append("\trotate")
411 write_attribute(new, "rotateAngle", rotateAngle)
412 write_attribute(new, "rotateOrigin", "leftBaseline")
413 write_attribute(new, "lyxsize_type", "1")
414 write_attribute(new, "lyxwidth", lyxwidth)
415 write_attribute(new, "lyxheight", lyxheight)
416 new = new + ["\end_inset"]
419 attr_re = re.compile(r' \w*="(false|0|)"')
420 line_re = re.compile(r'<(features|column|row|cell)')
422 def update_tabular(lines):
425 i = find_token(lines, '\\begin_inset Tabular', i)
429 for k in get_tabular_lines(lines, i):
430 if check_token(lines[k], "<lyxtabular"):
431 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
432 elif check_token(lines[k], "<column"):
433 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
435 if line_re.match(lines[k]):
436 lines[k] = re.sub(attr_re, "", lines[k])
440 def change_preamble(lines):
441 i = find_token(lines, "\\use_amsmath", 0)
444 lines[i+1:i+1] = ["\\use_natbib 0",
445 "\use_numerical_citations 0"]
447 def convert(header, body):
448 language = get_value(header, "\\language", 0)
452 change_preamble(header)
454 remove_oldminipage(body)
455 remove_oldfloat(body, language)
456 remove_figinset(body)
457 remove_oldertinset(body)
461 if __name__ == "__main__":