]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_4.py
f9d6b9e09769e9c061e2a2cec01d70487fe8cf76
[lyx.git] / lib / lyx2lyx / lyx_1_4.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 # Copyright (C) 2004-2005 Georg Baum <Georg.Baum@post.rwth-aachen.de>
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.4"""
21
22 import re
23 from os import access, F_OK
24 import os.path
25 from parser_tools import check_token, find_token, \
26                          get_value, is_nonempty_line, \
27                          find_tokens, find_end_of, find_beginning_of, find_token_exact, find_tokens_exact, \
28                          find_re, find_tokens_backwards
29 from sys import stdin
30
31 from lyx_0_12 import update_latexaccents
32
33 ####################################################################
34 # Private helper functions
35
36 def get_layout(line, default_layout):
37     " Get layout, if empty return the default layout."
38     tokens = line.split()
39     if len(tokens) > 1:
40         return tokens[1]
41     return default_layout
42
43
44 def get_paragraph(lines, i, format):
45     "Finds the paragraph that contains line i."
46
47     if format < 225:
48         begin_layout = "\\layout"
49     else:
50         begin_layout = "\\begin_layout"
51     while i != -1:
52         i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
53         if i == -1: return -1
54         if check_token(lines[i], begin_layout):
55             return i
56         i = find_beginning_of_inset(lines, i)
57     return -1
58
59
60 def find_beginning_of_inset(lines, i):
61     " Find beginning of inset, where lines[i] is included."
62     return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
63
64
65 def get_next_paragraph(lines, i, format):
66     "Finds the paragraph after the paragraph that contains line i."
67
68     if format < 225:
69         tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
70     elif format < 236:
71         tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_document"]
72     else:
73         tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_body", "\\end_document"]
74     while i != -1:
75         i = find_tokens(lines, tokens, i)
76         if not check_token(lines[i], "\\begin_inset"):
77             return i
78         i = find_end_of_inset(lines, i)
79     return -1
80
81
82 def find_end_of_inset(lines, i):
83     r"Finds the matching \end_inset"
84     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
85
86 def del_token(lines, token, start, end):
87     """ del_token(lines, token, start, end) -> int
88
89     Find the lower line in lines where token is the first element and
90     delete that line.
91
92     Returns the number of lines remaining."""
93
94     k = find_token_exact(lines, token, start, end)
95     if k == -1:
96         return end
97     else:
98         del lines[k]
99         return end - 1
100
101 # End of helper functions
102 ####################################################################
103
104 def remove_color_default(document):
105     r" Remove \color default"
106     i = 0
107     while True:
108         i = find_token(document.body, "\\color default", i)
109         if i == -1:
110             return
111         document.body[i] = document.body[i].replace("\\color default",
112                                                     "\\color inherit")
113
114
115 def add_end_header(document):
116     r" Add \end_header"
117     document.header.append("\\end_header");
118
119
120 def rm_end_header(document):
121     r" Remove \end_header"
122     i = find_token(document.header, "\\end_header", 0)
123     if i == -1:
124         return
125     del document.header[i]
126
127
128 def convert_amsmath(document):
129     " Convert \\use_amsmath"
130     i = find_token(document.header, "\\use_amsmath", 0)
131     if i == -1:
132         document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
133         return
134     tokens = document.header[i].split()
135     if len(tokens) != 2:
136         document.warning("Malformed LyX document: Could not parse line '%s'." % document.header[i])
137         use_amsmath = '0'
138     else:
139         use_amsmath = tokens[1]
140     # old: 0 == off, 1 == on
141     # new: 0 == off, 1 == auto, 2 == on
142     # translate off -> auto, since old format 'off' means auto in reality
143     if use_amsmath == '0':
144         document.header[i] = "\\use_amsmath 1"
145     else:
146         document.header[i] = "\\use_amsmath 2"
147
148
149 def revert_amsmath(document):
150     " Revert \\use_amsmath"
151     i = find_token(document.header, "\\use_amsmath", 0)
152     if i == -1:
153         document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
154         return
155     tokens = document.header[i].split()
156     if len(tokens) != 2:
157         document.warning("Malformed LyX document: Could not parse line '%s'." % document.header[i])
158         use_amsmath = '0'
159     else:
160         use_amsmath = tokens[1]
161     # old: 0 == off, 1 == on
162     # new: 0 == off, 1 == auto, 2 == on
163     # translate auto -> off, since old format 'off' means auto in reality
164     if use_amsmath == '2':
165         document.header[i] = "\\use_amsmath 1"
166     else:
167         document.header[i] = "\\use_amsmath 0"
168
169
170 def convert_spaces(document):
171     r" \SpecialChar ~ -> \InsetSpace ~"
172     for i in range(len(document.body)):
173         document.body[i] = document.body[i].replace("\\SpecialChar ~",
174                                                     "\\InsetSpace ~")
175
176
177 def revert_spaces(document):
178     r" \InsetSpace ~ -> \SpecialChar ~"
179     regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)')
180     i = 0
181     while True:
182         i = find_re(document.body, regexp, i)
183         if i == -1:
184             break
185         space = regexp.match(document.body[i]).group(3)
186         prepend = regexp.match(document.body[i]).group(1)
187         if space == '~':
188             document.body[i] = regexp.sub(prepend + '\\SpecialChar ~', document.body[i])
189             i = i + 1
190         else:
191             document.body[i] = regexp.sub(prepend, document.body[i])
192             document.body[i+1:i+1] = ''
193             if space == "\\space":
194                 space = "\\ "
195             i = insert_ert(document.body, i+1, 'Collapsed', space, document.format - 1, document.default_layout)
196
197
198 def rename_spaces(document):
199     """ \\InsetSpace \\, -> \\InsetSpace \thinspace{}
200         \\InsetSpace \\space -> \\InsetSpace \\space{}"""
201     for i in range(len(document.body)):
202         document.body[i] = document.body[i].replace("\\InsetSpace \\space",
203                                                     "\\InsetSpace \\space{}")
204         document.body[i] = document.body[i].replace("\\InsetSpace \\,",
205                                                     "\\InsetSpace \\thinspace{}")
206
207
208 def revert_space_names(document):
209     """ \\InsetSpace \thinspace{} -> \\InsetSpace \\,
210          \\InsetSpace \\space{} -> \\InsetSpace \\space"""
211     for i in range(len(document.body)):
212         document.body[i] = document.body[i].replace("\\InsetSpace \\space{}",
213                                                     "\\InsetSpace \\space")
214         document.body[i] = document.body[i].replace("\\InsetSpace \\thinspace{}",
215                                                     "\\InsetSpace \\,")
216
217
218 def lyx_support_escape(lab):
219     " Equivalent to pre-unicode lyx::support::escape()"
220     hexdigit = ['0', '1', '2', '3', '4', '5', '6', '7',
221                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
222     enc = ""
223     for c in lab:
224         o = ord(c)
225         if o >= 128 or c == '=' or c == '%':
226             enc = enc + '='
227             enc = enc + hexdigit[o >> 4]
228             enc = enc + hexdigit[o & 15]
229         else:
230             enc = enc + c
231     return enc;
232
233
234 def revert_eqref(document):
235     "\\begin_inset LatexCommand \\eqref -> ERT"
236     regexp = re.compile(r'^\\begin_inset\s+LatexCommand\s+\\eqref')
237     i = 0
238     while True:
239         i = find_re(document.body, regexp, i)
240         if i == -1:
241             break
242         eqref = lyx_support_escape(regexp.sub("", document.body[i]))
243         document.body[i:i+1] = ["\\begin_inset ERT", "status Collapsed", "",
244                             '\\layout %s' % document.default_layout, "", "\\backslash ",
245                             "eqref" + eqref]
246         i = i + 7
247
248
249 def convert_bibtex(document):
250     " Convert BibTeX changes."
251     for i in range(len(document.body)):
252         document.body[i] = document.body[i].replace("\\begin_inset LatexCommand \\BibTeX",
253                                                     "\\begin_inset LatexCommand \\bibtex")
254
255
256 def revert_bibtex(document):
257     " Revert BibTeX changes."
258     for i in range(len(document.body)):
259         document.body[i] = document.body[i].replace("\\begin_inset LatexCommand \\bibtex",
260                                                     "\\begin_inset LatexCommand \\BibTeX")
261
262
263 def remove_insetparent(document):
264     r" Remove \lyxparent"
265     i = 0
266     while True:
267         i = find_token(document.body, "\\begin_inset LatexCommand \\lyxparent", i)
268         if i == -1:
269             break
270         del document.body[i:i+3]
271
272
273 def convert_external(document):
274     " Convert inset External."
275     external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
276     external_header = "\\begin_inset External"
277     i = 0
278     while True:
279         i = find_token(document.body, external_header, i)
280         if i == -1:
281             break
282         look = external_rexp.search(document.body[i])
283         args = ['','']
284         if look:
285             args[0] = look.group(1)
286             args[1] = look.group(2)
287         #FIXME: if the previous search fails then warn
288
289         if args[0] == "RasterImage":
290             # Convert a RasterImage External Inset to a Graphics Inset.
291             top = "\\begin_inset Graphics"
292             if args[1]:
293                 filename = "\tfilename " + args[1]
294             document.body[i:i+1] = [top, filename]
295             i = i + 1
296         else:
297             # Convert the old External Inset format to the new.
298             top = external_header
299             template = "\ttemplate " + args[0]
300             if args[1]:
301                 filename = "\tfilename " + args[1]
302                 document.body[i:i+1] = [top, template, filename]
303                 i = i + 2
304             else:
305                 document.body[i:i+1] = [top, template]
306                 i = i + 1
307
308
309 def revert_external_1(document):
310     " Revert inset External."
311     external_header = "\\begin_inset External"
312     i = 0
313     while True:
314         i = find_token(document.body, external_header, i)
315         if i == -1:
316             break
317
318         template = document.body[i+1].split()
319         template.reverse()
320         del document.body[i+1]
321
322         filename = document.body[i+1].split()
323         filename.reverse()
324         del document.body[i+1]
325
326         params = document.body[i+1].split()
327         params.reverse()
328         if document.body[i+1]: del document.body[i+1]
329
330         document.body[i] = document.body[i] + " " + template[0]+ ', "' + filename[0] + '", " '+ " ".join(params[1:]) + '"'
331         i = i + 1
332
333
334 def revert_external_2(document):
335     " Revert inset External. (part II)"
336     draft_token = '\tdraft'
337     i = 0
338     while True:
339         i = find_token(document.body, '\\begin_inset External', i)
340         if i == -1:
341             break
342         j = find_end_of_inset(document.body, i + 1)
343         if j == -1:
344             #this should not happen
345             break
346         k = find_token(document.body, draft_token, i+1, j-1)
347         if (k != -1 and len(draft_token) == len(document.body[k])):
348             del document.body[k]
349         i = j + 1
350
351
352 def convert_comment(document):
353     " Convert \\layout comment"
354     i = 0
355     comment = "\\layout Comment"
356     while True:
357         i = find_token(document.body, comment, i)
358         if i == -1:
359             return
360
361         document.body[i:i+1] = ['\\layout %s' % document.default_layout,"","",
362                         "\\begin_inset Comment",
363                         "collapsed true","",
364                         '\\layout %s' % document.default_layout]
365         i = i + 7
366
367         while True:
368                 old_i = i
369                 i = find_token(document.body, "\\layout", i)
370                 if i == -1:
371                     i = len(document.body) - 1
372                     document.body[i:i] = ["\\end_inset","",""]
373                     return
374
375                 j = find_token(document.body, '\\begin_deeper', old_i, i)
376                 if j == -1: j = i + 1
377                 k = find_token(document.body, '\\begin_inset', old_i, i)
378                 if k == -1: k = i + 1
379
380                 if j < i and j < k:
381                     i = j
382                     del document.body[i]
383                     i = find_end_of( document.body, i, "\\begin_deeper","\\end_deeper")
384                     if i == -1:
385                         #This case should not happen
386                         #but if this happens deal with it greacefully adding
387                         #the missing \end_deeper.
388                         i = len(document.body) - 1
389                         document.body[i:i] = ["\\end_deeper",""]
390                         return
391                     else:
392                         del document.body[i]
393                         continue
394
395                 if k < i:
396                     i = k
397                     i = find_end_of( document.body, i, "\\begin_inset","\\end_inset")
398                     if i == -1:
399                         #This case should not happen
400                         #but if this happens deal with it greacefully adding
401                         #the missing \end_inset.
402                         i = len(document.body) - 1
403                         document.body[i:i] = ["\\end_inset","","","\\end_inset","",""]
404                         return
405                     else:
406                         i = i + 1
407                         continue
408
409                 if document.body[i].find(comment) == -1:
410                     document.body[i:i] = ["\\end_inset"]
411                     i = i + 1
412                     break
413                 document.body[i:i+1] = ['\\layout %s' % document.default_layout]
414                 i = i + 1
415
416
417 def revert_comment(document):
418     " Revert comments"
419     i = 0
420     while True:
421         i = find_tokens(document.body, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i)
422
423         if i == -1:
424             return
425         document.body[i] = "\\begin_inset Note"
426         i = i + 1
427
428
429 def add_end_layout(document):
430     r" Add \end_layout"
431     i = find_token(document.body, '\\layout', 0)
432
433     if i == -1:
434         return
435
436     i = i + 1
437     struct_stack = ["\\layout"]
438
439     while True:
440         i = find_tokens(document.body, ["\\begin_inset", "\\end_inset", "\\layout",
441                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
442
443         if i != -1:
444             token = document.body[i].split()[0]
445         else:
446             document.warning("Truncated document.")
447             i = len(document.body)
448             document.body.insert(i, '\\the_end')
449             token = ""
450
451         if token == "\\begin_inset":
452             struct_stack.append(token)
453             i = i + 1
454             continue
455
456         if token == "\\end_inset":
457             tail = struct_stack.pop()
458             if tail == "\\layout":
459                 document.body.insert(i,"")
460                 document.body.insert(i,"\\end_layout")
461                 i = i + 2
462                 #Check if it is the correct tag
463                 struct_stack.pop()
464             i = i + 1
465             continue
466
467         if token == "\\layout":
468             tail = struct_stack.pop()
469             if tail == token:
470                 document.body.insert(i,"")
471                 document.body.insert(i,"\\end_layout")
472                 i = i + 3
473             else:
474                 struct_stack.append(tail)
475                 i = i + 1
476             struct_stack.append(token)
477             continue
478
479         if token == "\\begin_deeper":
480             document.body.insert(i,"")
481             document.body.insert(i,"\\end_layout")
482             i = i + 3
483             # consecutive begin_deeper only insert one end_layout
484             while document.body[i].startswith('\\begin_deeper'):
485                 i += 1
486             struct_stack.append(token)
487             continue
488
489         if token == "\\end_deeper":
490             if struct_stack[-1] == '\\layout':
491                 document.body.insert(i, '\\end_layout')
492                 i = i + 1
493                 struct_stack.pop()
494             i = i + 1
495             continue
496
497         #case \end_document
498         document.body.insert(i, "")
499         document.body.insert(i, "\\end_layout")
500         return
501
502
503 def rm_end_layout(document):
504     r" Remove \end_layout"
505     i = 0
506     while True:
507         i = find_token(document.body, '\\end_layout', i)
508
509         if i == -1:
510             return
511
512         del document.body[i]
513
514
515 def insert_tracking_changes(document):
516     " Handle change tracking keywords."
517     i = find_token(document.header, "\\tracking_changes", 0)
518     if i == -1:
519         document.header.append("\\tracking_changes 0")
520
521
522 def rm_tracking_changes(document):
523     " Remove change tracking keywords."
524     i = find_token(document.header, "\\author", 0)
525     if i != -1:
526         del document.header[i]
527
528     i = find_token(document.header, "\\tracking_changes", 0)
529     if i == -1:
530         return
531     del document.header[i]
532
533
534 def rm_body_changes(document):
535     " Remove body changes."
536     i = 0
537     while True:
538         i = find_token(document.body, "\\change_", i)
539         if i == -1:
540             return
541
542         del document.body[i]
543
544
545 def layout2begin_layout(document):
546     r" \layout -> \begin_layout "
547     i = 0
548     while True:
549         i = find_token(document.body, '\\layout', i)
550         if i == -1:
551             return
552
553         document.body[i] = document.body[i].replace('\\layout', '\\begin_layout')
554         i = i + 1
555
556
557 def begin_layout2layout(document):
558     r" \begin_layout -> \layout "
559     i = 0
560     while True:
561         i = find_token(document.body, '\\begin_layout', i)
562         if i == -1:
563             return
564
565         document.body[i] = document.body[i].replace('\\begin_layout', '\\layout')
566         i = i + 1
567
568
569 def convert_valignment_middle(body, start, end):
570     'valignment="center" -> valignment="middle"'
571     for i in range(start, end):
572         if re.search('^<(column|cell) .*valignment="center".*>$', body[i]):
573             body[i] = body[i].replace('valignment="center"', 'valignment="middle"')
574
575
576 def convert_table_valignment_middle(document):
577     " Convert table  valignment, center -> middle"
578     regexp = re.compile(r'^\\begin_inset\s+Tabular')
579     i = 0
580     while True:
581         i = find_re(document.body, regexp, i)
582         if i == -1:
583             return
584         j = find_end_of_inset(document.body, i + 1)
585         if j == -1:
586             #this should not happen
587             convert_valignment_middle(document.body, i + 1, len(document.body))
588             return
589         convert_valignment_middle(document.body, i + 1, j)
590         i = j + 1
591
592
593 def revert_table_valignment_middle(body, start, end):
594     " valignment, middle -> center"
595     for i in range(start, end):
596         if re.search('^<(column|cell) .*valignment="middle".*>$', body[i]):
597             body[i] = body[i].replace('valignment="middle"', 'valignment="center"')
598
599
600 def revert_valignment_middle(document):
601     " Convert table  valignment, middle -> center"
602     regexp = re.compile(r'^\\begin_inset\s+Tabular')
603     i = 0
604     while True:
605         i = find_re(document.body, regexp, i)
606         if i == -1:
607             return
608         j = find_end_of_inset(document.body, i + 1)
609         if j == -1:
610             #this should not happen
611             revert_table_valignment_middle(document.body, i + 1, len(document.body))
612             return
613         revert_table_valignment_middle(document.body, i + 1, j)
614         i = j + 1
615
616
617 def convert_end_document(document):
618     "\\the_end -> \\end_document"
619     i = find_token(document.body, "\\the_end", 0)
620     if i == -1:
621         document.body.append("\\end_document")
622         return
623     document.body[i] = "\\end_document"
624
625
626 def revert_end_document(document):
627     "\\end_document -> \\the_end"
628     i = find_token(document.body, "\\end_document", 0)
629     if i == -1:
630         document.body.append("\\the_end")
631         return
632     document.body[i] = "\\the_end"
633
634
635 def convert_breaks(document):
636     r"""
637 Convert line and page breaks
638  Old:
639 \layout Standard
640 \line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
641 0
642
643  New:
644 \begin layout Standard
645
646 \newpage
647
648 \lyxline
649 \begin_inset ERT
650 \begin layout Standard
651 \backslash
652 vspace{-1\backslash
653 parskip}
654 \end_layout
655 \end_inset
656
657 \begin_inset VSpace xxx
658 \end_inset
659
660 0
661
662 \begin_inset VSpace xxx
663 \end_inset
664 \lyxline
665
666 \newpage
667
668 \end_layout
669     """
670     par_params = ('added_space_bottom', 'added_space_top', 'align',
671                  'labelwidthstring', 'line_bottom', 'line_top', 'noindent',
672                  'pagebreak_bottom', 'pagebreak_top', 'paragraph_spacing',
673                  'start_of_appendix')
674     font_attributes = ['\\family', '\\series', '\\shape', '\\emph',
675                        '\\numeric', '\\bar', '\\noun', '\\color', '\\lang']
676     attribute_values = ['default', 'default', 'default', 'default',
677                         'default', 'default', 'default', 'none', document.language]
678     i = 0
679     while True:
680         i = find_token(document.body, "\\begin_layout", i)
681         if i == -1:
682             return
683         layout = get_layout(document.body[i], document.default_layout)
684         i = i + 1
685
686         # Merge all paragraph parameters into a single line
687         # We cannot check for '\\' only because paragraphs may start e.g.
688         # with '\\backslash'
689         while document.body[i + 1][:1] == '\\' and document.body[i + 1][1:].split()[0] in par_params:
690             document.body[i] = document.body[i + 1] + ' ' + document.body[i]
691             del document.body[i+1]
692
693         line_top   = document.body[i].find("\\line_top")
694         line_bot   = document.body[i].find("\\line_bottom")
695         pb_top     = document.body[i].find("\\pagebreak_top")
696         pb_bot     = document.body[i].find("\\pagebreak_bottom")
697         vspace_top = document.body[i].find("\\added_space_top")
698         vspace_bot = document.body[i].find("\\added_space_bottom")
699
700         if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
701             continue
702
703         # Do we have a nonstandard paragraph? We need to create new paragraphs
704         # if yes to avoid putting lyxline etc. inside of special environments.
705         # This is wrong for itemize and enumerate environments, but it is
706         # impossible to convert these correctly.
707         # We want to avoid new paragraphs if possible becauase we want to
708         # inherit font sizes.
709         nonstandard = 0
710         if (not document.is_default_layout(layout) or
711             document.body[i].find("\\align") != -1 or
712             document.body[i].find("\\labelwidthstring") != -1 or
713             document.body[i].find("\\noindent") != -1):
714             nonstandard = 1
715
716         # get the font size of the beginning of this paragraph, since we need
717         # it for the lyxline inset
718         j = i + 1
719         while not is_nonempty_line(document.body[j]):
720             j = j + 1
721         size_top = ""
722         if document.body[j].find("\\size") != -1:
723             size_top = document.body[j].split()[1]
724
725         for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
726             document.body[i] = document.body[i].replace(tag, "")
727
728         if vspace_top != -1:
729             # the position could be change because of the removal of other
730             # paragraph properties above
731             vspace_top = document.body[i].find("\\added_space_top")
732             tmp_list = document.body[i][vspace_top:].split()
733             vspace_top_value = tmp_list[1]
734             document.body[i] = document.body[i][:vspace_top] + " ".join(tmp_list[2:])
735
736         if vspace_bot != -1:
737             # the position could be change because of the removal of other
738             # paragraph properties above
739             vspace_bot = document.body[i].find("\\added_space_bottom")
740             tmp_list = document.body[i][vspace_bot:].split()
741             vspace_bot_value = tmp_list[1]
742             document.body[i] = document.body[i][:vspace_bot] + " ".join(tmp_list[2:])
743
744         document.body[i] = document.body[i].strip()
745         i = i + 1
746
747         # Create an empty paragraph or paragraph fragment for line and
748         # page break that belong above the paragraph
749         if pb_top !=-1 or line_top != -1 or vspace_top != -1:
750
751             paragraph_above = list()
752             if nonstandard:
753                 # We need to create an extra paragraph for nonstandard environments
754                 paragraph_above = ['\\begin_layout %s' % document.default_layout, '']
755
756             if pb_top != -1:
757                 paragraph_above.extend(['\\newpage ',''])
758
759             if vspace_top != -1:
760                 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset','',''])
761
762             if line_top != -1:
763                 if size_top != '':
764                     paragraph_above.extend(['\\size ' + size_top + ' '])
765                 # We need an additional vertical space of -\parskip.
766                 # We can't use the vspace inset because it does not know \parskip.
767                 paragraph_above.extend(['\\lyxline ', '', ''])
768                 insert_ert(paragraph_above, len(paragraph_above) - 1, 'Collapsed',
769                            '\\vspace{-1\\parskip}\n', document.format + 1, document.default_layout)
770                 paragraph_above.extend([''])
771
772             if nonstandard:
773                 paragraph_above.extend(['\\end_layout ',''])
774                 # insert new paragraph above the current paragraph
775                 document.body[i-2:i-2] = paragraph_above
776             else:
777                 # insert new lines at the beginning of the current paragraph
778                 document.body[i:i] = paragraph_above
779
780             i = i + len(paragraph_above)
781
782         # Ensure that nested style are converted later.
783         k = find_end_of(document.body, i, "\\begin_layout", "\\end_layout")
784
785         if k == -1:
786             return
787
788         if pb_bot !=-1 or line_bot != -1 or vspace_bot != -1:
789
790             # get the font size of the end of this paragraph
791             size_bot = size_top
792             j = i + 1
793             while j < k:
794                 if document.body[j].find("\\size") != -1:
795                     size_bot = document.body[j].split()[1]
796                     j = j + 1
797                 elif document.body[j].find("\\begin_inset") != -1:
798                     # skip insets
799                     j = find_end_of_inset(document.body, j)
800                 else:
801                     j = j + 1
802
803             paragraph_below = list()
804             if nonstandard:
805                 # We need to create an extra paragraph for nonstandard environments
806                 paragraph_below = ['', '\\begin_layout %s' % document.default_layout, '']
807             else:
808                 for a in range(len(font_attributes)):
809                     if find_token(document.body, font_attributes[a], i, k) != -1:
810                         paragraph_below.extend([font_attributes[a] + ' ' + attribute_values[a]])
811
812             if line_bot != -1:
813                 if nonstandard and size_bot != '':
814                     paragraph_below.extend(['\\size ' + size_bot + ' '])
815                 paragraph_below.extend(['\\lyxline ',''])
816                 if size_bot != '':
817                     paragraph_below.extend(['\\size default '])
818
819             if vspace_bot != -1:
820                 paragraph_below.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
821
822             if pb_bot != -1:
823                 paragraph_below.extend(['\\newpage ',''])
824
825             if nonstandard:
826                 paragraph_below.extend(['\\end_layout '])
827                 # insert new paragraph below the current paragraph
828                 document.body[k+1:k+1] = paragraph_below
829             else:
830                 # insert new lines at the end of the current paragraph
831                 document.body[k:k] = paragraph_below
832
833
834 def convert_note(document):
835     " Convert Notes. "
836     i = 0
837     while True:
838         i = find_tokens(document.body, ["\\begin_inset Note",
839                                 "\\begin_inset Comment",
840                                 "\\begin_inset Greyedout"], i)
841         if i == -1:
842             break
843
844         document.body[i] = document.body[i][0:13] + 'Note ' + document.body[i][13:]
845         i = i + 1
846
847
848 def revert_note(document):
849     " Revert Notes. "
850     note_header = "\\begin_inset Note "
851     i = 0
852     while True:
853         i = find_token(document.body, note_header, i)
854         if i == -1:
855             break
856
857         document.body[i] = "\\begin_inset " + document.body[i][len(note_header):]
858         i = i + 1
859
860
861 def convert_box(document):
862     " Convert Boxes. "
863     i = 0
864     while True:
865         i = find_tokens(document.body, ["\\begin_inset Boxed",
866                                 "\\begin_inset Doublebox",
867                                 "\\begin_inset Frameless",
868                                 "\\begin_inset ovalbox",
869                                 "\\begin_inset Ovalbox",
870                                 "\\begin_inset Shadowbox"], i)
871         if i == -1:
872             break
873
874         document.body[i] = document.body[i][0:13] + 'Box ' + document.body[i][13:]
875         i = i + 1
876
877
878 def revert_box(document):
879     " Revert Boxes."
880     box_header = "\\begin_inset Box "
881     i = 0
882     while True:
883         i = find_token(document.body, box_header, i)
884         if i == -1:
885             break
886
887         document.body[i] = "\\begin_inset " + document.body[i][len(box_header):]
888         i = i + 1
889
890
891 def convert_collapsible(document):
892     " Convert collapsed insets. "
893     i = 0
894     while True:
895         i = find_tokens_exact(document.body, ["\\begin_inset Box",
896                                 "\\begin_inset Branch",
897                                 "\\begin_inset CharStyle",
898                                 "\\begin_inset Float",
899                                 "\\begin_inset Foot",
900                                 "\\begin_inset Marginal",
901                                 "\\begin_inset Note",
902                                 "\\begin_inset OptArg",
903                                 "\\begin_inset Wrap"], i)
904         if i == -1:
905             break
906
907         # Seach for a line starting 'collapsed'
908         # If, however, we find a line starting '\begin_layout'
909         # (_always_ present) then break with a warning message
910         i = i + 1
911         while True:
912             if (document.body[i] == "collapsed false"):
913                 document.body[i] = "status open"
914                 break
915             elif (document.body[i] == "collapsed true"):
916                 document.body[i] = "status collapsed"
917                 break
918             elif (document.body[i][:13] == "\\begin_layout"):
919                 document.warning("Malformed LyX document: Missing 'collapsed'.")
920                 break
921             i = i + 1
922
923         i = i + 1
924
925
926 def revert_collapsible(document):
927     " Revert collapsed insets. "
928     i = 0
929     while True:
930         i = find_tokens_exact(document.body, ["\\begin_inset Box",
931                                 "\\begin_inset Branch",
932                                 "\\begin_inset CharStyle",
933                                 "\\begin_inset Float",
934                                 "\\begin_inset Foot",
935                                 "\\begin_inset Marginal",
936                                 "\\begin_inset Note",
937                                 "\\begin_inset OptArg",
938                                 "\\begin_inset Wrap"], i)
939         if i == -1:
940             break
941
942         # Seach for a line starting 'status'
943         # If, however, we find a line starting '\begin_layout'
944         # (_always_ present) then break with a warning message
945         i = i + 1
946         while True:
947             if (document.body[i] == "status open"):
948                 document.body[i] = "collapsed false"
949                 break
950             elif (document.body[i] == "status collapsed" or
951                   document.body[i] == "status inlined"):
952                 document.body[i] = "collapsed true"
953                 break
954             elif (document.body[i][:13] == "\\begin_layout"):
955                 document.warning("Malformed LyX document: Missing 'status'.")
956                 break
957             i = i + 1
958
959         i = i + 1
960
961
962 def convert_ert(document):
963     " Convert ERT. "
964     i = 0
965     while True:
966         i = find_token(document.body, "\\begin_inset ERT", i)
967         if i == -1:
968             break
969
970         # Seach for a line starting 'status'
971         # If, however, we find a line starting '\begin_layout'
972         # (_always_ present) then break with a warning message
973         i = i + 1
974         while True:
975             if (document.body[i] == "status Open"):
976                 document.body[i] = "status open"
977                 break
978             elif (document.body[i] == "status Collapsed"):
979                 document.body[i] = "status collapsed"
980                 break
981             elif (document.body[i] == "status Inlined"):
982                 document.body[i] = "status inlined"
983                 break
984             elif (document.body[i][:13] == "\\begin_layout"):
985                 document.warning("Malformed LyX document: Missing 'status'.")
986                 break
987             i = i + 1
988
989         i = i + 1
990
991
992 def revert_ert(document):
993     " Revert ERT. "
994     i = 0
995     while True:
996         i = find_token(document.body, "\\begin_inset ERT", i)
997         if i == -1:
998             break
999
1000         # Seach for a line starting 'status'
1001         # If, however, we find a line starting '\begin_layout'
1002         # (_always_ present) then break with a warning message
1003         i = i + 1
1004         while True:
1005             if (document.body[i] == "status open"):
1006                 document.body[i] = "status Open"
1007                 break
1008             elif (document.body[i] == "status collapsed"):
1009                 document.body[i] = "status Collapsed"
1010                 break
1011             elif (document.body[i] == "status inlined"):
1012                 document.body[i] = "status Inlined"
1013                 break
1014             elif (document.body[i][:13] == "\\begin_layout"):
1015                 document.warning("Malformed LyX document : Missing 'status'.")
1016                 break
1017             i = i + 1
1018
1019         i = i + 1
1020
1021
1022 def convert_minipage(document):
1023     """ Convert minipages to the box inset.
1024     We try to use the same order of arguments as lyx does.
1025     """
1026     pos = ["t","c","b"]
1027     inner_pos = ["c","t","b","s"]
1028
1029     i = 0
1030     while True:
1031         i = find_token(document.body, "\\begin_inset Minipage", i)
1032         if i == -1:
1033             return
1034
1035         document.body[i] = "\\begin_inset Box Frameless"
1036         i = i + 1
1037
1038         # convert old to new position using the pos list
1039         if document.body[i][:8] == "position":
1040             document.body[i] = 'position "%s"' % pos[int(document.body[i][9])]
1041         else:
1042             document.body.insert(i, 'position "%s"' % pos[0])
1043         i = i + 1
1044
1045         document.body.insert(i, 'hor_pos "c"')
1046         i = i + 1
1047         document.body.insert(i, 'has_inner_box 1')
1048         i = i + 1
1049
1050         # convert the inner_position
1051         if document.body[i][:14] == "inner_position":
1052             innerpos = inner_pos[int(document.body[i][15])]
1053             del document.body[i]
1054         else:
1055             innerpos = inner_pos[0]
1056
1057         # We need this since the new file format has a height and width
1058         # in a different order.
1059         if document.body[i][:6] == "height":
1060             height = document.body[i][6:]
1061             # test for default value of 221 and convert it accordingly
1062             if height == ' "0pt"' or height == ' "0"':
1063                 height = ' "1pt"'
1064             del document.body[i]
1065         else:
1066             height = ' "1pt"'
1067
1068         if document.body[i][:5] == "width":
1069             width = document.body[i][5:]
1070             del document.body[i]
1071         else:
1072             width = ' "0"'
1073
1074         if document.body[i][:9] == "collapsed":
1075             if document.body[i][9:] == "true":
1076                 status = "collapsed"
1077             else:
1078                 status = "open"
1079             del document.body[i]
1080         else:
1081             status = "collapsed"
1082
1083         # Handle special default case:
1084         if height == ' "1pt"' and innerpos == 'c':
1085             innerpos = 't'
1086
1087         document.body.insert(i, 'inner_pos "' + innerpos + '"')
1088         i = i + 1
1089         document.body.insert(i, 'use_parbox 0')
1090         i = i + 1
1091         document.body.insert(i, 'width' + width)
1092         i = i + 1
1093         document.body.insert(i, 'special "none"')
1094         i = i + 1
1095         document.body.insert(i, 'height' + height)
1096         i = i + 1
1097         document.body.insert(i, 'height_special "totalheight"')
1098         i = i + 1
1099         document.body.insert(i, 'status ' + status)
1100         i = i + 1
1101
1102
1103 def convert_ertbackslash(body, i, ert, format, default_layout):
1104     r""" -------------------------------------------------------------------------------------------
1105     Convert backslashes and '\n' into valid ERT code, append the converted
1106     text to body[i] and return the (maybe incremented) line index i"""
1107
1108     for c in ert:
1109         if c == '\\':
1110             body[i] = body[i] + '\\backslash '
1111             i = i + 1
1112             body.insert(i, '')
1113         elif c == '\n':
1114             if format <= 240:
1115                 body[i+1:i+1] = ['\\newline ', '']
1116                 i = i + 2
1117             else:
1118                 body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout %s' % default_layout, '']
1119                 i = i + 4
1120         else:
1121             body[i] = body[i] + c
1122     return i
1123
1124
1125 def ert2latex(lines, format):
1126     r""" Converts lines in ERT code to LaTeX
1127     The surrounding \begin_layout ... \end_layout pair must not be included"""
1128
1129     backslash = re.compile(r'\\backslash\s*$')
1130     newline = re.compile(r'\\newline\s*$')
1131     if format <= 224:
1132         begin_layout = re.compile(r'\\layout\s*\S+$')
1133     else:
1134         begin_layout = re.compile(r'\\begin_layout\s*\S+$')
1135     end_layout = re.compile(r'\\end_layout\s*$')
1136     ert = ''
1137     for i in range(len(lines)):
1138         line = backslash.sub('\\\\', lines[i])
1139         if format <= 240:
1140             if begin_layout.match(line):
1141                 line = '\n\n'
1142             else:
1143                 line = newline.sub('\n', line)
1144         else:
1145             if begin_layout.match(line):
1146                 line = '\n'
1147         if format > 224 and end_layout.match(line):
1148             line = ''
1149         ert = ert + line
1150     return ert
1151
1152
1153 def get_par_params(lines, i):
1154     """ get all paragraph parameters. They can be all on one line or on several lines.
1155     lines[i] must be the first parameter line"""
1156     par_params = ('added_space_bottom', 'added_space_top', 'align',
1157                  'labelwidthstring', 'line_bottom', 'line_top', 'noindent',
1158                  'pagebreak_bottom', 'pagebreak_top', 'paragraph_spacing',
1159                  'start_of_appendix')
1160     # We cannot check for '\\' only because paragraphs may start e.g.
1161     # with '\\backslash'
1162     params = ''
1163     while lines[i][:1] == '\\' and lines[i][1:].split()[0] in par_params:
1164         params = params + ' ' + lines[i].strip()
1165         i = i + 1
1166     return params.strip()
1167
1168
1169 def lyxsize2latexsize(lyxsize):
1170     " Convert LyX font size to LaTeX fontsize. "
1171     sizes = {"tiny" : "tiny", "scriptsize" : "scriptsize",
1172              "footnotesize" : "footnotesize", "small" : "small",
1173              "normal" : "normalsize", "large" : "large", "larger" : "Large",
1174              "largest" : "LARGE", "huge" : "huge", "giant" : "Huge"}
1175     if lyxsize in sizes:
1176         return '\\' + sizes[lyxsize]
1177     return ''
1178
1179
1180 def revert_breaks(document):
1181     """ Change vspace insets, page breaks and lyxlines to paragraph options
1182     (if possible) or ERT"""
1183
1184     # Get default spaceamount
1185     i = find_token(document.header, '\\defskip', 0)
1186     if i == -1:
1187         defskipamount = 'medskip'
1188     else:
1189         defskipamount = document.header[i].split()[1]
1190
1191     keys = {"\\begin_inset" : "vspace", "\\lyxline" : "lyxline",
1192             "\\newpage" : "newpage"}
1193     keywords_top = {"vspace" : "\\added_space_top", "lyxline" : "\\line_top",
1194                     "newpage" : "\\pagebreak_top"}
1195     keywords_bot = {"vspace" : "\\added_space_bottom", "lyxline" : "\\line_bottom",
1196                     "newpage" : "\\pagebreak_bottom"}
1197     tokens = ["\\begin_inset VSpace", "\\lyxline", "\\newpage"]
1198
1199     # Convert the insets
1200     i = 0
1201     while True:
1202         i = find_tokens(document.body, tokens, i)
1203         if i == -1:
1204             return
1205
1206         # Are we at the beginning of a paragraph?
1207         paragraph_start = 1
1208         this_par = get_paragraph(document.body, i, document.format - 1)
1209         start = this_par + 1
1210         params = get_par_params(document.body, start)
1211         size = "normal"
1212         # Paragraph parameters may be on one or more lines.
1213         # Find the start of the real paragraph text.
1214         while document.body[start][:1] == '\\' and document.body[start].split()[0] in params:
1215             start = start + 1
1216         for k in range(start, i):
1217             if document.body[k].find("\\size") != -1:
1218                 # store font size
1219                 size = document.body[k].split()[1]
1220             elif is_nonempty_line(document.body[k]):
1221                 paragraph_start = 0
1222                 break
1223         # Find the end of the real paragraph text.
1224         next_par = get_next_paragraph(document.body, i, document.format - 1)
1225         if next_par == -1:
1226             document.warning("Malformed LyX document: Missing next paragraph.")
1227             i = i + 1
1228             continue
1229
1230         # first line of our insets
1231         inset_start = i
1232         # last line of our insets
1233         inset_end = inset_start
1234         # Are we at the end of a paragraph?
1235         paragraph_end = 1
1236         # start and end line numbers to delete if we convert this inset
1237         del_lines = list()
1238         # is this inset a lyxline above a paragraph?
1239         top = list()
1240         # raw inset information
1241         lines = list()
1242         # name of this inset
1243         insets = list()
1244         # font size of this inset
1245         sizes = list()
1246
1247         # Detect subsequent lyxline, vspace and pagebreak insets created by convert_breaks()
1248         n = 0
1249         k = inset_start
1250         while k < next_par:
1251             if find_tokens(document.body, tokens, k) == k:
1252                 # inset to convert
1253                 lines.append(document.body[k].split())
1254                 insets.append(keys[lines[n][0]])
1255                 del_lines.append([k, k])
1256                 top.append(0)
1257                 sizes.append(size)
1258                 n = n + 1
1259                 inset_end = k
1260             elif document.body[k].find("\\size") != -1:
1261                 # store font size
1262                 size = document.body[k].split()[1]
1263             elif find_token(document.body, "\\begin_inset ERT", k) == k:
1264                 ert_begin = find_token(document.body, "\\layout", k) + 1
1265                 if ert_begin == 0:
1266                     document.warning("Malformed LyX document: Missing '\\layout'.")
1267                     continue
1268                 ert_end = find_end_of_inset(document.body, k)
1269                 if ert_end == -1:
1270                     document.warning("Malformed LyX document: Missing '\\end_inset'.")
1271                     continue
1272                 ert = ert2latex(document.body[ert_begin:ert_end], document.format - 1)
1273                 if (n > 0 and insets[n - 1] == "lyxline" and
1274                     ert == '\\vspace{-1\\parskip}\n'):
1275                     # vspace ERT created by convert_breaks() for top lyxline
1276                     top[n - 1] = 1
1277                     del_lines[n - 1][1] = ert_end
1278                     inset_end = ert_end
1279                     k = ert_end
1280                 else:
1281                     paragraph_end = 0
1282                     break
1283             elif (n > 0 and insets[n - 1] == "vspace" and
1284                   find_token(document.body, "\\end_inset", k) == k):
1285                 # ignore end of vspace inset
1286                 del_lines[n - 1][1] = k
1287                 inset_end = k
1288             elif is_nonempty_line(document.body[k]):
1289                 paragraph_end = 0
1290                 break
1291             k = k + 1
1292
1293         # Determine space amount for vspace insets
1294         spaceamount = list()
1295         arguments = list()
1296         for k in range(n):
1297             if insets[k] == "vspace":
1298                 spaceamount.append(lines[k][2])
1299                 arguments.append(' ' + spaceamount[k] + ' ')
1300             else:
1301                 spaceamount.append('')
1302                 arguments.append(' ')
1303
1304         # Can we convert to top paragraph parameters?
1305         before = 0
1306         if ((n == 3 and insets[0] == "newpage" and insets[1] == "vspace" and
1307              insets[2] == "lyxline" and top[2]) or
1308             (n == 2 and
1309              ((insets[0] == "newpage" and insets[1] == "vspace") or
1310               (insets[0] == "newpage" and insets[1] == "lyxline" and top[1]) or
1311               (insets[0] == "vspace"  and insets[1] == "lyxline" and top[1]))) or
1312             (n == 1 and insets[0] == "lyxline" and top[0])):
1313             # These insets have been created before a paragraph by
1314             # convert_breaks()
1315             before = 1
1316
1317         # Can we convert to bottom paragraph parameters?
1318         after = 0
1319         if ((n == 3 and insets[0] == "lyxline" and not top[0] and
1320              insets[1] == "vspace" and insets[2] == "newpage") or
1321             (n == 2 and
1322              ((insets[0] == "lyxline" and not top[0] and insets[1] == "vspace") or
1323               (insets[0] == "lyxline" and not top[0] and insets[1] == "newpage") or
1324               (insets[0] == "vspace"  and insets[1] == "newpage"))) or
1325             (n == 1 and insets[0] == "lyxline" and not top[0])):
1326             # These insets have been created after a paragraph by
1327             # convert_breaks()
1328             after = 1
1329
1330         if paragraph_start and paragraph_end:
1331             # We are in a paragraph of our own.
1332             # We must not delete this paragraph if it has parameters
1333             if params == '':
1334                 # First try to merge with the previous paragraph.
1335                 # We try the previous paragraph first because we would
1336                 # otherwise need ERT for two subsequent vspaces.
1337                 prev_par = get_paragraph(document.body, this_par - 1, document.format - 1) + 1
1338                 if prev_par > 0 and not before:
1339                     prev_params = get_par_params(document.body, prev_par + 1)
1340                     ert = 0
1341                     # determine font size
1342                     prev_size = "normal"
1343                     k = prev_par + 1
1344                     while document.body[k][:1] == '\\' and document.body[k].split()[0] in prev_params:
1345                         k = k + 1
1346                     while k < this_par:
1347                         if document.body[k].find("\\size") != -1:
1348                             prev_size = document.body[k].split()[1]
1349                             break
1350                         elif document.body[k].find("\\begin_inset") != -1:
1351                             # skip insets
1352                             k = find_end_of_inset(document.body, k)
1353                         elif is_nonempty_line(document.body[k]):
1354                             break
1355                         k = k + 1
1356                     for k in range(n):
1357                         if (keywords_bot[insets[k]] in prev_params or
1358                             (insets[k] == "lyxline" and sizes[k] != prev_size)):
1359                             ert = 1
1360                             break
1361                     if not ert:
1362                         for k in range(n):
1363                             document.body.insert(prev_par + 1,
1364                                              keywords_bot[insets[k]] + arguments[k])
1365                         del document.body[this_par+n:next_par-1+n]
1366                         i = this_par + n
1367                         continue
1368                 # Then try next paragraph
1369                 if next_par > 0 and not after:
1370                     next_params = get_par_params(document.body, next_par + 1)
1371                     ert = 0
1372                     while document.body[k][:1] == '\\' and document.body[k].split()[0] in next_params:
1373                         k = k + 1
1374                     # determine font size
1375                     next_size = "normal"
1376                     k = next_par + 1
1377                     while k < this_par:
1378                         if document.body[k].find("\\size") != -1:
1379                             next_size = document.body[k].split()[1]
1380                             break
1381                         elif is_nonempty_line(document.body[k]):
1382                             break
1383                         k = k + 1
1384                     for k in range(n):
1385                         if (keywords_top[insets[k]] in next_params or
1386                             (insets[k] == "lyxline" and sizes[k] != next_size)):
1387                             ert = 1
1388                             break
1389                     if not ert:
1390                         for k in range(n):
1391                             document.body.insert(next_par + 1,
1392                                              keywords_top[insets[k]] + arguments[k])
1393                         del document.body[this_par:next_par-1]
1394                         i = this_par
1395                         continue
1396         elif paragraph_start or paragraph_end:
1397             # Convert to paragraph formatting if we are at the beginning or end
1398             # of a paragraph and the resulting paragraph would not be empty
1399             # The order is important: del and insert invalidate some indices
1400             if paragraph_start:
1401                 keywords = keywords_top
1402             else:
1403                 keywords = keywords_bot
1404             ert = 0
1405             for k in range(n):
1406                 if keywords[insets[k]] in params:
1407                     ert = 1
1408                     break
1409             if not ert:
1410                 for k in range(n):
1411                     document.body.insert(this_par + 1,
1412                                      keywords[insets[k]] + arguments[k])
1413                     for j in range(k, n):
1414                         del_lines[j][0] = del_lines[j][0] + 1
1415                         del_lines[j][1] = del_lines[j][1] + 1
1416                     del document.body[del_lines[k][0]:del_lines[k][1]+1]
1417                     deleted = del_lines[k][1] - del_lines[k][0] + 1
1418                     for j in range(k + 1, n):
1419                         del_lines[j][0] = del_lines[j][0] - deleted
1420                         del_lines[j][1] = del_lines[j][1] - deleted
1421                 i = this_par
1422                 continue
1423
1424         # Convert the first inset to ERT.
1425         # The others are converted in the next loop runs (if they exist)
1426         if insets[0] == "vspace":
1427             document.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
1428                                 '\\layout %s' % document.default_layout, '', '\\backslash ']
1429             i = i + 6
1430             if spaceamount[0][-1] == '*':
1431                 spaceamount[0] = spaceamount[0][:-1]
1432                 keep = 1
1433             else:
1434                 keep = 0
1435
1436             # Replace defskip by the actual value
1437             if spaceamount[0] == 'defskip':
1438                 spaceamount[0] = defskipamount
1439
1440             # LaTeX does not know \\smallskip* etc
1441             if keep:
1442                 if spaceamount[0] == 'smallskip':
1443                     spaceamount[0] = '\\smallskipamount'
1444                 elif spaceamount[0] == 'medskip':
1445                     spaceamount[0] = '\\medskipamount'
1446                 elif spaceamount[0] == 'bigskip':
1447                     spaceamount[0] = '\\bigskipamount'
1448                 elif spaceamount[0] == 'vfill':
1449                     spaceamount[0] = '\\fill'
1450
1451             # Finally output the LaTeX code
1452             if (spaceamount[0] == 'smallskip' or spaceamount[0] == 'medskip' or
1453                 spaceamount[0] == 'bigskip'   or spaceamount[0] == 'vfill'):
1454                 document.body.insert(i, spaceamount[0] + '{}')
1455             else :
1456                 if keep:
1457                     document.body.insert(i, 'vspace*{')
1458                 else:
1459                     document.body.insert(i, 'vspace{')
1460                 i = convert_ertbackslash(document.body, i, spaceamount[0], document.format - 1, document.default_layout)
1461                 document.body[i] = document.body[i] + '}'
1462             i = i + 1
1463         elif insets[0] == "lyxline":
1464             document.body[i] = ''
1465             latexsize = lyxsize2latexsize(size)
1466             if latexsize == '':
1467                 document.warning("Could not convert LyX fontsize '%s' to LaTeX font size." % size)
1468                 latexsize = '\\normalsize'
1469             i = insert_ert(document.body, i, 'Collapsed',
1470                            '\\lyxline{%s}' % latexsize,
1471                            document.format - 1, document.default_layout)
1472             # We use \providecommand so that we don't get an error if native
1473             # lyxlines are used (LyX writes first its own preamble and then
1474             # the user specified one)
1475             add_to_preamble(document,
1476                             ['% Commands inserted by lyx2lyx for lyxlines',
1477                              '\\providecommand{\\lyxline}[1]{',
1478                              '  {#1 \\vspace{1ex} \\hrule width \\columnwidth \\vspace{1ex}}'
1479                              '}'])
1480         elif insets[0] == "newpage":
1481             document.body[i] = ''
1482             i = insert_ert(document.body, i, 'Collapsed', '\\newpage{}',
1483                            document.format - 1, document.default_layout)
1484
1485
1486 # Convert a LyX length into a LaTeX length
1487 def convert_len(len, special):
1488     units = {"text%":"\\textwidth", "col%":"\\columnwidth",
1489              "page%":"\\pagewidth", "line%":"\\linewidth",
1490              "theight%":"\\textheight", "pheight%":"\\pageheight"}
1491
1492     # Convert special lengths
1493     if special != 'none':
1494         len = '%f\\' % len2value(len) + special
1495
1496     # Convert LyX units to LaTeX units
1497     for unit in list(units.keys()):
1498         if len.find(unit) != -1:
1499             len = '%f' % (len2value(len) / 100) + units[unit]
1500             break
1501
1502     return len
1503
1504
1505 def convert_ertlen(body, i, len, special, format, default_layout):
1506     """ Convert a LyX length into valid ERT code and append it to body[i]
1507     Return the (maybe incremented) line index i
1508     Convert backslashes and insert the converted length into body. """
1509     return convert_ertbackslash(body, i, convert_len(len, special), format, default_layout)
1510
1511
1512 def len2value(len):
1513     " Return the value of len without the unit in numerical form. "
1514     result = re.search('([+-]?[0-9.]+)', len)
1515     if result:
1516         return float(result.group(1))
1517     # No number means 1.0
1518     return 1.0
1519
1520
1521 def insert_ert(body, i, status, text, format, default_layout):
1522     """ Convert text to ERT and insert it at body[i]
1523     Return the index of the line after the inserted ERT"""
1524
1525     body[i:i] = ['\\begin_inset ERT', 'status ' + status, '']
1526     i = i + 3
1527     if format <= 224:
1528         body[i:i] = ['\\layout %s' % default_layout, '']
1529     else:
1530         body[i:i] = ['\\begin_layout %s' % default_layout, '']
1531     i = i + 1       # i points now to the just created empty line
1532     i = convert_ertbackslash(body, i, text, format, default_layout) + 1
1533     if format > 224:
1534         body[i:i] = ['\\end_layout']
1535         i = i + 1
1536     body[i:i] = ['', '\\end_inset', '']
1537     i = i + 3
1538     return i
1539
1540
1541 def add_to_preamble(document, text):
1542     """ Add text to the preamble if it is not already there.
1543     Only the first line is checked!"""
1544
1545     if find_token(document.preamble, text[0], 0) != -1:
1546         return
1547
1548     document.preamble.extend(text)
1549
1550
1551 def convert_frameless_box(document):
1552     " Convert frameless box."
1553     pos = ['t', 'c', 'b']
1554     inner_pos = ['c', 't', 'b', 's']
1555     i = 0
1556     while True:
1557         i = find_token(document.body, '\\begin_inset Frameless', i)
1558         if i == -1:
1559             return
1560         j = find_end_of_inset(document.body, i)
1561         if j == -1:
1562             document.warning("Malformed LyX document: Missing '\\end_inset'.")
1563             i = i + 1
1564             continue
1565         del document.body[i]
1566         j = j - 1
1567
1568         # Gather parameters
1569         params = {'position':0, 'hor_pos':'c', 'has_inner_box':'1',
1570                   'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
1571                   'special':'none', 'height':'1in',
1572                   'height_special':'totalheight', 'collapsed':'false'}
1573         for key in list(params.keys()):
1574             value = get_value(document.body, key, i, j).replace('"', '')
1575             if value != "":
1576                 if key == 'position':
1577                     # convert new to old position: 'position "t"' -> 0
1578                     value = find_token(pos, value, 0)
1579                     if value != -1:
1580                         params[key] = value
1581                 elif key == 'inner_pos':
1582                     # convert inner position
1583                     value = find_token(inner_pos, value, 0)
1584                     if value != -1:
1585                         params[key] = value
1586                 else:
1587                     params[key] = value
1588                 j = del_token(document.body, key, i, j)
1589         i = i + 1
1590
1591         # Convert to minipage or ERT?
1592         # Note that the inner_position and height parameters of a minipage
1593         # inset are ignored and not accessible for the user, although they
1594         # are present in the file format and correctly read in and written.
1595         # Therefore we convert to ERT if they do not have their LaTeX
1596         # defaults. These are:
1597         # - the value of "position" for "inner_pos"
1598         # - "\totalheight"          for "height"
1599         if (params['use_parbox'] != '0' or
1600             params['has_inner_box'] != '1' or
1601             params['special'] != 'none' or
1602             params['height_special'] != 'totalheight' or
1603             len2value(params['height']) != 1.0):
1604
1605             # Here we know that this box is not supported in file format 224.
1606             # Therefore we need to convert it to ERT. We can't simply convert
1607             # the beginning and end of the box to ERT, because the
1608             # box inset may contain layouts that are different from the
1609             # surrounding layout. After the conversion the contents of the
1610             # box inset is on the same level as the surrounding text, and
1611             # paragraph layouts and align parameters can get mixed up.
1612
1613             # A possible solution for this problem:
1614             # Convert the box to a minipage and redefine the minipage
1615             # environment in ERT so that the original box is simulated.
1616             # For minipages we could do this in a way that the width and
1617             # position can still be set from LyX, but this did not work well.
1618             # This is not possible for parboxes either, so we convert the
1619             # original box to ERT, put the minipage inset inside the box
1620             # and redefine the minipage environment to be empty.
1621
1622             # Commands that are independant of a particular box can go to
1623             # the preamble.
1624             # We need to define lyxtolyxrealminipage with 3 optional
1625             # arguments although LyX 1.3 uses only the first one.
1626             # Otherwise we will get LaTeX errors if this document is
1627             # converted to format 225 or above again (LyX 1.4 uses all
1628             # optional arguments).
1629             add_to_preamble(document,
1630                 ['% Commands inserted by lyx2lyx for frameless boxes',
1631                  '% Save the original minipage environment',
1632                  '\\let\\lyxtolyxrealminipage\\minipage',
1633                  '\\let\\endlyxtolyxrealminipage\\endminipage',
1634                  '% Define an empty lyxtolyximinipage environment',
1635                  '% with 3 optional arguments',
1636                  '\\newenvironment{lyxtolyxiiiminipage}[4]{}{}',
1637                  '\\newenvironment{lyxtolyxiiminipage}[2][\\lyxtolyxargi]%',
1638                  '  {\\begin{lyxtolyxiiiminipage}{\\lyxtolyxargi}{\\lyxtolyxargii}{#1}{#2}}%',
1639                  '  {\\end{lyxtolyxiiiminipage}}',
1640                  '\\newenvironment{lyxtolyximinipage}[1][\\totalheight]%',
1641                  '  {\\def\\lyxtolyxargii{{#1}}\\begin{lyxtolyxiiminipage}}%',
1642                  '  {\\end{lyxtolyxiiminipage}}',
1643                  '\\newenvironment{lyxtolyxminipage}[1][c]%',
1644                  '  {\\def\\lyxtolyxargi{{#1}}\\begin{lyxtolyximinipage}}',
1645                  '  {\\end{lyxtolyximinipage}}'])
1646
1647             if params['use_parbox'] != '0':
1648                 ert = '\\parbox'
1649             else:
1650                 ert = '\\begin{lyxtolyxrealminipage}'
1651
1652             # convert optional arguments only if not latex default
1653             if (pos[params['position']] != 'c' or
1654                 inner_pos[params['inner_pos']] != pos[params['position']] or
1655                 params['height_special'] != 'totalheight' or
1656                 len2value(params['height']) != 1.0):
1657                 ert = ert + '[' + pos[params['position']] + ']'
1658             if (inner_pos[params['inner_pos']] != pos[params['position']] or
1659                 params['height_special'] != 'totalheight' or
1660                 len2value(params['height']) != 1.0):
1661                 ert = ert + '[' + convert_len(params['height'],
1662                                               params['height_special']) + ']'
1663             if inner_pos[params['inner_pos']] != pos[params['position']]:
1664                 ert = ert + '[' + inner_pos[params['inner_pos']] + ']'
1665
1666             ert = ert + '{' + convert_len(params['width'],
1667                                           params['special']) + '}'
1668
1669             if params['use_parbox'] != '0':
1670                 ert = ert + '{'
1671             ert = ert + '\\let\\minipage\\lyxtolyxminipage%\n'
1672             ert = ert + '\\let\\endminipage\\endlyxtolyxminipage%\n'
1673
1674             old_i = i
1675             i = insert_ert(document.body, i, 'Collapsed', ert, document.format - 1, document.default_layout)
1676             j = j + i - old_i - 1
1677
1678             document.body[i:i] = ['\\begin_inset Minipage',
1679                               'position %d' % params['position'],
1680                               'inner_position 1',
1681                               'height "1in"',
1682                               'width "' + params['width'] + '"',
1683                               'collapsed ' + params['collapsed']]
1684             i = i + 6
1685             j = j + 6
1686
1687             # Restore the original minipage environment since we may have
1688             # minipages inside this box.
1689             # Start a new paragraph because the following may be nonstandard
1690             document.body[i:i] = ['\\layout %s' % document.default_layout, '', '']
1691             i = i + 2
1692             j = j + 3
1693             ert = '\\let\\minipage\\lyxtolyxrealminipage%\n'
1694             ert = ert + '\\let\\endminipage\\lyxtolyxrealendminipage%'
1695             old_i = i
1696             i = insert_ert(document.body, i, 'Collapsed', ert, document.format - 1, document.default_layout)
1697             j = j + i - old_i - 1
1698
1699             # Redefine the minipage end before the inset end.
1700             # Start a new paragraph because the previous may be nonstandard
1701             document.body[j:j] = ['\\layout %s' % document.default_layout, '', '']
1702             j = j + 2
1703             ert = '\\let\\endminipage\\endlyxtolyxminipage'
1704             j = insert_ert(document.body, j, 'Collapsed', ert, document.format - 1, document.default_layout)
1705             j = j + 1
1706             document.body.insert(j, '')
1707             j = j + 1
1708
1709             # LyX writes '%\n' after each box. Therefore we need to end our
1710             # ERT with '%\n', too, since this may swallow a following space.
1711             if params['use_parbox'] != '0':
1712                 ert = '}%\n'
1713             else:
1714                 ert = '\\end{lyxtolyxrealminipage}%\n'
1715             j = insert_ert(document.body, j, 'Collapsed', ert, document.format - 1, document.default_layout)
1716
1717             # We don't need to restore the original minipage after the inset
1718             # end because the scope of the redefinition is the original box.
1719
1720         else:
1721
1722             # Convert to minipage
1723             document.body[i:i] = ['\\begin_inset Minipage',
1724                               'position %d' % params['position'],
1725                               'inner_position %d' % params['inner_pos'],
1726                               'height "' + params['height'] + '"',
1727                               'width "' + params['width'] + '"',
1728                               'collapsed ' + params['collapsed']]
1729             i = i + 6
1730
1731
1732 def remove_branches(document):
1733     " Remove branches. "
1734     i = 0
1735     while True:
1736         i = find_token(document.header, "\\branch", i)
1737         if i == -1:
1738             break
1739         document.warning("Removing branch %s." % document.header[i].split()[1])
1740         j = find_token(document.header, "\\end_branch", i)
1741         if j == -1:
1742             document.warning("Malformed LyX document: Missing '\\end_branch'.")
1743             break
1744         del document.header[i:j+1]
1745
1746     i = 0
1747     while True:
1748         i = find_token(document.body, "\\begin_inset Branch", i)
1749         if i == -1:
1750             return
1751         j = find_end_of_inset(document.body, i)
1752         if j == -1:
1753             document.warning("Malformed LyX document: Missing '\\end_inset'.")
1754             i = i + 1
1755             continue
1756         del document.body[i]
1757         del document.body[j - 1]
1758         # Seach for a line starting 'collapsed'
1759         # If, however, we find a line starting '\layout'
1760         # (_always_ present) then break with a warning message
1761         collapsed_found = 0
1762         while True:
1763             if (document.body[i][:9] == "collapsed"):
1764                 del document.body[i]
1765                 collapsed_found = 1
1766                 continue
1767             elif (document.body[i][:7] == "\\layout"):
1768                 if collapsed_found == 0:
1769                     document.warning("Malformed LyX document: Missing 'collapsed'.")
1770                 # Delete this new paragraph, since it would not appear in
1771                 # .tex output. This avoids also empty paragraphs.
1772                 del document.body[i]
1773                 break
1774             i = i + 1
1775
1776
1777 def convert_jurabib(document):
1778     " Convert jurabib. "
1779     i = find_token(document.header, '\\use_numerical_citations', 0)
1780     if i == -1:
1781         document.warning("Malformed lyx document: Missing '\\use_numerical_citations'.")
1782         return
1783     document.header.insert(i + 1, '\\use_jurabib 0')
1784
1785
1786 def revert_jurabib(document):
1787     " Revert jurabib. "
1788     i = find_token(document.header, '\\use_jurabib', 0)
1789     if i == -1:
1790         document.warning("Malformed lyx document: Missing '\\use_jurabib'.")
1791         return
1792     if get_value(document.header, '\\use_jurabib', 0) != "0":
1793         document.warning("Conversion of '\\use_jurabib = 1' not yet implemented.")
1794         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1795         return
1796     del document.header[i]
1797
1798
1799 def convert_bibtopic(document):
1800     " Convert bibtopic. "
1801     i = find_token(document.header, '\\use_jurabib', 0)
1802     if i == -1:
1803         document.warning("Malformed lyx document: Missing '\\use_jurabib'.")
1804         return
1805     document.header.insert(i + 1, '\\use_bibtopic 0')
1806
1807
1808 def revert_bibtopic(document):
1809     " Revert bibtopic. "
1810     i = find_token(document.header, '\\use_bibtopic', 0)
1811     if i == -1:
1812         document.warning("Malformed lyx document: Missing '\\use_bibtopic'.")
1813         return
1814     if get_value(document.header, '\\use_bibtopic', 0) != "0":
1815         document.warning("Conversion of '\\use_bibtopic = 1' not yet implemented.")
1816         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1817     del document.header[i]
1818
1819
1820 def convert_float(document):
1821     " Convert sideway floats. "
1822     i = 0
1823     while True:
1824         i = find_token_exact(document.body, '\\begin_inset Float', i)
1825         if i == -1:
1826             return
1827         # Seach for a line starting 'wide'
1828         # If, however, we find a line starting '\begin_layout'
1829         # (_always_ present) then break with a warning message
1830         i = i + 1
1831         while True:
1832             if (document.body[i][:4] == "wide"):
1833                 document.body.insert(i + 1, 'sideways false')
1834                 break
1835             elif (document.body[i][:13] == "\\begin_layout"):
1836                 document.warning("Malformed lyx document: Missing 'wide'.")
1837                 break
1838             i = i + 1
1839         i = i + 1
1840
1841
1842 def revert_float(document):
1843     " Revert sideways floats. "
1844     i = 0
1845     while True:
1846         i = find_token_exact(document.body, '\\begin_inset Float', i)
1847         if i == -1:
1848             return
1849         line = document.body[i]
1850         r = re.compile(r'\\begin_inset Float (.*)$')
1851         m = r.match(line)
1852         floattype = m.group(1)
1853         if floattype != "figure" and floattype != "table":
1854             i = i + 1
1855             continue
1856         j = find_end_of_inset(document.body, i)
1857         if j == -1:
1858             document.warning("Malformed lyx document: Missing '\\end_inset'.")
1859             i = i + 1
1860             continue
1861         if get_value(document.body, 'sideways', i, j) != "false":
1862             l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
1863             if l == -1:
1864                 document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.")
1865                 return
1866             document.body[j] = '\\layout Standard\n\\begin_inset ERT\nstatus Collapsed\n\n' \
1867             '\\layout Standard\n\n\n\\backslash\n' \
1868             'end{sideways' + floattype + '}\n\n\\end_inset\n'
1869             del document.body[i+1:l-1]
1870             document.body[i] = '\\begin_inset ERT\nstatus Collapsed\n\n' \
1871             '\\layout Standard\n\n\n\\backslash\n' \
1872             'begin{sideways' + floattype + '}\n\n\\end_inset\n\n'
1873             add_to_preamble(document,
1874                             ['\\usepackage{rotfloat}\n'])
1875             i = i + 1
1876             continue
1877         del_token(document.body, 'sideways', i, j)
1878         i = i + 1
1879
1880
1881 def convert_graphics(document):
1882     """ Add extension to documentnames of insetgraphics if necessary.
1883     """
1884     i = 0
1885     while True:
1886         i = find_token(document.body, "\\begin_inset Graphics", i)
1887         if i == -1:
1888             return
1889
1890         j = find_token_exact(document.body, "documentname", i)
1891         if j == -1:
1892             return
1893         i = i + 1
1894         filename = document.body[j].split()[1]
1895         if document.dir == '' and not os.path.isabs(filename):
1896             # We don't know the directory and cannot check the document.
1897             # We could use a heuristic and take the current directory,
1898             # and we could try to find out if documentname has an extension,
1899             # but that would be just guesses and could be wrong.
1900             document.warning("""Warning: Cannot determine whether document
1901          %s
1902          needs an extension when reading from standard input.
1903          You may need to correct the document manually or run
1904          lyx2lyx again with the .lyx document as commandline argument.""" % filename)
1905             continue
1906         absname = os.path.normpath(os.path.join(document.dir, filename))
1907         # This needs to be the same algorithm as in pre 233 insetgraphics
1908         if access(absname, F_OK):
1909             continue
1910         if access(absname + ".ps", F_OK):
1911             document.body[j] = document.body[j].replace(filename, filename + ".ps")
1912             continue
1913         if access(absname + ".eps", F_OK):
1914             document.body[j] = document.body[j].replace(filename, filename + ".eps")
1915
1916
1917 def convert_names(document):
1918     """ Convert in the docbook backend from firstname and surname style
1919     to charstyles.
1920     """
1921     if document.backend != "docbook":
1922         return
1923
1924     i = 0
1925
1926     while True:
1927         i = find_token(document.body, "\\begin_layout Author", i)
1928         if i == -1:
1929             return
1930
1931         i = i + 1
1932         while document.body[i] == "":
1933             i = i + 1
1934
1935         if document.body[i][:11] != "\\end_layout" or document.body[i+2][:13] != "\\begin_deeper":
1936             i = i + 1
1937             continue
1938
1939         k = i
1940         i = find_end_of( document.body, i+3, "\\begin_deeper","\\end_deeper")
1941         if i == -1:
1942             # something is really wrong, abort
1943             document.warning("Missing \\end_deeper, after style Author.")
1944             document.warning("Aborted attempt to parse FirstName and Surname.")
1945             return
1946         firstname, surname = "", ""
1947
1948         name = document.body[k:i]
1949
1950         j = find_token(name, "\\begin_layout FirstName", 0)
1951         if j != -1:
1952             j = j + 1
1953             while(name[j] != "\\end_layout"):
1954                 firstname = firstname + name[j]
1955                 j = j + 1
1956
1957         j = find_token(name, "\\begin_layout Surname", 0)
1958         if j != -1:
1959             j = j + 1
1960             while(name[j] != "\\end_layout"):
1961                 surname = surname + name[j]
1962                 j = j + 1
1963
1964         # delete name
1965         del document.body[k+2:i+1]
1966
1967         document.body[k-1:k-1] = ["", "",
1968                           "\\begin_inset CharStyle Firstname",
1969                           "status inlined",
1970                           "",
1971                           '\\begin_layout %s' % document.default_layout,
1972                           "",
1973                           "%s" % firstname,
1974                           r"\end_layout",
1975                           "",
1976                           r"\end_inset",
1977                           "",
1978                           "",
1979                           "\\begin_inset CharStyle Surname",
1980                           "status inlined",
1981                           "",
1982                           '\\begin_layout %s' % document.default_layout,
1983                           "",
1984                           "%s" % surname,
1985                           "\\end_layout",
1986                           "",
1987                           "\\end_inset",
1988                           ""]
1989
1990
1991 def revert_names(document):
1992     """ Revert in the docbook backend from firstname and surname char style
1993     to styles.
1994     """
1995     if document.backend != "docbook":
1996         return
1997
1998
1999 def convert_cite_engine(document):
2000     r""" \use_natbib 1                       \cite_engine <style>
2001          \use_numerical_citations 0     ->   where <style> is one of
2002          \use_jurabib 0                      "basic", "natbib_authoryear","""
2003
2004     a = find_token(document.header, "\\use_natbib", 0)
2005     if a == -1:
2006         document.warning("Malformed lyx document: Missing '\\use_natbib'.")
2007         return
2008
2009     b = find_token(document.header, "\\use_numerical_citations", 0)
2010     if b == -1 or b != a+1:
2011         document.warning("Malformed lyx document: Missing '\\use_numerical_citations'.")
2012         return
2013
2014     c = find_token(document.header, "\\use_jurabib", 0)
2015     if c == -1 or c != b+1:
2016         document.warning("Malformed lyx document: Missing '\\use_jurabib'.")
2017         return
2018
2019     use_natbib = int(document.header[a].split()[1])
2020     use_numerical_citations = int(document.header[b].split()[1])
2021     use_jurabib = int(document.header[c].split()[1])
2022
2023     cite_engine = "basic"
2024     if use_natbib:
2025         if use_numerical_citations:
2026             cite_engine = "natbib_numerical"
2027         else:
2028              cite_engine = "natbib_authoryear"
2029     elif use_jurabib:
2030         cite_engine = "jurabib"
2031
2032     del document.header[a:c+1]
2033     document.header.insert(a, "\\cite_engine " + cite_engine)
2034
2035
2036 def revert_cite_engine(document):
2037     " Revert the cite engine. "
2038     i = find_token(document.header, "\\cite_engine", 0)
2039     if i == -1:
2040         document.warning("Malformed lyx document: Missing '\\cite_engine'.")
2041         return
2042
2043     cite_engine = document.header[i].split()[1]
2044
2045     use_natbib = '0'
2046     use_numerical = '0'
2047     use_jurabib = '0'
2048     if cite_engine == "natbib_numerical":
2049         use_natbib = '1'
2050         use_numerical = '1'
2051     elif cite_engine == "natbib_authoryear":
2052         use_natbib = '1'
2053     elif cite_engine == "jurabib":
2054         use_jurabib = '1'
2055
2056     del document.header[i]
2057     document.header.insert(i, "\\use_jurabib " + use_jurabib)
2058     document.header.insert(i, "\\use_numerical_citations " + use_numerical)
2059     document.header.insert(i, "\\use_natbib " + use_natbib)
2060
2061
2062 def convert_paperpackage(document):
2063     " Convert paper package. "
2064     i = find_token(document.header, "\\paperpackage", 0)
2065     if i == -1:
2066         return
2067
2068     packages = {'default':'none','a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
2069     if len(document.header[i].split()) > 1:
2070         paperpackage = document.header[i].split()[1]
2071         document.header[i] = document.header[i].replace(paperpackage, packages[paperpackage])
2072     else:
2073         document.header[i] = document.header[i] + ' widemarginsa4'
2074
2075
2076 def revert_paperpackage(document):
2077     " Revert paper package. "
2078     i = find_token(document.header, "\\paperpackage", 0)
2079     if i == -1:
2080         return
2081
2082     packages = {'none':'a4', 'a4':'a4wide', 'a4wide':'widemarginsa4',
2083                 'widemarginsa4':'', 'default': 'default'}
2084     if len(document.header[i].split()) > 1:
2085         paperpackage = document.header[i].split()[1]
2086     else:
2087         paperpackage = 'default'
2088     document.header[i] = document.header[i].replace(paperpackage, packages[paperpackage])
2089
2090
2091 def convert_bullets(document):
2092     " Convert bullets. "
2093     i = 0
2094     while True:
2095         i = find_token(document.header, "\\bullet", i)
2096         if i == -1:
2097             return
2098         if document.header[i][:12] == '\\bulletLaTeX':
2099             document.header[i] = document.header[i] + ' ' + document.header[i+1].strip()
2100             n = 3
2101         else:
2102             document.header[i] = document.header[i] + ' ' + document.header[i+1].strip() +\
2103                         ' ' + document.header[i+2].strip() + ' ' + document.header[i+3].strip()
2104             n = 5
2105         del document.header[i+1:i + n]
2106         i = i + 1
2107
2108
2109 def revert_bullets(document):
2110     " Revert bullets. "
2111     i = 0
2112     while True:
2113         i = find_token(document.header, "\\bullet", i)
2114         if i == -1:
2115             return
2116         if document.header[i][:12] == '\\bulletLaTeX':
2117             n = document.header[i].find('"')
2118             if n == -1:
2119                 document.warning("Malformed header.")
2120                 return
2121             else:
2122                 document.header[i:i+1] = [document.header[i][:n-1],'\t' + document.header[i][n:], '\\end_bullet']
2123             i = i + 3
2124         else:
2125             frag = document.header[i].split()
2126             if len(frag) != 5:
2127                 document.warning("Malformed header.")
2128                 return
2129             else:
2130                 document.header[i:i+1] = [frag[0] + ' ' + frag[1],
2131                                  '\t' + frag[2],
2132                                  '\t' + frag[3],
2133                                  '\t' + frag[4],
2134                                  '\\end_bullet']
2135                 i = i + 5
2136
2137
2138 def add_begin_header(document):
2139     r" Add \begin_header and \begin_document. "
2140     i = find_token(document.header, '\\lyxformat', 0)
2141     document.header.insert(i+1, '\\begin_header')
2142     document.header.insert(i+1, '\\begin_document')
2143
2144
2145 def remove_begin_header(document):
2146     r" Remove \begin_header and \begin_document. "
2147     i = find_token(document.header, "\\begin_document", 0)
2148     if i != -1:
2149         del document.header[i]
2150     i = find_token(document.header, "\\begin_header", 0)
2151     if i != -1:
2152         del document.header[i]
2153
2154
2155 def add_begin_body(document):
2156     r" Add and \begin_document and \end_document"
2157     document.body.insert(0, '\\begin_body')
2158     document.body.insert(1, '')
2159     i = find_token(document.body, "\\end_document", 0)
2160     document.body.insert(i, '\\end_body')
2161
2162 def remove_begin_body(document):
2163     r" Remove \begin_body and \end_body"
2164     i = find_token(document.body, "\\begin_body", 0)
2165     if i != -1:
2166         del document.body[i]
2167         if not document.body[i]:
2168             del document.body[i]
2169     i = find_token(document.body, "\\end_body", 0)
2170     if i != -1:
2171         del document.body[i]
2172
2173
2174 def normalize_papersize(document):
2175     r" Normalize \papersize"
2176     i = find_token(document.header, '\\papersize', 0)
2177     if i == -1:
2178         return
2179
2180     tmp = document.header[i].split()
2181     if tmp[1] == "Default":
2182         document.header[i] = '\\papersize default'
2183         return
2184     if tmp[1] == "Custom":
2185         document.header[i] = '\\papersize custom'
2186
2187
2188 def denormalize_papersize(document):
2189     r" Revert \papersize"
2190     i = find_token(document.header, '\\papersize', 0)
2191     if i == -1:
2192         return
2193
2194     tmp = document.header[i].split()
2195     if tmp[1] == "custom":
2196         document.header[i] = '\\papersize Custom'
2197
2198
2199 def strip_end_space(document):
2200     " Strip spaces at end of command line. "
2201     for i in range(len(document.body)):
2202         if document.body[i][:1] == '\\':
2203             document.body[i] = document.body[i].strip()
2204
2205
2206 def use_x_boolean(document):
2207     r" Use boolean values for \use_geometry, \use_bibtopic and \tracking_changes"
2208     bin2bool = {'0': 'false', '1': 'true'}
2209     for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
2210         i = find_token(document.header, use, 0)
2211         if i == -1:
2212             continue
2213         decompose = document.header[i].split()
2214         document.header[i] = decompose[0] + ' ' + bin2bool[decompose[1]]
2215
2216
2217 def use_x_binary(document):
2218     r" Use digit values for \use_geometry, \use_bibtopic and \tracking_changes"
2219     bool2bin = {'false': '0', 'true': '1'}
2220     for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
2221         i = find_token(document.header, use, 0)
2222         if i == -1:
2223             continue
2224         decompose = document.header[i].split()
2225         document.header[i] = decompose[0] + ' ' + bool2bin[decompose[1]]
2226
2227
2228 def normalize_paragraph_params(document):
2229     " Place all the paragraph parameters in their own line. "
2230     body = document.body
2231
2232     allowed_parameters = '\\paragraph_spacing', '\\noindent', \
2233                          '\\align', '\\labelwidthstring', "\\start_of_appendix", \
2234                          "\\leftindent"
2235
2236     i = 0
2237     while True:
2238         i = find_token(document.body, '\\begin_layout', i)
2239         if i == -1:
2240             return
2241
2242         i = i + 1
2243         while True:
2244             if body[i].strip() and body[i].split()[0] not in allowed_parameters:
2245                 break
2246
2247             j = body[i].find('\\', 1)
2248
2249             if j != -1:
2250                 body[i:i+1] = [body[i][:j].strip(), body[i][j:]]
2251
2252             i = i + 1
2253
2254
2255 def convert_output_changes (document):
2256     " Add output_changes parameter. "
2257     i = find_token(document.header, '\\tracking_changes', 0)
2258     if i == -1:
2259         document.warning("Malformed lyx document: Missing '\\tracking_changes'.")
2260         return
2261     document.header.insert(i+1, '\\output_changes true')
2262
2263
2264 def revert_output_changes (document):
2265     " Remove output_changes parameter. "
2266     i = find_token(document.header, '\\output_changes', 0)
2267     if i == -1:
2268         return
2269     del document.header[i]
2270
2271
2272 def convert_ert_paragraphs(document):
2273     " Convert paragraph breaks and sanitize paragraphs. "
2274     forbidden_settings = [
2275                           # paragraph parameters
2276                           '\\paragraph_spacing', '\\labelwidthstring',
2277                           '\\start_of_appendix', '\\noindent',
2278                           '\\leftindent', '\\align',
2279                           # font settings
2280                           '\\family', '\\series', '\\shape', '\\size',
2281                           '\\emph', '\\numeric', '\\bar', '\\noun',
2282                           '\\color', '\\lang']
2283     i = 0
2284     while True:
2285         i = find_token(document.body, '\\begin_inset ERT', i)
2286         if i == -1:
2287             return
2288         j = find_end_of_inset(document.body, i)
2289         if j == -1:
2290             document.warning("Malformed lyx document: Missing '\\end_inset'.")
2291             i = i + 1
2292             continue
2293
2294         # convert non-standard paragraphs to standard
2295         k = i
2296         while True:
2297             k = find_token(document.body, "\\begin_layout", k, j)
2298             if k == -1:
2299                 break
2300             document.body[k] = '\\begin_layout %s' % document.default_layout
2301             k = k + 1
2302
2303         # remove all paragraph parameters and font settings
2304         k = i
2305         while k < j:
2306             if (document.body[k].strip() and
2307                 document.body[k].split()[0] in forbidden_settings):
2308                 del document.body[k]
2309                 j = j - 1
2310             else:
2311                 k = k + 1
2312
2313         # insert an empty paragraph before each paragraph but the first
2314         k = i
2315         first_pagraph = 1
2316         while True:
2317             k = find_token(document.body, "\\begin_layout", k, j)
2318             if k == -1:
2319                 break
2320             if first_pagraph:
2321                 first_pagraph = 0
2322                 k = k + 1
2323                 continue
2324             document.body[k:k] = ['\\begin_layout %s' % document.default_layout, "",
2325                               "\\end_layout", ""]
2326             k = k + 5
2327             j = j + 4
2328
2329         # convert \\newline to new paragraph
2330         k = i
2331         while True:
2332             k = find_token(document.body, "\\newline", k, j)
2333             if k == -1:
2334                 break
2335             document.body[k:k+1] = ["\\end_layout", "", '\\begin_layout %s' % document.default_layout]
2336             k = k + 3
2337             j = j + 2
2338             # We need an empty line if document.default_layout == ''
2339             if document.body[k] != '':
2340                 document.body.insert(k, '')
2341                 k = k + 1
2342                 j = j + 1
2343         i = i + 1
2344
2345
2346 def revert_ert_paragraphs(document):
2347     " Remove double paragraph breaks. "
2348     i = 0
2349     while True:
2350         i = find_token(document.body, '\\begin_inset ERT', i)
2351         if i == -1:
2352             return
2353         j = find_end_of_inset(document.body, i)
2354         if j == -1:
2355             document.warning("Malformed lyx document: Missing '\\end_inset'.")
2356             i = i + 1
2357             continue
2358
2359         # replace paragraph breaks with \newline
2360         k = i
2361         while True:
2362             k = find_token(document.body, "\\end_layout", k, j)
2363             l = find_token(document.body, "\\begin_layout", k, j)
2364             if k == -1 or l == -1:
2365                 break
2366             document.body[k:l+1] = ["\\newline"]
2367             j = j - l + k
2368             k = k + 1
2369
2370         # replace double \newlines with paragraph breaks
2371         k = i
2372         while True:
2373             k = find_token(document.body, "\\newline", k, j)
2374             if k == -1:
2375                 break
2376             l = k + 1
2377             while document.body[l] == "":
2378                 l = l + 1
2379             if document.body[l].strip() and document.body[l].split()[0] == "\\newline":
2380                 document.body[k:l+1] = ["\\end_layout", "",
2381                                     '\\begin_layout %s' % document.default_layout]
2382                 j = j - l + k + 2
2383                 k = k + 3
2384                 # We need an empty line if document.default_layout == ''
2385                 if document.body[l+1] != '':
2386                     document.body.insert(l+1, '')
2387                     k = k + 1
2388                     j = j + 1
2389             else:
2390                 k = k + 1
2391         i = i + 1
2392
2393
2394 def convert_french(document):
2395     " Convert frenchb. "
2396     regexp = re.compile(r'^\\language\s+frenchb')
2397     i = find_re(document.header, regexp, 0)
2398     if i != -1:
2399         document.header[i] = "\\language french"
2400
2401     # Change language in the document body
2402     regexp = re.compile(r'^\\lang\s+frenchb')
2403     i = 0
2404     while True:
2405         i = find_re(document.body, regexp, i)
2406         if i == -1:
2407             break
2408         document.body[i] = "\\lang french"
2409         i = i + 1
2410
2411
2412 def remove_paperpackage(document):
2413     " Remove paper package. "
2414     i = find_token(document.header, '\\paperpackage', 0)
2415
2416     if i == -1:
2417         return
2418
2419     paperpackage = document.header[i].split()[1]
2420
2421     del document.header[i]
2422
2423     if paperpackage not in ("a4", "a4wide", "widemarginsa4"):
2424         return
2425
2426     conv = {"a4":"\\usepackage{a4}","a4wide": "\\usepackage{a4wide}",
2427             "widemarginsa4": "\\usepackage[widemargins]{a4}"}
2428     # for compatibility we ensure it is the first entry in preamble
2429     document.preamble[0:0] = [conv[paperpackage]]
2430
2431     i = find_token(document.header, '\\papersize', 0)
2432     if i != -1:
2433         document.header[i] = "\\papersize default"
2434
2435
2436 def remove_quotestimes(document):
2437     " Remove quotestimes. "
2438     i = find_token(document.header, '\\quotes_times', 0)
2439     if i == -1:
2440         return
2441     del document.header[i]
2442
2443
2444 def convert_sgml_paragraphs(document):
2445     " Convert SGML paragraphs. "
2446     if document.backend != "docbook":
2447         return
2448
2449     i = 0
2450     while True:
2451         i = find_token(document.body, "\\begin_layout SGML", i)
2452
2453         if i == -1:
2454             return
2455
2456         document.body[i] = "\\begin_layout Standard"
2457         j = find_token(document.body, "\\end_layout", i)
2458
2459         document.body[j+1:j+1] = ['','\\end_inset','','','\\end_layout']
2460         document.body[i+1:i+1] = ['\\begin_inset ERT','status inlined','','\\begin_layout Standard','']
2461
2462         i = i + 10
2463
2464 ##
2465 # Conversion hub
2466 #
2467
2468 supported_versions = ["1.4.%d" % i for i in range(3)] + ["1.4"]
2469 convert = [[222, [insert_tracking_changes, add_end_header, convert_amsmath]],
2470            [223, [remove_color_default, convert_spaces, convert_bibtex, remove_insetparent]],
2471            [224, [convert_external, convert_comment]],
2472            [225, [add_end_layout, layout2begin_layout, convert_end_document,
2473                   convert_table_valignment_middle, convert_breaks]],
2474            [226, [convert_note]],
2475            [227, [convert_box]],
2476            [228, [convert_collapsible, convert_ert]],
2477            [229, [convert_minipage]],
2478            [230, [convert_jurabib]],
2479            [231, [convert_float]],
2480            [232, [convert_bibtopic]],
2481            [233, [convert_graphics, convert_names]],
2482            [234, [convert_cite_engine]],
2483            [235, [convert_paperpackage]],
2484            [236, [convert_bullets, add_begin_header, add_begin_body,
2485                   normalize_papersize, strip_end_space]],
2486            [237, [use_x_boolean]],
2487            [238, [update_latexaccents]],
2488            [239, [normalize_paragraph_params]],
2489            [240, [convert_output_changes]],
2490            [241, [convert_ert_paragraphs]],
2491            [242, [convert_french]],
2492            [243, [remove_paperpackage]],
2493            [244, [rename_spaces]],
2494            [245, [remove_quotestimes, convert_sgml_paragraphs]]]
2495
2496 revert =  [[244, []],
2497            [243, [revert_space_names]],
2498            [242, []],
2499            [241, []],
2500            [240, [revert_ert_paragraphs]],
2501            [239, [revert_output_changes]],
2502            [238, []],
2503            [237, []],
2504            [236, [use_x_binary]],
2505            [235, [denormalize_papersize, remove_begin_body,remove_begin_header,
2506                   revert_bullets]],
2507            [234, [revert_paperpackage]],
2508            [233, [revert_cite_engine]],
2509            [232, [revert_names]],
2510            [231, [revert_bibtopic]],
2511            [230, [revert_float]],
2512            [229, [revert_jurabib]],
2513            [228, []],
2514            [227, [revert_collapsible, revert_ert]],
2515            [226, [revert_box, revert_external_2]],
2516            [225, [revert_note]],
2517            [224, [rm_end_layout, begin_layout2layout, revert_end_document,
2518                   revert_valignment_middle, revert_breaks, convert_frameless_box,
2519                   remove_branches]],
2520            [223, [revert_external_2, revert_comment, revert_eqref]],
2521            [222, [revert_spaces, revert_bibtex]],
2522            [221, [revert_amsmath, rm_end_header, rm_tracking_changes, rm_body_changes]]]
2523
2524
2525 if __name__ == "__main__":
2526     pass