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
160 if line != " "*len(line):
164 font_rexp = re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
165 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
166 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
168 def remove_oldert(lines):
169 ert_begin = ["\\begin_inset ERT",
175 i = find_token(lines, "\\latex latex", i)
178 j = find_tokens(lines, ["\\latex default", "\\layout", "\\end_float"],
182 if check_token(lines[j], "\\layout"):
183 while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
186 # We need to remove insets, special chars & font commands from ERT text
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]]
233 oldunits = ["pt", "cm", "in", "text%", "col%"]
235 def get_length(lines, name, start, end):
236 i = find_token(lines, name, start, end)
239 x = string.split(lines[i])
240 return x[2]+oldunits[int(x[1])]
242 def append(x, token, value):
244 x.append("\t"+token+" "+value)
246 def remove_figinset(lines):
249 i = find_token(lines, "\\begin_inset Figure", i)
252 j = find_token(lines, "\\end_inset", i+1)
254 lyxwidth = string.split(lines[i])[3]+"pt"
255 lyxheight = string.split(lines[i])[4]+"pt"
257 filename = get_value(lines, "file", i+1, j)
259 width = get_length(lines, "width", i+1, j)
260 # what does width=5 mean ?
261 height = get_length(lines, "height", i+1, j)
262 rotateAngle = get_value(lines, "angle", i+1, j)
263 if width == "" and height == "":
268 flags = get_value(lines, "flags", i+1, j)
271 display = "monochrome"
277 subcaptionText = get_value(lines, "subcaption", i+1, j)
278 if subcaptionText != "":
279 subcaptionText = '"'+subcaptionText+'"'
280 k = find_token(lines, "subfigure", i+1,j)
286 new = ["\\begin_inset Graphics FormatVersion 1"]
287 append(new, "filename", filename)
288 append(new, "display", display)
290 new.append("\tsubcaption")
291 append(new, "subcaptionText", subcaptionText)
292 append(new, "size_type", size_type)
293 append(new, "width", width)
294 append(new, "height", height)
295 if rotateAngle != "":
296 new.append("\trotate")
297 append(new, "rotateAngle", rotateAngle)
298 new.append("\trotateOrigin center")
299 new.append("\tlyxsize_type 1")
300 append(new, "lyxwidth", lyxwidth)
301 append(new, "lyxheight", lyxheight)
302 new = new + ["\end_inset"]
305 def change_preamble(lines):
306 i = find_token(lines, "\\use_amsmath", 0)
309 lines[i+1:i+1] = ["\\use_natbib 0",
310 "\use_numerical_citations 0"]
312 def convert(header, body):
313 language = get_value(header, "\\language", 0)
317 change_preamble(header)
319 remove_oldminipage(body)
320 remove_oldfloat(body, language)
321 remove_figinset(body)
323 if __name__ == "__main__":