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 def remove_oldfloat(lines, language):
52 i = find_token(lines, "\\begin_float", i)
55 j = find_token(lines, "\\end_float", i+1)
56 floattype = string.split(lines[i])[1]
57 if not floats.has_key(floattype):
58 sys.stderr.write("Error! Unknown float type "+floattype+"\n")
61 # skip \end_deeper tokens
63 while check_token(lines[i2], "\\end_deeper"):
66 j2 = find_token(lines, "\\layout", j+1)
67 lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
69 start = floats[floattype]+[""]
71 end = ["\\end_inset ",
77 # "\\numeric default ",
81 "\\lang %s " % language]
82 # It isn't nice to always put all the '\xxx default' statements,
84 lines[i:j+1]= start+mid+end
88 def remove_oldminipage(lines):
92 i = find_token(lines, "\\pextra_type 2", i)
96 line = string.split(lines[i])
97 if line[4] == "\\pextra_hfill":
100 if line[4] == "\\pextra_start_minipage":
101 # We just ignore this
106 if line[4] == "\\pextra_widthp":
107 width = line[5]+"col%"
110 start = ["\\begin_inset Minipage",
111 "position " + position,
114 'width "%s"' % width,
120 start = ["","\hfill",""]+start
122 start = ["\\layout Standard"] + start
124 j = find_token_backwards(lines,"\\layout", i-1)
128 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
129 # j can be -1, but this is still ok
130 mid = mid+lines[i+1:j]
134 # collect more paragraphs to the minipage
136 if j == -1 or not check_token(lines[j], "\\layout"):
138 i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
139 if i == -1 or not check_token(lines[i], "\\pextra_type"):
141 line = string.split(lines[i])
142 if line[4] == "\\pextra_hfill":
144 if line[4] == "\\pextra_start_minipage" and line[5] == "1":
147 j = find_token_backwards(lines,"\\layout", i-1)
149 j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
150 mid = mid+lines[i+1:j]
152 end = ["\\end_inset "]
154 lines[j0:j] = start+mid+end
158 return filter(is_nonempty_line, lines) == []
160 font_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
161 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
162 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
163 ert_begin = ["\\begin_inset ERT",
168 def remove_oldert(lines):
171 i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
174 j = find_tokens(lines, ["\\latex default", "\\layout", "\\end_float"],
178 if check_token(lines[j], "\\layout"):
179 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
182 # We need to remove insets, special chars & font commands from ERT text
185 if check_token(lines[i], "\\layout LaTeX"):
186 new = ["\layout Standard", "", ""]
187 # We have a problem with classes in which Standard is not the default layout!
191 k2 = find_re(lines, ert_rexp, k, j)
192 inset = specialchar = 0
195 elif check_token(lines[k2], "\\begin_inset"):
199 mo = spchar_rexp.match(lines[k2])
200 lines[k2] = mo.group(1)
201 specialchar_str = mo.group(2)
205 for line in lines[k:k2]:
206 if font_rexp.match(line):
214 new = new+ert_begin+tmp+["\\end_inset ", ""]
217 k3 = find_token(lines, "\\end_inset", k2+1)
218 new = new+[""]+lines[k2:k3+1]+["", ""]
221 new = new+[specialchar_str]
227 if not check_token(lines[j], "\\latex default"):
228 new = new+[""]+[lines[j]]
232 def is_ert_paragraph(lines, i):
233 i = find_nonempty_line(lines, i+1)
234 if not check_token(lines[i], "\\begin_inset ERT"):
236 j = find_token(lines, "\\end_inset", i)
237 k = find_nonempty_line(lines, j+1)
238 return check_token(lines[k], "\\layout")
240 def combine_ert(lines):
243 i = find_token(lines, "\\begin_inset ERT", i)
246 j = find_token_backwards(lines,"\\layout", i-1)
249 while is_ert_paragraph(lines, j):
252 i2 = find_token(lines, "\\layout", j+1)
253 k = find_token(lines, "\\end_inset", i2+1)
254 text = text+lines[i2:k]
255 j = find_token(lines, "\\layout", k+1)
264 oldunits = ["pt", "cm", "in", "text%", "col%"]
266 def get_length(lines, name, start, end):
267 i = find_token(lines, name, start, end)
270 x = string.split(lines[i])
271 return x[2]+oldunits[int(x[1])]
273 def append(x, token, value):
275 x.append("\t"+token+" "+value)
277 def remove_figinset(lines):
280 i = find_token(lines, "\\begin_inset Figure", i)
283 j = find_token(lines, "\\end_inset", i+1)
285 lyxwidth = string.split(lines[i])[3]+"pt"
286 lyxheight = string.split(lines[i])[4]+"pt"
288 filename = get_value(lines, "file", i+1, j)
290 width = get_length(lines, "width", i+1, j)
291 # what does width=5 mean ?
292 height = get_length(lines, "height", i+1, j)
293 rotateAngle = get_value(lines, "angle", i+1, j)
294 if width == "" and height == "":
299 flags = get_value(lines, "flags", i+1, j)
302 display = "monochrome"
308 subcaptionText = get_value(lines, "subcaption", i+1, j)
309 if subcaptionText != "":
310 subcaptionText = '"'+subcaptionText+'"'
311 k = find_token(lines, "subfigure", i+1,j)
317 new = ["\\begin_inset Graphics FormatVersion 1"]
318 append(new, "filename", filename)
319 append(new, "display", display)
321 new.append("\tsubcaption")
322 append(new, "subcaptionText", subcaptionText)
323 append(new, "size_type", size_type)
324 append(new, "width", width)
325 append(new, "height", height)
326 if rotateAngle != "":
327 new.append("\trotate")
328 append(new, "rotateAngle", rotateAngle)
329 new.append("\trotateOrigin center")
330 new.append("\tlyxsize_type 1")
331 append(new, "lyxwidth", lyxwidth)
332 append(new, "lyxheight", lyxheight)
333 new = new + ["\end_inset"]
336 def change_preamble(lines):
337 i = find_token(lines, "\\use_amsmath", 0)
340 lines[i+1:i+1] = ["\\use_natbib 0",
341 "\use_numerical_citations 0"]
343 def convert(header, body):
344 language = get_value(header, "\\language", 0)
348 change_preamble(header)
351 remove_oldminipage(body)
352 remove_oldfloat(body, language)
353 remove_figinset(body)
355 if __name__ == "__main__":