1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
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.
21 from parser_tools import *
24 "footnote": ["\\begin_inset Foot",
26 "margin": ["\\begin_inset Marginal",
28 "fig": ["\\begin_inset Float figure",
31 "tab": ["\\begin_inset Float table",
34 "alg": ["\\begin_inset Float algorithm",
37 "wide-fig": ["\\begin_inset Float figure",
40 "wide-tab": ["\\begin_inset Float table",
45 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
46 "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
48 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
49 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
50 r"(\s+\\pextra_alignment\s+(\S+))?"+\
51 r"(\s+\\pextra_hfill\s+(\S+))?"+\
52 r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
53 r"(\s+(\\pextra_widthp?)\s+(\S*))?")
57 if mo.group(9) == "\\pextra_widthp":
58 return mo.group(10)+"col%"
65 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
68 def remove_oldfloat(lines, language):
71 i = find_token(lines, "\\begin_float", i)
74 # There are no nested floats, so finding the end of the float is simple
75 j = find_token(lines, "\\end_float", i+1)
77 floattype = string.split(lines[i])[1]
78 if not floats.has_key(floattype):
79 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
82 # skip \end_deeper tokens
84 while check_token(lines[i2], "\\end_deeper"):
87 j2 = get_next_paragraph(lines, j+1)
88 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
90 new = floats[floattype]+[""]
92 # Check if the float is floatingfigure
93 k = find_re(lines, pextra_type3_rexp, i, j)
95 mo = pextra_rexp.search(lines[k])
97 lines[k] = re.sub(pextra_rexp, "", lines[k])
98 new = ["\\begin_inset Wrap figure",
103 new = new+lines[i2:j]+["\\end_inset ", ""]
105 # After a float, all font attributes are reseted.
106 # We need to output '\foo default' for every attribute foo
107 # whose value is not default before the float.
108 # The check here is not accurate, but it doesn't matter
109 # as extra '\foo default' commands are ignored.
110 # In fact, it might be safer to output '\foo default' for all
112 k = get_paragraph(lines, i)
114 for token in font_tokens:
115 if find_token(lines, token, k, i) != -1:
117 # This is not necessary, but we want the output to be
118 # as similar as posible to the lyx format
121 if token == "\\lang":
122 new.append(token+" "+language)
124 new.append(token+" default ")
129 pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
130 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
132 def remove_pextra(lines):
136 i = find_re(lines, pextra_type2_rexp, i)
140 mo = pextra_rexp.search(lines[i])
141 width = get_width(mo)
143 if mo.group(1) == "1":
144 # handle \pextra_type 1 (indented paragraph)
145 lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i])
149 # handle \pextra_type 2 (minipage)
150 position = mo.group(3)
152 lines[i] = re.sub(pextra_rexp, "", lines[i])
154 start = ["\\begin_inset Minipage",
155 "position " + position,
158 'width "%s"' % width,
164 start = ["","\hfill",""]+start
166 start = ["\\layout Standard"] + start
168 j0 = find_token_backwards(lines,"\\layout", i-1)
169 j = get_next_paragraph(lines, i)
173 # collect more paragraphs to the minipage
175 if j == -1 or not check_token(lines[j], "\\layout"):
177 i = find_re(lines, pextra_type2_rexp2, j+1)
180 mo = pextra_rexp.search(lines[i])
183 if mo.group(7) == "1":
186 lines[i] = re.sub(pextra_rexp, "", lines[i])
187 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
190 end = ["\\end_inset "]
192 lines[j0:j] = start+mid+end
196 return filter(is_nonempty_line, lines) == []
198 move_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
199 ert_rexp = re.compile(r"\\begin_inset|\\hfill|.*\\SpecialChar")
200 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
201 ert_begin = ["\\begin_inset ERT",
206 def remove_oldert(lines):
209 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
214 # \end_inset is for ert inside a tabular cell. The other tokens
216 j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
218 if check_token(lines[j], "\\begin_inset"):
219 j = find_end_of_inset(lines, j)+1
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 = hfill = specialchar = 0
240 elif check_token(lines[k2], "\\begin_inset"):
242 elif check_token(lines[k2], "\\hfill"):
248 mo = spchar_rexp.match(lines[k2])
249 lines[k2] = mo.group(1)
250 specialchar_str = mo.group(2)
254 for line in lines[k:k2]:
255 # Move some lines outside the ERT inset:
256 if move_rexp.match(line):
258 # This is not necessary, but we want the output to be
259 # as similar as posible to the lyx format
262 elif not check_token(line, "\\latex"):
266 if filter(lambda x:x != "", tmp) != []:
268 # This is not necessary, but we want the output to be
269 # as similar as posible to the lyx format
270 lines[i-1] = lines[i-1]+" "
274 new = new+ert_begin+tmp+["\\end_inset ", ""]
277 k3 = find_end_of_inset(lines, k2)
278 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
280 # Skip the empty line after \end_inset
281 if not is_nonempty_line(lines[k]):
285 new = new+["\hfill", ""]
289 # This is not necessary, but we want the output to be
290 # as similar as posible to the lyx format
291 lines[i-1] = lines[i-1]+specialchar_str
294 new = new+[specialchar_str, ""]
300 if not check_token(lines[j], "\\latex "):
301 new = new+[""]+[lines[j]]
305 # Delete remaining "\latex xxx" tokens
308 i = find_token(lines, "\\latex ", i)
313 # ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future.
314 def remove_oldertinset(lines):
317 i = find_token(lines, "\\begin_inset ERT", i)
320 j = find_end_of_inset(lines, i)
321 k = find_token(lines, "\\layout", i+1)
322 l = get_paragraph(lines, i)
323 if lines[k] == lines[l]: # same layout
329 def is_ert_paragraph(lines, i):
330 if not check_token(lines[i], "\\layout Standard"):
333 i = find_nonempty_line(lines, i+1)
334 if not check_token(lines[i], "\\begin_inset ERT"):
337 j = find_end_of_inset(lines, i)
338 k = find_nonempty_line(lines, j+1)
339 return check_token(lines[k], "\\layout")
341 def combine_ert(lines):
344 i = find_token(lines, "\\begin_inset ERT", i)
347 j = get_paragraph(lines, i)
350 while is_ert_paragraph(lines, j):
353 i2 = find_token(lines, "\\layout", j+1)
354 k = find_token(lines, "\\end_inset", i2+1)
355 text = text+lines[i2:k]
356 j = find_token(lines, "\\layout", k+1)
361 j = find_token(lines, "\\layout", i+1)
366 oldunits = ["pt", "cm", "in", "text%", "col%"]
368 def get_length(lines, name, start, end):
369 i = find_token(lines, name, start, end)
372 x = string.split(lines[i])
373 return x[2]+oldunits[int(x[1])]
375 def write_attribute(x, token, value):
377 x.append("\t"+token+" "+value)
379 def remove_figinset(lines):
382 i = find_token(lines, "\\begin_inset Figure", i)
385 j = find_end_of_inset(lines, i)
387 if ( len(string.split(lines[i])) > 2 ):
388 lyxwidth = string.split(lines[i])[3]+"pt"
389 lyxheight = string.split(lines[i])[4]+"pt"
394 filename = get_value(lines, "file", i+1, j)
396 width = get_length(lines, "width", i+1, j)
397 # what does width=5 mean ?
398 height = get_length(lines, "height", i+1, j)
399 rotateAngle = get_value(lines, "angle", i+1, j)
400 if width == "" and height == "":
405 flags = get_value(lines, "flags", i+1, j)
408 display = "monochrome"
415 subcaptionLine = find_token(lines, "subcaption", i+1, j)
416 if subcaptionLine != -1:
417 subcaptionText = lines[subcaptionLine][11:]
418 if subcaptionText != "":
419 subcaptionText = '"'+subcaptionText+'"'
421 k = find_token(lines, "subfigure", i+1,j)
427 new = ["\\begin_inset Graphics FormatVersion 1"]
428 write_attribute(new, "filename", filename)
429 write_attribute(new, "display", display)
431 new.append("\tsubcaption")
432 write_attribute(new, "subcaptionText", subcaptionText)
433 write_attribute(new, "size_type", size_type)
434 write_attribute(new, "width", width)
435 write_attribute(new, "height", height)
436 if rotateAngle != "":
437 new.append("\trotate")
438 write_attribute(new, "rotateAngle", rotateAngle)
439 write_attribute(new, "rotateOrigin", "leftBaseline")
440 write_attribute(new, "lyxsize_type", "1")
441 write_attribute(new, "lyxwidth", lyxwidth)
442 write_attribute(new, "lyxheight", lyxheight)
443 new = new + ["\end_inset"]
446 attr_re = re.compile(r' \w*="(false|0|)"')
447 line_re = re.compile(r'<(features|column|row|cell)')
449 def update_tabular(lines):
452 i = find_token(lines, '\\begin_inset Tabular', i)
456 for k in get_tabular_lines(lines, i):
457 if check_token(lines[k], "<lyxtabular"):
458 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
459 elif check_token(lines[k], "<column"):
460 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
462 if line_re.match(lines[k]):
463 lines[k] = re.sub(attr_re, "", lines[k])
467 # Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future.
468 def fix_oldfloatinset(lines):
471 i = find_token(lines, "\\begin_inset Float ", i)
474 j = find_token(lines, "collapsed", i)
476 lines[j:j] = ["wide false"]
479 def change_listof(lines):
482 i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
485 type = re.search(r"listof(\w*)", lines[i]).group(1)[:-1]
486 lines[i] = "\\begin_inset FloatList "+type
489 def change_infoinset(lines):
492 i = find_token(lines, "\\begin_inset Info", i)
495 txt = string.lstrip(lines[i][18:])
496 new = ["\\begin_inset Note", "collapsed true", ""]
497 j = find_token(lines, "\\end_inset", i)
501 note_lines = lines[i+1:j]
503 note_lines = [txt]+note_lines
505 for line in note_lines:
506 new = new + ["\layout Standard", ""]
507 tmp = string.split(line, '\\')
510 new = new + ["\\backslash ", x]
514 def change_preamble(lines):
515 i = find_token(lines, "\\use_amsmath", 0)
518 lines[i+1:i+1] = ["\\use_natbib 0",
519 "\use_numerical_citations 0"]
521 def convert(header, body):
522 language = get_value(header, "\\language", 0)
526 change_preamble(header)
528 fix_oldfloatinset(body)
531 remove_oldfloat(body, language)
532 remove_figinset(body)
533 remove_oldertinset(body)
536 change_infoinset(body)
538 if __name__ == "__main__":