2 # -*- coding: utf-8 -*-
 
   4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   8 from optparse import OptionParser
 
  10 from librarian import DirDocProvider, ParseError
 
  11 from librarian.cover import ImageCover
 
  12 from librarian.parser import WLDocument
 
  15 if __name__ == '__main__':
 
  16     usage = """Usage: %prog [options] SOURCE [SOURCE...]
 
  17     Convert SOURCE files to PDF format."""
 
  19     parser = OptionParser(usage)
 
  20     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
 
  21                       help='make lots of noise and revert to default interaction in LaTeX')
 
  22     parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
 
  23                       help='create default cover')
 
  24     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
 
  25                       help='create a directory for author and put the PDF in it')
 
  26     parser.add_option('-t', '--save-tex', dest='save_tex', metavar='FILE',
 
  27                       help='path to save the intermediary LaTeX file to')
 
  28     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
 
  29                       help='specifies the output file')
 
  30     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
 
  31                       help='specifies the directory for output')
 
  32     parser.add_option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
 
  33                       help='force morefloats in old (<1.0c), new (>=1.0c) or none')
 
  35     parser.add_option('-i', '--with-images', action='store_true', dest='images', default=False,
 
  36                       help='add images with <ilustr src="..."/>')
 
  37     parser.add_option('-A', '--less-advertising', action='store_true', dest='less_advertising', default=False,
 
  38                       help='less advertising, for commercial purposes')
 
  39     parser.add_option('-W', '--not-wl', action='store_true', dest='not_wl', default=False,
 
  40                       help='not a WolneLektury book')
 
  41     parser.add_option('--cover', dest='cover', metavar='FILE',
 
  42                       help='specifies the cover file')
 
  44     (options, args) = parser.parse_args()
 
  50     if options.output_dir and options.output_file:
 
  51         raise ValueError("Either --output-dir or --output file should be specified")
 
  54         for main_input in args:
 
  55             path, fname = os.path.realpath(main_input).rsplit('/', 1)
 
  56             provider = DirDocProvider(path)
 
  57             output_file, output_dir = options.output_file, options.output_dir
 
  58             if not (options.output_file or options.output_dir):
 
  59                 output_file = os.path.splitext(main_input)[0] + '.pdf'
 
  63             doc = WLDocument.from_file(main_input, provider=provider)
 
  66                 cover = ImageCover(options.cover)
 
  68                 cover = options.with_cover
 
  72                 flags.append('images')
 
  73             if options.less_advertising:
 
  74                 flags.append('less-advertising')
 
  76                 flags.append('not-wl')
 
  78             pdf = doc.as_pdf(save_tex=options.save_tex,
 
  81                         morefloats=options.morefloats,
 
  82                         verbose=options.verbose
 
  85             doc.save_output_file(pdf,
 
  86                 output_file, options.output_dir, options.make_dir, 'pdf')
 
  88         print '%(file)s:%(name)s:%(message)s; use -v to see more output' % {
 
  90             'name': e.__class__.__name__,