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