-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxml_to_html.py
executable file
·66 lines (53 loc) · 2.29 KB
/
xml_to_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
This script will re-generate a static HTML page
Author: Katherine Huang, Scott Olesen
"""
import sys, os, argparse, re
from xml.dom.minidom import parseString
def xml_to_html(xml_txt, template_txt, html_fn):
'''
xml and template to new html
parameters
xml_txt : str
text of the xml file
template_txt : str
text of the template html
html_fn : str
filename to look for when doing "current" menu
returns : str
html content
'''
# replace the keywords in the HTML template with the xml content
# HTML keywords are: _rightPane_, _contentPane_, _footer_ (case sensitive)
html_txt = template_txt
xdom = parseString(xml_txt) # convert xml to xdom object
for xmlTag in ('Title', 'contentPane', 'rightPane', 'midPane', 'footer'):
openTag = "<%s>" %xmlTag
closeTag = "</%s>" %xmlTag
try:
paneDom = xdom.getElementsByTagName(xmlTag)[0].toxml().replace(openTag, "").replace(closeTag, "") # itself an xdom object
if xmlTag == 'footer':
if 'img/bar1.png' in paneDom:
paneDom += '\n<br>'
paneDom += '\n<a id="journal" href="http://accessibility.mit.edu">Accessibility</a>'
html_txt = html_txt.replace("_%s_" %(xmlTag), paneDom)
if type(html_txt) is unicode:
# if this is python2, we need to convert from unicode to string
html_txt = html_txt.encode('ascii', 'ignore')
except:
pass
html_txt = html_txt.replace('class="menu" href="{}"'.format(html_fn), 'class="current" href="{}"'.format(html_fn))
return html_txt
if __name__== '__main__':
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument('xml', type=argparse.FileType('r'), help='xml filename')
p.add_argument('output', help='output html')
p.add_argument('-t', '--template', type=argparse.FileType('r'), help='html template filename', default='template.html')
args = p.parse_args()
xml_content = args.xml.read()
template_content = args.template.read()
html_content = xml_to_html(xml_content, template_content, args.output)
# write the html content out
with open(args.output, 'w') as f:
f.write(html_content)