set title from form if given
[objavi2.git] / booki-twiki-gateway.cgi
blob09445d8d60acbabf2baca86df4cd8dd3c7adbd88
1 #!/usr/bin/python
3 # Part of Objavi2, which turns html manuals into books. This emulates
4 # the Booki-epub-Espri interface for old TWiki books.
6 # Copyright (C) 2009 Douglas Bagnall
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 import os, sys
23 import re, traceback
24 from pprint import pformat
26 from objavi.twiki_wrapper import TWikiBook, get_book_list
27 from objavi.cgi_utils import parse_args, optionise, shift_file, output_blob_and_exit, log, make_book_name
28 from objavi import config
31 def make_booki_package(server, bookid, use_cache=False):
32 """Extract all chapters from the specified book, as well as
33 associated images and metadata, and zip it all up for conversion
34 to epub.
36 If cache is true, images that have been fetched on previous runs
37 will be reused.
38 """
39 book = TWikiBook(bookid, server)
40 return book.make_bookizip()
43 # ARG_VALIDATORS is a mapping between the expected cgi arguments and
44 # functions to validate their values. (None means no validation).
45 ARG_VALIDATORS = {
46 "book": re.compile(r'^(\w+/?)*\w+$').match, # can be: BlahBlah/Blah_Blah
47 "server": config.SERVER_DEFAULTS.__contains__,
48 "use-cache": None,
49 'mode': ('zip', 'html').__contains__,
50 "all": ['all', 'skip-existing'].__contains__,
53 if __name__ == '__main__':
55 args = parse_args(ARG_VALIDATORS)
56 use_cache = args.get('use-cache')
57 if use_cache is None:
58 use_cache = (os.environ.get('HTTP_HOST') in config.USE_IMG_CACHE_ALWAYS_HOSTS)
60 make_all = args.get('all')
61 if 'server' in args and 'book' in args:
62 zfn = make_booki_package(args['server'], args['book'], use_cache)
63 fn = shift_file(zfn, config.BOOKI_BOOK_DIR)
64 ziplink = '<p><a href="%s">%s zip file.</a></p>' % (fn, args['book'])
66 mode = args.get('mode', 'html')
67 if mode == 'zip':
68 f = open(fn)
69 data = f.read()
70 f.close()
71 output_blob_and_exit(data, 'application/x-booki+zip', args['book'] + '.zip')
73 elif 'server' in args and make_all is not None:
74 links = []
75 for book in get_book_list(args['server']):
76 if make_all == 'skip-existing' and book + '.zip' in os.listdir(config.BOOKI_BOOK_DIR):
77 log("skipping %s" % book)
78 continue
79 try:
80 zfn = make_booki_package(args['server'], book, use_cache=use_cache)
81 fn = shift_file(zfn, config.BOOKI_BOOK_DIR)
82 links.append('<a href="%s">%s</a> ' % (fn, book))
83 except Exception:
84 log('FAILED to make book "%s"' % book)
85 traceback.print_exc()
86 ziplink = ''.join(links)
87 else:
88 ziplink = ''
90 print "Content-type: text/html; charset=utf-8\n"
91 f = open('templates/booki-twiki-gateway.html')
92 template = f.read()
93 f.close()
95 print template % {'ziplink': ziplink,
96 'server-list': optionise(sorted(config.SERVER_DEFAULTS.keys()))}