2 # -*- coding: utf-8 -*-
12 def transform(input_filename, output_filename):
13 """Transforms file input_filename in XML to output_filename in XHTML."""
15 style = etree.parse('book2html.xslt')
17 doc_file = cStringIO.StringIO()
18 expr = re.compile(r'/\s', re.MULTILINE | re.UNICODE);
20 f = open(input_filename, 'r')
22 line = line.decode('utf-8')
23 line = expr.sub(u'<br/>\n', line).replace(u'---', u'—').replace(u',,', u'„')
24 doc_file.write(line.encode('utf-8'))
29 parser = etree.XMLParser(remove_blank_text=True)
30 doc = etree.parse(doc_file, parser)
32 result = doc.xslt(style)
33 result.write(output_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')
36 if __name__ == '__main__':
37 # Parse commandline arguments
38 usage = """Usage: %prog [options] SOURCE [SOURCE...]
39 Convert SOURCE files to HTML format."""
41 parser = optparse.OptionParser(usage=usage)
43 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
44 help='print status messages to stdout')
46 options, input_filenames = parser.parse_args()
48 if len(input_filenames) < 1:
53 for input_filename in input_filenames:
57 output_filename = os.path.splitext(input_filename)[0] + '.html'
58 transform(input_filename, output_filename)