]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx_1_4.py
fix bug 2031 (\lyxline conversion)
[features.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 ERT
510 #\begin layout Standard
511 #\backslash
512 #vspace{-1\backslash
513 #parskip}
514 #\end_layout
515 #\end_inset
516 #
517 #\begin_inset VSpace xxx
518 #\end_inset
519 #
520 #0
521 #
522 #\begin_inset VSpace xxx
523 #\end_inset
524 #\lyxline
525 #
526 #\newpage
527 #
528 #\end_layout
529 def convert_breaks(file):
530     par_params = ('added_space_bottom', 'added_space_top', 'align',
531                  'labelwidthstring', 'line_bottom', 'line_top', 'noindent',
532                  'pagebreak_bottom', 'pagebreak_top', 'paragraph_spacing',
533                  'start_of_appendix')
534     font_attributes = ['\\family', '\\series', '\\shape', '\\emph',
535                        '\\numeric', '\\bar', '\\noun', '\\color', '\\lang']
536     attribute_values = ['default', 'default', 'default', 'default',
537                         'default', 'default', 'default', 'none', file.language]
538     i = 0
539     while 1:
540         i = find_token(file.body, "\\begin_layout", i)
541         if i == -1:
542             return
543         layout = split(file.body[i])[1]
544         i = i + 1
545
546         # Merge all paragraph parameters into a single line
547         # We cannot check for '\\' only because paragraphs may start e.g.
548         # with '\\backslash'
549         while file.body[i + 1][:1] == '\\' and split(file.body[i + 1][1:])[0] in par_params:
550             file.body[i] = file.body[i + 1] + ' ' + file.body[i]
551             del file.body[i+1]
552
553         line_top   = find(file.body[i],"\\line_top")
554         line_bot   = find(file.body[i],"\\line_bottom")
555         pb_top     = find(file.body[i],"\\pagebreak_top")
556         pb_bot     = find(file.body[i],"\\pagebreak_bottom")
557         vspace_top = find(file.body[i],"\\added_space_top")
558         vspace_bot = find(file.body[i],"\\added_space_bottom")
559
560         if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
561             continue
562
563         # Do we have a nonstandard paragraph? We need to create new paragraphs
564         # if yes to avoid putting lyxline etc. inside of special environments.
565         # This is wrong for itemize and enumerate environments, but it is
566         # impossible to convert these correctly.
567         # We want to avoid new paragraphs if possible becauase we want to
568         # inherit font sizes.
569         nonstandard = 0
570         if (layout != "Standard" or find(file.body[i],"\\align") != -1 or
571             find(file.body[i],"\\labelwidthstring") != -1 or
572             find(file.body[i],"\\noindent") != -1):
573             nonstandard = 1
574
575         # get the font size of the beginning of this paragraph, since we need
576         # it for the lyxline inset
577         j = i + 1
578         while not is_nonempty_line(file.body[j]):
579             j = j + 1
580         size_top = ""
581         if find(file.body[j], "\\size") != -1:
582             size_top = split(file.body[j])[1]
583
584         for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
585             file.body[i] = replace(file.body[i], tag, "")
586
587         if vspace_top != -1:
588             # the position could be change because of the removal of other
589             # paragraph properties above
590             vspace_top = find(file.body[i],"\\added_space_top")
591             tmp_list = split(file.body[i][vspace_top:])
592             vspace_top_value = tmp_list[1]
593             file.body[i] = file.body[i][:vspace_top] + join(tmp_list[2:])
594
595         if vspace_bot != -1:
596             # the position could be change because of the removal of other
597             # paragraph properties above
598             vspace_bot = find(file.body[i],"\\added_space_bottom")
599             tmp_list = split(file.body[i][vspace_bot:])
600             vspace_bot_value = tmp_list[1]
601             file.body[i] = file.body[i][:vspace_bot] + join(tmp_list[2:])
602
603         file.body[i] = strip(file.body[i])
604         i = i + 1
605
606         # Create an empty paragraph or paragraph fragment for line and
607         # page break that belong above the paragraph
608         if pb_top !=-1 or line_top != -1 or vspace_top != -1:
609
610             paragraph_above = list()
611             if nonstandard:
612                 # We need to create an extra paragraph for nonstandard environments
613                 paragraph_above = ['\\begin_layout Standard', '']
614
615             if pb_top != -1:
616                 paragraph_above.extend(['\\newpage ',''])
617
618             if vspace_top != -1:
619                 paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset','',''])
620
621             if line_top != -1:
622                 if size_top != '':
623                     paragraph_above.extend(['\\size ' + size_top + ' '])
624                 # We need an additional vertical space of -\parskip.
625                 # We can't use the vspace inset because it does not know \parskip.
626                 paragraph_above.extend(['\\lyxline ', '', ''])
627                 insert_ert(paragraph_above, len(paragraph_above) - 1, 'Collapsed',
628                            '\\vspace{-1\\parskip}\n', file.format + 1)
629                 paragraph_above.extend([''])
630
631             if nonstandard:
632                 paragraph_above.extend(['\\end_layout ',''])
633                 # insert new paragraph above the current paragraph
634                 file.body[i-2:i-2] = paragraph_above
635             else:
636                 # insert new lines at the beginning of the current paragraph
637                 file.body[i:i] = paragraph_above
638
639             i = i + len(paragraph_above)
640
641         # Ensure that nested style are converted later.
642         k = find_end_of(file.body, i, "\\begin_layout", "\\end_layout")
643
644         if k == -1:
645             return
646
647         if pb_bot !=-1 or line_bot != -1 or vspace_bot != -1:
648
649             # get the font size of the end of this paragraph
650             size_bot = size_top
651             j = i + 1
652             while j < k:
653                 if find(file.body[j], "\\size") != -1:
654                     size_bot = split(file.body[j])[1]
655                     j = j + 1
656                 elif find(file.body[j], "\\begin_inset") != -1:
657                     # skip insets
658                     j = find_end_of_inset(file.body, j)
659                 else:
660                     j = j + 1
661
662             paragraph_below = list()
663             if nonstandard:
664                 # We need to create an extra paragraph for nonstandard environments
665                 paragraph_below = ['', '\\begin_layout Standard', '']
666             else:
667                 for a in range(len(font_attributes)):
668                     if find_token(file.body, font_attributes[a], i, k) != -1:
669                         paragraph_below.extend([font_attributes[a] + ' ' + attribute_values[a]])
670
671             if line_bot != -1:
672                 if nonstandard and size_bot != '':
673                     paragraph_below.extend(['\\size ' + size_bot + ' '])
674                 paragraph_below.extend(['\\lyxline ',''])
675                 if size_bot != '':
676                     paragraph_below.extend(['\\size default '])
677
678             if vspace_bot != -1:
679                 paragraph_below.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
680
681             if pb_bot != -1:
682                 paragraph_below.extend(['\\newpage ',''])
683
684             if nonstandard:
685                 paragraph_below.extend(['\\end_layout '])
686                 # insert new paragraph below the current paragraph
687                 file.body[k+1:k+1] = paragraph_below
688             else:
689                 # insert new lines at the end of the current paragraph
690                 file.body[k:k] = paragraph_below
691
692
693 ##
694 #  Notes
695 #
696 def convert_note(file):
697     i = 0
698     while 1:
699         i = find_tokens(file.body, ["\\begin_inset Note",
700                                 "\\begin_inset Comment",
701                                 "\\begin_inset Greyedout"], i)
702         if i == -1:
703             break
704
705         file.body[i] = file.body[i][0:13] + 'Note ' + file.body[i][13:]
706         i = i + 1
707
708
709 def revert_note(file):
710     note_header = "\\begin_inset Note "
711     i = 0
712     while 1:
713         i = find_token(file.body, note_header, i)
714         if i == -1:
715             break
716
717         file.body[i] = "\\begin_inset " + file.body[i][len(note_header):]
718         i = i + 1
719
720
721 ##
722 # Box
723 #
724 def convert_box(file):
725     i = 0
726     while 1:
727         i = find_tokens(file.body, ["\\begin_inset Boxed",
728                                 "\\begin_inset Doublebox",
729                                 "\\begin_inset Frameless",
730                                 "\\begin_inset ovalbox",
731                                 "\\begin_inset Ovalbox",
732                                 "\\begin_inset Shadowbox"], i)
733         if i == -1:
734             break
735
736         file.body[i] = file.body[i][0:13] + 'Box ' + file.body[i][13:]
737         i = i + 1
738
739
740 def revert_box(file):
741     box_header = "\\begin_inset Box "
742     i = 0
743     while 1:
744         i = find_token(file.body, box_header, i)
745         if i == -1:
746             break
747
748         file.body[i] = "\\begin_inset " + file.body[i][len(box_header):]
749         i = i + 1
750
751
752 ##
753 # Collapse
754 #
755 def convert_collapsable(file):
756     i = 0
757     while 1:
758         i = find_tokens(file.body, ["\\begin_inset Box",
759                                 "\\begin_inset Branch",
760                                 "\\begin_inset CharStyle",
761                                 "\\begin_inset Float",
762                                 "\\begin_inset Foot",
763                                 "\\begin_inset Marginal",
764                                 "\\begin_inset Note",
765                                 "\\begin_inset OptArg",
766                                 "\\begin_inset Wrap"], i)
767         if i == -1:
768             break
769
770         # Seach for a line starting 'collapsed'
771         # If, however, we find a line starting '\begin_layout'
772         # (_always_ present) then break with a warning message
773         i = i + 1
774         while 1:
775             if (file.body[i] == "collapsed false"):
776                 file.body[i] = "status open"
777                 break
778             elif (file.body[i] == "collapsed true"):
779                 file.body[i] = "status collapsed"
780                 break
781             elif (file.body[i][:13] == "\\begin_layout"):
782                 file.warning("Malformed LyX file: Missing 'collapsed'.")
783                 break
784             i = i + 1
785
786         i = i + 1
787
788
789 def revert_collapsable(file):
790     i = 0
791     while 1:
792         i = find_tokens(file.body, ["\\begin_inset Box",
793                                 "\\begin_inset Branch",
794                                 "\\begin_inset CharStyle",
795                                 "\\begin_inset Float",
796                                 "\\begin_inset Foot",
797                                 "\\begin_inset Marginal",
798                                 "\\begin_inset Note",
799                                 "\\begin_inset OptArg",
800                                 "\\begin_inset Wrap"], i)
801         if i == -1:
802             break
803
804         # Seach for a line starting 'status'
805         # If, however, we find a line starting '\begin_layout'
806         # (_always_ present) then break with a warning message
807         i = i + 1
808         while 1:
809             if (file.body[i] == "status open"):
810                 file.body[i] = "collapsed false"
811                 break
812             elif (file.body[i] == "status collapsed" or
813                   file.body[i] == "status inlined"):
814                 file.body[i] = "collapsed true"
815                 break
816             elif (file.body[i][:13] == "\\begin_layout"):
817                 file.warning("Malformed LyX file: Missing 'status'.")
818                 break
819             i = i + 1
820
821         i = i + 1
822
823
824 ##
825 #  ERT
826 #
827 def convert_ert(file):
828     i = 0
829     while 1:
830         i = find_token(file.body, "\\begin_inset ERT", i)
831         if i == -1:
832             break
833
834         # Seach for a line starting 'status'
835         # If, however, we find a line starting '\begin_layout'
836         # (_always_ present) then break with a warning message
837         i = i + 1
838         while 1:
839             if (file.body[i] == "status Open"):
840                 file.body[i] = "status open"
841                 break
842             elif (file.body[i] == "status Collapsed"):
843                 file.body[i] = "status collapsed"
844                 break
845             elif (file.body[i] == "status Inlined"):
846                 file.body[i] = "status inlined"
847                 break
848             elif (file.body[i][:13] == "\\begin_layout"):
849                 file.warning("Malformed LyX file: Missing 'status'.")
850                 break
851             i = i + 1
852
853         i = i + 1
854
855
856 def revert_ert(file):
857     i = 0
858     while 1:
859         i = find_token(file.body, "\\begin_inset ERT", i)
860         if i == -1:
861             break
862
863         # Seach for a line starting 'status'
864         # If, however, we find a line starting '\begin_layout'
865         # (_always_ present) then break with a warning message
866         i = i + 1
867         while 1:
868             if (file.body[i] == "status open"):
869                 file.body[i] = "status Open"
870                 break
871             elif (file.body[i] == "status collapsed"):
872                 file.body[i] = "status Collapsed"
873                 break
874             elif (file.body[i] == "status inlined"):
875                 file.body[i] = "status Inlined"
876                 break
877             elif (file.body[i][:13] == "\\begin_layout"):
878                 file.warning("Malformed LyX file : Missing 'status'.")
879                 break
880             i = i + 1
881
882         i = i + 1
883
884
885 ##
886 # Minipages
887 #
888 def convert_minipage(file):
889     """ Convert minipages to the box inset.
890     We try to use the same order of arguments as lyx does.
891     """
892     pos = ["t","c","b"]
893     inner_pos = ["c","t","b","s"]
894
895     i = 0
896     while 1:
897         i = find_token(file.body, "\\begin_inset Minipage", i)
898         if i == -1:
899             return
900
901         file.body[i] = "\\begin_inset Box Frameless"
902         i = i + 1
903
904         # convert old to new position using the pos list
905         if file.body[i][:8] == "position":
906             file.body[i] = 'position "%s"' % pos[int(file.body[i][9])]
907         else:
908             file.body.insert(i, 'position "%s"' % pos[0])
909         i = i + 1
910
911         file.body.insert(i, 'hor_pos "c"')
912         i = i + 1
913         file.body.insert(i, 'has_inner_box 1')
914         i = i + 1
915
916         # convert the inner_position
917         if file.body[i][:14] == "inner_position":
918             file.body[i] = 'inner_pos "%s"' %  inner_pos[int(file.body[i][15])]
919         else:
920             file.body.insert('inner_pos "%s"' % inner_pos[0])
921         i = i + 1
922
923         # We need this since the new file format has a height and width
924         # in a different order.
925         if file.body[i][:6] == "height":
926             height = file.body[i][6:]
927             # test for default value of 221 and convert it accordingly
928             if height == ' "0pt"' or height == ' "0"':
929                 height = ' "1pt"'
930             del file.body[i]
931         else:
932             height = ' "1pt"'
933
934         if file.body[i][:5] == "width":
935             width = file.body[i][5:]
936             del file.body[i]
937         else:
938             width = ' "0"'
939
940         if file.body[i][:9] == "collapsed":
941             if file.body[i][9:] == "true":
942                 status = "collapsed"
943             else:
944                 status = "open"
945             del file.body[i]
946         else:
947             status = "collapsed"
948
949         file.body.insert(i, 'use_parbox 0')
950         i = i + 1
951         file.body.insert(i, 'width' + width)
952         i = i + 1
953         file.body.insert(i, 'special "none"')
954         i = i + 1
955         file.body.insert(i, 'height' + height)
956         i = i + 1
957         file.body.insert(i, 'height_special "totalheight"')
958         i = i + 1
959         file.body.insert(i, 'status ' + status)
960         i = i + 1
961
962
963 # -------------------------------------------------------------------------------------------
964 # Convert backslashes and '\n' into valid ERT code, append the converted
965 # text to body[i] and return the (maybe incremented) line index i
966 def convert_ertbackslash(body, i, ert, format):
967     for c in ert:
968         if c == '\\':
969             body[i] = body[i] + '\\backslash '
970             i = i + 1
971             body.insert(i, '')
972         elif c == '\n':
973             if format <= 240:
974                 body[i+1:i+1] = ['\\newline ', '']
975                 i = i + 2
976             else:
977                 body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout Standard', '']
978                 i = i + 4
979         else:
980             body[i] = body[i] + c
981     return i
982
983
984 # Converts lines in ERT code to LaTeX
985 # The surrounding \begin_layout ... \end_layout pair must not be included
986 def ert2latex(lines, format):
987     backslash = re.compile(r'\\backslash\s*$')
988     newline = re.compile(r'\\newline\s*$')
989     if format <= 224:
990         begin_layout = re.compile(r'\\layout\s*\S+$')
991     else:
992         begin_layout = re.compile(r'\\begin_layout\s*\S+$')
993     end_layout = re.compile(r'\\end_layout\s*$')
994     ert = ''
995     for i in range(len(lines)):
996         line = backslash.sub('\\\\', lines[i])
997         if format <= 240:
998             if begin_layout.match(line):
999                 line = '\n\n'
1000             else:
1001                 line = newline.sub('\n', line)
1002         else:
1003             if begin_layout.match(line):
1004                 line = '\n'
1005         if format > 224 and end_layout.match(line):
1006             line = ''
1007         ert = ert + line
1008     return ert
1009
1010
1011 # get all paragraph parameters. They can be all on one line or on several lines.
1012 # lines[i] must be the first parameter line
1013 def get_par_params(lines, i):
1014     par_params = ('added_space_bottom', 'added_space_top', 'align',
1015                  'labelwidthstring', 'line_bottom', 'line_top', 'noindent',
1016                  'pagebreak_bottom', 'pagebreak_top', 'paragraph_spacing',
1017                  'start_of_appendix')
1018     # We cannot check for '\\' only because paragraphs may start e.g.
1019     # with '\\backslash'
1020     params = ''
1021     while lines[i][:1] == '\\' and split(lines[i][1:])[0] in par_params:
1022         params = params + ' ' + strip(lines[i])
1023         i = i + 1
1024     return strip(params)
1025
1026
1027 # convert LyX font size to LaTeX fontsize
1028 def lyxsize2latexsize(lyxsize):
1029     sizes = {"tiny" : "tiny", "scriptsize" : "scriptsize",
1030              "footnotesize" : "footnotesize", "small" : "small",
1031              "normal" : "normalsize", "large" : "large", "larger" : "Large",
1032              "largest" : "LARGE", "huge" : "huge", "giant" : "Huge"}
1033     if lyxsize in sizes:
1034         return '\\' + sizes[lyxsize]
1035     return ''
1036
1037
1038 # Change vspace insets, page breaks and lyxlines to paragraph options
1039 # (if possible) or ERT
1040 def revert_breaks(file):
1041
1042     # Get default spaceamount
1043     i = find_token(file.header, '\\defskip', 0)
1044     if i == -1:
1045         defskipamount = 'medskip'
1046     else:
1047         defskipamount = split(file.header[i])[1]
1048
1049     keys = {"\\begin_inset" : "vspace", "\\lyxline" : "lyxline",
1050             "\\newpage" : "newpage"}
1051     keywords_top = {"vspace" : "\\added_space_top", "lyxline" : "\\line_top",
1052                     "newpage" : "\\pagebreak_top"}
1053     keywords_bot = {"vspace" : "\\added_space_bottom", "lyxline" : "\\line_bottom",
1054                     "newpage" : "\\pagebreak_bottom"}
1055     tokens = ["\\begin_inset VSpace", "\\lyxline", "\\newpage"]
1056
1057     # Convert the insets
1058     i = 0
1059     while 1:
1060         i = find_tokens(file.body, tokens, i)
1061         if i == -1:
1062             return
1063
1064         # Are we at the beginning of a paragraph?
1065         paragraph_start = 1
1066         this_par = get_paragraph(file.body, i, file.format - 1)
1067         start = this_par + 1
1068         params = get_par_params(file.body, start)
1069         size = "normal"
1070         # Paragraph parameters may be on one or more lines.
1071         # Find the start of the real paragraph text.
1072         while file.body[start][:1] == '\\' and split(file.body[start])[0] in params:
1073             start = start + 1
1074         for k in range(start, i):
1075             if find(file.body[k], "\\size") != -1:
1076                 # store font size
1077                 size = split(file.body[k])[1]
1078             elif is_nonempty_line(file.body[k]):
1079                 paragraph_start = 0
1080                 break
1081         # Find the end of the real paragraph text.
1082         next_par = get_next_paragraph(file.body, i, file.format - 1)
1083         if next_par == -1:
1084             file.warning("Malformed LyX file: Missing next paragraph.")
1085             i = i + 1
1086             continue
1087
1088         # first line of our insets
1089         inset_start = i
1090         # last line of our insets
1091         inset_end = inset_start
1092         # Are we at the end of a paragraph?
1093         paragraph_end = 1
1094         # start and end line numbers to delete if we convert this inset
1095         del_lines = list()
1096         # is this inset a lyxline above a paragraph?
1097         top = list()
1098         # raw inset information
1099         lines = list()
1100         # name of this inset
1101         insets = list()
1102         # font size of this inset
1103         sizes = list()
1104
1105         # Detect subsequent lyxline, vspace and pagebreak insets created by convert_breaks()
1106         n = 0
1107         k = inset_start
1108         while k < next_par:
1109             if find_tokens(file.body, tokens, k) == k:
1110                 # inset to convert
1111                 lines.append(split(file.body[k]))
1112                 insets.append(keys[lines[n][0]])
1113                 del_lines.append([k, k])
1114                 top.append(0)
1115                 sizes.append(size)
1116                 n = n + 1
1117                 inset_end = k
1118             elif find(file.body[k], "\\size") != -1:
1119                 # store font size
1120                 size = split(file.body[k])[1]
1121             elif find_token(file.body, "\\begin_inset ERT", k) == k:
1122                 ert_begin = find_token(file.body, "\\layout", k) + 1
1123                 if ert_begin == 0:
1124                     file.warning("Malformed LyX file: Missing '\\layout'.")
1125                     continue
1126                 ert_end = find_end_of_inset(file.body, k)
1127                 if ert_end == -1:
1128                     file.warning("Malformed LyX file: Missing '\\end_inset'.")
1129                     continue
1130                 ert = ert2latex(file.body[ert_begin:ert_end], file.format - 1)
1131                 if (n > 0 and insets[n - 1] == "lyxline" and
1132                     ert == '\\vspace{-1\\parskip}\n'):
1133                     # vspace ERT created by convert_breaks() for top lyxline
1134                     top[n - 1] = 1
1135                     del_lines[n - 1][1] = ert_end
1136                     inset_end = ert_end
1137                     k = ert_end
1138                 else:
1139                     paragraph_end = 0
1140                     break
1141             elif (n > 0 and insets[n - 1] == "vspace" and
1142                   find_token(file.body, "\\end_inset", k) == k):
1143                 # ignore end of vspace inset
1144                 del_lines[n - 1][1] = k
1145                 inset_end = k
1146             elif is_nonempty_line(file.body[k]):
1147                 paragraph_end = 0
1148                 break
1149             k = k + 1
1150
1151         # Determine space amount for vspace insets
1152         spaceamount = list()
1153         arguments = list()
1154         for k in range(n):
1155             if insets[k] == "vspace":
1156                 spaceamount.append(lines[k][2])
1157                 arguments.append(' ' + spaceamount[k] + ' ')
1158             else:
1159                 spaceamount.append('')
1160                 arguments.append(' ')
1161
1162         # Can we convert to top paragraph parameters?
1163         before = 0
1164         if ((n == 3 and insets[0] == "newpage" and insets[1] == "vspace" and
1165              insets[2] == "lyxline" and top[2]) or
1166             (n == 2 and
1167              ((insets[0] == "newpage" and insets[1] == "vspace") or
1168               (insets[0] == "newpage" and insets[1] == "lyxline" and top[1]) or
1169               (insets[0] == "vspace"  and insets[1] == "lyxline" and top[1]))) or
1170             (n == 1 and insets[0] == "lyxline" and top[0])):
1171             # These insets have been created before a paragraph by
1172             # convert_breaks()
1173             before = 1
1174
1175         # Can we convert to bottom paragraph parameters?
1176         after = 0
1177         if ((n == 3 and insets[0] == "lyxline" and not top[0] and
1178              insets[1] == "vspace" and insets[2] == "newpage") or
1179             (n == 2 and
1180              ((insets[0] == "lyxline" and not top[0] and insets[1] == "vspace") or
1181               (insets[0] == "lyxline" and not top[0] and insets[1] == "newpage") or
1182               (insets[0] == "vspace"  and insets[1] == "newpage"))) or
1183             (n == 1 and insets[0] == "lyxline" and not top[0])):
1184             # These insets have been created after a paragraph by
1185             # convert_breaks()
1186             after = 1
1187
1188         if paragraph_start and paragraph_end:
1189             # We are in a paragraph of our own.
1190             # We must not delete this paragraph if it has parameters
1191             if params == '':
1192                 # First try to merge with the previous paragraph.
1193                 # We try the previous paragraph first because we would
1194                 # otherwise need ERT for two subsequent vspaces.
1195                 prev_par = get_paragraph(file.body, this_par - 1, file.format - 1) + 1
1196                 if prev_par > 0 and not before:
1197                     prev_params = get_par_params(file.body, prev_par + 1)
1198                     ert = 0
1199                     # determine font size
1200                     prev_size = "normal"
1201                     k = prev_par + 1
1202                     while file.body[k][:1] == '\\' and split(file.body[k])[0] in prev_params:
1203                         k = k + 1
1204                     while k < this_par:
1205                         if find(file.body[k], "\\size") != -1:
1206                             prev_size = split(file.body[k])[1]
1207                             break
1208                         elif find(file.body[k], "\\begin_inset") != -1:
1209                             # skip insets
1210                             k = find_end_of_inset(file.body, k)
1211                         elif is_nonempty_line(file.body[k]):
1212                             break
1213                         k = k + 1
1214                     for k in range(n):
1215                         if (keywords_bot[insets[k]] in prev_params or
1216                             (insets[k] == "lyxline" and sizes[k] != prev_size)):
1217                             ert = 1
1218                             break
1219                     if not ert:
1220                         for k in range(n):
1221                             file.body.insert(prev_par + 1,
1222                                              keywords_bot[insets[k]] + arguments[k])
1223                         del file.body[this_par+n:next_par-1+n]
1224                         i = this_par + n
1225                         continue
1226                 # Then try next paragraph
1227                 if next_par > 0 and not after:
1228                     next_params = get_par_params(file.body, next_par + 1)
1229                     ert = 0
1230                     while file.body[k][:1] == '\\' and split(file.body[k])[0] in next_params:
1231                         k = k + 1
1232                     # determine font size
1233                     next_size = "normal"
1234                     k = next_par + 1
1235                     while k < this_par:
1236                         if find(file.body[k], "\\size") != -1:
1237                             next_size = split(file.body[k])[1]
1238                             break
1239                         elif is_nonempty_line(file.body[k]):
1240                             break
1241                         k = k + 1
1242                     for k in range(n):
1243                         if (keywords_top[insets[k]] in next_params or
1244                             (insets[k] == "lyxline" and sizes[k] != next_size)):
1245                             ert = 1
1246                             break
1247                     if not ert:
1248                         for k in range(n):
1249                             file.body.insert(next_par + 1,
1250                                              keywords_top[insets[k]] + arguments[k])
1251                         del file.body[this_par:next_par-1]
1252                         i = this_par
1253                         continue
1254         elif paragraph_start or paragraph_end:
1255             # Convert to paragraph formatting if we are at the beginning or end
1256             # of a paragraph and the resulting paragraph would not be empty
1257             # The order is important: del and insert invalidate some indices
1258             if paragraph_start:
1259                 keywords = keywords_top
1260             else:
1261                 keywords = keywords_bot
1262             ert = 0
1263             for k in range(n):
1264                 if keywords[insets[k]] in params:
1265                     ert = 1
1266                     break
1267             if not ert:
1268                 for k in range(n):
1269                     file.body.insert(this_par + 1,
1270                                      keywords[insets[k]] + arguments[k])
1271                     for j in range(k, n):
1272                         del_lines[j][0] = del_lines[j][0] + 1
1273                         del_lines[j][1] = del_lines[j][1] + 1
1274                     del file.body[del_lines[k][0]:del_lines[k][1]+1]
1275                     deleted = del_lines[k][1] - del_lines[k][0] + 1
1276                     for j in range(k + 1, n):
1277                         del_lines[j][0] = del_lines[j][0] - deleted
1278                         del_lines[j][1] = del_lines[j][1] - deleted
1279                 i = this_par
1280                 continue
1281
1282         # Convert the first inset to ERT.
1283         # The others are converted in the next loop runs (if they exist)
1284         if insets[0] == "vspace":
1285             file.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
1286                                 '\\layout Standard', '', '\\backslash ']
1287             i = i + 6
1288             if spaceamount[0][-1] == '*':
1289                 spaceamount[0] = spaceamount[0][:-1]
1290                 keep = 1
1291             else:
1292                 keep = 0
1293
1294             # Replace defskip by the actual value
1295             if spaceamount[0] == 'defskip':
1296                 spaceamount[0] = defskipamount
1297
1298             # LaTeX does not know \\smallskip* etc
1299             if keep:
1300                 if spaceamount[0] == 'smallskip':
1301                     spaceamount[0] = '\\smallskipamount'
1302                 elif spaceamount[0] == 'medskip':
1303                     spaceamount[0] = '\\medskipamount'
1304                 elif spaceamount[0] == 'bigskip':
1305                     spaceamount[0] = '\\bigskipamount'
1306                 elif spaceamount[0] == 'vfill':
1307                     spaceamount[0] = '\\fill'
1308
1309             # Finally output the LaTeX code
1310             if (spaceamount[0] == 'smallskip' or spaceamount[0] == 'medskip' or
1311                 spaceamount[0] == 'bigskip'   or spaceamount[0] == 'vfill'):
1312                 file.body.insert(i, spaceamount[0] + '{}')
1313             else :
1314                 if keep:
1315                     file.body.insert(i, 'vspace*{')
1316                 else:
1317                     file.body.insert(i, 'vspace{')
1318                 i = convert_ertbackslash(file.body, i, spaceamount[0], file.format - 1)
1319                 file.body[i] = file.body[i] + '}'
1320             i = i + 1
1321         elif insets[0] == "lyxline":
1322             file.body[i] = ''
1323             latexsize = lyxsize2latexsize(size)
1324             if latexsize == '':
1325                 file.warning("Could not convert LyX fontsize '%s' to LaTeX font size." % size)
1326                 latexsize = '\\normalsize'
1327             i = insert_ert(file.body, i, 'Collapsed',
1328                            '\\lyxline{%s}' % latexsize,
1329                            file.format - 1)
1330             # We use \providecommand so that we don't get an error if native
1331             # lyxlines are used (LyX writes first its own preamble and then
1332             # the user specified one)
1333             add_to_preamble(file,
1334                             ['% Commands inserted by lyx2lyx for lyxlines',
1335                              '\\providecommand{\\lyxline}[1]{',
1336                              '  {#1 \\vspace{1ex} \\hrule width \\columnwidth \\vspace{1ex}}'
1337                              '}'])
1338         elif insets[0] == "newpage":
1339             file.body[i] = ''
1340             i = insert_ert(file.body, i, 'Collapsed', '\\newpage{}',
1341                            file.format - 1)
1342
1343
1344 # Convert a LyX length into a LaTeX length
1345 def convert_len(len, special):
1346     units = {"text%":"\\textwidth", "col%":"\\columnwidth",
1347              "page%":"\\pagewidth", "line%":"\\linewidth",
1348              "theight%":"\\textheight", "pheight%":"\\pageheight"}
1349
1350     # Convert special lengths
1351     if special != 'none':
1352         len = '%f\\' % len2value(len) + special
1353
1354     # Convert LyX units to LaTeX units
1355     for unit in units.keys():
1356         if find(len, unit) != -1:
1357             len = '%f' % (len2value(len) / 100) + units[unit]
1358             break
1359
1360     return len
1361
1362
1363 # Convert a LyX length into valid ERT code and append it to body[i]
1364 # Return the (maybe incremented) line index i
1365 def convert_ertlen(body, i, len, special, format):
1366     # Convert backslashes and insert the converted length into body
1367     return convert_ertbackslash(body, i, convert_len(len, special), format)
1368
1369
1370 # Return the value of len without the unit in numerical form
1371 def len2value(len):
1372     result = re.search('([+-]?[0-9.]+)', len)
1373     if result:
1374         return float(result.group(1))
1375     # No number means 1.0
1376     return 1.0
1377
1378
1379 # Convert text to ERT and insert it at body[i]
1380 # Return the index of the line after the inserted ERT
1381 def insert_ert(body, i, status, text, format):
1382     body[i:i] = ['\\begin_inset ERT', 'status ' + status, '']
1383     i = i + 3
1384     if format <= 224:
1385         body[i:i] = ['\\layout Standard', '']
1386     else:
1387         body[i:i] = ['\\begin_layout Standard', '']
1388     i = i + 1       # i points now to the just created empty line
1389     i = convert_ertbackslash(body, i, text, format) + 1
1390     if format > 224:
1391         body[i:i] = ['\\end_layout']
1392         i = i + 1
1393     body[i:i] = ['', '\\end_inset', '']
1394     i = i + 3
1395     return i
1396
1397
1398 # Add text to the preamble if it is not already there.
1399 # Only the first line is checked!
1400 def add_to_preamble(file, text):
1401     if find_token(file.preamble, text[0], 0) != -1:
1402         return
1403
1404     file.preamble.extend(text)
1405
1406
1407 def convert_frameless_box(file):
1408     pos = ['t', 'c', 'b']
1409     inner_pos = ['c', 't', 'b', 's']
1410     i = 0
1411     while 1:
1412         i = find_token(file.body, '\\begin_inset Frameless', i)
1413         if i == -1:
1414             return
1415         j = find_end_of_inset(file.body, i)
1416         if j == -1:
1417             file.warning("Malformed LyX file: Missing '\\end_inset'.")
1418             i = i + 1
1419             continue
1420         del file.body[i]
1421         j = j - 1
1422
1423         # Gather parameters
1424         params = {'position':0, 'hor_pos':'c', 'has_inner_box':'1',
1425                   'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
1426                   'special':'none', 'height':'1in',
1427                   'height_special':'totalheight', 'collapsed':'false'}
1428         for key in params.keys():
1429             value = replace(get_value(file.body, key, i, j), '"', '')
1430             if value != "":
1431                 if key == 'position':
1432                     # convert new to old position: 'position "t"' -> 0
1433                     value = find_token(pos, value, 0)
1434                     if value != -1:
1435                         params[key] = value
1436                 elif key == 'inner_pos':
1437                     # convert inner position
1438                     value = find_token(inner_pos, value, 0)
1439                     if value != -1:
1440                         params[key] = value
1441                 else:
1442                     params[key] = value
1443                 j = del_token(file.body, key, i, j)
1444         i = i + 1
1445
1446         # Convert to minipage or ERT?
1447         # Note that the inner_position and height parameters of a minipage
1448         # inset are ignored and not accessible for the user, although they
1449         # are present in the file format and correctly read in and written.
1450         # Therefore we convert to ERT if they do not have their LaTeX
1451         # defaults. These are:
1452         # - the value of "position" for "inner_pos"
1453         # - "\totalheight"          for "height"
1454         if (params['use_parbox'] != '0' or
1455             params['has_inner_box'] != '1' or
1456             params['special'] != 'none' or
1457             params['height_special'] != 'totalheight' or
1458             len2value(params['height']) != 1.0):
1459
1460             # Here we know that this box is not supported in file format 224.
1461             # Therefore we need to convert it to ERT. We can't simply convert
1462             # the beginning and end of the box to ERT, because the
1463             # box inset may contain layouts that are different from the
1464             # surrounding layout. After the conversion the contents of the
1465             # box inset is on the same level as the surrounding text, and
1466             # paragraph layouts and align parameters can get mixed up.
1467
1468             # A possible solution for this problem:
1469             # Convert the box to a minipage and redefine the minipage
1470             # environment in ERT so that the original box is simulated.
1471             # For minipages we could do this in a way that the width and
1472             # position can still be set from LyX, but this did not work well.
1473             # This is not possible for parboxes either, so we convert the
1474             # original box to ERT, put the minipage inset inside the box
1475             # and redefine the minipage environment to be empty.
1476
1477             # Commands that are independant of a particular box can go to
1478             # the preamble.
1479             # We need to define lyxtolyxrealminipage with 3 optional
1480             # arguments although LyX 1.3 uses only the first one.
1481             # Otherwise we will get LaTeX errors if this document is
1482             # converted to format 225 or above again (LyX 1.4 uses all
1483             # optional arguments).
1484             add_to_preamble(file,
1485                 ['% Commands inserted by lyx2lyx for frameless boxes',
1486                  '% Save the original minipage environment',
1487                  '\\let\\lyxtolyxrealminipage\\minipage',
1488                  '\\let\\endlyxtolyxrealminipage\\endminipage',
1489                  '% Define an empty lyxtolyximinipage environment',
1490                  '% with 3 optional arguments',
1491                  '\\newenvironment{lyxtolyxiiiminipage}[4]{}{}',
1492                  '\\newenvironment{lyxtolyxiiminipage}[2][\\lyxtolyxargi]%',
1493                  '  {\\begin{lyxtolyxiiiminipage}{\\lyxtolyxargi}{\\lyxtolyxargii}{#1}{#2}}%',
1494                  '  {\\end{lyxtolyxiiiminipage}}',
1495                  '\\newenvironment{lyxtolyximinipage}[1][\\totalheight]%',
1496                  '  {\\def\\lyxtolyxargii{{#1}}\\begin{lyxtolyxiiminipage}}%',
1497                  '  {\\end{lyxtolyxiiminipage}}',
1498                  '\\newenvironment{lyxtolyxminipage}[1][c]%',
1499                  '  {\\def\\lyxtolyxargi{{#1}}\\begin{lyxtolyximinipage}}',
1500                  '  {\\end{lyxtolyximinipage}}'])
1501
1502             if params['use_parbox'] != '0':
1503                 ert = '\\parbox'
1504             else:
1505                 ert = '\\begin{lyxtolyxrealminipage}'
1506
1507             # convert optional arguments only if not latex default
1508             if (pos[params['position']] != 'c' or
1509                 inner_pos[params['inner_pos']] != pos[params['position']] or
1510                 params['height_special'] != 'totalheight' or
1511                 len2value(params['height']) != 1.0):
1512                 ert = ert + '[' + pos[params['position']] + ']'
1513             if (inner_pos[params['inner_pos']] != pos[params['position']] or
1514                 params['height_special'] != 'totalheight' or
1515                 len2value(params['height']) != 1.0):
1516                 ert = ert + '[' + convert_len(params['height'],
1517                                               params['height_special']) + ']'
1518             if inner_pos[params['inner_pos']] != pos[params['position']]:
1519                 ert = ert + '[' + inner_pos[params['inner_pos']] + ']'
1520
1521             ert = ert + '{' + convert_len(params['width'],
1522                                           params['special']) + '}'
1523
1524             if params['use_parbox'] != '0':
1525                 ert = ert + '{'
1526             ert = ert + '\\let\\minipage\\lyxtolyxminipage%\n'
1527             ert = ert + '\\let\\endminipage\\endlyxtolyxminipage%\n'
1528
1529             old_i = i
1530             i = insert_ert(file.body, i, 'Collapsed', ert, file.format + 1)
1531             j = j + i - old_i - 1
1532
1533             file.body[i:i] = ['\\begin_inset Minipage',
1534                               'position %d' % params['position'],
1535                               'inner_position 1',
1536                               'height "1in"',
1537                               'width "' + params['width'] + '"',
1538                               'collapsed ' + params['collapsed']]
1539             i = i + 6
1540             j = j + 6
1541
1542             # Restore the original minipage environment since we may have
1543             # minipages inside this box.
1544             # Start a new paragraph because the following may be nonstandard
1545             file.body[i:i] = ['\\layout Standard', '', '']
1546             i = i + 2
1547             j = j + 3
1548             ert = '\\let\\minipage\\lyxtolyxrealminipage%\n'
1549             ert = ert + '\\let\\endminipage\\lyxtolyxrealendminipage%'
1550             old_i = i
1551             i = insert_ert(file.body, i, 'Collapsed', ert, file.format + 1)
1552             j = j + i - old_i - 1
1553
1554             # Redefine the minipage end before the inset end.
1555             # Start a new paragraph because the previous may be nonstandard
1556             file.body[j:j] = ['\\layout Standard', '', '']
1557             j = j + 2
1558             ert = '\\let\\endminipage\\endlyxtolyxminipage'
1559             j = insert_ert(file.body, j, 'Collapsed', ert, file.format + 1)
1560             j = j + 1
1561             file.body.insert(j, '')
1562             j = j + 1
1563
1564             # LyX writes '%\n' after each box. Therefore we need to end our
1565             # ERT with '%\n', too, since this may swallow a following space.
1566             if params['use_parbox'] != '0':
1567                 ert = '}%\n'
1568             else:
1569                 ert = '\\end{lyxtolyxrealminipage}%\n'
1570             j = insert_ert(file.body, j, 'Collapsed', ert, file.format + 1)
1571
1572             # We don't need to restore the original minipage after the inset
1573             # end because the scope of the redefinition is the original box.
1574
1575         else:
1576
1577             # Convert to minipage
1578             file.body[i:i] = ['\\begin_inset Minipage',
1579                               'position %d' % params['position'],
1580                               'inner_position %d' % params['inner_pos'],
1581                               'height "' + params['height'] + '"',
1582                               'width "' + params['width'] + '"',
1583                               'collapsed ' + params['collapsed']]
1584             i = i + 6
1585
1586 ##
1587 # Convert jurabib
1588 #
1589
1590 def convert_jurabib(file):
1591     i = find_token(file.header, '\\use_numerical_citations', 0)
1592     if i == -1:
1593         file.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
1594         return
1595     file.header.insert(i + 1, '\\use_jurabib 0')
1596
1597
1598 def revert_jurabib(file):
1599     i = find_token(file.header, '\\use_jurabib', 0)
1600     if i == -1:
1601         file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1602         return
1603     if get_value(file.header, '\\use_jurabib', 0) != "0":
1604         file.warning("Conversion of '\\use_jurabib = 1' not yet implemented.")
1605         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1606         return
1607     del file.header[i]
1608
1609 ##
1610 # Convert bibtopic
1611 #
1612
1613 def convert_bibtopic(file):
1614     i = find_token(file.header, '\\use_jurabib', 0)
1615     if i == -1:
1616         file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1617         return
1618     file.header.insert(i + 1, '\\use_bibtopic 0')
1619
1620
1621 def revert_bibtopic(file):
1622     i = find_token(file.header, '\\use_bibtopic', 0)
1623     if i == -1:
1624         file.warning("Malformed lyx file: Missing '\\use_bibtopic'.")
1625         return
1626     if get_value(file.header, '\\use_bibtopic', 0) != "0":
1627         file.warning("Conversion of '\\use_bibtopic = 1' not yet implemented.")
1628         # Don't remove '\\use_jurabib' so that people will get warnings by lyx
1629     del file.header[i]
1630
1631 ##
1632 # Sideway Floats
1633 #
1634
1635 def convert_float(file):
1636     i = 0
1637     while 1:
1638         i = find_token(file.body, '\\begin_inset Float', i)
1639         if i == -1:
1640             return
1641         # Seach for a line starting 'wide'
1642         # If, however, we find a line starting '\begin_layout'
1643         # (_always_ present) then break with a warning message
1644         i = i + 1
1645         while 1:
1646             if (file.body[i][:4] == "wide"):
1647                 file.body.insert(i + 1, 'sideways false')
1648                 break
1649             elif (file.body[i][:13] == "\\begin_layout"):
1650                 file.warning("Malformed lyx file: Missing 'wide'.")
1651                 break
1652             i = i + 1
1653         i = i + 1
1654
1655
1656 def revert_float(file):
1657     i = 0
1658     while 1:
1659         i = find_token(file.body, '\\begin_inset Float', i)
1660         if i == -1:
1661             return
1662         j = find_end_of_inset(file.body, i)
1663         if j == -1:
1664             file.warning("Malformed lyx file: Missing '\\end_inset'.")
1665             i = i + 1
1666             continue
1667         if get_value(file.body, 'sideways', i, j) != "false":
1668             file.warning("Conversion of 'sideways true' not yet implemented.")
1669             # Don't remove 'sideways' so that people will get warnings by lyx
1670             i = i + 1
1671             continue
1672         del_token(file.body, 'sideways', i, j)
1673         i = i + 1
1674
1675
1676 def convert_graphics(file):
1677     """ Add extension to filenames of insetgraphics if necessary.
1678     """
1679     i = 0
1680     while 1:
1681         i = find_token(file.body, "\\begin_inset Graphics", i)
1682         if i == -1:
1683             return
1684
1685         j = find_token2(file.body, "filename", i)
1686         if j == -1:
1687             return
1688         i = i + 1
1689         filename = split(file.body[j])[1]
1690         absname = os.path.normpath(os.path.join(file.dir, filename))
1691         if file.input == stdin and not os.path.isabs(filename):
1692             # We don't know the directory and cannot check the file.
1693             # We could use a heuristic and take the current directory,
1694             # and we could try to find out if filename has an extension,
1695             # but that would be just guesses and could be wrong.
1696             file.warning("""Warning: Can not determine whether file
1697          %s
1698          needs an extension when reading from standard input.
1699          You may need to correct the file manually or run
1700          lyx2lyx again with the .lyx file as commandline argument.""" % filename)
1701             continue
1702         # This needs to be the same algorithm as in pre 233 insetgraphics
1703         if access(absname, F_OK):
1704             continue
1705         if access(absname + ".ps", F_OK):
1706             file.body[j] = replace(file.body[j], filename, filename + ".ps")
1707             continue
1708         if access(absname + ".eps", F_OK):
1709             file.body[j] = replace(file.body[j], filename, filename + ".eps")
1710
1711
1712 ##
1713 # Convert firstname and surname from styles -> char styles
1714 #
1715 def convert_names(file):
1716     """ Convert in the docbook backend from firstname and surname style
1717     to charstyles.
1718     """
1719     if file.backend != "docbook":
1720         return
1721
1722     i = 0
1723
1724     while 1:
1725         i = find_token(file.body, "\\begin_layout Author", i)
1726         if i == -1:
1727             return
1728
1729         i = i + 1
1730         while file.body[i] == "":
1731             i = i + 1
1732
1733         if file.body[i][:11] != "\\end_layout" or file.body[i+2][:13] != "\\begin_deeper":
1734             i = i + 1
1735             continue
1736
1737         k = i
1738         i = find_end_of( file.body, i+3, "\\begin_deeper","\\end_deeper")
1739         if i == -1:
1740             # something is really wrong, abort
1741             file.warning("Missing \\end_deeper, after style Author.")
1742             file.warning("Aborted attempt to parse FirstName and Surname.")
1743             return
1744         firstname, surname = "", ""
1745
1746         name = file.body[k:i]
1747
1748         j = find_token(name, "\\begin_layout FirstName", 0)
1749         if j != -1:
1750             j = j + 1
1751             while(name[j] != "\\end_layout"):
1752                 firstname = firstname + name[j]
1753                 j = j + 1
1754
1755         j = find_token(name, "\\begin_layout Surname", 0)
1756         if j != -1:
1757             j = j + 1
1758             while(name[j] != "\\end_layout"):
1759                 surname = surname + name[j]
1760                 j = j + 1
1761
1762         # delete name
1763         del file.body[k+2:i+1]
1764
1765         file.body[k-1:k-1] = ["", "",
1766                           "\\begin_inset CharStyle Firstname",
1767                           "status inlined",
1768                           "",
1769                           "\\begin_layout Standard",
1770                           "",
1771                           "%s" % firstname,
1772                           "\end_layout",
1773                           "",
1774                           "\end_inset",
1775                           "",
1776                           "",
1777                           "\\begin_inset CharStyle Surname",
1778                           "status inlined",
1779                           "",
1780                           "\\begin_layout Standard",
1781                           "",
1782                           "%s" % surname,
1783                           "\\end_layout",
1784                           "",
1785                           "\\end_inset",
1786                           ""]
1787
1788
1789 def revert_names(file):
1790     """ Revert in the docbook backend from firstname and surname char style
1791     to styles.
1792     """
1793     if file.backend != "docbook":
1794         return
1795
1796
1797 ##
1798 #    \use_natbib 1                       \cite_engine <style>
1799 #    \use_numerical_citations 0     ->   where <style> is one of
1800 #    \use_jurabib 0                      "basic", "natbib_authoryear",
1801 #                                        "natbib_numerical" or "jurabib"
1802 def convert_cite_engine(file):
1803     a = find_token(file.header, "\\use_natbib", 0)
1804     if a == -1:
1805         file.warning("Malformed lyx file: Missing '\\use_natbib'.")
1806         return
1807
1808     b = find_token(file.header, "\\use_numerical_citations", 0)
1809     if b == -1 or b != a+1:
1810         file.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
1811         return
1812
1813     c = find_token(file.header, "\\use_jurabib", 0)
1814     if c == -1 or c != b+1:
1815         file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
1816         return
1817
1818     use_natbib = int(split(file.header[a])[1])
1819     use_numerical_citations = int(split(file.header[b])[1])
1820     use_jurabib = int(split(file.header[c])[1])
1821
1822     cite_engine = "basic"
1823     if use_natbib:
1824         if use_numerical_citations:
1825             cite_engine = "natbib_numerical"
1826         else:
1827              cite_engine = "natbib_authoryear"
1828     elif use_jurabib:
1829         cite_engine = "jurabib"
1830
1831     del file.header[a:c+1]
1832     file.header.insert(a, "\\cite_engine " + cite_engine)
1833
1834
1835 def revert_cite_engine(file):
1836     i = find_token(file.header, "\\cite_engine", 0)
1837     if i == -1:
1838         file.warning("Malformed lyx file: Missing '\\cite_engine'.")
1839         return
1840
1841     cite_engine = split(file.header[i])[1]
1842
1843     use_natbib = '0'
1844     use_numerical = '0'
1845     use_jurabib = '0'
1846     if cite_engine == "natbib_numerical":
1847         use_natbib = '1'
1848         use_numerical = '1'
1849     elif cite_engine == "natbib_authoryear":
1850         use_natbib = '1'
1851     elif cite_engine == "jurabib":
1852         use_jurabib = '1'
1853
1854     del file.header[i]
1855     file.header.insert(i, "\\use_jurabib " + use_jurabib)
1856     file.header.insert(i, "\\use_numerical_citations " + use_numerical)
1857     file.header.insert(i, "\\use_natbib " + use_natbib)
1858
1859
1860 ##
1861 # Paper package
1862 #
1863 def convert_paperpackage(file):
1864     i = find_token(file.header, "\\paperpackage", 0)
1865     if i == -1:
1866         return
1867
1868     packages = {'default':'none','a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
1869     if len(split(file.header[i])) > 1:
1870         paperpackage = split(file.header[i])[1]
1871         file.header[i] = replace(file.header[i], paperpackage, packages[paperpackage])
1872     else:
1873         file.header[i] = file.header[i] + ' widemarginsa4'
1874
1875
1876 def revert_paperpackage(file):
1877     i = find_token(file.header, "\\paperpackage", 0)
1878     if i == -1:
1879         return
1880
1881     packages = {'none':'a4', 'a4':'a4wide', 'a4wide':'widemarginsa4',
1882                 'widemarginsa4':'', 'default': 'default'}
1883     if len(split(file.header[i])) > 1:
1884         paperpackage = split(file.header[i])[1]
1885     else:
1886         paperpackage = 'default'
1887     file.header[i] = replace(file.header[i], paperpackage, packages[paperpackage])
1888
1889
1890 ##
1891 # Bullets
1892 #
1893 def convert_bullets(file):
1894     i = 0
1895     while 1:
1896         i = find_token(file.header, "\\bullet", i)
1897         if i == -1:
1898             return
1899         if file.header[i][:12] == '\\bulletLaTeX':
1900             file.header[i] = file.header[i] + ' ' + strip(file.header[i+1])
1901             n = 3
1902         else:
1903             file.header[i] = file.header[i] + ' ' + strip(file.header[i+1]) +\
1904                         ' ' + strip(file.header[i+2]) + ' ' + strip(file.header[i+3])
1905             n = 5
1906         del file.header[i+1:i + n]
1907         i = i + 1
1908
1909
1910 def revert_bullets(file):
1911     i = 0
1912     while 1:
1913         i = find_token(file.header, "\\bullet", i)
1914         if i == -1:
1915             return
1916         if file.header[i][:12] == '\\bulletLaTeX':
1917             n = find(file.header[i], '"')
1918             if n == -1:
1919                 file.warning("Malformed header.")
1920                 return
1921             else:
1922                 file.header[i:i+1] = [file.header[i][:n-1],'\t' + file.header[i][n:], '\\end_bullet']
1923             i = i + 3
1924         else:
1925             frag = split(file.header[i])
1926             if len(frag) != 5:
1927                 file.warning("Malformed header.")
1928                 return
1929             else:
1930                 file.header[i:i+1] = [frag[0] + ' ' + frag[1],
1931                                  '\t' + frag[2],
1932                                  '\t' + frag[3],
1933                                  '\t' + frag[4],
1934                                  '\\end_bullet']
1935                 i = i + 5
1936
1937
1938 ##
1939 # \begin_header and \begin_document
1940 #
1941 def add_begin_header(file):
1942     i = find_token(file.header, '\\lyxformat', 0)
1943     file.header.insert(i+1, '\\begin_header')
1944     file.header.insert(i+1, '\\begin_document')
1945
1946
1947 def remove_begin_header(file):
1948     i = find_token(file.header, "\\begin_document", 0)
1949     if i != -1:
1950         del file.header[i]
1951     i = find_token(file.header, "\\begin_header", 0)
1952     if i != -1:
1953         del file.header[i]
1954
1955
1956 ##
1957 # \begin_file.body and \end_file.body
1958 #
1959 def add_begin_body(file):
1960     file.body.insert(0, '\\begin_body')
1961     file.body.insert(1, '')
1962     i = find_token(file.body, "\\end_document", 0)
1963     file.body.insert(i, '\\end_body')
1964
1965 def remove_begin_body(file):
1966     i = find_token(file.body, "\\begin_body", 0)
1967     if i != -1:
1968         del file.body[i]
1969         if not file.body[i]:
1970             del file.body[i]
1971     i = find_token(file.body, "\\end_body", 0)
1972     if i != -1:
1973         del file.body[i]
1974
1975
1976 ##
1977 # \papersize
1978 #
1979 def normalize_papersize(file):
1980     i = find_token(file.header, '\\papersize', 0)
1981     if i == -1:
1982         return
1983
1984     tmp = split(file.header[i])
1985     if tmp[1] == "Default":
1986         file.header[i] = '\\papersize default'
1987         return
1988     if tmp[1] == "Custom":
1989         file.header[i] = '\\papersize custom'
1990
1991
1992 def denormalize_papersize(file):
1993     i = find_token(file.header, '\\papersize', 0)
1994     if i == -1:
1995         return
1996
1997     tmp = split(file.header[i])
1998     if tmp[1] == "custom":
1999         file.header[i] = '\\papersize Custom'
2000
2001
2002 ##
2003 # Strip spaces at end of command line
2004 #
2005 def strip_end_space(file):
2006     for i in range(len(file.body)):
2007         if file.body[i][:1] == '\\':
2008             file.body[i] = strip(file.body[i])
2009
2010
2011 ##
2012 # Use boolean values for \use_geometry, \use_bibtopic and \tracking_changes
2013 #
2014 def use_x_boolean(file):
2015     bin2bool = {'0': 'false', '1': 'true'}
2016     for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
2017         i = find_token(file.header, use, 0)
2018         if i == -1:
2019             continue
2020         decompose = split(file.header[i])
2021         file.header[i] = decompose[0] + ' ' + bin2bool[decompose[1]]
2022
2023
2024 def use_x_binary(file):
2025     bool2bin = {'false': '0', 'true': '1'}
2026     for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
2027         i = find_token(file.header, use, 0)
2028         if i == -1:
2029             continue
2030         decompose = split(file.header[i])
2031         file.header[i] = decompose[0] + ' ' + bool2bin[decompose[1]]
2032
2033 ##
2034 # Place all the paragraph parameters in their own line
2035 #
2036 def normalize_paragraph_params(file):
2037     body = file.body
2038     allowed_parameters = '\\paragraph_spacing', '\\noindent', '\\align', '\\labelwidthstring', "\\start_of_appendix", "\\leftindent"
2039
2040     i = 0
2041     while 1:
2042         i = find_token(file.body, '\\begin_layout', i)
2043         if i == -1:
2044             return
2045
2046         i = i + 1
2047         while 1:
2048             if strip(body[i]) and split(body[i])[0] not in allowed_parameters:
2049                 break
2050
2051             j = find(body[i],'\\', 1)
2052
2053             if j != -1:
2054                 body[i:i+1] = [strip(body[i][:j]), body[i][j:]]
2055
2056             i = i + 1
2057
2058
2059 ##
2060 # Add/remove output_changes parameter
2061 #
2062 def convert_output_changes (file):
2063     i = find_token(file.header, '\\tracking_changes', 0)
2064     if i == -1:
2065         file.warning("Malformed lyx file: Missing '\\tracking_changes'.")
2066         return
2067     file.header.insert(i+1, '\\output_changes true')
2068
2069
2070 def revert_output_changes (file):
2071     i = find_token(file.header, '\\output_changes', 0)
2072     if i == -1:
2073         return
2074     del file.header[i]
2075
2076
2077 ##
2078 # Convert paragraph breaks and sanitize paragraphs
2079 #
2080 def convert_ert_paragraphs(file):
2081     forbidden_settings = [
2082                           # paragraph parameters
2083                           '\\paragraph_spacing', '\\labelwidthstring',
2084                           '\\start_of_appendix', '\\noindent',
2085                           '\\leftindent', '\\align',
2086                           # font settings
2087                           '\\family', '\\series', '\\shape', '\\size',
2088                           '\\emph', '\\numeric', '\\bar', '\\noun',
2089                           '\\color', '\\lang']
2090     i = 0
2091     while 1:
2092         i = find_token(file.body, '\\begin_inset ERT', i)
2093         if i == -1:
2094             return
2095         j = find_end_of_inset(file.body, i)
2096         if j == -1:
2097             file.warning("Malformed lyx file: Missing '\\end_inset'.")
2098             i = i + 1
2099             continue
2100
2101         # convert non-standard paragraphs to standard
2102         k = i
2103         while 1:
2104             k = find_token(file.body, "\\begin_layout", k, j)
2105             if k == -1:
2106                 break
2107             file.body[k] = "\\begin_layout Standard"
2108             k = k + 1
2109
2110         # remove all paragraph parameters and font settings
2111         k = i
2112         while k < j:
2113             if (strip(file.body[k]) and
2114                 split(file.body[k])[0] in forbidden_settings):
2115                 del file.body[k]
2116                 j = j - 1
2117             else:
2118                 k = k + 1
2119
2120         # insert an empty paragraph before each paragraph but the first
2121         k = i
2122         first_pagraph = 1
2123         while 1:
2124             k = find_token(file.body, "\\begin_layout Standard", k, j)
2125             if k == -1:
2126                 break
2127             if first_pagraph:
2128                 first_pagraph = 0
2129                 k = k + 1
2130                 continue
2131             file.body[k:k] = ["\\begin_layout Standard", "",
2132                               "\\end_layout", ""]
2133             k = k + 5
2134             j = j + 4
2135
2136         # convert \\newline to new paragraph
2137         k = i
2138         while 1:
2139             k = find_token(file.body, "\\newline", k, j)
2140             if k == -1:
2141                 break
2142             file.body[k:k+1] = ["\\end_layout", "", "\\begin_layout Standard"]
2143             k = k + 4
2144             j = j + 3
2145         i = i + 1
2146
2147
2148 ##
2149 # Remove double paragraph breaks
2150 #
2151 def revert_ert_paragraphs(file):
2152     i = 0
2153     while 1:
2154         i = find_token(file.body, '\\begin_inset ERT', i)
2155         if i == -1:
2156             return
2157         j = find_end_of_inset(file.body, i)
2158         if j == -1:
2159             file.warning("Malformed lyx file: Missing '\\end_inset'.")
2160             i = i + 1
2161             continue
2162
2163         # replace paragraph breaks with \newline
2164         k = i
2165         while 1:
2166             k = find_token(file.body, "\\end_layout", k, j)
2167             l = find_token(file.body, "\\begin_layout", k, j)
2168             if k == -1 or l == -1:
2169                 break
2170             file.body[k:l+1] = ["\\newline"]
2171             j = j - l + k
2172             k = k + 1
2173
2174         # replace double \newlines with paragraph breaks
2175         k = i
2176         while 1:
2177             k = find_token(file.body, "\\newline", k, j)
2178             if k == -1:
2179                 break
2180             l = k + 1
2181             while file.body[l] == "":
2182                 l = l + 1
2183             if strip(file.body[l]) and split(file.body[l])[0] == "\\newline":
2184                 file.body[k:l+1] = ["\\end_layout", "",
2185                                     "\\begin_layout Standard"]
2186                 j = j - l + k + 2
2187                 k = k + 3
2188             else:
2189                 k = k + 1
2190         i = i + 1
2191
2192
2193 def convert_french(file):
2194     regexp = re.compile(r'^\\language\s+frenchb')
2195     i = find_re(file.header, regexp, 0)
2196     if i != -1:
2197         file.header[i] = "\\language french"
2198
2199     # Change language in the document body
2200     regexp = re.compile(r'^\\lang\s+frenchb')
2201     i = 0
2202     while 1:
2203         i = find_re(file.body, regexp, i)
2204         if i == -1:
2205             break
2206         file.body[i] = "\\lang french"
2207         i = i + 1
2208
2209
2210 def remove_paperpackage(file):
2211     i = find_token(file.header, '\\paperpackage', 0)
2212
2213     if i == -1:
2214         return
2215
2216     paperpackage = split(file.header[i])[1]
2217
2218     if paperpackage in ("a4", "a4wide", "widemarginsa4"):
2219         conv = {"a4":"\\usepackage{a4}","a4wide": "\\usepackage{a4wide}",
2220                 "widemarginsa4": "\\usepackage[widemargins]{a4}"}
2221         # for compatibility we ensure it is the first entry in preamble
2222         file.preamble[0:0] = [conv[paperpackage]]
2223
2224     del file.header[i]
2225
2226     i = find_token(file.header, '\\papersize', 0)
2227     if i != -1:
2228         file.header[i] = "\\papersize default"
2229
2230
2231 ##
2232 # Convertion hub
2233 #
2234
2235 convert = [[222, [insert_tracking_changes, add_end_header]],
2236            [223, [remove_color_default, convert_spaces, convert_bibtex, remove_insetparent]],
2237            [224, [convert_external, convert_comment]],
2238            [225, [add_end_layout, layout2begin_layout, convert_end_document,
2239                   convert_table_valignment_middle, convert_breaks]],
2240            [226, [convert_note]],
2241            [227, [convert_box]],
2242            [228, [convert_collapsable, convert_ert]],
2243            [229, [convert_minipage]],
2244            [230, [convert_jurabib]],
2245            [231, [convert_float]],
2246            [232, [convert_bibtopic]],
2247            [233, [convert_graphics, convert_names]],
2248            [234, [convert_cite_engine]],
2249            [235, [convert_paperpackage]],
2250            [236, [convert_bullets, add_begin_header, add_begin_body,
2251                   normalize_papersize, strip_end_space]],
2252            [237, [use_x_boolean]],
2253            [238, [update_latexaccents]],
2254            [239, [normalize_paragraph_params]],
2255            [240, [convert_output_changes]],
2256            [241, [convert_ert_paragraphs]],
2257            [242, [convert_french]],
2258            [243, [remove_paperpackage]]]
2259
2260 revert =  [[242, []],
2261            [241, []],
2262            [240, [revert_ert_paragraphs]],
2263            [239, [revert_output_changes]],
2264            [238, []],
2265            [237, []],
2266            [236, [use_x_binary]],
2267            [235, [denormalize_papersize, remove_begin_body,remove_begin_header,
2268                   revert_bullets]],
2269            [234, [revert_paperpackage]],
2270            [233, [revert_cite_engine]],
2271            [232, [revert_names]],
2272            [231, [revert_bibtopic]],
2273            [230, [revert_float]],
2274            [229, [revert_jurabib]],
2275            [228, []],
2276            [227, [revert_collapsable, revert_ert]],
2277            [226, [revert_box, revert_external_2]],
2278            [225, [revert_note]],
2279            [224, [rm_end_layout, begin_layout2layout, revert_end_document,
2280                   revert_valignment_middle, revert_breaks, convert_frameless_box]],
2281            [223, [revert_external_2, revert_comment, revert_eqref]],
2282            [222, [revert_spaces, revert_bibtex]],
2283            [221, [rm_end_header, rm_tracking_changes, rm_body_changes]]]
2284
2285
2286 if __name__ == "__main__":
2287     pass