Added copyright notice about AGPL. Removed setuputils in favour of plain distutils.
[librarian.git] / scripts / bookfragments
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 #    This file is part of Librarian.
5 #
6 #    Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
7 #    
8 #    For full list of contributors see AUTHORS file. 
9 #
10 #    This program is free software: you can redistribute it and/or modify
11 #    it under the terms of the GNU Affero General Public License as published by
12 #    the Free Software Foundation, either version 3 of the License, or
13 #    (at your option) any later version.
14 #
15 #    This program is distributed in the hope that it will be useful,
16 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #    GNU Affero General Public License for more details.
19 #
20 #    You should have received a copy of the GNU Affero General Public License
21 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 import os
24 import optparse
25
26 from librarian import html
27
28
29 if __name__ == '__main__':
30     # Parse commandline arguments
31     usage = """Usage: %prog [options] SOURCE [SOURCE...]
32     Extract theme fragments from SOURCE."""
33     
34     parser = optparse.OptionParser(usage=usage)
35     
36     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
37         help='print status messages to stdout')
38     
39     options, input_filenames = parser.parse_args()
40     
41     if len(input_filenames) < 1:
42         parser.print_help()
43         exit(1)
44     
45     # Do some real work
46     for input_filename in input_filenames:
47         if options.verbose:
48             print input_filename
49     
50         output_filename = os.path.splitext(input_filename)[0] + '.fragments.html'
51     
52         closed_fragments, open_fragments = html.extract_fragments(input_filename)
53
54         for fragment_id in open_fragments:
55             print '%s:warning:unclosed fragment #%s' % (input_filename, fragment_id)
56
57         output_file = open(output_filename, 'w')
58         output_file.write("""
59             <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
60             <html><head>
61                 <title>bookfragments output</title>
62                 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
63                 <link rel="stylesheet" href="master.css" type="text/css" media="screen" charset="utf-8" />
64             </head>
65             <body>""")
66         for fragment in closed_fragments.values():
67             fragment_html = u'<div class="fragment"><h3>[#%s] %s</h3>%s</div>' % (fragment.id, fragment.themes, fragment)
68             output_file.write(fragment_html.encode('utf-8'))
69         output_file.write('</body></html>')
70         output_file.close()
71