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.
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
36 If cache is true, images that have been fetched on previous runs
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).
46 "book": re
.compile(r
'^(\w+/?)*\w+$').match
, # can be: BlahBlah/Blah_Blah
47 "server": config
.SERVER_DEFAULTS
.__contains
__,
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')
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')
71 output_blob_and_exit(data
, 'application/x-booki+zip', args
['book'] + '.zip')
73 elif 'server' in args
and make_all
is not None:
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
)
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
))
84 log('FAILED to make book "%s"' % book
)
86 ziplink
= ''.join(links
)
90 print "Content-type: text/html; charset=utf-8\n"
91 f
= open('templates/booki-twiki-gateway.html')
95 print template
% {'ziplink': ziplink
,
96 'server-list': optionise(sorted(config
.SERVER_DEFAULTS
.keys()))}