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"]
47 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
48 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
49 r"(\s+\\pextra_alignment\s+(\S+))?"+\
50 r"(\s+\\pextra_hfill\s+(\S+))?"+\
51 r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
52 r"(\s+(\\pextra_widthp?)\s+(\S*))?")
56 if mo.group(9) == "\\pextra_widthp":
57 return mo.group(10)+"col%"
64 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
67 def remove_oldfloat(lines, language):
70 i = find_token(lines, "\\begin_float", i)
73 # There are no nested floats, so finding the end of the float is simple
74 j = find_token(lines, "\\end_float", i+1)
76 floattype = string.split(lines[i])[1]
77 if not floats.has_key(floattype):
78 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
81 # skip \end_deeper tokens
83 while check_token(lines[i2], "\\end_deeper"):
86 j2 = get_next_paragraph(lines, j+1)
87 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
89 new = floats[floattype]+[""]
91 # Check if the float is floatingfigure
92 k = find_re(lines, pextra_type3_rexp, i, j)
94 mo = pextra_rexp.search(lines[k])
96 lines[k] = re.sub(pextra_rexp, "", lines[k])
97 new = ["\\begin_inset Wrap figure",
102 new = new+lines[i2:j]+["\\end_inset ", ""]
104 # After a float, all font attributes are reseted.
105 # We need to output '\foo default' for every attribute foo
106 # whose value is not default before the float.
107 # The check here is not accurate, but it doesn't matter
108 # as extra '\foo default' commands are ignored.
109 # In fact, it might be safer to output '\foo default' for all
111 k = get_paragraph(lines, i)
113 for token in font_tokens:
114 if find_token(lines, token, k, i) != -1:
116 # This is not necessary, but we want the output to be
117 # as similar as posible to the lyx format
120 if token == "\\lang":
121 new.append(token+" "+language)
123 new.append(token+" default ")
128 pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
129 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
131 def remove_pextra(lines):
135 i = find_re(lines, pextra_type2_rexp, i)
139 mo = pextra_rexp.search(lines[i])
140 width = get_width(mo)
142 if mo.group(1) == "1":
143 # handle \pextra_type 1 (indented paragraph)
144 lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i])
148 # handle \pextra_type 2 (minipage)
149 position = mo.group(3)
151 lines[i] = re.sub(pextra_rexp, "", lines[i])
153 start = ["\\begin_inset Minipage",
154 "position " + position,
157 'width "%s"' % width,
163 start = ["","\hfill",""]+start
165 start = ["\\layout Standard"] + start
167 j = find_token_backwards(lines,"\\layout", i-1)
170 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
175 # collect more paragraphs to the minipage
177 if j == -1 or not check_token(lines[j], "\\layout"):
179 i = find_re(lines, pextra_type2_rexp2, j+1)
182 mo = pextra_rexp.search(lines[i])
185 if mo.group(7) == "1":
188 lines[i] = re.sub(pextra_rexp, "", lines[i])
189 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
192 end = ["\\end_inset "]
194 lines[j0:j] = start+mid+end
198 return filter(is_nonempty_line, lines) == []
200 move_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
201 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
202 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
203 ert_begin = ["\\begin_inset ERT",
208 def remove_oldert(lines):
211 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
216 j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_inset", "\\end_float", "\\the_end"],
218 if check_token(lines[j], "\\begin_inset"):
219 j = find_end_of_inset(lines, j)
223 if check_token(lines[j], "\\layout"):
224 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
227 # We need to remove insets, special chars & font commands from ERT text
230 if check_token(lines[i], "\\layout LaTeX"):
231 new = ["\layout Standard", "", ""]
232 # We have a problem with classes in which Standard is not the default layout!
236 k2 = find_re(lines, ert_rexp, k, j)
237 inset = specialchar = 0
240 elif check_token(lines[k2], "\\begin_inset"):
244 mo = spchar_rexp.match(lines[k2])
245 lines[k2] = mo.group(1)
246 specialchar_str = mo.group(2)
250 for line in lines[k:k2]:
251 # Move some lines outside the ERT inset:
252 if move_rexp.match(line):
254 # This is not necessary, but we want the output to be
255 # as similar as posible to the lyx format
258 elif not check_token(line, "\\latex"):
262 if filter(lambda x:x != "", tmp) != []:
264 # This is not necessary, but we want the output to be
265 # as similar as posible to the lyx format
266 lines[i-1] = lines[i-1]+" "
270 new = new+ert_begin+tmp+["\\end_inset ", ""]
273 k3 = find_end_of_inset(lines, k2)
274 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
276 # Skip the empty line after \end_inset
277 if not is_nonempty_line(lines[k]):
282 # This is not necessary, but we want the output to be
283 # as similar as posible to the lyx format
284 lines[i-1] = lines[i-1]+specialchar_str
287 new = new+[specialchar_str, ""]
293 if not check_token(lines[j], "\\latex "):
294 new = new+[""]+[lines[j]]
298 # Delete remaining "\latex xxx" tokens
301 i = find_token(lines, "\\latex ", i)
306 # ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future.
307 def remove_oldertinset(lines):
310 i = find_token(lines, "\\begin_inset ERT", i)
313 j = find_end_of_inset(lines, i)
314 k = find_token(lines, "\\layout", i+1)
315 l = get_paragraph(lines, i)
316 if lines[k] == lines[l]: # same layout
322 def is_ert_paragraph(lines, i):
323 if not check_token(lines[i], "\\layout Standard"):
326 i = find_nonempty_line(lines, i+1)
327 if not check_token(lines[i], "\\begin_inset ERT"):
330 j = find_end_of_inset(lines, i)
331 k = find_nonempty_line(lines, j+1)
332 return check_token(lines[k], "\\layout")
334 def combine_ert(lines):
337 i = find_token(lines, "\\begin_inset ERT", i)
340 j = get_paragraph(lines, i)
343 while is_ert_paragraph(lines, j):
346 i2 = find_token(lines, "\\layout", j+1)
347 k = find_token(lines, "\\end_inset", i2+1)
348 text = text+lines[i2:k]
349 j = find_token(lines, "\\layout", k+1)
354 j = find_token(lines, "\\layout", i+1)
359 oldunits = ["pt", "cm", "in", "text%", "col%"]
361 def get_length(lines, name, start, end):
362 i = find_token(lines, name, start, end)
365 x = string.split(lines[i])
366 return x[2]+oldunits[int(x[1])]
368 def write_attribute(x, token, value):
370 x.append("\t"+token+" "+value)
372 def remove_figinset(lines):
375 i = find_token(lines, "\\begin_inset Figure", i)
378 j = find_end_of_inset(lines, i)
380 lyxwidth = string.split(lines[i])[3]+"pt"
381 lyxheight = string.split(lines[i])[4]+"pt"
383 filename = get_value(lines, "file", i+1, j)
385 width = get_length(lines, "width", i+1, j)
386 # what does width=5 mean ?
387 height = get_length(lines, "height", i+1, j)
388 rotateAngle = get_value(lines, "angle", i+1, j)
389 if width == "" and height == "":
394 flags = get_value(lines, "flags", i+1, j)
397 display = "monochrome"
403 subcaptionText = get_value(lines, "subcaption", i+1, j)
404 if subcaptionText != "":
405 subcaptionText = '"'+subcaptionText+'"'
406 k = find_token(lines, "subfigure", i+1,j)
412 new = ["\\begin_inset Graphics FormatVersion 1"]
413 write_attribute(new, "filename", filename)
414 write_attribute(new, "display", display)
416 new.append("\tsubcaption")
417 write_attribute(new, "subcaptionText", subcaptionText)
418 write_attribute(new, "size_type", size_type)
419 write_attribute(new, "width", width)
420 write_attribute(new, "height", height)
421 if rotateAngle != "":
422 new.append("\trotate")
423 write_attribute(new, "rotateAngle", rotateAngle)
424 write_attribute(new, "rotateOrigin", "leftBaseline")
425 write_attribute(new, "lyxsize_type", "1")
426 write_attribute(new, "lyxwidth", lyxwidth)
427 write_attribute(new, "lyxheight", lyxheight)
428 new = new + ["\end_inset"]
431 attr_re = re.compile(r' \w*="(false|0|)"')
432 line_re = re.compile(r'<(features|column|row|cell)')
434 def update_tabular(lines):
437 i = find_token(lines, '\\begin_inset Tabular', i)
441 for k in get_tabular_lines(lines, i):
442 if check_token(lines[k], "<lyxtabular"):
443 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
444 elif check_token(lines[k], "<column"):
445 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
447 if line_re.match(lines[k]):
448 lines[k] = re.sub(attr_re, "", lines[k])
452 # Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future.
453 def fix_oldfloatinset(lines):
456 i = find_token(lines, "\\begin_inset Float ", i)
459 j = find_token(lines, "collapsed", i)
461 lines[j:j] = ["wide false"]
464 def change_listof(lines):
467 i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
470 type = lines[i][33:-3]
471 lines[i] = "\\begin_inset FloatList "+type
474 def change_preamble(lines):
475 i = find_token(lines, "\\use_amsmath", 0)
478 lines[i+1:i+1] = ["\\use_natbib 0",
479 "\use_numerical_citations 0"]
481 def convert(header, body):
482 language = get_value(header, "\\language", 0)
486 change_preamble(header)
488 fix_oldfloatinset(body)
491 remove_oldfloat(body, language)
492 remove_figinset(body)
493 remove_oldertinset(body)
497 if __name__ == "__main__":