1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
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.
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.
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.
21 from parser_tools import find_token, find_end_of
23 def convert_external(lines):
24 external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
25 external_header = "\\begin_inset External"
28 i = find_token(lines, external_header, i)
31 look = external_rexp.search(lines[i])
34 args[0] = look.group(1)
35 args[1] = look.group(2)
36 #FIXME: if the previous search fails then warn
38 if args[0] == "RasterImage":
39 # Convert a RasterImage External Inset to a Graphics Inset.
40 top = "\\begin_inset Graphics"
42 filename = "\tfilename " + args[1]
43 lines[i:i+1] = [top, filename]
46 # Convert the old External Inset format to the new.
48 template = "\ttemplate " + args[0]
50 filename = "\tfilename " + args[1]
51 lines[i:i+1] = [top, template, filename]
54 lines[i:i+1] = [top, template]
58 def convert_comment(lines):
60 comment = "\\layout Comment"
62 i = find_token(lines, comment, i)
66 lines[i:i+1] = ["\\layout Standard","","",
67 "\\begin_inset Comment",
74 i = find_token(lines, "\\layout", i)
77 lines[i:i] = ["\\end_inset ","",""]
80 j = find_token(lines, '\\begin_deeper', old_i, i)
82 k = find_token(lines, '\\begin_inset', old_i, i)
88 i = find_end_of( lines, i, "\\begin_deeper","\\end_deeper")
90 #This case should not happen
91 #but if this happens deal with it greacefully adding
92 #the missing \end_deeper.
94 lines[i:i] = ["\end_deeper","","","\\end_inset ","",""]
102 i = find_end_of( lines, i, "\\begin_inset","\\end_inset")
104 #This case should not happen
105 #but if this happens deal with it greacefully adding
106 #the missing \end_inset.
108 lines[i:i] = ["\\end_inset ","","","\\end_inset ","",""]
114 if string.find(lines[i], comment) == -1:
115 lines[i:i] = ["\\end_inset"]
118 lines[i:i+1] = ["\\layout Standard"]
121 def convert(header, body):
122 convert_external(body)
123 convert_comment(body)
125 if __name__ == "__main__":