]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_218.py
Small changes
[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 = find_end_of_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_end_of_inset(lines, k2)
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
284         # Remove font commands
285         j = find_end_of_inset(lines, i)
286         new = []
287         for line in lines[i:j]:
288             if not font_rexp.match(line) and not check_token(line, "\\latex"):
289                 new.append(line)
290         lines[i:j] = new
291
292         i = i+1
293
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"):
297         return 0
298     j = find_end_of_inset(lines, i)
299     k = find_nonempty_line(lines, j+1)
300     return check_token(lines[k], "\\layout")
301
302 def combine_ert(lines):
303     i = 0
304     while 1:
305         i = find_token(lines, "\\begin_inset ERT", i)
306         if i == -1:
307             break
308         j = find_token_backwards(lines,"\\layout", i-1)
309         count = 0
310         text = []
311         while is_ert_paragraph(lines, j):
312
313             count = count+1
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)
318             if j == -1:
319                 break
320
321         if count >= 2:
322             j = find_token(lines, "\\layout", i+1)
323             lines[j:k] = text
324
325         i = i+1
326         
327 oldunits = ["pt", "cm", "in", "text%", "col%"]
328
329 def get_length(lines, name, start, end):
330     i = find_token(lines, name, start, end)
331     if i == -1:
332         return ""
333     x = string.split(lines[i])
334     return x[2]+oldunits[int(x[1])]
335
336 def append(x, token, value):
337     if value != "":
338         x.append("\t"+token+" "+value)
339
340 def remove_figinset(lines):
341     i = 0
342     while 1:
343         i = find_token(lines, "\\begin_inset Figure", i)
344         if i == -1:
345             break
346         j = find_end_of_inset(lines, i)
347
348         lyxwidth = string.split(lines[i])[3]+"pt"
349         lyxheight = string.split(lines[i])[4]+"pt"
350
351         filename = get_value(lines, "file", i+1, j)
352
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 == "":
358             size_type = "0"
359         else:
360             size_type = "1"
361
362         flags = get_value(lines, "flags", i+1, j)
363         x = int(flags)%4
364         if x == 1:
365             display = "monochrome"
366         elif x == 2:
367             display = "gray"
368         else:
369             display = "color"
370
371         subcaptionText = get_value(lines, "subcaption", i+1, j)
372         if subcaptionText != "":
373             subcaptionText = '"'+subcaptionText+'"'
374         k = find_token(lines, "subfigure", i+1,j)
375         if k == -1:
376             subcaption = 0
377         else:
378             subcaption = 1
379
380         new = ["\\begin_inset Graphics FormatVersion 1"]
381         append(new, "filename", filename)
382         append(new, "display", display)
383         if subcaption:
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"]
397         lines[i:j+1] = new
398
399 def change_preamble(lines):
400     i = find_token(lines, "\\use_amsmath", 0)
401     if i == -1:
402         return
403     lines[i+1:i+1] = ["\\use_natbib 0",
404                       "\use_numerical_citations 0"]
405
406 def convert(header, body):
407     language = get_value(header, "\\language", 0)
408     if language == "":
409         language = "english"
410
411     change_preamble(header)
412     convert_ertinset(body)
413     remove_oldert(body)
414     combine_ert(body)
415     remove_oldminipage(body)
416     remove_oldfloat(body, language)
417     remove_figinset(body)
418
419 if __name__ == "__main__":
420     pass