]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyxconvert_223.py
99b4a85d908a3637d4bec36d7f8f11977f5be56f
[features.git] / lib / lyx2lyx / lyxconvert_223.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 import string
20 import re
21 from parser_tools import find_token, find_end_of
22
23 def convert_external(lines):
24     external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
25     external_header = "\\begin_inset External"
26     i = 0
27     while 1:
28         i = find_token(lines, external_header, i)
29         if i == -1:
30             break
31         look = external_rexp.search(lines[i])
32         args = ['','']
33         if look:
34             args[0] = look.group(1)
35             args[1] = look.group(2)
36         #FIXME: if the previous search fails then warn
37
38         if args[0] == "RasterImage":
39             # Convert a RasterImage External Inset to a Graphics Inset.
40             top = "\\begin_inset Graphics"
41             if args[1]:
42                 filename = "\tfilename " + args[1]
43             lines[i:i+1] = [top, filename]
44             i = i + 1
45         else:
46             # Convert the old External Inset format to the new.
47             top = external_header
48             template = "\ttemplate " + args[0]
49             if args[1]:
50                 filename = "\tfilename " + args[1]
51                 lines[i:i+1] = [top, template, filename]
52                 i = i + 2
53             else:
54                 lines[i:i+1] = [top, template]
55                 i = i + 1
56
57
58 def convert_comment(lines):
59     i = 0
60     comment = "\\layout Comment"
61     while 1:
62         i = find_token(lines, comment, i)
63         if i == -1:
64             return
65
66         lines[i:i+1] = ["\\layout Standard","","",
67                         "\\begin_inset Comment",
68                         "collapsed true","",
69                         "\\layout Standard"]
70         i = i + 7
71
72         while 1:
73                 old_i = i
74                 i = find_token(lines, "\\layout", i)
75                 if i == -1:
76                     i = len(lines) - 1
77                     lines[i:i] = ["\\end_inset ","",""]
78                     return
79
80                 j = find_token(lines, '\\begin_deeper', old_i, i)
81                 if j == -1: j = i + 1
82                 k = find_token(lines, '\\begin_inset', old_i, i)
83                 if k == -1: k = i + 1
84
85                 if j < i and j < k:
86                     i = j
87                     del lines[i]
88                     i = find_end_of( lines, i, "\\begin_deeper","\\end_deeper")
89                     if i == -1:
90                         #This case should not happen
91                         #but if this happens deal with it greacefully adding
92                         #the missing \end_deeper.
93                         i = len(lines) - 1
94                         lines[i:i] = ["\end_deeper","","","\\end_inset ","",""]
95                         return
96                     else:
97                         del lines[i]
98                         continue
99
100                 if k < i:
101                     i = k
102                     i = find_end_of( lines, i, "\\begin_inset","\\end_inset")
103                     if i == -1:
104                         #This case should not happen
105                         #but if this happens deal with it greacefully adding
106                         #the missing \end_inset.
107                         i = len(lines) - 1
108                         lines[i:i] = ["\\end_inset ","","","\\end_inset ","",""]
109                         return
110                     else:
111                         i = i + 1
112                         continue
113
114                 if string.find(lines[i], comment) == -1:
115                     lines[i:i] = ["\\end_inset"]
116                     i = i + 1
117                     break
118                 lines[i:i+1] = ["\\layout Standard"]
119                 i = i + 1
120
121 def convert(header, body):
122     convert_external(body)
123     convert_comment(body)
124
125 if __name__ == "__main__":
126     pass