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