]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_2.py
d2c6912f8cd2d5d0a53ed467d646a806b989e411
[lyx.git] / lib / lyx2lyx / lyx_1_2.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
4 # Copyright (C) 2004 José Matos <jamatos@lyx.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """ Convert files to the file format generated by lyx 1.2"""
21
22 import string
23 import re
24
25 from parser_tools import find_token, find_token_backwards, \
26                          find_tokens, find_tokens_backwards, \
27                          find_beginning_of, find_end_of, find_re, \
28                          is_nonempty_line, find_nonempty_line, \
29                          get_value, check_token
30
31 ####################################################################
32 # Private helper functions
33
34 def get_layout(line, default_layout):
35     " Get layout, if empty return the default layout."
36     tokens = string.split(line)
37     if len(tokens) > 1:
38         return tokens[1]
39     return default_layout
40
41
42 def get_paragraph(lines, i, format):
43     " Finds the paragraph that contains line i."
44     begin_layout = "\\layout"
45
46     while i != -1:
47         i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
48         if i == -1: return -1
49         if check_token(lines[i], begin_layout):
50             return i
51         i = find_beginning_of_inset(lines, i)
52     return -1
53
54
55 def get_next_paragraph(lines, i, format):
56     " Finds the paragraph after the paragraph that contains line i."
57     tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
58
59     while i != -1:
60         i = find_tokens(lines, tokens, i)
61         if not check_token(lines[i], "\\begin_inset"):
62             return i
63         i = find_end_of_inset(lines, i)
64     return -1
65
66
67 def find_beginning_of_inset(lines, i):
68     " Find beginning of inset, where lines[i] is included."
69     return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
70
71
72 def find_end_of_inset(lines, i):
73     " Finds the matching \end_inset"
74     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
75
76
77 def find_end_of_tabular(lines, i):
78     " Finds the matching end of tabular."
79     return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
80
81
82 def get_tabular_lines(lines, i):
83     " Returns a lists of tabular lines."
84     result = []
85     i = i+1
86     j = find_end_of_tabular(lines, i)
87     if j == -1:
88         return []
89
90     while i <= j:
91         if check_token(lines[i], "\\begin_inset"):
92             i = find_end_of_inset(lines, i)+1
93         else:
94             result.append(i)
95             i = i+1
96     return result
97
98 # End of helper functions
99 ####################################################################
100
101
102 floats = {
103     "footnote": ["\\begin_inset Foot",
104                  "collapsed true"],
105     "margin":   ["\\begin_inset Marginal",
106                  "collapsed true"],
107     "fig":      ["\\begin_inset Float figure",
108                  "wide false",
109                  "collapsed false"],
110     "tab":      ["\\begin_inset Float table",
111                  "wide false",
112                  "collapsed false"],
113     "alg":      ["\\begin_inset Float algorithm",
114                  "wide false",
115                  "collapsed false"],
116     "wide-fig": ["\\begin_inset Float figure",
117                  "wide true",
118                  "collapsed false"],
119     "wide-tab": ["\\begin_inset Float table",
120                  "wide true",
121                  "collapsed false"]
122 }
123
124 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
125                "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
126
127 pextra_type3_rexp = re.compile(r".*\\pextra_type\s+3")
128 pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
129                          r"(\s+\\pextra_alignment\s+(\S+))?"+\
130                          r"(\s+\\pextra_hfill\s+(\S+))?"+\
131                          r"(\s+\\pextra_start_minipage\s+(\S+))?"+\
132                          r"(\s+(\\pextra_widthp?)\s+(\S*))?")
133
134
135 def get_width(mo):
136     " Get width from a regular expression. "
137     if mo.group(10):
138         if mo.group(9) == "\\pextra_widthp":
139             return mo.group(10)+"col%"
140         else:
141             return mo.group(10)
142     else:
143         return "100col%"
144
145
146 def remove_oldfloat(document):
147     " Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
148     lines = document.body
149     i = 0
150     while 1:
151         i = find_token(lines, "\\begin_float", i)
152         if i == -1:
153             break
154         # There are no nested floats, so finding the end of the float is simple
155         j = find_token(lines, "\\end_float", i+1)
156
157         floattype = string.split(lines[i])[1]
158         if not floats.has_key(floattype):
159             document.warning("Error! Unknown float type " + floattype)
160             floattype = "fig"
161
162         # skip \end_deeper tokens
163         i2 = i+1
164         while check_token(lines[i2], "\\end_deeper"):
165             i2 = i2+1
166         if i2 > i+1:
167             j2 = get_next_paragraph(lines, j + 1, document.format + 1)
168             lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
169
170         new = floats[floattype]+[""]
171
172         # Check if the float is floatingfigure
173         k = find_re(lines, pextra_type3_rexp, i, j)
174         if k != -1:
175             mo = pextra_rexp.search(lines[k])
176             width = get_width(mo)
177             lines[k] = re.sub(pextra_rexp, "", lines[k])
178             new = ["\\begin_inset Wrap figure",
179                    'width "%s"' % width,
180                    "collapsed false",
181                    ""]
182
183         new = new+lines[i2:j]+["\\end_inset ", ""]
184
185         # After a float, all font attributes are reseted.
186         # We need to output '\foo default' for every attribute foo
187         # whose value is not default before the float.
188         # The check here is not accurate, but it doesn't matter
189         # as extra '\foo default' commands are ignored.
190         # In fact, it might be safer to output '\foo default' for all
191         # font attributes.
192         k = get_paragraph(lines, i, document.format + 1)
193         flag = 0
194         for token in font_tokens:
195             if find_token(lines, token, k, i) != -1:
196                 if not flag:
197                     # This is not necessary, but we want the output to be
198                     # as similar as posible to the lyx format
199                     flag = 1
200                     new.append("")
201                 if token == "\\lang":
202                     new.append(token+" "+ document.language)
203                 else:
204                     new.append(token+" default ")
205
206         lines[i:j+1] = new
207         i = i+1
208
209
210 pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
211 pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
212 pextra_widthp = re.compile(r"\\pextra_widthp")
213
214 def remove_pextra(document):
215     " Remove pextra token."
216     lines = document.body
217     i = 0
218     flag = 0
219     while 1:
220         i = find_re(lines, pextra_type2_rexp, i)
221         if i == -1:
222             break
223
224         # Sometimes the \pextra_widthp argument comes in it own
225         # line. If that happens insert it back in this line.
226         if pextra_widthp.search(lines[i+1]):
227             lines[i] = lines[i] + ' ' + lines[i+1]
228             del lines[i+1]
229
230         mo = pextra_rexp.search(lines[i])
231         width = get_width(mo)
232
233         if mo.group(1) == "1":
234             # handle \pextra_type 1 (indented paragraph)
235             lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i])
236             i = i+1
237             continue
238
239         # handle \pextra_type 2 (minipage)
240         position = mo.group(3)
241         hfill = mo.group(5)
242         lines[i] = re.sub(pextra_rexp, "", lines[i])
243
244         start = ["\\begin_inset Minipage",
245                  "position " + position,
246                  "inner_position 0",
247                  'height "0pt"',
248                  'width "%s"' % width,
249                  "collapsed false"
250                  ]
251         if flag:
252             flag = 0
253             if hfill:
254                 start = ["","\hfill",""]+start
255         else:
256             start = ['\\layout %s' % document.default_layout,''] + start
257
258         j0 = find_token_backwards(lines,"\\layout", i-1)
259         j = get_next_paragraph(lines, i, document.format + 1)
260
261         count = 0
262         while 1:
263             # collect more paragraphs to the minipage
264             count = count+1
265             if j == -1 or not check_token(lines[j], "\\layout"):
266                 break
267             i = find_re(lines, pextra_type2_rexp2, j+1)
268             if i == -1:
269                 break
270             mo = pextra_rexp.search(lines[i])
271             if not mo:
272                 break
273             if mo.group(7) == "1":
274                 flag = 1
275                 break
276             lines[i] = re.sub(pextra_rexp, "", lines[i])
277             j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
278
279         mid = lines[j0:j]
280         end = ["\\end_inset "]
281
282         lines[j0:j] = start+mid+end
283         i = i+1
284
285
286 def is_empty(lines):
287     " Are all the lines empty?"
288     return filter(is_nonempty_line, lines) == []
289
290
291 move_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun|end_deeper)")
292 ert_rexp = re.compile(r"\\begin_inset|\\hfill|.*\\SpecialChar")
293 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
294
295
296 def remove_oldert(document):
297     " Remove old ERT inset."
298     ert_begin = ["\\begin_inset ERT",
299                  "status Collapsed",
300                  "",
301                  '\\layout %s' % document.default_layout,
302                  ""]
303     lines = document.body
304     i = 0
305     while 1:
306         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
307         if i == -1:
308             break
309         j = i+1
310         while 1:
311             # \end_inset is for ert inside a tabular cell. The other tokens
312             # are obvious.
313             j = find_tokens(lines, ["\\latex default", "\\layout", "\\begin_inset", "\\end_inset", "\\end_float", "\\the_end"],
314                             j)
315             if check_token(lines[j], "\\begin_inset"):
316                 j = find_end_of_inset(lines, j)+1
317             else:
318                 break
319
320         if check_token(lines[j], "\\layout"):
321             while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
322                 j = j-1
323
324         # We need to remove insets, special chars & font commands from ERT text
325         new = []
326         new2 = []
327         if check_token(lines[i], "\\layout LaTeX"):
328             new = ['\layout %s' % document.default_layout, "", ""]
329
330         k = i+1
331         while 1:
332             k2 = find_re(lines, ert_rexp, k, j)
333             inset = hfill = specialchar = 0
334             if k2 == -1:
335                 k2 = j
336             elif check_token(lines[k2], "\\begin_inset"):
337                 inset = 1
338             elif check_token(lines[k2], "\\hfill"):
339                 hfill = 1
340                 del lines[k2]
341                 j = j-1
342             else:
343                 specialchar = 1
344                 mo = spchar_rexp.match(lines[k2])
345                 lines[k2] = mo.group(1)
346                 specialchar_str = mo.group(2)
347                 k2 = k2+1
348
349             tmp = []
350             for line in lines[k:k2]:
351                 # Move some lines outside the ERT inset:
352                 if move_rexp.match(line):
353                     if new2 == []:
354                         # This is not necessary, but we want the output to be
355                         # as similar as posible to the lyx format
356                         new2 = [""]
357                     new2.append(line)
358                 elif not check_token(line, "\\latex"):
359                     tmp.append(line)
360
361             if is_empty(tmp):
362                 if filter(lambda x:x != "", tmp) != []:
363                     if new == []:
364                         # This is not necessary, but we want the output to be
365                         # as similar as posible to the lyx format
366                         lines[i-1] = lines[i-1]+" "
367                     else:
368                         new = new+[" "]
369             else:
370                 new = new+ert_begin+tmp+["\\end_inset ", ""]
371
372             if inset:
373                 k3 = find_end_of_inset(lines, k2)
374                 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
375                 k = k3+1
376                 # Skip the empty line after \end_inset
377                 if not is_nonempty_line(lines[k]):
378                     k = k+1
379                     new.append("")
380             elif hfill:
381                 new = new + ["\\hfill", ""]
382                 k = k2
383             elif specialchar:
384                 if new == []:
385                     # This is not necessary, but we want the output to be
386                     # as similar as posible to the lyx format
387                     lines[i-1] = lines[i-1]+specialchar_str
388                     new = [""]
389                 else:
390                     new = new+[specialchar_str, ""]
391                 k = k2
392             else:
393                 break
394
395         new = new+new2
396         if not check_token(lines[j], "\\latex "):
397             new = new+[""]+[lines[j]]
398         lines[i:j+1] = new
399         i = i+1
400
401     # Delete remaining "\latex xxx" tokens
402     i = 0
403     while 1:
404         i = find_token(lines, "\\latex ", i)
405         if i == -1:
406             break
407         del lines[i]
408
409
410 def remove_oldertinset(document):
411     " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
412     lines = document.body
413     i = 0
414     while 1:
415         i = find_token(lines, "\\begin_inset ERT", i)
416         if i == -1:
417             break
418         j = find_end_of_inset(lines, i)
419         k = find_token(lines, "\\layout", i+1)
420         l = get_paragraph(lines, i, document.format + 1)
421         if lines[k] == lines[l]: # same layout
422             k = k+1
423         new = lines[k:j]
424         lines[i:j+1] = new
425         i = i+1
426
427
428 def is_ert_paragraph(document, i):
429     " Is this a ert paragraph? "
430     lines = document.body
431     if not check_token(lines[i], "\\layout"):
432         return 0
433     if not document.is_default_layout(get_layout(lines[i], document.default_layout)):
434         return 0
435
436     i = find_nonempty_line(lines, i+1)
437     if not check_token(lines[i], "\\begin_inset ERT"):
438         return 0
439
440     j = find_end_of_inset(lines, i)
441     k = find_nonempty_line(lines, j+1)
442     return check_token(lines[k], "\\layout")
443
444
445 def combine_ert(document):
446     " Combine ERT paragraphs."
447     lines = document.body
448     i = 0
449     while 1:
450         i = find_token(lines, "\\begin_inset ERT", i)
451         if i == -1:
452             break
453         j = get_paragraph(lines, i, document.format + 1)
454         count = 0
455         text = []
456         while is_ert_paragraph(document, j):
457
458             count = count+1
459             i2 = find_token(lines, "\\layout", j+1)
460             k = find_token(lines, "\\end_inset", i2+1)
461             text = text+lines[i2:k]
462             j = find_token(lines, "\\layout", k+1)
463             if j == -1:
464                 break
465
466         if count >= 2:
467             j = find_token(lines, "\\layout", i+1)
468             lines[j:k] = text
469
470         i = i+1
471
472
473 oldunits = ["pt", "cm", "in", "text%", "col%"]
474
475 def get_length(lines, name, start, end):
476     " Get lenght."
477     i = find_token(lines, name, start, end)
478     if i == -1:
479         return ""
480     x = string.split(lines[i])
481     return x[2]+oldunits[int(x[1])]
482
483
484 def write_attribute(x, token, value):
485     " Write attribute."
486     if value != "":
487         x.append("\t"+token+" "+value)
488
489
490 def remove_figinset(document):
491     " Remove figinset."
492     lines = document.body
493     i = 0
494     while 1:
495         i = find_token(lines, "\\begin_inset Figure", i)
496         if i == -1:
497             break
498         j = find_end_of_inset(lines, i)
499
500         if ( len(string.split(lines[i])) > 2 ):
501             lyxwidth = string.split(lines[i])[3]+"pt"
502             lyxheight = string.split(lines[i])[4]+"pt"
503         else:
504             lyxwidth = ""
505             lyxheight = ""
506
507         filename = get_value(lines, "file", i+1, j)
508
509         width = get_length(lines, "width", i+1, j)
510         # what does width=5 mean ?
511         height = get_length(lines, "height", i+1, j)
512         rotateAngle = get_value(lines, "angle", i+1, j)
513         if width == "" and height == "":
514             size_type = "0"
515         else:
516             size_type = "1"
517
518         flags = get_value(lines, "flags", i+1, j)
519         x = int(flags)%4
520         if x == 1:
521             display = "monochrome"
522         elif x == 2:
523             display = "gray"
524         else:
525             display = "color"
526
527         subcaptionText = ""
528         subcaptionLine = find_token(lines, "subcaption", i+1, j)
529         if subcaptionLine != -1:
530             subcaptionText = lines[subcaptionLine][11:]
531             if subcaptionText != "":
532                 subcaptionText = '"'+subcaptionText+'"'
533
534         k = find_token(lines, "subfigure", i+1,j)
535         if k == -1:
536             subcaption = 0
537         else:
538             subcaption = 1
539
540         new = ["\\begin_inset Graphics FormatVersion 1"]
541         write_attribute(new, "filename", filename)
542         write_attribute(new, "display", display)
543         if subcaption:
544             new.append("\tsubcaption")
545         write_attribute(new, "subcaptionText", subcaptionText)
546         write_attribute(new, "size_type", size_type)
547         write_attribute(new, "width", width)
548         write_attribute(new, "height", height)
549         if rotateAngle != "":
550             new.append("\trotate")
551             write_attribute(new, "rotateAngle", rotateAngle)
552         write_attribute(new, "rotateOrigin", "leftBaseline")
553         write_attribute(new, "lyxsize_type", "1")
554         write_attribute(new, "lyxwidth", lyxwidth)
555         write_attribute(new, "lyxheight", lyxheight)
556         new = new + ["\\end_inset"]
557         lines[i:j+1] = new
558
559
560 attr_re = re.compile(r' \w*="(false|0|)"')
561 line_re = re.compile(r'<(features|column|row|cell)')
562
563 def update_tabular(document):
564     " Convert tabular format 2 to 3."
565     regexp = re.compile(r'^\\begin_inset\s+Tabular')
566     lines = document.body
567     i = 0
568     while 1:
569         i = find_re(lines, regexp, i)
570         if i == -1:
571             break
572
573         for k in get_tabular_lines(lines, i):
574             if check_token(lines[k], "<lyxtabular"):
575                 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
576             elif check_token(lines[k], "<column"):
577                 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
578
579             if line_re.match(lines[k]):
580                 lines[k] = re.sub(attr_re, "", lines[k])
581
582         i = i+1
583
584
585 ##
586 # Convert tabular format 2 to 3
587 #
588 # compatibility read for old longtable options. Now we can make any
589 # row part of the header/footer type we want before it was strict
590 # sequential from the first row down (as LaTeX does it!). So now when
591 # we find a header/footer line we have to go up the rows and set it
592 # on all preceding rows till the first or one with already a h/f option
593 # set. If we find a firstheader on the same line as a header or a
594 # lastfooter on the same line as a footer then this should be set empty.
595 # (Jug 20011220)
596
597 # just for compatibility with old python versions
598 # python >= 2.3 has real booleans (False and True)
599 false = 0
600 true = 1
601
602 class row:
603     " Simple data structure to deal with long table info."
604     def __init__(self):
605         self.endhead = false                # header row
606         self.endfirsthead = false        # first header row
607         self.endfoot = false                # footer row
608         self.endlastfoot = false        # last footer row
609
610
611 def haveLTFoot(row_info):
612     " Does row has LTFoot?"
613     for row_ in row_info:
614         if row_.endfoot:
615             return true
616     return false
617
618
619 def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
620     " Set Header/Footer rows."
621     endfirsthead_empty = false
622     endlastfoot_empty = false
623     # set header info
624     while (hr > 0):
625         hr = hr - 1
626         row_info[hr].endhead = true
627
628     # set firstheader info
629     if fhr and fhr < rows_:
630         if row_info[fhr].endhead:
631             while fhr > 0:
632                 fhr = fhr - 1
633                 row_info[fhr].endfirsthead = true
634                 row_info[fhr].endhead = false
635         elif row_info[fhr - 1].endhead:
636             endfirsthead_empty = true
637         else:
638             while fhr > 0 and not row_info[fhr - 1].endhead:
639                 fhr = fhr - 1
640                 row_info[fhr].endfirsthead = true
641
642     # set footer info
643     if fr and fr < rows_:
644         if row_info[fr].endhead and row_info[fr - 1].endhead:
645             while fr > 0 and not row_info[fr - 1].endhead:
646                 fr = fr - 1
647                 row_info[fr].endfoot = true
648                 row_info[fr].endhead = false
649         elif row_info[fr].endfirsthead and row_info[fr - 1].endfirsthead:
650             while fr > 0 and not row_info[fr - 1].endfirsthead:
651                 fr = fr - 1
652                 row_info[fr].endfoot = true
653                 row_info[fr].endfirsthead = false
654         elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead:
655             while fr > 0 and not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead:
656                 fr = fr - 1
657                 row_info[fr].endfoot = true
658
659     # set lastfooter info
660     if lfr and lfr < rows_:
661         if row_info[lfr].endhead and row_info[lfr - 1].endhead:
662             while lfr > 0 and not row_info[lfr - 1].endhead:
663                 lfr = lfr - 1
664                 row_info[lfr].endlastfoot = true
665                 row_info[lfr].endhead = false
666         elif row_info[lfr].endfirsthead and row_info[lfr - 1].endfirsthead:
667             while lfr > 0 and not row_info[lfr - 1].endfirsthead:
668                 lfr = lfr - 1
669                 row_info[lfr].endlastfoot = true
670                 row_info[lfr].endfirsthead = false
671         elif row_info[lfr].endfoot and row_info[lfr - 1].endfoot:
672             while lfr > 0 and not row_info[lfr - 1].endfoot:
673                 lfr = lfr - 1
674                 row_info[lfr].endlastfoot = true
675                 row_info[lfr].endfoot = false
676         elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead and not row_info[fr - 1].endfoot:
677             while lfr > 0 and not row_info[lfr - 1].endhead and not row_info[lfr - 1].endfirsthead and not row_info[lfr - 1].endfoot:
678                 lfr = lfr - 1
679                 row_info[lfr].endlastfoot = true
680         elif haveLTFoot(row_info):
681             endlastfoot_empty = true
682
683     return endfirsthead_empty, endlastfoot_empty
684
685
686 def insert_attribute(lines, i, attribute):
687     " Insert attribute in lines[i]."
688     last = string.find(lines[i],'>')
689     lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:]
690
691
692 rows_re = re.compile(r'rows="(\d*)"')
693 longtable_re = re.compile(r'islongtable="(\w)"')
694 ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"')
695 lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")')
696 def update_longtables(document):
697     " Update longtables to new format."
698     regexp = re.compile(r'^\\begin_inset\s+Tabular')
699     body = document.body
700     i = 0
701     while 1:
702         i = find_re(body, regexp, i)
703         if i == -1:
704             break
705         i = i + 1
706         i = find_token(body, "<lyxtabular", i)
707         if i == -1:
708             break
709
710         # get number of rows in the table
711         rows = int(rows_re.search(body[i]).group(1))
712
713         i = i + 1
714         i = find_token(body, '<features', i)
715         if i == -1:
716             break
717
718         # is this a longtable?
719         longtable = longtable_re.search(body[i])
720
721         if not longtable:
722             # islongtable is missing add it
723             body[i] = body[i][:10] + 'islongtable="false" ' + body[i][10:]
724
725         if not longtable or longtable.group(1) != "true":
726             # remove longtable elements from features
727             features = lt_features_re.search(body[i])
728             if features:
729                 body[i] = string.replace(body[i], features.group(1), "")
730             continue
731
732         row_info = row() * rows
733         res = ltvalues_re.search(body[i])
734         if not res:
735             continue
736
737         endfirsthead_empty, endlastfoot_empty = setHeaderFooterRows(res.group(1), res.group(2), res.group(3), res.group(4), rows, row_info)
738
739         if endfirsthead_empty:
740             insert_attribute(body, i, 'firstHeadEmpty="true"')
741
742         if endfirsthead_empty:
743             insert_attribute(body, i, 'lastFootEmpty="true"')
744
745         i = i + 1
746         for j in range(rows):
747             i = find_token(body, '<row', i)
748
749             self.endfoot = false                # footer row
750             self.endlastfoot = false        # last footer row
751             if row_info[j].endhead:
752                 insert_attribute(body, i, 'endhead="true"')
753
754             if row_info[j].endfirsthead:
755                 insert_attribute(body, i, 'endfirsthead="true"')
756
757             if row_info[j].endfoot:
758                 insert_attribute(body, i, 'endfoot="true"')
759
760             if row_info[j].endlastfoot:
761                 insert_attribute(body, i, 'endlastfoot="true"')
762
763             i = i + 1
764
765
766 def fix_oldfloatinset(document):
767     " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
768     lines = document.body
769     i = 0
770     while 1:
771         i = find_token(lines, "\\begin_inset Float ", i)
772         if i == -1:
773             break
774         j = find_token(lines, "collapsed", i)
775         if j != -1:
776             lines[j:j] = ["wide false"]
777         i = i+1
778
779
780 def change_listof(document):
781     " Change listof insets."
782     lines = document.body
783     i = 0
784     while 1:
785         i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
786         if i == -1:
787             break
788         type = re.search(r"listof(\w*)", lines[i]).group(1)[:-1]
789         lines[i] = "\\begin_inset FloatList "+type
790         i = i+1
791
792
793 def change_infoinset(document):
794     " Change info inset."
795     lines = document.body
796     i = 0
797     while 1:
798         i = find_token(lines, "\\begin_inset Info", i)
799         if i == -1:
800             break
801         txt = string.lstrip(lines[i][18:])
802         new = ["\\begin_inset Note", "collapsed true", ""]
803         j = find_token(lines, "\\end_inset", i)
804         if j == -1:
805             break
806
807         note_lines = lines[i+1:j]
808         if len(txt) > 0:
809             note_lines = [txt]+note_lines
810
811         for line in note_lines:
812             new = new + ['\layout %s' % document.default_layout, ""]
813             tmp = string.split(line, '\\')
814             new = new + [tmp[0]]
815             for x in tmp[1:]:
816                 new = new + ["\\backslash ", x]
817         lines[i:j] = new
818         i = i+5
819
820
821 def change_header(document):
822     " Update header."
823     lines = document.header
824     i = find_token(lines, "\\use_amsmath", 0)
825     if i == -1:
826         return
827     lines[i+1:i+1] = ["\\use_natbib 0",
828                       "\use_numerical_citations 0"]
829
830
831 supported_versions = ["1.2.%d" % i for i in range(5)] + ["1.2"]
832 convert = [[220, [change_header, change_listof, fix_oldfloatinset,
833                   update_tabular, update_longtables, remove_pextra,
834                   remove_oldfloat, remove_figinset, remove_oldertinset,
835                   remove_oldert, combine_ert, change_infoinset]]]
836 revert  = []
837
838
839 if __name__ == "__main__":
840     pass