Added copyright notice about AGPL. Removed setuputils in favour of plain distutils.
[librarian.git] / scripts / book2ihtml
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, ParseError
27
28
29 if __name__ == '__main__':
30     # Parse commandline arguments
31     usage = """Usage: %prog [options] SOURCE [SOURCE...]
32     Convert SOURCE files to HTML format."""
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     parser.add_option('-i', '--ignore-dublin-core', action='store_false', dest='parse_dublincore', default=True,
39         help='don\'t try to parse dublin core metadata')
40             
41     options, input_filenames = parser.parse_args()
42
43     if len(input_filenames) < 1:
44         parser.print_help()
45         exit(1)
46
47     # Do some real work
48     for input_filename in input_filenames:
49         if options.verbose:
50             print input_filename
51         
52         output_filename = os.path.splitext(input_filename)[0] + '.html'
53         try:
54             html.transform(input_filename, output_filename, parse_dublincore=options.parse_dublincore,\
55                 stylesheet='partial')
56         except ParseError, e:
57             print '%(file)s:%(name)s:%(message)s' % {
58                 'file': input_filename,
59                 'name': e.__class__.__name__,
60                 'message': e.message.encode('utf-8')
61             }
62         except IOError, e:
63             print '%(file)s:%(name)s:%(message)s' % {
64                 'file': input_filename,
65                 'name': e.__class__.__name__,
66                 'message': e.strerror,
67             }
68         except BaseException, e:
69             print '%(file)s:%(etype)s:%(message)s' % {
70                 'file': input_filename,
71                 'etype': e.__class__.__name__,
72                 'message': e.message.encode('utf-8'),
73             }
74             raise e
75