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