Update translations from Transifex
[platform/upstream/openconnect.git] / www / html.py
1 #!/usr/bin/env python
2 #
3 # Simple XML to HTML converter.
4
5 # (C) 2005 Thomas Gleixner <tglx@linutronix.de>
6 #
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.
10 #
11
12 import os
13 import sys
14 import getopt
15 import pprint
16 import shutil
17 import string
18 import smtplib
19 import socket
20 import time
21 import xml.sax
22 import commands
23 import codecs
24
25 reload(sys)
26 sys.setdefaultencoding('utf-8')
27
28 lookupdir = ''
29
30 # Print the usage information
31 def usage():
32         print "USAGE:"
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)"
36         print "  -h        help"
37         return
38
39
40 # Headerfields
41 header = [
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",
46 ]
47
48 html = []
49 replace = []
50 fdout = sys.stdout
51
52 def replaceVars(line):
53         cnt = 0
54         while cnt < len(replace):
55                 if line.find(replace[cnt]) >= 0:
56                         line = line.replace(replace[cnt], replace[cnt+1])
57                 cnt = cnt + 2
58         return line
59
60 def writeHtml(line):
61         fdout.write(replaceVars(line))
62
63 def startMenu(level):
64         writeHtml("<div id=\"menu%s\">\n" %(level))
65
66 def placeMenu(topic, link, mode):
67
68         topic = replaceVars(topic)
69         mode = replaceVars(mode)
70         
71         if mode == 'text':
72                 writeHtml("<p>%s</p>\n" %(topic))
73                 return
74         if mode == 'selected':
75                 writeHtml("<span class=\"sel\">\n")
76         else:
77                 writeHtml("<span class=\"nonsel\">\n")
78                 
79         writeHtml("<a href=\"%s\"><span>%s</span></a>\n" %(link, topic))
80         writeHtml("</span>\n")
81         
82
83 # configuration parser
84 class docHandler(xml.sax.ContentHandler):
85
86         def __init__(self):
87                 self.content = ""
88                 return
89     
90         def startElement(self, name, attrs):
91                 self.element = name
92                                 
93                 if len(self.content) > 0:
94                         writeHtml(self.content)
95                 self.content = ""
96                 
97                 if name == "PAGE":
98                         return
99                 elif name == "INCLUDE":
100                         try:
101                                 fd = open(attrs.get('file'), 'r')
102                         except:
103                                 fd = open(lookupdir + attrs.get('file'), 'r')
104                         lines = fd.readlines()
105                         fd.close()
106                         for line in lines:
107                                 writeHtml(line)
108                 elif name == "PARSE":
109                         parseConfig(attrs.get('file'))
110                         
111                 elif name == 'STARTMENU':
112                         startMenu(attrs.get('level'))
113                         
114                 elif name == 'MENU':
115                         placeMenu(attrs.get('topic'), attrs.get('link'), attrs.get('mode'))
116                         
117                 elif name == 'ENDMENU':
118                         writeHtml("</div>\n")
119                         
120                 elif name == 'VAR':
121                         match = attrs.get('match')
122                         repl = attrs.get('replace')
123                         idx = len(replace)
124                         replace[idx:] = [match]
125                         idx = len(replace)
126                         replace[idx:] = [repl]
127                 
128                 elif name == "br":
129                         writeHtml("<br")
130                         if attrs.getLength > 0:
131                                 names = attrs.getNames()
132                                 for name in names:
133                                         writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
134                         writeHtml(" />")
135                         
136                 else:
137                         writeHtml("<" + name)
138                         if attrs.getLength > 0:
139                                 names = attrs.getNames()
140                                 for name in names:
141                                         writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
142                         writeHtml(">")
143
144         def characters(self, ch):
145                 self.content = self.content + ch
146
147         def endElement(self, name):
148
149                 if name == "PAGE":
150                         return
151                 elif name == 'INCLUDE':
152                         return
153                 elif name == 'PARSE':
154                         return
155                 elif name == 'PAGE':
156                         return
157                 elif name == 'STARTMENU':
158                         return
159                 elif name == 'ENDMENU':
160                         return
161                 elif name == 'MENU':
162                         return
163                 elif name == 'VAR':
164                         return
165                 elif name == 'br':
166                         return
167
168                 if len(self.content) > 0:
169                         writeHtml(self.content)
170                 self.content = ""
171                 writeHtml("</" + name + ">")
172         
173
174 # error handler
175 class errHandler(xml.sax.ErrorHandler):
176         def __init__(self):
177                 return
178
179         def error(self, exception):
180                 sys.stderr.write("%s\n" % exception)
181
182         def fatalError(self, exception):
183                 sys.stderr.write("Fatal error while parsing configuration\n")
184                 sys.stderr.write("%s\n" % exception)
185                 sys.exit(1)
186
187 # parse the configuration file
188 def parseConfig(file):
189         # handlers
190         dh = docHandler()
191         eh = errHandler()
192
193         # Create an XML parser
194         parser = xml.sax.make_parser()
195
196         # Set the handlers
197         parser.setContentHandler(dh)
198         parser.setErrorHandler(eh)
199
200         try:
201                 fd = open(file, 'r')
202         except:
203                 fd = open(lookupdir + file, 'r')
204
205         # Parse the file
206         parser.parse(fd)
207         fd.close()
208
209
210 # Here we go
211 # Parse the commandline
212
213 writefile = 0
214
215 try:
216         (options, arguments) = getopt.getopt(sys.argv[1:],'fhd:')
217 except getopt.GetoptError, ex:
218         print
219         print "ERROR:"
220         print ex.msg
221         usage()
222         sys.exit(1)
223         pass
224
225 for option, value in options:
226         if option == '-d':
227                 lookupdir = value + '/'
228         if option == '-f':
229                 writefile = 1
230         elif option == '-h':
231                 usage()
232                 sys.exit(0)
233                 pass
234         pass
235
236 if not arguments:
237         print "No source file specified"
238         usage()
239         sys.exit(1)
240         pass
241
242 if writefile > 0:
243         fname = arguments[0].split('.')[0]
244         fname = fname + ".html"
245         fdout = codecs.open(fname, 'w', 'utf-8')        
246
247 parseConfig(arguments[0])
248
249 if writefile > 0:
250         fdout.close()
251