]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_218.py
- Fix ert conversion
[lyx.git] / lib / lyx2lyx / lyxconvert_218.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
3 #
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.
8 #
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.
13 #
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.
17
18
19 import sys,string,re
20 from parser_tools import *
21
22 floats = {
23     "footnote": ["\\begin_inset Foot",
24                  "collapsed true"],
25     "margin":   ["\\begin_inset Marginal",
26                  "collapsed true"],
27     "fig":      ["\\begin_inset Float figure",
28                  "placement htbp",
29                  "wide false",
30                  "collapsed false"],
31     "tab":      ["\\begin_inset Float table",
32                  "placement htbp",
33                  "wide false",
34                  "collapsed false"],
35     "alg":      ["\\begin_inset Float algorithm",
36                  "placement htbp",
37                  "wide false",
38                  "collapsed false"],
39     "wide-fig": ["\\begin_inset Float figure",
40                  "placement htbp",
41                  "wide true",
42                  "collapsed false"],
43     "wide-tab": ["\\begin_inset Float table",
44                  "placement htbp",
45                  "wide true",
46                  "collapsed false"]
47 }
48
49 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
50                "\\bar", "\\noun", "\\color", "\\lang"]
51
52 #
53 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
54 #
55
56 def remove_oldfloat(lines, language):
57     i = 0
58     while 1:
59         i = find_token(lines, "\\begin_float", i)
60         if i == -1:
61             break
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")
66             floattype = "fig"
67
68         # skip \end_deeper tokens
69         i2 = i+1
70         while check_token(lines[i2], "\\end_deeper"):
71             i2 = i2+1
72         if i2 > i+1:
73             j2 = find_token(lines, "\\layout", j+1)
74             lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
75
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 
84         # font attributes.
85         k = get_paragraph(lines, i)
86         flag = 0
87         for token in font_tokens:
88             if find_token(lines, token, k, i) != -1:
89                 if not flag:
90                     # This is not necessary, but we want the output to be
91                     # as similar as posible to the lyx format
92                     flag = 1
93                     new.append("")
94                 if token == "\\lang":
95                     new.append(token+" "+language+" ")
96                 else:
97                     new.append(token+" default ")
98         lines[i:j+1]= new
99
100         i = i+1
101
102 def remove_oldminipage(lines):
103     i = 0
104     flag = 0
105     while 1:
106         i = find_token(lines, "\\pextra_type 2", i)
107         if i == -1:
108             break
109         hfill = 0
110         line = string.split(lines[i])
111         if line[4] == "\\pextra_hfill":
112             hfill = 1
113             line[4:6] = []
114         if line[4] == "\\pextra_start_minipage":
115             # We just ignore this
116             line[4:6] = []
117
118         position = line[3]
119         width = line[5]
120         if line[4] == "\\pextra_widthp":
121             width = line[5]+"col%"
122         
123
124         start = ["\\begin_inset Minipage",
125                  "position " + position,
126                  "inner_position 0",
127                  'height "0pt"',
128                  'width "%s"' % width,
129                  "collapsed false"
130                  ]
131         if flag:
132             flag = 0
133             if hfill:
134                 start = ["","\hfill",""]+start
135         else:
136             start = ["\\layout Standard"] + start
137
138         j = find_token_backwards(lines,"\\layout", i-1)
139         j0 = j
140         mid = lines[j:i]
141
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]
145
146         count = 0
147         while 1:
148             # collect more paragraphs to the minipage
149             count = count+1
150             if j == -1 or not check_token(lines[j], "\\layout"):
151                 break
152             i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
153             if i == -1 or not check_token(lines[i], "\\pextra_type"):
154                 break
155             line = string.split(lines[i])
156             if line[4] == "\\pextra_hfill":
157                 line[4:6] = []
158             if line[4] == "\\pextra_start_minipage" and line[5] == "1":
159                 flag = 1
160                 break
161             j = find_token_backwards(lines,"\\layout", i-1)
162             mid = mid+lines[j:i]
163             j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
164             mid = mid+lines[i+1:j]
165
166         end = ["\\end_inset "]
167
168         lines[j0:j] = start+mid+end
169         i = i+1
170
171 def is_empty(lines):
172     return filter(is_nonempty_line, lines) == []
173
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",
178              "status Collapsed",
179              "",
180              "\\layout Standard"]
181
182 def remove_oldert(lines):
183     i = 0
184     while 1:
185         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
186         if i == -1:
187             break
188         j = i+1
189         while 1:
190             j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_float", "\\the_end"],
191                             j)
192             if check_token(lines[j], "\\begin_inset"):
193                 j = skip_inset(lines, j)
194             else:
195                 break
196
197         if check_token(lines[j], "\\layout"):
198             while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
199                 j = j-1
200
201         # We need to remove insets, special chars & font commands from ERT text
202         new = []
203         new2 = []
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!
207
208         k = i+1
209         while 1:
210             k2 = find_re(lines, ert_rexp, k, j)
211             inset = specialchar = 0
212             if k2 == -1:
213                 k2 = j
214             elif check_token(lines[k2], "\\begin_inset"):
215                 inset = 1
216             else:
217                 specialchar = 1
218                 mo = spchar_rexp.match(lines[k2])
219                 lines[k2] = mo.group(1)
220                 specialchar_str = mo.group(2)
221                 k2 = k2+1
222
223             tmp = []
224             for line in lines[k:k2]:
225                 if font_rexp.match(line):
226                     if new2 == []:
227                         # This is not necessary, but we want the output to be
228                         # as similar as posible to the lyx format
229                         new2 = [""]
230                     new2.append(line)
231                 else:
232                     tmp.append(line)
233
234             if is_empty(tmp):
235                 if filter(lambda x:x != "", tmp) != []:
236                     if new == []:
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]+" "
240                     else:
241                         new = new+[" "]
242             else:
243                 new = new+ert_begin+tmp+["\\end_inset ", ""]
244
245             if inset:
246                 k3 = find_token(lines, "\\end_inset", k2+1)
247                 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
248                 k = k3+1
249                 # Skip the empty line after \end_inset
250                 if not is_nonempty_line(lines[k]):
251                     k = k+1
252                     new.append("")
253             elif specialchar:
254                 if new == []:
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
258                     new = [""]
259                 else:
260                     new = new+[specialchar_str, ""]
261                 k = k2
262             else:
263                 break
264
265         new = new+new2
266         if not check_token(lines[j], "\\latex default"):
267             new = new+[""]+[lines[j]]
268         lines[i:j+1] = new
269         i = i+1
270
271 def convert_ertinset(lines):
272     i = 0
273     while 1:
274         i = find_token(lines, "\\begin_inset ERT", i)
275         if i == -1:
276             break
277         j = find_token(lines, "collapsed", i+1)
278         if string.split(lines[j])[1] == "true":
279             status = "Collapsed"
280         else:
281             status = "Open"
282         lines[j] = "status " + status
283         i = i+1
284
285 def is_ert_paragraph(lines, i):
286     i = find_nonempty_line(lines, i+1)
287     if not check_token(lines[i], "\\begin_inset ERT"):
288         return 0
289     j = find_token(lines, "\\end_inset", i)
290     k = find_nonempty_line(lines, j+1)
291     return check_token(lines[k], "\\layout")
292
293 def combine_ert(lines):
294     i = 0
295     while 1:
296         i = find_token(lines, "\\begin_inset ERT", i)
297         if i == -1:
298             break
299         j = find_token_backwards(lines,"\\layout", i-1)
300         count = 0
301         text = []
302         while is_ert_paragraph(lines, j):
303
304             count = count+1
305             i2 = find_token(lines, "\\layout", j+1)
306             k = find_token(lines, "\\end_inset", i2+1)
307             text = text+lines[i2:k]
308             j = find_token(lines, "\\layout", k+1)
309             if j == -1:
310                 break
311
312         if count >= 2:
313             j = find_token(lines, "\\layout", i+1)
314             lines[j:k] = text
315
316         i = i+1
317         
318 oldunits = ["pt", "cm", "in", "text%", "col%"]
319
320 def get_length(lines, name, start, end):
321     i = find_token(lines, name, start, end)
322     if i == -1:
323         return ""
324     x = string.split(lines[i])
325     return x[2]+oldunits[int(x[1])]
326
327 def append(x, token, value):
328     if value != "":
329         x.append("\t"+token+" "+value)
330
331 def remove_figinset(lines):
332     i = 0
333     while 1:
334         i = find_token(lines, "\\begin_inset Figure", i)
335         if i == -1:
336             break
337         j = find_token(lines, "\\end_inset", i+1)
338
339         lyxwidth = string.split(lines[i])[3]+"pt"
340         lyxheight = string.split(lines[i])[4]+"pt"
341
342         filename = get_value(lines, "file", i+1, j)
343
344         width = get_length(lines, "width", i+1, j)
345         # what does width=5 mean ?
346         height = get_length(lines, "height", i+1, j)
347         rotateAngle = get_value(lines, "angle", i+1, j)
348         if width == "" and height == "":
349             size_type = "0"
350         else:
351             size_type = "1"
352
353         flags = get_value(lines, "flags", i+1, j)
354         x = int(flags)%4
355         if x == 1:
356             display = "monochrome"
357         elif x == 2:
358             display = "gray"
359         else:
360             display = "color"
361
362         subcaptionText = get_value(lines, "subcaption", i+1, j)
363         if subcaptionText != "":
364             subcaptionText = '"'+subcaptionText+'"'
365         k = find_token(lines, "subfigure", i+1,j)
366         if k == -1:
367             subcaption = 0
368         else:
369             subcaption = 1
370
371         new = ["\\begin_inset Graphics FormatVersion 1"]
372         append(new, "filename", filename)
373         append(new, "display", display)
374         if subcaption:
375             new.append("\tsubcaption")
376         append(new, "subcaptionText", subcaptionText)
377         append(new, "size_type", size_type)
378         append(new, "width", width)
379         append(new, "height", height)
380         if rotateAngle != "":
381             new.append("\trotate")
382             append(new, "rotateAngle", rotateAngle)
383         new.append("\trotateOrigin center")
384         new.append("\tlyxsize_type 1")
385         append(new, "lyxwidth", lyxwidth)
386         append(new, "lyxheight", lyxheight)
387         new = new + ["\end_inset"]
388         lines[i:j+1] = new
389
390 def change_preamble(lines):
391     i = find_token(lines, "\\use_amsmath", 0)
392     if i == -1:
393         return
394     lines[i+1:i+1] = ["\\use_natbib 0",
395                       "\use_numerical_citations 0"]
396
397 def convert(header, body):
398     language = get_value(header, "\\language", 0)
399     if language == "":
400         language = "english"
401
402     change_preamble(header)
403     convert_ertinset(body)
404     remove_oldert(body)
405     combine_ert(body)
406     remove_oldminipage(body)
407     remove_oldfloat(body, language)
408     remove_figinset(body)
409
410 if __name__ == "__main__":
411     pass