1
#!/usr/bin/env python2.4
4
from StringIO import StringIO
7
from docutils.core import publish_file
8
from docutils.parsers import rst
9
from elementtree.ElementTree import XML
10
from elementtree import HTMLTreeBuilder
13
def kidified_rest(rest_file, template_name):
14
xhtml_file = StringIO()
15
# prevent docutils from autoclosing the StringIO
16
xhtml_file.close = lambda: None
17
xhtml = publish_file(rest_file, writer_name='html', destination=xhtml_file,
18
settings_overrides={"doctitle_xform": 0}
22
xml = HTMLTreeBuilder.parse(xhtml_file)
23
head = xml.find('head')
24
body = xml.find('body')
25
assert head is not None
26
assert body is not None
27
template=kid.Template(file=template_name,
29
return (template.serialize(output="html"))
31
def safe_open(filename, mode):
33
return open(filename, mode + 'b')
35
if e.errno != errno.ENOENT:
37
sys.stderr.write('file not found: %s\n' % sys.argv[2])
44
rest_file = safe_open(args[1], 'r')
49
out_file = safe_open(args[2], 'w')
53
out_file.write(kidified_rest(rest_file, args[0]))