]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_218.py
remove last of NO_STD_LIST stuff
[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                  "wide false",
29                  "collapsed false"],
30     "tab":      ["\\begin_inset Float table",
31                  "wide false",
32                  "collapsed false"],
33     "alg":      ["\\begin_inset Float algorithm",
34                  "wide false",
35                  "collapsed false"],
36     "wide-fig": ["\\begin_inset Float figure",
37                  "wide true",
38                  "collapsed false"],
39     "wide-tab": ["\\begin_inset Float table",
40                  "wide true",
41                  "collapsed false"]
42 }
43
44 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
45                "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
46
47 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
48 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
49                          r"(\s+\\pextra_alignment\s+(\S+))?"+\
50                          r"(\s+\\pextra_hfill\s+(\S+))?"+\
51                          r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
52                          r"(\s+(\\pextra_widthp?)\s+(\S*))?")
53
54 def get_width(mo):
55     if mo.group(10):
56         if mo.group(9) == "\\pextra_widthp":
57             return mo.group(10)+"col%"
58         else:
59             return mo.group(10)
60     else:
61         return "100col%"
62
63 #
64 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
65 #
66
67 def remove_oldfloat(lines, language):
68     i = 0
69     while 1:
70         i = find_token(lines, "\\begin_float", i)
71         if i == -1:
72             break
73         # There are no nested floats, so finding the end of the float is simple
74         j = find_token(lines, "\\end_float", i+1)
75
76         floattype = string.split(lines[i])[1]
77         if not floats.has_key(floattype):
78             sys.stderr.write("Error! Unknown float type "+floattype+"\n")
79             floattype = "fig"
80
81         # skip \end_deeper tokens
82         i2 = i+1
83         while check_token(lines[i2], "\\end_deeper"):
84             i2 = i2+1
85         if i2 > i+1:
86             j2 = get_next_paragraph(lines, j+1)
87             lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
88
89         new = floats[floattype]+[""]
90
91         # Check if the float is floatingfigure
92         k = find_re(lines, pextra_type3_rexp, i, j)
93         if k != -1:
94             mo = pextra_rexp.search(lines[k])
95             width = get_width(mo)
96             lines[k] = re.sub(pextra_rexp, "", lines[k])
97             new = ["\\begin_inset Wrap figure",
98                    'width "%s"' % width,
99                    "collapsed false",
100                    ""]
101
102         new = new+lines[i2:j]+["\\end_inset ", ""]
103
104         # After a float, all font attributes are reseted.
105         # We need to output '\foo default' for every attribute foo
106         # whose value is not default before the float.
107         # The check here is not accurate, but it doesn't matter
108         # as extra '\foo default' commands are ignored.
109         # In fact, it might be safer to output '\foo default' for all 
110         # font attributes.
111         k = get_paragraph(lines, i)
112         flag = 0
113         for token in font_tokens:
114             if find_token(lines, token, k, i) != -1:
115                 if not flag:
116                     # This is not necessary, but we want the output to be
117                     # as similar as posible to the lyx format
118                     flag = 1
119                     new.append("")
120                 if token == "\\lang":
121                     new.append(token+" "+language)
122                 else:
123                     new.append(token+" default ")
124
125         lines[i:j+1] = new
126         i = i+1
127
128 pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
129 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
130
131 def remove_pextra(lines):
132     i = 0
133     flag = 0
134     while 1:
135         i = find_re(lines, pextra_type2_rexp, i)
136         if i == -1:
137             break
138
139         mo = pextra_rexp.search(lines[i])
140         width = get_width(mo)
141
142         if mo.group(1) == "1":
143             # handle \pextra_type 1 (indented paragraph)
144             lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i])
145             i = i+1
146             continue
147
148         # handle \pextra_type 2 (minipage)
149         position = mo.group(3)
150         hfill = mo.group(5)
151         lines[i] = re.sub(pextra_rexp, "", lines[i])
152
153         start = ["\\begin_inset Minipage",
154                  "position " + position,
155                  "inner_position 0",
156                  'height "0pt"',
157                  'width "%s"' % width,
158                  "collapsed false"
159                  ]
160         if flag:
161             flag = 0
162             if hfill:
163                 start = ["","\hfill",""]+start
164         else:
165             start = ["\\layout Standard"] + start
166
167         j0 = find_token_backwards(lines,"\\layout", i-1)
168         j = get_next_paragraph(lines, i)
169
170         count = 0
171         while 1:
172             # collect more paragraphs to the minipage
173             count = count+1
174             if j == -1 or not check_token(lines[j], "\\layout"):
175                 break
176             i = find_re(lines, pextra_type2_rexp2, j+1)
177             if i == -1:
178                 break
179             mo = pextra_rexp.search(lines[i])
180             if not mo:
181                 break
182             if mo.group(7) == "1":
183                 flag = 1
184                 break
185             lines[i] = re.sub(pextra_rexp, "", lines[i])
186             j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
187
188         mid = lines[j0:j]
189         end = ["\\end_inset "]
190
191         lines[j0:j] = start+mid+end
192         i = i+1
193
194 def is_empty(lines):
195     return filter(is_nonempty_line, lines) == []
196
197 move_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
198 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
199 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
200 ert_begin = ["\\begin_inset ERT",
201              "status Collapsed",
202              "",
203              "\\layout Standard"]
204
205 def remove_oldert(lines):
206     i = 0
207     while 1:
208         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
209         if i == -1:
210             break
211         j = i+1
212         while 1:
213             # \end_inset is for ert inside a tabular cell. The other tokens
214             # are obvious.
215             j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
216                             j)
217             if check_token(lines[j], "\\begin_inset"):
218                 j = find_end_of_inset(lines, j)+1
219             else:
220                 break
221
222         if check_token(lines[j], "\\layout"):
223             while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
224                 j = j-1
225
226         # We need to remove insets, special chars & font commands from ERT text
227         new = []
228         new2 = []
229         if check_token(lines[i], "\\layout LaTeX"):
230             new = ["\layout Standard", "", ""]
231             # We have a problem with classes in which Standard is not the default layout!
232
233         k = i+1
234         while 1:
235             k2 = find_re(lines, ert_rexp, k, j)
236             inset = specialchar = 0
237             if k2 == -1:
238                 k2 = j
239             elif check_token(lines[k2], "\\begin_inset"):
240                 inset = 1
241             else:
242                 specialchar = 1
243                 mo = spchar_rexp.match(lines[k2])
244                 lines[k2] = mo.group(1)
245                 specialchar_str = mo.group(2)
246                 k2 = k2+1
247
248             tmp = []
249             for line in lines[k:k2]:
250                 # Move some lines outside the ERT inset:
251                 if move_rexp.match(line):
252                     if new2 == []:
253                         # This is not necessary, but we want the output to be
254                         # as similar as posible to the lyx format
255                         new2 = [""]
256                     new2.append(line)
257                 elif not check_token(line, "\\latex"):
258                     tmp.append(line)
259
260             if is_empty(tmp):
261                 if filter(lambda x:x != "", tmp) != []:
262                     if new == []:
263                         # This is not necessary, but we want the output to be
264                         # as similar as posible to the lyx format
265                         lines[i-1] = lines[i-1]+" "
266                     else:
267                         new = new+[" "]
268             else:
269                 new = new+ert_begin+tmp+["\\end_inset ", ""]
270
271             if inset:
272                 k3 = find_end_of_inset(lines, k2)
273                 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
274                 k = k3+1
275                 # Skip the empty line after \end_inset
276                 if not is_nonempty_line(lines[k]):
277                     k = k+1
278                     new.append("")
279             elif specialchar:
280                 if new == []:
281                     # This is not necessary, but we want the output to be
282                     # as similar as posible to the lyx format
283                     lines[i-1] = lines[i-1]+specialchar_str
284                     new = [""]
285                 else:
286                     new = new+[specialchar_str, ""]
287                 k = k2
288             else:
289                 break
290
291         new = new+new2
292         if not check_token(lines[j], "\\latex "):
293             new = new+[""]+[lines[j]]
294         lines[i:j+1] = new
295         i = i+1
296
297     # Delete remaining "\latex xxx" tokens
298     i = 0
299     while 1:
300         i = find_token(lines, "\\latex ", i)
301         if i == -1:
302             break
303         del lines[i]
304
305 # ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future.
306 def remove_oldertinset(lines):
307     i = 0
308     while 1:
309         i = find_token(lines, "\\begin_inset ERT", i)
310         if i == -1:
311             break
312         j = find_end_of_inset(lines, i)
313         k = find_token(lines, "\\layout", i+1)
314         l = get_paragraph(lines, i)
315         if lines[k] == lines[l]: # same layout
316             k = k+1
317         new = lines[k:j]
318         lines[i:j+1] = new
319         i = i+1
320
321 def is_ert_paragraph(lines, i):
322     if not check_token(lines[i], "\\layout Standard"):
323         return 0
324
325     i = find_nonempty_line(lines, i+1)
326     if not check_token(lines[i], "\\begin_inset ERT"):
327         return 0
328
329     j = find_end_of_inset(lines, i)
330     k = find_nonempty_line(lines, j+1)
331     return check_token(lines[k], "\\layout")
332
333 def combine_ert(lines):
334     i = 0
335     while 1:
336         i = find_token(lines, "\\begin_inset ERT", i)
337         if i == -1:
338             break
339         j = get_paragraph(lines, i)
340         count = 0
341         text = []
342         while is_ert_paragraph(lines, j):
343
344             count = count+1
345             i2 = find_token(lines, "\\layout", j+1)
346             k = find_token(lines, "\\end_inset", i2+1)
347             text = text+lines[i2:k]
348             j = find_token(lines, "\\layout", k+1)
349             if j == -1:
350                 break
351
352         if count >= 2:
353             j = find_token(lines, "\\layout", i+1)
354             lines[j:k] = text
355
356         i = i+1
357         
358 oldunits = ["pt", "cm", "in", "text%", "col%"]
359
360 def get_length(lines, name, start, end):
361     i = find_token(lines, name, start, end)
362     if i == -1:
363         return ""
364     x = string.split(lines[i])
365     return x[2]+oldunits[int(x[1])]
366
367 def write_attribute(x, token, value):
368     if value != "":
369         x.append("\t"+token+" "+value)
370
371 def remove_figinset(lines):
372     i = 0
373     while 1:
374         i = find_token(lines, "\\begin_inset Figure", i)
375         if i == -1:
376             break
377         j = find_end_of_inset(lines, i)
378
379         if ( len(string.split(lines[i])) > 2 ):
380             lyxwidth = string.split(lines[i])[3]+"pt"
381             lyxheight = string.split(lines[i])[4]+"pt"
382         else:
383             lyxwidth = ""
384             lyxheight = ""
385
386         filename = get_value(lines, "file", i+1, j)
387
388         width = get_length(lines, "width", i+1, j)
389         # what does width=5 mean ?
390         height = get_length(lines, "height", i+1, j)
391         rotateAngle = get_value(lines, "angle", i+1, j)
392         if width == "" and height == "":
393             size_type = "0"
394         else:
395             size_type = "1"
396
397         flags = get_value(lines, "flags", i+1, j)
398         x = int(flags)%4
399         if x == 1:
400             display = "monochrome"
401         elif x == 2:
402             display = "gray"
403         else:
404             display = "color"
405
406         subcaptionText = ""
407         subcaptionLine = find_token(lines, "subcaption", i+1, j)
408         if subcaptionLine != -1:
409             subcaptionText = lines[subcaptionLine][11:]
410             if subcaptionText != "":
411                 subcaptionText = '"'+subcaptionText+'"'
412
413         k = find_token(lines, "subfigure", i+1,j)
414         if k == -1:
415             subcaption = 0
416         else:
417             subcaption = 1
418
419         new = ["\\begin_inset Graphics FormatVersion 1"]
420         write_attribute(new, "filename", filename)
421         write_attribute(new, "display", display)
422         if subcaption:
423             new.append("\tsubcaption")
424         write_attribute(new, "subcaptionText", subcaptionText)
425         write_attribute(new, "size_type", size_type)
426         write_attribute(new, "width", width)
427         write_attribute(new, "height", height)
428         if rotateAngle != "":
429             new.append("\trotate")
430             write_attribute(new, "rotateAngle", rotateAngle)
431         write_attribute(new, "rotateOrigin", "leftBaseline")
432         write_attribute(new, "lyxsize_type", "1")
433         write_attribute(new, "lyxwidth", lyxwidth)
434         write_attribute(new, "lyxheight", lyxheight)
435         new = new + ["\end_inset"]
436         lines[i:j+1] = new
437
438 attr_re = re.compile(r' \w*="(false|0|)"')
439 line_re = re.compile(r'<(features|column|row|cell)')
440
441 def update_tabular(lines):
442     i = 0
443     while 1:
444         i = find_token(lines, '\\begin_inset  Tabular', i)
445         if i == -1:
446             break
447
448         for k in get_tabular_lines(lines, i):
449             if check_token(lines[k], "<lyxtabular"):
450                 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
451             elif check_token(lines[k], "<column"):
452                 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
453
454             if line_re.match(lines[k]):
455                 lines[k] = re.sub(attr_re, "", lines[k])
456
457         i = i+1
458
459 # Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future.
460 def fix_oldfloatinset(lines):
461     i = 0
462     while 1:
463         i = find_token(lines, "\\begin_inset Float ", i)
464         if i == -1:
465             break
466         j = find_token(lines, "collapsed", i)
467         if j != -1:
468             lines[j:j] = ["wide false"]
469         i = i+1
470
471 def change_listof(lines):
472     i = 0
473     while 1:
474         i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
475         if i == -1:
476             break
477         type = re.search(r"listof(\w*)", lines[i]).group(1)[:-1]
478         lines[i] = "\\begin_inset FloatList "+type
479         i = i+1
480
481 def change_infoinset(lines):
482     i = 0
483     while 1:
484         i = find_token(lines, "\\begin_inset Info", i)
485         if i == -1:
486             break
487         txt = string.lstrip(lines[i][18:])
488         new = ["\\begin_inset Note", "collapsed true", ""]
489         j = find_token(lines, "\\end_inset", i)
490         if j == -1:
491             break
492
493         note_lines = lines[i+1:j]
494         if len(txt) > 0:
495             note_lines = [txt]+note_lines
496
497         for line in note_lines:
498             new = new + ["\layout Standard", ""]
499             tmp = string.split(line, '\\')
500             new = new + [tmp[0]]
501             for x in tmp[1:]:
502                 new = new + ["\\backslash ", x]
503         lines[i:j] = new
504         i = i+5
505
506 def change_preamble(lines):
507     i = find_token(lines, "\\use_amsmath", 0)
508     if i == -1:
509         return
510     lines[i+1:i+1] = ["\\use_natbib 0",
511                       "\use_numerical_citations 0"]
512
513 def convert(header, body):
514     language = get_value(header, "\\language", 0)
515     if language == "":
516         language = "english"
517
518     change_preamble(header)
519     change_listof(body)
520     fix_oldfloatinset(body)
521     update_tabular(body)
522     remove_pextra(body)
523     remove_oldfloat(body, language)
524     remove_figinset(body)
525     remove_oldertinset(body)
526     remove_oldert(body)
527     combine_ert(body)
528     change_infoinset(body)
529
530 if __name__ == "__main__":
531     pass