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