import with for py2.5
[librarian.git] / scripts / bookfragments
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 #
7 import os
8 import optparse
9
10 from librarian import html
11
12
13 if __name__ == '__main__':
14     # Parse commandline arguments
15     usage = """Usage: %prog [options] SOURCE [SOURCE...]
16     Extract theme fragments from SOURCE."""
17
18     parser = optparse.OptionParser(usage=usage)
19
20     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
21         help='print status messages to stdout')
22
23     options, input_filenames = parser.parse_args()
24
25     if len(input_filenames) < 1:
26         parser.print_help()
27         exit(1)
28
29     # Do some real work
30     for input_filename in input_filenames:
31         if options.verbose:
32             print input_filename
33
34         output_filename = os.path.splitext(input_filename)[0] + '.fragments.html'
35
36         closed_fragments, open_fragments = html.extract_fragments(input_filename)
37
38         for fragment_id in open_fragments:
39             print '%s:warning:unclosed fragment #%s' % (input_filename, fragment_id)
40
41         output_file = open(output_filename, 'w')
42         output_file.write("""
43             <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
44             <html><head>
45                 <title>bookfragments output</title>
46                 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
47                 <link rel="stylesheet" href="master.css" type="text/css" media="screen" charset="utf-8" />
48             </head>
49             <body>""")
50         for fragment in closed_fragments.values():
51             fragment_html = u'<div class="fragment"><h3>[#%s] %s</h3>%s</div>' % (fragment.id, fragment.themes, fragment)
52             output_file.write(fragment_html.encode('utf-8'))
53         output_file.write('</body></html>')
54         output_file.close()
55