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