--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from collections import namedtuple
+import os.path
+import optparse
+
+from librarian import DirDocProvider, ParseError
+from librarian.parser import WLDocument
+from librarian.cover import WLCover
+
+
+class Option(object):
+ """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
+ def __init__(self, *names, **options):
+ self.names = names
+ self.options = options
+
+ def add(self, parser):
+ parser.add_option(*self.names, **self.options)
+
+ def name(self):
+ return self.options['dest']
+
+ def value(self, options):
+ return getattr(options, self.name())
+
+
+class Book2Anything(object):
+ """A class for creating book2... scripts.
+
+ Subclass it for any format you want to convert to.
+ """
+ format_name = None # Set format name, like "PDF".
+ ext = None # Set file extension, like "pdf".
+ uses_cover = False # Can it add a cover?
+ cover_optional = True # Only relevant if uses_cover
+ uses_provider = False # Does it need a DocProvider?
+ transform = None # Transform method. Uses WLDocument.as_{ext} by default.
+ parser_options = [] # List of Option objects for additional parser args.
+ transform_options = [] # List of Option objects for additional transform args.
+ transform_flags = [] # List of Option objects for supported transform flags.
+
+
+ @classmethod
+ def run(cls):
+ # Parse commandline arguments
+ usage = """Usage: %%prog [options] SOURCE [SOURCE...]
+ Convert SOURCE files to %s format.""" % cls.format_name
+
+ parser = optparse.OptionParser(usage=usage)
+
+ parser.add_option('-v', '--verbose',
+ action='store_true', dest='verbose', default=False,
+ help='print status messages to stdout')
+ parser.add_option('-d', '--make-dir',
+ action='store_true', dest='make_dir', default=False,
+ help='create a directory for author and put the output file in it')
+ parser.add_option('-o', '--output-file',
+ dest='output_file', metavar='FILE',
+ help='specifies the output file')
+ parser.add_option('-O', '--output-dir',
+ dest='output_dir', metavar='DIR',
+ help='specifies the directory for output')
+ if cls.uses_cover:
+ if cls.cover_optional:
+ parser.add_option('-c', '--with-cover',
+ action='store_true', dest='with_cover', default=False,
+ help='create default cover')
+ parser.add_option('-C', '--image-cache',
+ dest='image_cache', metavar='URL',
+ help='prefix for image download cache' +
+ (' (implies --with-cover)' if cls.cover_optional else ''))
+ for option in cls.parser_options + cls.transform_options + cls.transform_flags:
+ option.add(parser)
+
+ options, input_filenames = parser.parse_args()
+
+ if len(input_filenames) < 1:
+ parser.print_help()
+ return(1)
+
+ # Prepare additional args for parser.
+ parser_args = {}
+ for option in cls.parser_options:
+ parser_args[option.name()] = option.value(options)
+ # Prepare additional args for transform method.
+ transform_args = {}
+ for option in cls.transform_options:
+ transform_args[option.name()] = option.value(options)
+ # Add flags to transform_args, if any.
+ transform_flags = [flag.name() for flag in cls.transform_flags
+ if flag.value(options)]
+ if transform_flags:
+ transform_args['flags'] = transform_flags
+ # Add cover support, if any.
+ if cls.uses_cover:
+ if options.image_cache:
+ transform_args['cover'] = lambda x: WLCover(x, image_cache = options.image_cache)
+ elif not cls.cover_optional or options.with_cover:
+ transform_args['cover'] = WLCover
+
+
+ # Do some real work
+ try:
+ for main_input in input_filenames:
+ if options.verbose:
+ print main_input
+
+ # Where to find input?
+ if cls.uses_provider:
+ path, fname = os.path.realpath(main_input).rsplit('/', 1)
+ provider = DirDocProvider(path)
+ else:
+ provider = None
+
+ # Where to write output?
+ if not (options.output_file or options.output_dir):
+ output_file = os.path.splitext(main_input)[0] + '.' + cls.ext
+ else:
+ output_file = None
+
+ # Do the transformation.
+ doc = WLDocument.from_file(main_input, provider=provider, **parser_args)
+ transform = cls.transform
+ if transform is None:
+ transform = getattr(WLDocument, 'as_%s' % cls.ext)
+ output = transform(doc, **transform_args)
+
+ doc.save_output_file(output,
+ output_file, options.output_dir, options.make_dir, cls.ext)
+
+ except ParseError, e:
+ print '%(file)s:%(name)s:%(message)s' % {
+ 'file': main_input,
+ 'name': e.__class__.__name__,
+ 'message': e
+ }
u'Współczesność': '#06393d',
}
- def __init__(self, book_info):
+ def __init__(self, book_info, image_cache=None):
super(WLCover, self).__init__(book_info)
self.kind = book_info.kind
self.epoch = book_info.epoch
from urllib2 import urlopen
from StringIO import StringIO
- bg_src = urlopen(book_info.cover_url)
+ url = book_info.cover_url
+ bg_src = None
+ if image_cache:
+ from urllib import quote
+ try:
+ bg_src = urlopen(image_cache + quote(url, safe=""))
+ except:
+ pass
+ if bg_src is None:
+ bg_src = urlopen(url)
self.background_img = StringIO(bg_src.read())
bg_src.close()
else:
""" produces a EPUB file
sample=n: generate sample e-book (with at least n paragraphs)
- cover: a cover.Cover object or True for default
+ cover: a cover.Cover factory or True for default
flags: less-advertising, without-fonts, working-copy
"""
if cover:
if cover is True:
cover = WLCover
- if cover.uses_dc_cover:
- if document.book_info.cover_by:
- document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
- if document.book_info.cover_source:
- document.edoc.getroot().set('data-cover-source', document.book_info.cover_source)
cover_file = StringIO()
- c = cover(document.book_info)
- c.save(cover_file)
- c_name = 'cover.%s' % c.ext()
- zip.writestr(os.path.join('OPS', c_name), cover_file.getvalue())
+ bound_cover = cover(document.book_info)
+ bound_cover.save(cover_file)
+ cover_name = 'cover.%s' % bound_cover.ext()
+ zip.writestr(os.path.join('OPS', cover_name), cover_file.getvalue())
del cover_file
cover_tree = etree.parse(get_resource('epub/cover.html'))
- cover_tree.find('//' + XHTMLNS('img')).set('src', c_name)
+ cover_tree.find('//' + XHTMLNS('img')).set('src', cover_name)
zip.writestr('OPS/cover.html', etree.tostring(
cover_tree, method="html", pretty_print=True))
+ if bound_cover.uses_dc_cover:
+ if document.book_info.cover_by:
+ document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
+ if document.book_info.cover_source:
+ document.edoc.getroot().set('data-cover-source', document.book_info.cover_source)
+
manifest.append(etree.fromstring(
'<item id="cover" href="cover.html" media-type="application/xhtml+xml" />'))
manifest.append(etree.fromstring(
- '<item id="cover-image" href="%s" media-type="%s" />' % (c_name, c.mime_type())))
+ '<item id="cover-image" href="%s" media-type="%s" />' % (cover_name, bound_cover.mime_type())))
spine.insert(0, etree.fromstring('<itemref idref="cover" linear="no" />'))
opf.getroot()[0].append(etree.fromstring('<meta name="cover" content="cover-image"/>'))
guide.append(etree.fromstring('<reference href="cover.html" type="cover" title="Okładka"/>'))
wldoc: a WLDocument
sample=n: generate sample e-book (with at least n paragraphs)
- cover: a cover.Cover object
+ cover: a cover.Cover factory overriding default
flags: less-advertising,
"""
if not cover:
cover = WLCover
cover_file = NamedTemporaryFile(suffix='.png', delete=False)
- c = cover(book_info)
- c.save(cover_file)
+ bound_cover = cover(book_info)
+ bound_cover.save(cover_file)
- if cover.uses_dc_cover:
+ if bound_cover.uses_dc_cover:
if document.book_info.cover_by:
document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
if document.book_info.cover_source:
verbose: prints all output from LaTeX
save_tex: path to save the intermediary LaTeX file to
morefloats (old/new/none): force specific morefloats
- cover: a cover.Cover object
+ cover: a cover.Cover factory or True for default
flags: less-advertising,
customizations: user requested customizations regarding various formatting parameters (passed to wl LaTeX class)
"""
if cover:
if cover is True:
cover = WLCover
- document.edoc.getroot().set('data-cover-width', str(cover.width))
- document.edoc.getroot().set('data-cover-height', str(cover.height))
- if cover.uses_dc_cover:
+ bound_cover = cover(document.book_info)
+ document.edoc.getroot().set('data-cover-width', str(bound_cover.width))
+ document.edoc.getroot().set('data-cover-height', str(bound_cover.height))
+ if bound_cover.uses_dc_cover:
if document.book_info.cover_by:
document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
if document.book_info.cover_source:
temp = mkdtemp('-wl2pdf')
if cover:
- c = cover(document.book_info)
with open(os.path.join(temp, 'cover.png'), 'w') as f:
- c.save(f)
+ bound_cover.save(f)
del document # no longer needed large object :)
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os
-import optparse
+from StringIO import StringIO
+from librarian import OutputFile
+from librarian.book2anything import Book2Anything, Option
-from librarian import ParseError
-from librarian.parser import WLDocument
-from librarian.cover import WLCover
+class Book2Cover(Book2Anything):
+ format_name = "JPEG"
+ ext = "jpg"
+ uses_cover = True
+ cover_optional = False
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Create cover images for SOURCE files."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
-
- options, input_filenames = parser.parse_args()
+ @staticmethod
+ def transform(wldoc, cover):
+ output = StringIO()
+ cover(wldoc.book_info).save(output)
+ return OutputFile.from_string(output.getvalue())
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
- # Do some real work
- for input_filename in input_filenames:
- if options.verbose:
- print input_filename
-
- output_filename = os.path.splitext(input_filename)[0] + '.jpg'
-
- doc = WLDocument.from_file(input_filename)
- WLCover(doc.book_info).save(output_filename)
+if __name__ == '__main__':
+ Book2Cover.run()
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os.path
-import optparse
+from librarian.book2anything import Book2Anything, Option
-from librarian import DirDocProvider, ParseError
-from librarian.parser import WLDocument
+class Book2Epub(Book2Anything):
+ format_name = "EPUB"
+ ext = "epub"
+ uses_cover = True
+ uses_provider = True
+ transform_flags = [
+ Option('-w', '--working-copy', dest='working-copy',
+ action='store_true', default=False,
+ help='mark the output as a working copy')
+ ]
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to EPUB format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
- help='create default cover')
- parser.add_option('-w', '--working-copy', action='store_true', dest='working_copy', default=False,
- help='mark the output as a working copy')
- parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
- help='create a directory for author and put the PDF in it')
- parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
- help='specifies the output file')
- parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
- help='specifies the directory for output')
-
- options, input_filenames = parser.parse_args()
-
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- flags = []
- if options.working_copy:
- flags.append('working-copy')
- # Do some real work
- try:
- for main_input in input_filenames:
- if options.verbose:
- print main_input
-
- path, fname = os.path.realpath(main_input).rsplit('/', 1)
- provider = DirDocProvider(path)
- if not (options.output_file or options.output_dir):
- output_file = os.path.splitext(main_input)[0] + '.epub'
- else:
- output_file = None
-
- doc = WLDocument.from_file(main_input, provider=provider)
- epub = doc.as_epub(cover=options.with_cover, flags=flags)
-
- doc.save_output_file(epub,
- output_file, options.output_dir, options.make_dir, 'epub')
-
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': main_input,
- 'name': e.__class__.__name__,
- 'message': e
- }
+if __name__ == '__main__':
+ Book2Epub.run()
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os.path
-import optparse
+from librarian.book2anything import Book2Anything
-from librarian import DirDocProvider, ParseError
-from librarian.parser import WLDocument
+class Book2Fb2(Book2Anything):
+ format_name = "FB2"
+ ext = "fb2"
+ uses_cover = False
+ uses_provider = True
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to FB2 format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
- help='create default cover')
- parser.add_option('-w', '--working-copy', action='store_true', dest='working_copy', default=False,
- help='mark the output as a working copy')
- parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
- help='create a directory for author and put the PDF in it')
- parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
- help='specifies the output file')
- parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
- help='specifies the directory for output')
-
- options, input_filenames = parser.parse_args()
-
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- flags = []
- if options.working_copy:
- flags.append('working-copy')
- # Do some real work
- try:
- for main_input in input_filenames:
- if options.verbose:
- print main_input
-
- path, fname = os.path.realpath(main_input).rsplit('/', 1)
- provider = DirDocProvider(path)
- if not (options.output_file or options.output_dir):
- output_file = os.path.splitext(main_input)[0] + '.fb2'
- else:
- output_file = None
-
- doc = WLDocument.from_file(main_input, provider=provider)
- fb2 = doc.as_fb2(cover=options.with_cover, flags=flags)
-
- doc.save_output_file(fb2,
- output_file, options.output_dir, options.make_dir, 'fb2')
-
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': main_input,
- 'name': e.__class__.__name__,
- 'message': e
- }
+if __name__ == '__main__':
+ Book2Fb2.run()
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os
-import optparse
-
-from librarian import ParseError
-from librarian.parser import WLDocument
+from librarian.book2anything import Book2Anything, Option
+
+
+class Book2Html(Book2Anything):
+ format_name = "HTML"
+ ext = "html"
+ uses_cover = False
+ uses_provider = False
+ transform_flags = [
+ Option('-r', '--raw', dest='full-page',
+ action='store_false', default=True,
+ help='output raw text for use in templates')
+ ]
+ parser_args = [
+ Option('-i', '--ignore-dublin-core', dest='parse_dublincore',
+ action='store_false', default=True,
+ help='don\'t try to parse dublin core metadata')
+ ]
if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to HTML format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-i', '--ignore-dublin-core', action='store_false', dest='parse_dublincore', default=True,
- help='don\'t try to parse dublin core metadata')
- parser.add_option('-r', '--raw', action='store_false', dest='full_page', default=True,
- help='outpu raw text for use in templates')
-
- options, input_filenames = parser.parse_args()
-
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- # Do some real work
- for input_filename in input_filenames:
- if options.verbose:
- print input_filename
-
- output_filename = os.path.splitext(input_filename)[0] + '.html'
- try:
- doc = WLDocument.from_file(input_filename,
- parse_dublincore=options.parse_dublincore)
- flags = ('full-page',) if options.full_page else None
- html = doc.as_html(flags=flags)
- doc.save_output_file(html, output_path=output_filename)
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e,
- }
- except IOError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e.strerror,
- }
- except BaseException, e:
- print '%(file)s:%(etype)s:%(message)s' % {
- 'file': input_filename,
- 'etype': e.__class__.__name__,
- 'message': e,
- }
- raise
-
+ Book2Html.run()
+++ /dev/null
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
-# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
-#
-import os
-import optparse
-
-from librarian import ParseError
-from librarian.parser import WLDocument
-
-
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to HTML format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-i', '--ignore-dublin-core', action='store_false', dest='parse_dublincore', default=True,
- help='don\'t try to parse dublin core metadata')
-
- options, input_filenames = parser.parse_args()
-
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- # Do some real work
- for input_filename in input_filenames:
- if options.verbose:
- print input_filename
-
- output_filename = os.path.splitext(input_filename)[0] + '.html'
- try:
- doc = WLDocument.from_file(input_filename,
- parse_dublincore=options.parse_dublincore)
- html = doc.as_html(flags=('full-page',), stylesheet='partial')
- doc.save_output_file(html, output_path=output_filename)
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e.message.encode('utf-8')
- }
- except IOError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e.strerror,
- }
- except BaseException, e:
- print '%(file)s:%(etype)s:%(message)s' % {
- 'file': input_filename,
- 'etype': e.__class__.__name__,
- 'message': e.message.encode('utf-8'),
- }
- raise
-
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os.path
-import optparse
+from librarian.book2anything import Book2Anything
-from librarian import DirDocProvider, ParseError
-from librarian.parser import WLDocument
+class Book2Mobi(Book2Anything):
+ format_name = "MOBI"
+ ext = "mobi"
+ uses_cover = True
+ cover_optional = False
+ uses_provider = True
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to MOBI format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
- help='create a directory for author and put the PDF in it')
- parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
- help='specifies the output file')
- parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
- help='specifies the directory for output')
-
- options, input_filenames = parser.parse_args()
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- # Do some real work
- try:
- for main_input in input_filenames:
- path, fname = os.path.realpath(main_input).rsplit('/', 1)
- provider = DirDocProvider(path)
- if not (options.output_file or options.output_dir):
- output_file = os.path.splitext(main_input)[0] + '.mobi'
- else:
- output_file = None
-
- doc = WLDocument.from_file(main_input, provider=provider)
- mobi = doc.as_mobi()
-
- doc.save_output_file(mobi,
- output_file, options.output_dir, options.make_dir, 'mobi')
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': main_input,
- 'name': e.__class__.__name__,
- 'message': e
- }
+if __name__ == '__main__':
+ Book2Mobi.run()
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os.path
-from optparse import OptionParser
+from librarian.book2anything import Book2Anything, Option
-from librarian import DirDocProvider, ParseError
-from librarian.parser import WLDocument
+class Book2Pdf(Book2Anything):
+ format_name = "PDF"
+ ext = "pdf"
+ uses_cover = True
+ uses_provider = True
+ transform_args = [
+ Option('-t', '--save-tex', dest='save_tex', metavar='FILE',
+ help='path to save the intermediary LaTeX file to'),
+ Option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
+ help='force morefloats in old (<1.0c), new (>=1.0c) or none')
+ ]
-if __name__ == '__main__':
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to PDF format."""
-
- parser = OptionParser(usage)
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='make lots of noise and revert to default interaction in LaTeX')
- parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
- help='create default cover')
- parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
- help='create a directory for author and put the PDF in it')
- parser.add_option('-t', '--save-tex', dest='save_tex', metavar='FILE',
- help='path to save the intermediary LaTeX file to')
- parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
- help='specifies the output file')
- parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
- help='specifies the directory for output')
- parser.add_option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
- help='force morefloats in old (<1.0c), new (>=1.0c) or none')
- (options, args) = parser.parse_args()
-
- if len(args) < 1:
- parser.print_help()
- exit(1)
-
- if options.output_dir and options.output_file:
- raise ValueError("Either --output-dir or --output file should be specified")
- try:
- for main_input in args:
- path, fname = os.path.realpath(main_input).rsplit('/', 1)
- provider = DirDocProvider(path)
- output_file, output_dir = options.output_file, options.output_dir
- if not (options.output_file or options.output_dir):
- output_file = os.path.splitext(main_input)[0] + '.pdf'
- else:
- output_file = None
-
- doc = WLDocument.from_file(main_input, provider=provider)
- pdf = doc.as_pdf(save_tex=options.save_tex,
- cover=options.with_cover,
- morefloats=options.morefloats)
-
- doc.save_output_file(pdf,
- output_file, options.output_dir, options.make_dir, 'pdf')
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s; use -v to see more output' % {
- 'file': main_input,
- 'name': e.__class__.__name__,
- 'message': e
- }
+if __name__ == '__main__':
+ Book2Pdf.run()
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import os
-import optparse
+from librarian.book2anything import Book2Anything, Option
-from librarian import ParseError
-from librarian.parser import WLDocument
+class Book2Txt(Book2Anything):
+ format_name = "TXT"
+ ext = "txt"
+ uses_cover = False
+ uses_provider = False
+ parser_args = [
+ Option('-i', '--ignore-dublin-core', dest='parse_dublincore',
+ action='store_false', default=True,
+ help='don\'t try to parse dublin core metadata')
+ ]
+ transform_args = [
+ Option('-w', '--wrap', action='store', type='int', dest='wrapping', default=0,
+ help='set line wrap column')
+ ]
-if __name__ == '__main__':
- # Parse commandline arguments
- usage = """Usage: %prog [options] SOURCE [SOURCE...]
- Convert SOURCE files to TXT format."""
-
- parser = optparse.OptionParser(usage=usage)
-
- parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
- help='print status messages to stdout')
- parser.add_option('-w', '--wrap', action='store', type='int', dest='wrapping', default=0,
- help='set line wrap column')
- parser.add_option('-i', '--ignore-dublin-core', action='store_false', dest='parse_dublincore', default=True,
- help='don\'t try to parse dublin core metadata')
-
- options, input_filenames = parser.parse_args()
- if len(input_filenames) < 1:
- parser.print_help()
- exit(1)
-
- # Do some real work
- for input_filename in input_filenames:
- if options.verbose:
- print input_filename
-
- output_filename = os.path.splitext(input_filename)[0] + '.txt'
- try:
- doc = WLDocument.from_file(input_filename,
- parse_dublincore=options.parse_dublincore)
- html = doc.as_text(wrapping=str(options.wrapping))
- doc.save_output_file(html, output_path=output_filename)
- except ParseError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e
- }
- except IOError, e:
- print '%(file)s:%(name)s:%(message)s' % {
- 'file': input_filename,
- 'name': e.__class__.__name__,
- 'message': e.strerror,
- }
- except BaseException, e:
- print '%(file)s:%(etype)s:%(message)s' % {
- 'file': input_filename,
- 'etype': e.__class__.__name__,
- 'message': e,
- }
- raise
+if __name__ == '__main__':
+ Book2Txt.run()
setup(
name='librarian',
- version='1.4.2a',
+ version='1.5',
description='Converter from WolneLektury.pl XML-based language to XHTML, TXT and other formats',
author="Marek Stępniowski",
author_email='marek@stepniowski.com',
maintainer='Radek Czajka',
- maintainer_email='radek.czajka@gmail.com',
+ maintainer_email='radoslaw.czajka@nowoczesnapolska.org.pl',
url='http://github.com/fnp/librarian',
packages=['librarian'],
package_data={'librarian': ['xslt/*.xslt', 'epub/*', 'mobi/*', 'pdf/*', 'fonts/*', 'res/*'] +