3 # Simple XML to HTML converter.
5 # (C) 2005 Thomas Gleixner <tglx@linutronix.de>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 as
9 # published by the Free Software Foundation.
26 sys.setdefaultencoding('utf-8')
30 # Print the usage information
33 print "html.py <-f -h file.xml>"
34 print " -d DIR use DIR as base directory for opening files"
35 print " -f write output to file.html (default is stdout)"
42 "Mime-Version: 1.0\r\n",
43 "Content-Type: text/plain; charset=utf-8\r\n",
44 "Content-Transfer-Encoding: 8bit\r\n",
45 "Content-Disposition: inline\r\n",
52 def replaceVars(line):
54 while cnt < len(replace):
55 if line.find(replace[cnt]) >= 0:
56 line = line.replace(replace[cnt], replace[cnt+1])
61 fdout.write(replaceVars(line))
64 writeHtml("<div id=\"menu%s\">\n" %(level))
66 def placeMenu(topic, link, mode):
68 topic = replaceVars(topic)
69 mode = replaceVars(mode)
72 writeHtml("<p>%s</p>\n" %(topic))
74 if mode == 'selected':
75 writeHtml("<span class=\"sel\">\n")
77 writeHtml("<span class=\"nonsel\">\n")
79 writeHtml("<a href=\"%s\"><span>%s</span></a>\n" %(link, topic))
80 writeHtml("</span>\n")
83 # configuration parser
84 class docHandler(xml.sax.ContentHandler):
90 def startElement(self, name, attrs):
93 if len(self.content) > 0:
94 writeHtml(self.content)
99 elif name == "INCLUDE":
101 fd = open(attrs.get('file'), 'r')
103 fd = open(lookupdir + attrs.get('file'), 'r')
104 lines = fd.readlines()
108 elif name == "PARSE":
109 parseConfig(attrs.get('file'))
111 elif name == 'STARTMENU':
112 startMenu(attrs.get('level'))
115 placeMenu(attrs.get('topic'), attrs.get('link'), attrs.get('mode'))
117 elif name == 'ENDMENU':
118 writeHtml("</div>\n")
121 match = attrs.get('match')
122 repl = attrs.get('replace')
124 replace[idx:] = [match]
126 replace[idx:] = [repl]
130 if attrs.getLength > 0:
131 names = attrs.getNames()
133 writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
137 writeHtml("<" + name)
138 if attrs.getLength > 0:
139 names = attrs.getNames()
141 writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
144 def characters(self, ch):
145 self.content = self.content + ch
147 def endElement(self, name):
151 elif name == 'INCLUDE':
153 elif name == 'PARSE':
157 elif name == 'STARTMENU':
159 elif name == 'ENDMENU':
168 if len(self.content) > 0:
169 writeHtml(self.content)
171 writeHtml("</" + name + ">")
175 class errHandler(xml.sax.ErrorHandler):
179 def error(self, exception):
180 sys.stderr.write("%s\n" % exception)
182 def fatalError(self, exception):
183 sys.stderr.write("Fatal error while parsing configuration\n")
184 sys.stderr.write("%s\n" % exception)
187 # parse the configuration file
188 def parseConfig(file):
193 # Create an XML parser
194 parser = xml.sax.make_parser()
197 parser.setContentHandler(dh)
198 parser.setErrorHandler(eh)
203 fd = open(lookupdir + file, 'r')
211 # Parse the commandline
216 (options, arguments) = getopt.getopt(sys.argv[1:],'fhd:')
217 except getopt.GetoptError, ex:
225 for option, value in options:
227 lookupdir = value + '/'
237 print "No source file specified"
243 fname = arguments[0].split('.')[0]
244 fname = fname + ".html"
245 fdout = codecs.open(fname, 'w', 'utf-8')
247 parseConfig(arguments[0])