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 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
50 "\\bar", "\\noun", "\\color", "\\lang"]
53 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
56 def remove_oldfloat(lines, language):
59 i = find_token(lines, "\\begin_float", i)
62 j = find_token(lines, "\\end_float", i+1)
63 floattype = string.split(lines[i])[1]
64 if not floats.has_key(floattype):
65 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
68 # skip \end_deeper tokens
70 while check_token(lines[i2], "\\end_deeper"):
73 j2 = find_token(lines, "\\layout", j+1)
74 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
76 new = floats[floattype]+[""]
77 new = new+lines[i2:j]+["\\end_inset ", ""]
78 # After a float, all font attribute are reseted.
79 # We need to output '\foo default' for every attribute foo
80 # whose value is not default before the float.
81 # The check here is not accurate, but it doesn't matter
82 # as extra '\foo default' commands are ignored.
83 # In fact, it might be safer to output '\foo default' for all
85 k = get_paragraph(lines, i)
87 for token in font_tokens:
88 if find_token(lines, token, k, i) != -1:
90 # This is not necessary, but we want the output to be
91 # as similar as posible to the lyx format
95 new.append(token+" "+language+" ")
97 new.append(token+" default ")
102 def remove_oldminipage(lines):
106 i = find_token(lines, "\\pextra_type 2", i)
110 line = string.split(lines[i])
111 if line[4] == "\\pextra_hfill":
114 if line[4] == "\\pextra_start_minipage":
115 # We just ignore this
120 if line[4] == "\\pextra_widthp":
121 width = line[5]+"col%"
124 start = ["\\begin_inset Minipage",
125 "position " + position,
128 'width "%s"' % width,
134 start = ["","\hfill",""]+start
136 start = ["\\layout Standard"] + start
138 j = find_token_backwards(lines,"\\layout", i-1)
142 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
143 # j can be -1, but this is still ok
144 mid = mid+lines[i+1:j]
148 # collect more paragraphs to the minipage
150 if j == -1 or not check_token(lines[j], "\\layout"):
152 i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
153 if i == -1 or not check_token(lines[i], "\\pextra_type"):
155 line = string.split(lines[i])
156 if line[4] == "\\pextra_hfill":
158 if line[4] == "\\pextra_start_minipage" and line[5] == "1":
161 j = find_token_backwards(lines,"\\layout", i-1)
163 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
164 mid = mid+lines[i+1:j]
166 end = ["\\end_inset "]
168 lines[j0:j] = start+mid+end
172 return filter(is_nonempty_line, lines) == []
174 font_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
175 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
176 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
177 ert_begin = ["\\begin_inset ERT",
182 def remove_oldert(lines):
185 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
190 j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_float", "\\the_end"],
192 if check_token(lines[j], "\\begin_inset"):
193 j = find_end_of_inset(lines, j)
197 if check_token(lines[j], "\\layout"):
198 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
201 # We need to remove insets, special chars & font commands from ERT text
204 if check_token(lines[i], "\\layout LaTeX"):
205 new = ["\layout Standard", "", ""]
206 # We have a problem with classes in which Standard is not the default layout!
210 k2 = find_re(lines, ert_rexp, k, j)
211 inset = specialchar = 0
214 elif check_token(lines[k2], "\\begin_inset"):
218 mo = spchar_rexp.match(lines[k2])
219 lines[k2] = mo.group(1)
220 specialchar_str = mo.group(2)
224 for line in lines[k:k2]:
225 if font_rexp.match(line):
227 # This is not necessary, but we want the output to be
228 # as similar as posible to the lyx format
235 if filter(lambda x:x != "", tmp) != []:
237 # This is not necessary, but we want the output to be
238 # as similar as posible to the lyx format
239 lines[i-1] = lines[i-1]+" "
243 new = new+ert_begin+tmp+["\\end_inset ", ""]
246 k3 = find_end_of_inset(lines, k2)
247 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
249 # Skip the empty line after \end_inset
250 if not is_nonempty_line(lines[k]):
255 # This is not necessary, but we want the output to be
256 # as similar as posible to the lyx format
257 lines[i-1] = lines[i-1]+specialchar_str
260 new = new+[specialchar_str, ""]
266 if not check_token(lines[j], "\\latex default"):
267 new = new+[""]+[lines[j]]
271 def convert_ertinset(lines):
274 i = find_token(lines, "\\begin_inset ERT", i)
277 j = find_token(lines, "collapsed", i+1)
278 if string.split(lines[j])[1] == "true":
282 lines[j] = "status " + status
284 # Remove font commands
285 j = find_end_of_inset(lines, i)
287 for line in lines[i:j]:
288 if not font_rexp.match(line) and not check_token(line, "\\latex"):
294 def is_ert_paragraph(lines, i):
295 i = find_nonempty_line(lines, i+1)
296 if not check_token(lines[i], "\\begin_inset ERT"):
298 j = find_end_of_inset(lines, i)
299 k = find_nonempty_line(lines, j+1)
300 return check_token(lines[k], "\\layout")
302 def combine_ert(lines):
305 i = find_token(lines, "\\begin_inset ERT", i)
308 j = find_token_backwards(lines,"\\layout", i-1)
311 while is_ert_paragraph(lines, j):
314 i2 = find_token(lines, "\\layout", j+1)
315 k = find_token(lines, "\\end_inset", i2+1)
316 text = text+lines[i2:k]
317 j = find_token(lines, "\\layout", k+1)
322 j = find_token(lines, "\\layout", i+1)
327 oldunits = ["pt", "cm", "in", "text%", "col%"]
329 def get_length(lines, name, start, end):
330 i = find_token(lines, name, start, end)
333 x = string.split(lines[i])
334 return x[2]+oldunits[int(x[1])]
336 def append(x, token, value):
338 x.append("\t"+token+" "+value)
340 def remove_figinset(lines):
343 i = find_token(lines, "\\begin_inset Figure", i)
346 j = find_end_of_inset(lines, i)
348 lyxwidth = string.split(lines[i])[3]+"pt"
349 lyxheight = string.split(lines[i])[4]+"pt"
351 filename = get_value(lines, "file", i+1, j)
353 width = get_length(lines, "width", i+1, j)
354 # what does width=5 mean ?
355 height = get_length(lines, "height", i+1, j)
356 rotateAngle = get_value(lines, "angle", i+1, j)
357 if width == "" and height == "":
362 flags = get_value(lines, "flags", i+1, j)
365 display = "monochrome"
371 subcaptionText = get_value(lines, "subcaption", i+1, j)
372 if subcaptionText != "":
373 subcaptionText = '"'+subcaptionText+'"'
374 k = find_token(lines, "subfigure", i+1,j)
380 new = ["\\begin_inset Graphics FormatVersion 1"]
381 append(new, "filename", filename)
382 append(new, "display", display)
384 new.append("\tsubcaption")
385 append(new, "subcaptionText", subcaptionText)
386 append(new, "size_type", size_type)
387 append(new, "width", width)
388 append(new, "height", height)
389 if rotateAngle != "":
390 new.append("\trotate")
391 append(new, "rotateAngle", rotateAngle)
392 new.append("\trotateOrigin center")
393 new.append("\tlyxsize_type 1")
394 append(new, "lyxwidth", lyxwidth)
395 append(new, "lyxheight", lyxheight)
396 new = new + ["\end_inset"]
399 def change_preamble(lines):
400 i = find_token(lines, "\\use_amsmath", 0)
403 lines[i+1:i+1] = ["\\use_natbib 0",
404 "\use_numerical_citations 0"]
406 def convert(header, body):
407 language = get_value(header, "\\language", 0)
411 change_preamble(header)
412 convert_ertinset(body)
415 remove_oldminipage(body)
416 remove_oldfloat(body, language)
417 remove_figinset(body)
419 if __name__ == "__main__":