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)
188 j = find_tokens(lines, ["\\latex default", "\\layout", "\\end_float", "\\the_end"],
190 if check_token(lines[j], "\\layout"):
191 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
194 # We need to remove insets, special chars & font commands from ERT text
197 if check_token(lines[i], "\\layout LaTeX"):
198 new = ["\layout Standard", "", ""]
199 # We have a problem with classes in which Standard is not the default layout!
203 k2 = find_re(lines, ert_rexp, k, j)
204 inset = specialchar = 0
207 elif check_token(lines[k2], "\\begin_inset"):
211 mo = spchar_rexp.match(lines[k2])
212 lines[k2] = mo.group(1)
213 specialchar_str = mo.group(2)
217 for line in lines[k:k2]:
218 if font_rexp.match(line):
220 # This is not necessary, but we want the output to be
221 # as similar as posible to the lyx format
228 if filter(lambda x:x != "", tmp) != []:
230 # This is not necessary, but we want the output to be
231 # as similar as posible to the lyx format
232 lines[i-1] = lines[i-1]+" "
236 new = new+ert_begin+tmp+["\\end_inset ", ""]
239 k3 = find_token(lines, "\\end_inset", k2+1)
240 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
242 # Skip the empty line after \end_inset
243 if not is_nonempty_line(lines[k]):
248 # This is not necessary, but we want the output to be
249 # as similar as posible to the lyx format
250 lines[i-1] = lines[i-1]+specialchar_str
253 new = new+[specialchar_str, ""]
259 if not check_token(lines[j], "\\latex default"):
260 new = new+[""]+[lines[j]]
264 def is_ert_paragraph(lines, i):
265 i = find_nonempty_line(lines, i+1)
266 if not check_token(lines[i], "\\begin_inset ERT"):
268 j = find_token(lines, "\\end_inset", i)
269 k = find_nonempty_line(lines, j+1)
270 return check_token(lines[k], "\\layout")
272 def combine_ert(lines):
275 i = find_token(lines, "\\begin_inset ERT", i)
278 j = find_token_backwards(lines,"\\layout", i-1)
281 while is_ert_paragraph(lines, j):
284 i2 = find_token(lines, "\\layout", j+1)
285 k = find_token(lines, "\\end_inset", i2+1)
286 text = text+lines[i2:k]
287 j = find_token(lines, "\\layout", k+1)
292 j = find_token(lines, "\\layout", i+1)
297 oldunits = ["pt", "cm", "in", "text%", "col%"]
299 def get_length(lines, name, start, end):
300 i = find_token(lines, name, start, end)
303 x = string.split(lines[i])
304 return x[2]+oldunits[int(x[1])]
306 def append(x, token, value):
308 x.append("\t"+token+" "+value)
310 def remove_figinset(lines):
313 i = find_token(lines, "\\begin_inset Figure", i)
316 j = find_token(lines, "\\end_inset", i+1)
318 lyxwidth = string.split(lines[i])[3]+"pt"
319 lyxheight = string.split(lines[i])[4]+"pt"
321 filename = get_value(lines, "file", i+1, j)
323 width = get_length(lines, "width", i+1, j)
324 # what does width=5 mean ?
325 height = get_length(lines, "height", i+1, j)
326 rotateAngle = get_value(lines, "angle", i+1, j)
327 if width == "" and height == "":
332 flags = get_value(lines, "flags", i+1, j)
335 display = "monochrome"
341 subcaptionText = get_value(lines, "subcaption", i+1, j)
342 if subcaptionText != "":
343 subcaptionText = '"'+subcaptionText+'"'
344 k = find_token(lines, "subfigure", i+1,j)
350 new = ["\\begin_inset Graphics FormatVersion 1"]
351 append(new, "filename", filename)
352 append(new, "display", display)
354 new.append("\tsubcaption")
355 append(new, "subcaptionText", subcaptionText)
356 append(new, "size_type", size_type)
357 append(new, "width", width)
358 append(new, "height", height)
359 if rotateAngle != "":
360 new.append("\trotate")
361 append(new, "rotateAngle", rotateAngle)
362 new.append("\trotateOrigin center")
363 new.append("\tlyxsize_type 1")
364 append(new, "lyxwidth", lyxwidth)
365 append(new, "lyxheight", lyxheight)
366 new = new + ["\end_inset"]
369 def change_preamble(lines):
370 i = find_token(lines, "\\use_amsmath", 0)
373 lines[i+1:i+1] = ["\\use_natbib 0",
374 "\use_numerical_citations 0"]
376 def convert(header, body):
377 language = get_value(header, "\\language", 0)
381 change_preamble(header)
384 remove_oldminipage(body)
385 remove_oldfloat(body, language)
386 remove_figinset(body)
388 if __name__ == "__main__":