]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_4.py
another small lyx2lyx fix
[lyx.git] / lib / lyx2lyx / lyx_1_4.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
4 # Copyright (C) 2002-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 import re
21 from os import access, F_OK
22 import os.path
23 from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
24                          get_paragraph, get_value, del_token, is_nonempty_line,\
25                          find_tokens, find_end_of, find_token2
26 from sys import stdin
27 from string import replace, split, find, strip, join
28
29 ##
30 # Remove \color default
31 #
32 def remove_color_default(lines):
33     i = 0
34     while 1:
35         i = find_token(lines, "\\color default", i)
36         if i == -1:
37             return
38         lines[i] = replace(lines[i], "\\color default",
39                            "\\color inherit")
40
41
42 ##
43 # Add \end_header
44 #
45 def add_end_header(header):
46     header.append("\\end_header");
47
48
49 def rm_end_header(lines):
50     i = find_token(lines, "\\end_header", 0)
51     if i == -1:
52         return
53     del lines[i]
54
55
56 ##
57 # \SpecialChar ~ -> \InsetSpace ~
58 #
59 def convert_spaces(lines):
60     for i in range(len(lines)):
61         lines[i] = replace(lines[i],"\\SpecialChar ~","\\InsetSpace ~")
62
63
64 def revert_spaces(lines):
65     for i in range(len(lines)):
66         lines[i] = replace(lines[i],"\\InsetSpace ~", "\\SpecialChar ~")
67
68
69 ##
70 # BibTeX changes
71 #
72 def convert_bibtex(lines):
73     for i in range(len(lines)):
74         lines[i] = replace(lines[i],"\\begin_inset LatexCommand \\BibTeX",
75                                   "\\begin_inset LatexCommand \\bibtex")
76
77
78 def revert_bibtex(lines):
79     for i in range(len(lines)):
80         lines[i] = replace(lines[i], "\\begin_inset LatexCommand \\bibtex",
81                                   "\\begin_inset LatexCommand \\BibTeX")
82
83
84 ##
85 # Remove \lyxparent
86 #
87 def remove_insetparent(lines):
88     i = 0
89     while 1:
90         i = find_token(lines, "\\begin_inset LatexCommand \\lyxparent", i)
91         if i == -1:
92             break
93         del lines[i:i+3]
94
95
96 ##
97 #  Inset External
98 #
99 def convert_external(lines):
100     external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
101     external_header = "\\begin_inset External"
102     i = 0
103     while 1:
104         i = find_token(lines, external_header, i)
105         if i == -1:
106             break
107         look = external_rexp.search(lines[i])
108         args = ['','']
109         if look:
110             args[0] = look.group(1)
111             args[1] = look.group(2)
112         #FIXME: if the previous search fails then warn
113
114         if args[0] == "RasterImage":
115             # Convert a RasterImage External Inset to a Graphics Inset.
116             top = "\\begin_inset Graphics"
117             if args[1]:
118                 filename = "\tfilename " + args[1]
119             lines[i:i+1] = [top, filename]
120             i = i + 1
121         else:
122             # Convert the old External Inset format to the new.
123             top = external_header
124             template = "\ttemplate " + args[0]
125             if args[1]:
126                 filename = "\tfilename " + args[1]
127                 lines[i:i+1] = [top, template, filename]
128                 i = i + 2
129             else:
130                 lines[i:i+1] = [top, template]
131                 i = i + 1
132
133
134 def revert_external_1(lines):
135     external_header = "\\begin_inset External"
136     i = 0
137     while 1:
138         i = find_token(lines, external_header, i)
139         if i == -1:
140             break
141
142         template = split(lines[i+1])
143         template.reverse()
144         del lines[i+1]
145
146         filename = split(lines[i+1])
147         filename.reverse()
148         del lines[i+1]
149
150         params = split(lines[i+1])
151         params.reverse()
152         if lines[i+1]: del lines[i+1]
153
154         lines[i] = lines[i] + " " + template[0]+ ', "' + filename[0] + '", " '+ join(params[1:]) + '"'
155         i = i + 1
156
157
158 def revert_external_2(lines):
159     draft_token = '\tdraft'
160     i = 0
161     while 1:
162         i = find_token(lines, '\\begin_inset External', i)
163         if i == -1:
164             break
165         j = find_end_of_inset(lines, i + 1)
166         if j == -1:
167             #this should not happen
168             break
169         k = find_token(lines, draft_token, i+1, j-1)
170         if (k != -1 and len(draft_token) == len(lines[k])):
171             del lines[k]
172         i = j + 1
173
174
175 ##
176 # Comment
177 #
178 def convert_comment(lines):
179     i = 0
180     comment = "\\layout Comment"
181     while 1:
182         i = find_token(lines, comment, i)
183         if i == -1:
184             return
185
186         lines[i:i+1] = ["\\layout Standard","","",
187                         "\\begin_inset Comment",
188                         "collapsed true","",
189                         "\\layout Standard"]
190         i = i + 7
191
192         while 1:
193                 old_i = i
194                 i = find_token(lines, "\\layout", i)
195                 if i == -1:
196                     i = len(lines) - 1
197                     lines[i:i] = ["\\end_inset","",""]
198                     return
199
200                 j = find_token(lines, '\\begin_deeper', old_i, i)
201                 if j == -1: j = i + 1
202                 k = find_token(lines, '\\begin_inset', old_i, i)
203                 if k == -1: k = i + 1
204
205                 if j < i and j < k:
206                     i = j
207                     del lines[i]
208                     i = find_end_of( lines, i, "\\begin_deeper","\\end_deeper")
209                     if i == -1:
210                         #This case should not happen
211                         #but if this happens deal with it greacefully adding
212                         #the missing \end_deeper.
213                         i = len(lines) - 1
214                         lines[i:i] = ["\end_deeper",""]
215                         return
216                     else:
217                         del lines[i]
218                         continue
219
220                 if k < i:
221                     i = k
222                     i = find_end_of( lines, i, "\\begin_inset","\\end_inset")
223                     if i == -1:
224                         #This case should not happen
225                         #but if this happens deal with it greacefully adding
226                         #the missing \end_inset.
227                         i = len(lines) - 1
228                         lines[i:i] = ["\\end_inset","","","\\end_inset","",""]
229                         return
230                     else:
231                         i = i + 1
232                         continue
233
234                 if find(lines[i], comment) == -1:
235                     lines[i:i] = ["\\end_inset"]
236                     i = i + 1
237                     break
238                 lines[i:i+1] = ["\\layout Standard"]
239                 i = i + 1
240
241
242 def revert_comment(lines):
243     i = 0
244     while 1:
245         i = find_tokens(lines, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i)
246
247         if i == -1:
248             return
249         lines[i] = "\\begin_inset Note"
250         i = i + 1
251
252
253 ##
254 # Add \end_layout
255 #
256 def add_end_layout(lines):
257     i = find_token(lines, '\\layout', 0)
258
259     if i == -1:
260         return
261
262     i = i + 1
263     struct_stack = ["\\layout"]
264
265     while 1:
266         i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
267                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
268
269         token = split(lines[i])[0]
270
271         if token == "\\begin_inset":
272             struct_stack.append(token)
273             i = i + 1
274             continue
275
276         if token == "\\end_inset":
277             tail = struct_stack.pop()
278             if tail == "\\layout":
279                 lines.insert(i,"")
280                 lines.insert(i,"\\end_layout")
281                 i = i + 2
282                 #Check if it is the correct tag
283                 struct_stack.pop()
284             i = i + 1
285             continue
286
287         if token == "\\layout":
288             tail = struct_stack.pop()
289             if tail == token:
290                 lines.insert(i,"")
291                 lines.insert(i,"\\end_layout")
292                 i = i + 3
293             else:
294                 struct_stack.append(tail)
295                 i = i + 1
296             struct_stack.append(token)
297             continue
298
299         if token == "\\begin_deeper":
300             lines.insert(i,"")
301             lines.insert(i,"\\end_layout")
302             i = i + 3
303             struct_stack.append(token)
304             continue
305
306         if token == "\\end_deeper":
307             if struct_stack[-1] == '\\layout':
308                 lines.insert(i, '\\end_layout')
309                 i = i + 1
310                 struct_stack.pop()
311             i = i + 1
312             continue
313
314         #case \end_document
315         lines.insert(i, "")
316         lines.insert(i, "\\end_layout")
317         return
318
319
320 def rm_end_layout(lines):
321     i = 0
322     while 1:
323         i = find_token(lines, '\\end_layout', i)
324
325         if i == -1:
326             return
327
328         del lines[i]
329
330
331 ##
332 # Handle change tracking keywords
333 #
334 def insert_tracking_changes(lines):
335     i = find_token(lines, "\\tracking_changes", 0)
336     if i == -1:
337         lines.append("\\tracking_changes 0")
338
339 def rm_tracking_changes(lines):
340     i = find_token(lines, "\\author", 0)
341     if i != -1:
342         del lines[i]
343
344     i = find_token(lines, "\\tracking_changes", 0)
345     if i == -1:
346         return
347     del lines[i]
348
349
350 def rm_body_changes(lines):
351     i = 0
352     while 1:
353         i = find_token(lines, "\\change_", i)
354         if i == -1:
355             return
356
357         del lines[i]
358
359
360 ##
361 # \layout -> \begin_layout
362 #
363 def layout2begin_layout(lines):
364     i = 0
365     while 1:
366         i = find_token(lines, '\\layout', i)
367         if i == -1:
368             return
369
370         lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
371         i = i + 1
372
373
374 def begin_layout2layout(lines):
375     i = 0
376     while 1:
377         i = find_token(lines, '\\begin_layout', i)
378         if i == -1:
379             return
380
381         lines[i] = replace(lines[i], '\\begin_layout', '\\layout')
382         i = i + 1
383
384
385 ##
386 # valignment="center" -> valignment="middle"
387 #
388 def convert_valignment_middle(lines, start, end):
389     for i in range(start, end):
390         if re.search('^<(column|cell) .*valignment="center".*>$', lines[i]):
391             lines[i] = replace(lines[i], 'valignment="center"', 'valignment="middle"')
392
393
394 def convert_table_valignment_middle(lines):
395     i = 0
396     while 1:
397         i = find_token(lines, '\\begin_inset  Tabular', i)
398         if i == -1:
399             return
400         j = find_end_of_inset(lines, i + 1)
401         if j == -1:
402             #this should not happen
403             convert_valignment_middle(lines, i + 1, len(lines))
404             return
405         convert_valignment_middle(lines, i + 1, j)
406         i = j + 1
407
408
409 def revert_table_valignment_middle(lines, start, end):
410     for i in range(start, end):
411         if re.search('^<(column|cell) .*valignment="middle".*>$', lines[i]):
412             lines[i] = replace(lines[i], 'valignment="middle"', 'valignment="center"')
413
414
415 def revert_valignment_middle(lines):
416     i = 0
417     while 1:
418         i = find_token(lines, '\\begin_inset  Tabular', i)
419         if i == -1:
420             return
421         j = find_end_of_inset(lines, i + 1)
422         if j == -1:
423             #this should not happen
424             revert_table_valignment_middle(lines, i + 1, len(lines))
425             return
426         revert_table_valignment_middle(lines, i + 1, j)
427         i = j + 1
428
429
430 ##
431 #  \the_end -> \end_document
432 #
433 def convert_end_document(lines):
434     i = find_token(lines, "\\the_end", 0)
435     if i == -1:
436         lines.append("\\end_document")
437         return
438     lines[i] = "\\end_document"
439
440
441 def revert_end_document(lines):
442     i = find_token(lines, "\\end_document", 0)
443     if i == -1:
444         lines.append("\\the_end")
445         return
446     lines[i] = "\\the_end"
447
448
449 ##
450 # Convert line and page breaks
451 # Old:
452 #\layout Standard
453 #\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
454 #0
455 #
456 # New:
457 #\begin layout Standard
458 #
459 #\newpage 
460 #
461 #\lyxline
462 #\begin_inset VSpace xxx
463 #\end_inset
464 #
465 #\end_layout
466 #\begin_layout Standard
467 #
468 #0
469 #\end_layout
470 #\begin_layout Standard
471 #
472 #\begin_inset VSpace xxx
473 #\end_inset
474 #\lyxline 
475 #
476 #\newpage
477 #
478 #\end_layout
479 def convert_breaks(lines):    
480     i = 0
481     while 1:
482         i = find_token(lines, "\\begin_layout", i)
483         if i == -1:
484             return
485         i = i + 1
486         line_top   = find(lines[i],"\\line_top")
487         line_bot   = find(lines[i],"\\line_bottom")
488         pb_top     = find(lines[i],"\\pagebreak_top")
489         pb_bot     = find(lines[i],"\\pagebreak_bottom")
490         vspace_top = find(lines[i],"\\added_space_top")
491         vspace_bot = find(lines[i],"\\added_space_bottom")
492
493         if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
494             continue
495
496         for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
497             lines[i] = replace(lines[i], tag, "")
498
499         if vspace_top != -1:
500             # the position could be change because of the removal of other
501             # paragraph properties above
502             vspace_top = find(lines[i],"\\added_space_top")
503             tmp_list = split(lines[i][vspace_top:])
504             vspace_top_value = tmp_list[1]
505             lines[i] = lines[i][:vspace_top] + join(tmp_list[2:])
506
507         if vspace_bot != -1:
508             # the position could be change because of the removal of other
509             # paragraph properties above
510             vspace_bot = find(lines[i],"\\added_space_bottom")
511             tmp_list = split(lines[i][vspace_bot:])
512             vspace_bot_value = tmp_list[1]
513             lines[i] = lines[i][:vspace_bot] + join(tmp_list[2:])
514
515         lines[i] = strip(lines[i])
516         i = i + 1
517
518         #  Create an empty paragraph for line and page break that belong
519         # above the paragraph
520         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
521             
522             paragraph_above = ['','\\begin_layout Standard','','']
523
524             if pb_top != -1:
525                 paragraph_above.extend(['\\newpage ',''])
526
527             if vspace_top != -1:
528                 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset','',''])
529
530             if line_top != -1:
531                 paragraph_above.extend(['\\lyxline ',''])
532
533             paragraph_above.extend(['\\end_layout',''])
534
535             #inset new paragraph above the current paragraph
536             lines[i-2:i-2] = paragraph_above
537             i = i + len(paragraph_above)
538
539         # Ensure that nested style are converted later.
540         k = find_end_of(lines, i, "\\begin_layout", "\\end_layout")
541
542         if k == -1:
543             return
544
545         if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
546             
547             paragraph_bellow = ['','\\begin_layout Standard','','']
548
549             if line_bot != -1:
550                 paragraph_bellow.extend(['\\lyxline ',''])
551
552             if vspace_bot != -1:
553                 paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
554
555             if pb_bot != -1:
556                 paragraph_bellow.extend(['\\newpage ',''])
557
558             paragraph_bellow.extend(['\\end_layout',''])
559
560             #inset new paragraph above the current paragraph
561             lines[k + 1: k + 1] = paragraph_bellow
562
563
564 ##
565 #  Notes
566 #
567 def convert_note(lines):
568     i = 0
569     while 1:
570         i = find_tokens(lines, ["\\begin_inset Note",
571                                 "\\begin_inset Comment",
572                                 "\\begin_inset Greyedout"], i)
573         if i == -1:
574             break
575
576         lines[i] = lines[i][0:13] + 'Note ' + lines[i][13:]
577         i = i + 1
578
579
580 def revert_note(lines):
581     note_header = "\\begin_inset Note "
582     i = 0
583     while 1:
584         i = find_token(lines, note_header, i)
585         if i == -1:
586             break
587
588         lines[i] = "\\begin_inset " + lines[i][len(note_header):]
589         i = i + 1
590
591
592 ##
593 # Box
594 #
595 def convert_box(lines):
596     i = 0
597     while 1:
598         i = find_tokens(lines, ["\\begin_inset Boxed",
599                                 "\\begin_inset Doublebox",
600                                 "\\begin_inset Frameless",
601                                 "\\begin_inset ovalbox",
602                                 "\\begin_inset Ovalbox",
603                                 "\\begin_inset Shadowbox"], i)
604         if i == -1:
605             break
606
607         lines[i] = lines[i][0:13] + 'Box ' + lines[i][13:]
608         i = i + 1
609
610
611 def revert_box(lines):
612     box_header = "\\begin_inset Box "
613     i = 0
614     while 1:
615         i = find_token(lines, box_header, i)
616         if i == -1:
617             break
618
619         lines[i] = "\\begin_inset " + lines[i][len(box_header):]
620         i = i + 1
621
622
623 ##
624 # Collapse
625 #
626 def convert_collapsable(lines, opt):
627     i = 0
628     while 1:
629         i = find_tokens(lines, ["\\begin_inset Box",
630                                 "\\begin_inset Branch",
631                                 "\\begin_inset CharStyle",
632                                 "\\begin_inset Float",
633                                 "\\begin_inset Foot",
634                                 "\\begin_inset Marginal",
635                                 "\\begin_inset Note",
636                                 "\\begin_inset OptArg",
637                                 "\\begin_inset Wrap"], i)
638         if i == -1:
639             break
640
641         # Seach for a line starting 'collapsed'
642         # If, however, we find a line starting '\begin_layout'
643         # (_always_ present) then break with a warning message
644         i = i + 1
645         while 1:
646             if (lines[i] == "collapsed false"):
647                 lines[i] = "status open"
648                 break
649             elif (lines[i] == "collapsed true"):
650                 lines[i] = "status collapsed"
651                 break
652             elif (lines[i][:13] == "\\begin_layout"):
653                 opt.warning("Malformed LyX file.")
654                 break
655             i = i + 1
656
657         i = i + 1
658
659
660 def revert_collapsable(lines, opt):
661     i = 0
662     while 1:
663         i = find_tokens(lines, ["\\begin_inset Box",
664                                 "\\begin_inset Branch",
665                                 "\\begin_inset CharStyle",
666                                 "\\begin_inset Float",
667                                 "\\begin_inset Foot",
668                                 "\\begin_inset Marginal",
669                                 "\\begin_inset Note",
670                                 "\\begin_inset OptArg",
671                                 "\\begin_inset Wrap"], i)
672         if i == -1:
673             break
674
675         # Seach for a line starting 'status'
676         # If, however, we find a line starting '\begin_layout'
677         # (_always_ present) then break with a warning message
678         i = i + 1
679         while 1:
680             if (lines[i] == "status open"):
681                 lines[i] = "collapsed false"
682                 break
683             elif (lines[i] == "status collapsed" or
684                   lines[i] == "status inlined"):
685                 lines[i] = "collapsed true"
686                 break
687             elif (lines[i][:13] == "\\begin_layout"):
688                 opt.warning("Malformed LyX file.")
689                 break
690             i = i + 1
691
692         i = i + 1
693
694
695 ##
696 #  ERT
697 #
698 def convert_ert(lines, opt):
699     i = 0
700     while 1:
701         i = find_token(lines, "\\begin_inset ERT", i)
702         if i == -1:
703             break
704
705         # Seach for a line starting 'status'
706         # If, however, we find a line starting '\begin_layout'
707         # (_always_ present) then break with a warning message
708         i = i + 1
709         while 1:
710             if (lines[i] == "status Open"):
711                 lines[i] = "status open"
712                 break
713             elif (lines[i] == "status Collapsed"):
714                 lines[i] = "status collapsed"
715                 break
716             elif (lines[i] == "status Inlined"):
717                 lines[i] = "status inlined"
718                 break
719             elif (lines[i][:13] == "\\begin_layout"):
720                 opt.warning("Malformed LyX file.")
721                 break
722             i = i + 1
723
724         i = i + 1
725
726
727 def revert_ert(lines, opt):
728     i = 0
729     while 1:
730         i = find_token(lines, "\\begin_inset ERT", i)
731         if i == -1:
732             break
733
734         # Seach for a line starting 'status'
735         # If, however, we find a line starting '\begin_layout'
736         # (_always_ present) then break with a warning message
737         i = i + 1
738         while 1:
739             if (lines[i] == "status open"):
740                 lines[i] = "status Open"
741                 break
742             elif (lines[i] == "status collapsed"):
743                 lines[i] = "status Collapsed"
744                 break
745             elif (lines[i] == "status inlined"):
746                 lines[i] = "status Inlined"
747                 break
748             elif (lines[i][:13] == "\\begin_layout"):
749                 opt.warning("Malformed LyX file.")
750                 break
751             i = i + 1
752
753         i = i + 1
754
755
756 ##
757 # Minipages
758 #
759 def convert_minipage(lines):
760     """ Convert minipages to the box inset.
761     We try to use the same order of arguments as lyx does.
762     """
763     pos = ["t","c","b"]
764     inner_pos = ["c","t","b","s"]
765
766     i = 0
767     while 1:
768         i = find_token(lines, "\\begin_inset Minipage", i)
769         if i == -1:
770             return
771
772         lines[i] = "\\begin_inset Box Frameless"
773         i = i + 1
774
775         # convert old to new position using the pos list
776         if lines[i][:8] == "position":
777             lines[i] = 'position "%s"' % pos[int(lines[i][9])]
778         else:
779             lines.insert(i, 'position "%s"' % pos[0])
780         i = i + 1
781
782         lines.insert(i, 'hor_pos "c"')
783         i = i + 1
784         lines.insert(i, 'has_inner_box 1')
785         i = i + 1
786
787         # convert the inner_position
788         if lines[i][:14] == "inner_position":
789             lines[i] = 'inner_pos "%s"' %  inner_pos[int(lines[i][15])]
790         else:
791             lines.insert('inner_pos "%s"' % inner_pos[0])
792         i = i + 1
793
794         # We need this since the new file format has a height and width
795         # in a different order.
796         if lines[i][:6] == "height":
797             height = lines[i][6:]
798             # test for default value of 221 and convert it accordingly
799             if height == ' "0pt"':
800                 height = ' "1pt"'
801             del lines[i]
802         else:
803             height = ' "1pt"'
804
805         if lines[i][:5] == "width":
806             width = lines[i][5:]
807             del lines[i]
808         else:
809             width = ' "0"'
810
811         if lines[i][:9] == "collapsed":
812             if lines[i][9:] == "true":
813                 status = "collapsed"
814             else:
815                 status = "open"
816             del lines[i]
817         else:
818             status = "collapsed"
819
820         lines.insert(i, 'use_parbox 0')
821         i = i + 1
822         lines.insert(i, 'width' + width)
823         i = i + 1
824         lines.insert(i, 'special "none"')
825         i = i + 1
826         lines.insert(i, 'height' + height)
827         i = i + 1
828         lines.insert(i, 'height_special "totalheight"')
829         i = i + 1
830         lines.insert(i, 'status ' + status)
831         i = i + 1
832
833
834 # -------------------------------------------------------------------------------------------
835 # Convert backslashes into valid ERT code, append the converted text to
836 # lines[i] and return the (maybe incremented) line index i
837 def convert_ertbackslash(lines, i, ert):
838     for c in ert:
839         if c == '\\':
840             lines[i] = lines[i] + '\\backslash '
841             i = i + 1
842             lines.insert(i, '')
843         else:
844             lines[i] = lines[i] + c
845     return i
846
847
848 def convert_vspace(header, lines, opt):
849
850     # Get default spaceamount
851     i = find_token(header, '\\defskip', 0)
852     if i == -1:
853         defskipamount = 'medskip'
854     else:
855         defskipamount = split(header[i])[1]
856
857     # Convert the insets
858     i = 0
859     while 1:
860         i = find_token(lines, '\\begin_inset VSpace', i)
861         if i == -1:
862             return
863         spaceamount = split(lines[i])[2]
864
865         # Are we at the beginning or end of a paragraph?
866         paragraph_start = 1
867         start = get_paragraph(lines, i) + 1
868         for k in range(start, i):
869             if is_nonempty_line(lines[k]):
870                 paragraph_start = 0
871                 break
872         paragraph_end = 1
873         j = find_end_of_inset(lines, i)
874         if j == -1:
875             opt.warning("Malformed LyX file: Missing '\\end_inset'.")
876             i = i + 1
877             continue
878         end = get_next_paragraph(lines, i)
879         for k in range(j + 1, end):
880             if is_nonempty_line(lines[k]):
881                 paragraph_end = 0
882                 break
883
884         # Convert to paragraph formatting if we are at the beginning or end
885         # of a paragraph and the resulting paragraph would not be empty
886         if ((paragraph_start and not paragraph_end) or
887             (paragraph_end   and not paragraph_start)):
888             # The order is important: del and insert invalidate some indices
889             del lines[j]
890             del lines[i]
891             if paragraph_start:
892                 lines.insert(start, '\\added_space_top ' + spaceamount + ' ')
893             else:
894                 lines.insert(start, '\\added_space_bottom ' + spaceamount + ' ')
895             continue
896
897         # Convert to ERT
898         lines[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
899                         '\\layout Standard', '', '\\backslash ']
900         i = i + 6
901         if spaceamount[-1] == '*':
902             spaceamount = spaceamount[:-1]
903             keep = 1
904         else:
905             keep = 0
906
907         # Replace defskip by the actual value
908         if spaceamount == 'defskip':
909             spaceamount = defskipamount
910
911         # LaTeX does not know \\smallskip* etc
912         if keep:
913             if spaceamount == 'smallskip':
914                 spaceamount = '\\smallskipamount'
915             elif spaceamount == 'medskip':
916                 spaceamount = '\\medskipamount'
917             elif spaceamount == 'bigskip':
918                 spaceamount = '\\bigskipamount'
919             elif spaceamount == 'vfill':
920                 spaceamount = '\\fill'
921
922         # Finally output the LaTeX code
923         if (spaceamount == 'smallskip' or spaceamount == 'medskip' or
924             spaceamount == 'bigskip'   or spaceamount == 'vfill'):
925             lines.insert(i, spaceamount)
926         else :
927             if keep:
928                 lines.insert(i, 'vspace*{')
929             else:
930                 lines.insert(i, 'vspace{')
931             i = convert_ertbackslash(lines, i, spaceamount)
932             lines[i] =  lines[i] + '}'
933         i = i + 1
934
935
936 # Convert a LyX length into valid ERT code and append it to lines[i]
937 # Return the (maybe incremented) line index i
938 def convert_ertlen(lines, i, len, special):
939     units = {"text%":"\\textwidth", "col%":"\\columnwidth",
940              "page%":"\\pagewidth", "line%":"\\linewidth",
941              "theight%":"\\textheight", "pheight%":"\\pageheight"}
942
943     # Convert special lengths
944     if special != 'none':
945         len = '%f\\' % len2value(len) + special
946
947     # Convert LyX units to LaTeX units
948     for unit in units.keys():
949         if find(len, unit) != -1:
950             len = '%f' % (len2value(len) / 100) + units[unit]
951             break
952
953     # Convert backslashes and insert the converted length into lines
954     return convert_ertbackslash(lines, i, len)
955
956
957 # Return the value of len without the unit in numerical form
958 def len2value(len):
959     result = re.search('([+-]?[0-9.]+)', len)
960     if result:
961         return float(result.group(1))
962     # No number means 1.0
963     return 1.0
964
965
966 def convert_frameless_box(lines, opt):
967     pos = ['t', 'c', 'b']
968     inner_pos = ['c', 't', 'b', 's']
969     i = 0
970     while 1:
971         i = find_token(lines, '\\begin_inset Frameless', i)
972         if i == -1:
973             return
974         j = find_end_of_inset(lines, i)
975         if j == -1:
976             opt.warning("Malformed LyX file: Missing '\\end_inset'.")
977             i = i + 1
978             continue
979         del lines[i]
980
981         # Gather parameters
982         params = {'position':'0', 'hor_pos':'c', 'has_inner_box':'1',
983                   'inner_pos':'1', 'use_parbox':'0', 'width':'100col%',
984                   'special':'none', 'height':'1in',
985                   'height_special':'totalheight', 'collapsed':'false'}
986         for key in params.keys():
987             value = replace(get_value(lines, key, i, j), '"', '')
988             if value != "":
989                 if key == 'position':
990                     # convert new to old position: 'position "t"' -> 0
991                     value = find_token(pos, value, 0)
992                     if value != -1:
993                         params[key] = value
994                 elif key == 'inner_pos':
995                     # convert inner position
996                     value = find_token(inner_pos, value, 0)
997                     if value != -1:
998                         params[key] = value
999                 else:
1000                     params[key] = value
1001                 j = del_token(lines, key, i, j)
1002         i = i + 1
1003
1004         # Convert to minipage or ERT?
1005         # Note that the inner_position and height parameters of a minipage
1006         # inset are ignored and not accessible for the user, although they
1007         # are present in the file format and correctly read in and written.
1008         # Therefore we convert to ERT if they do not have their LaTeX
1009         # defaults. These are:
1010         # - the value of "position" for "inner_pos"
1011         # - "\totalheight"          for "height"
1012         if (params['use_parbox'] != '0' or
1013             params['has_inner_box'] != '1' or
1014             params['special'] != 'none' or
1015             inner_pos[params['inner_pos']] != pos[params['position']] or
1016             params['height_special'] != 'totalheight' or
1017             len2value(params['height']) != 1.0):
1018
1019             # Convert to ERT
1020             if params['collapsed'] == 'true':
1021                 params['collapsed'] = 'Collapsed'
1022             else:
1023                 params['collapsed'] = 'Open'
1024             lines[i : i] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
1025                             '', '\\layout Standard', '', '\\backslash ']
1026             i = i + 6
1027             if params['use_parbox'] == '1':
1028                 lines.insert(i, 'parbox')
1029             else:
1030                 lines.insert(i, 'begin{minipage}')
1031             lines[i] = lines[i] + '[' + pos[params['position']] + ']['
1032             i = convert_ertlen(lines, i, params['height'], params['height_special'])
1033             lines[i] = lines[i] + '][' + inner_pos[params['inner_pos']] + ']{'
1034             i = convert_ertlen(lines, i, params['width'], params['special'])
1035             if params['use_parbox'] == '1':
1036                 lines[i] = lines[i] + '}{'
1037             else:
1038                 lines[i] = lines[i] + '}'
1039             i = i + 1
1040             lines[i:i] = ['', '\\end_inset']
1041             i = i + 2
1042             j = find_end_of_inset(lines, i)
1043             if j == -1:
1044                 opt.warning("Malformed LyX file: Missing '\\end_inset'.")
1045                 break
1046             lines[j-1:j-1] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
1047                                '', '\\layout Standard', '']
1048             j = j + 4
1049             if params['use_parbox'] == '1':
1050                 lines.insert(j, '}')
1051             else:
1052                 lines[j:j] = ['\\backslash ', 'end{minipage}']
1053         else:
1054
1055             # Convert to minipage
1056             lines[i:i] = ['\\begin_inset Minipage',
1057                           'position %d' % params['position'],
1058                           'inner_position %d' % params['inner_pos'],
1059                           'height "' + params['height'] + '"',
1060                           'width "' + params['width'] + '"',
1061                           'collapsed ' + params['collapsed']]
1062             i = i + 6
1063
1064 ##
1065 # Convert jurabib
1066 #
1067
1068 def convert_jurabib(header, opt):
1069     i = find_token(header, '\\use_numerical_citations', 0)
1070     if i == -1:
1071         opt.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
1072         return
1073     header.insert(i + 1, '\\use_jurabib 0')
1074
1075
1076 def revert_jurabib(header, opt):
1077     i = find_token(header, '\\use_jurabib', 0)
1078     if i == -1:
1079         opt.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1080         return
1081     if get_value(header, '\\use_jurabib', 0) != "0":
1082         opt.warning("Conversion of '\\use_jurabib = 1' not yet implemented.")
1083         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1084         return
1085     del header[i]
1086
1087 ##
1088 # Convert bibtopic
1089 #
1090
1091 def convert_bibtopic(header, opt):
1092     i = find_token(header, '\\use_jurabib', 0)
1093     if i == -1:
1094         opt.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1095         return
1096     header.insert(i + 1, '\\use_bibtopic 0')
1097
1098
1099 def revert_bibtopic(header, opt):
1100     i = find_token(header, '\\use_bibtopic', 0)
1101     if i == -1:
1102         opt.warning("Malformed lyx file: Missing '\\use_bibtopic'.")
1103         return
1104     if get_value(header, '\\use_bibtopic', 0) != "0":
1105         opt.warning("Conversion of '\\use_bibtopic = 1' not yet implemented.")
1106         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1107     del header[i]
1108
1109 ##
1110 # Sideway Floats
1111 #
1112
1113 def convert_float(lines, opt):
1114     i = 0
1115     while 1:
1116         i = find_token(lines, '\\begin_inset Float', i)
1117         if i == -1:
1118             return
1119         # Seach for a line starting 'wide'
1120         # If, however, we find a line starting '\begin_layout'
1121         # (_always_ present) then break with a warning message
1122         i = i + 1
1123         while 1:
1124             if (lines[i][:4] == "wide"):
1125                 lines.insert(i + 1, 'sideways false')
1126                 break
1127             elif (lines[i][:13] == "\\begin_layout"):
1128                 opt.warning("Malformed lyx file.")
1129                 break
1130             i = i + 1
1131         i = i + 1
1132
1133
1134 def revert_float(lines, opt):
1135     i = 0
1136     while 1:
1137         i = find_token(lines, '\\begin_inset Float', i)
1138         if i == -1:
1139             return
1140         j = find_end_of_inset(lines, i)
1141         if j == -1:
1142             opt.warning("Malformed lyx file: Missing '\\end_inset'.")
1143             i = i + 1
1144             continue
1145         if get_value(lines, 'sideways', i, j) != "false":
1146             opt.warning("Conversion of 'sideways true' not yet implemented.")
1147             # Don't remove 'sideways' so that people will get warnings by lyx
1148             i = i + 1
1149             continue
1150         del_token(lines, 'sideways', i, j)
1151         i = i + 1
1152
1153
1154 def convert_graphics(lines, opt):
1155     """ Add extension to filenames of insetgraphics if necessary.
1156     """
1157     i = 0
1158     while 1:
1159         i = find_token(lines, "\\begin_inset Graphics", i)
1160         if i == -1:
1161             return
1162
1163         j = find_token2(lines, "filename", i)
1164         if j == -1:
1165             return
1166         i = i + 1
1167         filename = split(lines[j])[1]
1168         absname = os.path.normpath(os.path.join(opt.dir, filename))
1169         if opt.input == stdin and not os.path.isabs(filename):
1170             # We don't know the directory and cannot check the file.
1171             # We could use a heuristic and take the current directory,
1172             # and we could try to find out if filename has an extension,
1173             # but that would be just guesses and could be wrong.
1174             opt.warning("""Warning: Can not determine whether file
1175          %s
1176          needs an extension when reading from standard input.
1177          You may need to correct the file manually or run
1178          lyx2lyx again with the .lyx file as commandline argument.""" % filename)
1179             continue
1180         # This needs to be the same algorithm as in pre 233 insetgraphics
1181         if access(absname, F_OK):
1182             continue
1183         if access(absname + ".ps", F_OK):
1184             lines[j] = replace(lines[j], filename, filename + ".ps")
1185             continue
1186         if access(absname + ".eps", F_OK):
1187             lines[j] = replace(lines[j], filename, filename + ".eps")
1188
1189
1190 ##
1191 # Convert firstname and surname from styles -> char styles
1192 #
1193 def convert_names(lines, opt):
1194     """ Convert in the docbook backend from firstname and surname style
1195     to charstyles.
1196     """
1197     if opt.backend != "docbook":
1198         return
1199
1200     i = 0
1201
1202     while 1:
1203         i = find_token(lines, "\\begin_layout Author", i)
1204         if i == -1:
1205             return
1206
1207         i = i + 1
1208         while lines[i] == "":
1209             i = i + 1
1210
1211         if lines[i][:11] != "\\end_layout" or lines[i+2][:13] != "\\begin_deeper":
1212             i = i + 1
1213             continue
1214
1215         k = i
1216         i = find_end_of( lines, i+3, "\\begin_deeper","\\end_deeper")
1217         if i == -1:
1218             # something is really wrong, abort
1219             opt.warning("Missing \\end_deeper, after style Author.")
1220             opt.warning("Aborted attempt to parse FirstName and Surname.")
1221             return
1222         firstname, surname = "", ""
1223
1224         name = lines[k:i]
1225
1226         j = find_token(name, "\\begin_layout FirstName", 0)
1227         if j != -1:
1228             j = j + 1
1229             while(name[j] != "\\end_layout"):
1230                 firstname = firstname + name[j]
1231                 j = j + 1
1232
1233         j = find_token(name, "\\begin_layout Surname", 0)
1234         if j != -1:
1235             j = j + 1
1236             while(name[j] != "\\end_layout"):
1237                 surname = surname + name[j]
1238                 j = j + 1
1239
1240         # delete name
1241         del lines[k+2:i+1]
1242
1243         lines[k-1:k-1] = ["", "",
1244                           "\\begin_inset CharStyle Firstname",
1245                           "status inlined",
1246                           "",
1247                           "\\begin_layout Standard",
1248                           "",
1249                           "%s" % firstname,
1250                           "\end_layout",
1251                           "",
1252                           "\end_inset",
1253                           "",
1254                           "",
1255                           "\\begin_inset CharStyle Surname",
1256                           "status inlined",
1257                           "",
1258                           "\\begin_layout Standard",
1259                           "",
1260                           "%s" % surname,
1261                           "\\end_layout",
1262                           "",
1263                           "\\end_inset",
1264                           ""]
1265
1266
1267 def revert_names(lines, opt):
1268     """ Revert in the docbook backend from firstname and surname char style
1269     to styles.
1270     """
1271     if opt.backend != "docbook":
1272         return
1273
1274
1275 ##
1276 #    \use_natbib 1                       \cite_engine <style>
1277 #    \use_numerical_citations 0     ->   where <style> is one of
1278 #    \use_jurabib 0                      "basic", "natbib_authoryear",
1279 #                                        "natbib_numerical" or "jurabib"
1280 def convert_cite_engine(header, opt):
1281     a = find_token(header, "\\use_natbib", 0)
1282     if a == -1:
1283         opt.warning("Malformed lyx file: Missing '\\use_natbib'.")
1284         return
1285
1286     b = find_token(header, "\\use_numerical_citations", 0)
1287     if b == -1 or b != a+1:
1288         opt.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
1289         return
1290
1291     c = find_token(header, "\\use_jurabib", 0)
1292     if c == -1 or c != b+1:
1293         opt.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1294         return
1295
1296     use_natbib = int(split(header[a])[1])
1297     use_numerical_citations = int(split(header[b])[1])
1298     use_jurabib = int(split(header[c])[1])
1299
1300     cite_engine = "basic"
1301     if use_natbib:
1302         if use_numerical_citations:
1303             cite_engine = "natbib_numerical"
1304         else:
1305              cite_engine = "natbib_authoryear"
1306     elif use_jurabib:
1307         cite_engine = "jurabib"
1308
1309     del header[a:c+1]
1310     header.insert(a, "\\cite_engine " + cite_engine)
1311
1312
1313 def revert_cite_engine(header, opt):
1314     i = find_token(header, "\\cite_engine", 0)
1315     if i == -1:
1316         opt.warning("Malformed lyx file: Missing '\\cite_engine'.")
1317         return
1318
1319     cite_engine = split(header[i])[1]
1320
1321     use_natbib = '0'
1322     use_numerical = '0'
1323     use_jurabib = '0'
1324     if cite_engine == "natbib_numerical":
1325         use_natbib = '1'
1326         use_numerical = '1'
1327     elif cite_engine == "natbib_authoryear":
1328         use_natbib = '1'
1329     elif cite_engine == "jurabib":
1330         use_jurabib = '1'
1331
1332     del header[i]
1333     header.insert(i, "\\use_jurabib " + use_jurabib)
1334     header.insert(i, "\\use_numerical_citations " + use_numerical)
1335     header.insert(i, "\\use_natbib " + use_natbib)
1336
1337
1338 ##
1339 # Paper package
1340 #
1341 def convert_paperpackage(header, opt):
1342     i = find_token(header, "\\paperpackage", 0)
1343     if i == -1:
1344         opt.warning("Malformed lyx file: Missing '\\paperpackage'.")
1345         return
1346
1347     packages = {'a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
1348     paperpackage = split(header[i])[1]
1349     header[i] = replace(header[i], paperpackage, packages[paperpackage])
1350
1351
1352 def revert_paperpackage(header, opt):
1353     i = find_token(header, "\\paperpackage", 0)
1354     if i == -1:
1355         opt.warning("Malformed lyx file: Missing '\\paperpackage'.")
1356         return
1357
1358     packages = {'none':'a4', 'a4':'a4wide', 'a4wide':'widemarginsa4',
1359                 'widemarginsa4':''}
1360     paperpackage = split(header[i])[1]
1361     header[i] = replace(header[i], paperpackage, packages[paperpackage])
1362
1363
1364 ##
1365 # Bullets
1366 #
1367 def convert_bullets(header, opt):
1368     i = 0
1369     while 1:
1370         i = find_token(header, "\\bullet", i)
1371         if i == -1:
1372             return
1373         if header[i][:12] == '\\bulletLaTeX':
1374             header[i] = header[i] + ' ' + strip(header[i+1])
1375             n = 3
1376         else:
1377             header[i] = header[i] + ' ' + strip(header[i+1]) +\
1378                         ' ' + strip(header[i+2]) + ' ' + strip(header[i+3])
1379             n = 5
1380         del header[i+1:i + n]
1381         i = i + 1
1382
1383
1384 def revert_bullets(header, opt):
1385     i = 0
1386     while 1:
1387         i = find_token(header, "\\bullet", i)
1388         if i == -1:
1389             return
1390         if header[i][:12] == '\\bulletLaTeX':
1391             n = find(header[i], '"')
1392             if n == -1:
1393                 opt.warning("Malformed header.")
1394                 return
1395             else:
1396                 header[i:i+1] = [header[i][:n-1],'\t' + header[i][n:], '\\end_bullet']
1397             i = i + 3
1398         else:
1399             frag = split(header[i])
1400             if len(frag) != 5:
1401                 opt.warning("Malformed header.")
1402                 return
1403             else:
1404                 header[i:i+1] = [frag[0] + ' ' + frag[1],
1405                                  '\t' + frag[2],
1406                                  '\t' + frag[3],
1407                                  '\t' + frag[4],
1408                                  '\\end_bullet']
1409                 i = i + 5
1410
1411
1412 ##
1413 # \begin_header and \begin_document
1414 #
1415 def add_begin_header(header, opt):
1416     i = find_token(header, '\\lyxformat', 0)
1417     header.insert(i+1, '\\begin_header')
1418     header.insert(i+1, '\\begin_document')
1419
1420
1421 def remove_begin_header(header, opt):
1422     i = find_token(header, "\\begin_document", 0)
1423     if i != -1:
1424         del header[i]
1425     i = find_token(header, "\\begin_header", 0)
1426     if i != -1:
1427         del header[i]
1428
1429
1430 ##
1431 # \begin_body and \end_body
1432 #
1433 def add_begin_body(body, opt):
1434     body.insert(0, '\\begin_body')
1435     body.insert(1, '')
1436     i = find_token(body, "\\end_document", 0)
1437     body.insert(i, '\\end_body')
1438
1439 def remove_begin_body(body, opt):
1440     i = find_token(body, "\\begin_body", 0)
1441     if i != -1:
1442         del body[i]
1443         if not body[i]:
1444             del body[i]
1445     i = find_token(body, "\\end_body", 0)
1446     if i != -1:
1447         del body[i]
1448
1449
1450 ##
1451 # \papersize
1452 #
1453 def normalize_papersize(header):
1454     i = find_token(header, '\\papersize', 0)
1455     if i == -1:
1456         return
1457
1458     tmp = split(header[i])
1459     if tmp[1] == "Default":
1460         header[i] = '\\papersize default'
1461         return
1462     if tmp[1] == "Custom":
1463         header[i] = '\\papersize custom'
1464
1465
1466 def denormalize_papersize(header):
1467     i = find_token(header, '\\papersize', 0)
1468     if i == -1:
1469         return
1470
1471     tmp = split(header[i])
1472     if tmp[1] == "custom":
1473         header[i] = '\\papersize Custom'
1474
1475
1476 ##
1477 # Strip spaces at end of command line
1478 #
1479 def strip_end_space(body):
1480     for i in range(len(body)):
1481         if body[i][:1] == '\\':
1482             body[i] = strip(body[i])
1483
1484
1485 ##
1486 # Convertion hub
1487 #
1488 def convert(file):
1489     if file.format < 223:
1490         insert_tracking_changes(file.header)
1491         add_end_header(file.header)
1492         remove_color_default(file.body)
1493         convert_spaces(file.body)
1494         convert_bibtex(file.body)
1495         remove_insetparent(file.body)
1496         file.format = 223
1497     if file.end_format == file.format: return
1498
1499     if file.format < 224:
1500         convert_external(file.body)
1501         convert_comment(file.body)
1502         file.format = 224
1503     if file.end_format == file.format: return
1504
1505     if file.format < 225:
1506         add_end_layout(file.body)
1507         layout2begin_layout(file.body)
1508         convert_end_document(file.body)
1509         convert_table_valignment_middle(file.body)
1510         convert_breaks(file.body)
1511         file.format = 225
1512     if file.end_format == file.format: return
1513
1514     if file.format < 226:
1515         convert_note(file.body)
1516         file.format = 226
1517     if file.end_format == file.format: return
1518
1519     if file.format < 227:
1520         convert_box(file.body)
1521         file.format = 227
1522     if file.end_format == file.format: return
1523
1524     if file.format < 228:
1525         convert_collapsable(file.body, file)
1526         convert_ert(file.body, file)
1527         file.format = 228
1528     if file.end_format == file.format: return
1529
1530     if file.format < 229:
1531         convert_minipage(file.body)
1532         file.format = 229
1533     if file.end_format == file.format: return
1534
1535     if file.format < 230:
1536         convert_jurabib(file.header, file)
1537         file.format = 230
1538     if file.end_format == file.format: return
1539
1540     if file.format < 231:
1541         convert_float(file.body, file)
1542         file.format = 231
1543     if file.end_format == file.format: return
1544
1545     if file.format < 232:
1546         convert_bibtopic(file.header, file)
1547         file.format = 232
1548     if file.end_format == file.format: return
1549
1550     if file.format < 233:
1551         convert_graphics(file.body, file)
1552         convert_names(file.body, file)
1553         file.format = 233
1554     if file.end_format == file.format: return
1555
1556     if file.format < 234:
1557         convert_cite_engine(file.header, file)
1558         file.format = 234
1559     if file.end_format == file.format: return
1560
1561     if file.format < 235:
1562         convert_paperpackage(file.header, file)
1563         file.format = 235
1564     if file.end_format == file.format: return
1565
1566     if file.format < 236:
1567         convert_bullets(file.header, file)
1568         add_begin_header(file.header, file)
1569         add_begin_body(file.body, file)
1570         normalize_papersize(file.header)
1571         strip_end_space(file.body)
1572         file.format = 236
1573
1574 def revert(file):
1575     if file.format > 235:
1576         denormalize_papersize(file.header)
1577         remove_begin_body(file.body, file)
1578         remove_begin_header(file.header, file)
1579         revert_bullets(file.header, file)
1580         file.format = 235
1581     if file.end_format == file.format: return
1582
1583     if file.format > 234:
1584         revert_paperpackage(file.header, file)
1585         file.format = 234
1586     if file.end_format == file.format: return
1587
1588     if file.format > 233:
1589         revert_cite_engine(file.header, file)
1590         file.format = 233
1591     if file.end_format == file.format: return
1592
1593     if file.format > 232:
1594         revert_names(file.body, file)
1595         file.format = 232
1596     if file.end_format == file.format: return
1597
1598     if file.format > 231:
1599         revert_bibtopic(file.header, file)
1600         file.format = 231
1601     if file.end_format == file.format: return
1602
1603     if file.format > 230:
1604         revert_float(file.body, file)
1605         file.format = 230
1606     if file.end_format == file.format: return
1607
1608     if file.format > 229:
1609         revert_jurabib(file.header, file)
1610         file.format = 229
1611     if file.end_format == file.format: return
1612
1613     if file.format > 228:
1614         file.format = 228
1615     if file.end_format == file.format: return
1616
1617     if file.format > 227:
1618         revert_collapsable(file.body, file)
1619         revert_ert(file.body, file)
1620         file.format = 227
1621     if file.end_format == file.format: return
1622
1623     if file.format > 226:
1624         revert_box(file.body)
1625         revert_external_2(file.body)
1626         file.format = 226
1627     if file.end_format == file.format: return
1628
1629     if file.format > 225:
1630         revert_note(file.body)
1631         file.format = 225
1632     if file.end_format == file.format: return
1633
1634     if file.format > 224:
1635         rm_end_layout(file.body)
1636         begin_layout2layout(file.body)
1637         revert_end_document(file.body)
1638         revert_valignment_middle(file.body)
1639         convert_vspace(file.header, file.body, file)
1640         convert_frameless_box(file.body, file)
1641     if file.end_format == file.format: return
1642
1643     if file.format > 223:
1644         revert_external_2(file.body)
1645         revert_comment(file.body)
1646         file.format = 223
1647     if file.end_format == file.format: return
1648
1649     if file.format > 221:
1650         rm_end_header(file.header)
1651         revert_spaces(file.body)
1652         revert_bibtex(file.body)
1653         rm_tracking_changes(file.header)
1654         rm_body_changes(file.body)
1655         file.format = 221
1656
1657
1658 if __name__ == "__main__":
1659     pass