From 9123398bd03d1e08256bdc97959cf981cc1a5f76 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Mon, 14 Oct 2013 13:59:37 +0200 Subject: [PATCH] epub changes --- librarian/book2anything.py | 6 +- librarian/cover.py | 19 +- librarian/epub.py | 35 ++- librarian/epub/style.css | 5 +- librarian/epub/toc.html | 2 +- librarian/epub/xsltContent.xsl | 3 + librarian/epub/xsltLast.xsl | 388 +++++++++++++++++++++++++++++---- librarian/epub/xsltScheme.xsl | 46 +++- librarian/pdf.py | 2 +- librarian/pdf/wl.cls | 8 + scripts/book2epub | 6 + scripts/book2pdf | 5 +- 12 files changed, 450 insertions(+), 75 deletions(-) diff --git a/librarian/book2anything.py b/librarian/book2anything.py index cf3c49d..d76177c 100755 --- a/librarian/book2anything.py +++ b/librarian/book2anything.py @@ -10,7 +10,7 @@ import optparse from librarian import DirDocProvider, ParseError from librarian.parser import WLDocument -from librarian.cover import WLCover, FutureOfCopyrightCover +from librarian.cover import WLCover, FutureOfCopyrightCover, CoverFromFile class Option(object): @@ -102,6 +102,10 @@ class Book2Anything(object): def cover_class(*args, **kwargs): return FutureOfCopyrightCover(image_cache=options.image_cache, *args, **kwargs) transform_args['cover'] = cover_class + elif options.cover_file: + def cover_class(*args, **kwargs): + return CoverFromFile(options.cover_file, *args, **kwargs) + transform_args['cover'] = cover_class elif not cls.cover_optional or options.with_cover: transform_args['cover'] = FutureOfCopyrightCover diff --git a/librarian/cover.py b/librarian/cover.py index ff01841..efb2974 100644 --- a/librarian/cover.py +++ b/librarian/cover.py @@ -7,7 +7,7 @@ import re from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageEnhance from StringIO import StringIO from librarian import get_resource, OutputFile, URLOpener - +from os import path class Metric(object): """Gets metrics from an object, scaling it by a factor.""" @@ -437,10 +437,25 @@ class GandalfCover(Cover): format = 'PNG' class FutureOfCopyrightCover(Cover): - width = 420 + width = 402 height = 595 background_img = 'cover.png' format = 'PNG' def save(self, dest): dest.write(open(self.background_img).read()) + + +class CoverFromFile(Cover): + def __init__(self, filename, *args, **kwargs): + super(CoverFromFile, self).__init__(*args, **kwargs) + + self.background_img = filename + + self.img = Image.open(filename) + (self.width, self.height) = self.img.size + + self.format = path.splitext(filename)[-1][1:].upper() + + def image(self): + return self.img diff --git a/librarian/epub.py b/librarian/epub.py index 841b7c5..43eb295 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -15,6 +15,7 @@ from lxml import etree import zipfile from tempfile import mkdtemp, NamedTemporaryFile from shutil import rmtree +from mimetypes import guess_type from librarian import RDFNS, WLNS, NCXNS, OPFNS, XHTMLNS, OutputFile from librarian.cover import WLCover, FutureOfCopyrightCover @@ -331,7 +332,8 @@ def transform_chunk(chunk_xml, chunk_no, annotations, empty=False, _empty_html_s def transform(wldoc, verbose=False, style=None, html_toc=False, - sample=None, cover=None, flags=None, resources=None): + sample=None, cover=None, flags=None, resources=None, + intro_file=None, cover_file=None): """ produces a EPUB file sample=n: generate sample e-book (with at least n paragraphs) @@ -355,7 +357,7 @@ def transform(wldoc, verbose=False, zip.writestr('OPS/title.html', etree.tostring(html_tree, method="html", pretty_print=True)) # add a title page TOC entry - toc.add(u"Title", "title.html") + toc.add(u"Tytuł", "title.html") elif wldoc.book_info.parts: # write title page for every parent if sample is not None and sample <= 0: @@ -448,6 +450,9 @@ def transform(wldoc, verbose=False, fpath = os.path.join(dp, fname) if os.path.isfile(fpath): zip.write(fpath, os.path.join('OPS', fname)) + manifest.append(etree.fromstring( + '' % (os.path.splitext(fname)[0], fname, guess_type(fpath)[0]))) + else: print "resources path %s is not directory" % resources @@ -494,10 +499,20 @@ def transform(wldoc, verbose=False, nav_map = toc_file[-1] manifest.append(etree.fromstring( - '')) + '')) spine.append(etree.fromstring( - '')) - zip.writestr('OPS/intro.html', open(get_resource('epub/intro.html')).read()) + '')) + html_tree = xslt(document.edoc, get_resource('epub/xsltFirst.xsl')) +# chars.update(used_chars(html_tree.getroot())) + zip.writestr('OPS/first.html', etree.tostring( + html_tree, method="html", pretty_print=True)) + + if intro_file: + manifest.append(etree.fromstring( + '')) + spine.append(etree.fromstring( + '')) + zip.writestr('OPS/intro.html', open(intro_file or get_resource('epub/intro.html')).read()) if html_toc: @@ -509,12 +524,14 @@ def transform(wldoc, verbose=False, toc, chunk_counter, chars, sample = transform_file(document, sample=sample) + toc.add("Informacje redakcyjne", "first.html", index=0) + if len(toc.children) < 2: - toc.add(u"Beginning of the book", "part1.html") + toc.add(u"Początek książki", "part1.html") # Last modifications in container files and EPUB creation if len(annotations) > 0: - toc.add("Footnotes", "annotations.html") + toc.add("Przypisy", "annotations.html") manifest.append(etree.fromstring( '')) spine.append(etree.fromstring( @@ -534,7 +551,7 @@ def transform(wldoc, verbose=False, # chars.update(used_chars(etree.fromstring(html_string))) # zip.writestr('OPS/support.html', html_string) - toc.add("Editors", "last.html") + toc.add("Informacje redakcyjne", "last.html") manifest.append(etree.fromstring( '')) spine.append(etree.fromstring( @@ -582,7 +599,7 @@ def transform(wldoc, verbose=False, # write TOC if html_toc: - toc.add(u"Table of Contents", "toc.html", index=1) + toc.add(u"Spis treści", "toc.html", index=1) zip.writestr('OPS/toc.html', toc.html().encode('utf-8')) toc.write_to_xml(nav_map) zip.writestr('OPS/toc.ncx', etree.tostring(toc_file, pretty_print=True)) diff --git a/librarian/epub/style.css b/librarian/epub/style.css index 622c8da..a27c5cf 100644 --- a/librarian/epub/style.css +++ b/librarian/epub/style.css @@ -75,10 +75,10 @@ a img { .h4 { - font-size: 1em; + font-size: 1em; margin: 0; margin-top: 1.5em; - line-height: 1.5em; + line-height: 1.5em; } p @@ -370,3 +370,4 @@ p.minor { p.footer { margin-top: 2em; } + diff --git a/librarian/epub/toc.html b/librarian/epub/toc.html index 1c2887d..ae004c2 100755 --- a/librarian/epub/toc.html +++ b/librarian/epub/toc.html @@ -2,7 +2,7 @@ - Future of Copyright + Table of Contents

Table of Contents

diff --git a/librarian/epub/xsltContent.xsl b/librarian/epub/xsltContent.xsl index eb14524..cd99add 100644 --- a/librarian/epub/xsltContent.xsl +++ b/librarian/epub/xsltContent.xsl @@ -12,6 +12,9 @@ + + + diff --git a/librarian/epub/xsltLast.xsl b/librarian/epub/xsltLast.xsl index bb83777..7c62d80 100644 --- a/librarian/epub/xsltLast.xsl +++ b/librarian/epub/xsltLast.xsl @@ -15,55 +15,16 @@ - <xsl:text>Credits</xsl:text> + <xsl:text>Informacje redakcyjne</xsl:text>
- - -

The Future of Copyright 2.0 Contest is a~part of Future of Copyright Project supported by Trust for Civil Society in Central and Eastern Europe

- - - -

This book is available under the terms of Creative Commons Attribution-ShareAlike 3.0 Unported License (read more: -http://creativecommons.org/licenses/by-sa/3.0/)
-Published by Modern Poland Foundation, Warsaw 2013

- -

Technical editor: Paulina Choromańska
-Book design: Jakub Waluchowski
-Typography: Marcin Koziej

- -

Modern Poland Fundation
-Marszalkowska St. 84/92 app. 125
-00-514 Warsaw
-tel/fax: +48 22 621-30-17
-email: fundacja@nowoczesnapolska.org.pl
-

- - -

If you wish to support our projects, feel free to make a~donation via our secure online payment service: -PLN or USD - or direct payment account number: 59 1030 0019 0109 8530 0040 5685

- -

Thank you for your support.

- - - - - - - -

 

-

- Generated on . -

- + +
+ +
+
@@ -88,4 +49,341 @@ email: fundacja@nowoczesnapolska.org.pl
+ + + + + + + + +
+ +
+
+ + +
+
+ +
+
    + +
+
+
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
+ + + + + + +

+ +

+
+ + + + + + + +

+ +

+
+ + + + +

+ +

+
+ + + + +

+ +

+
+ + + + +

+ +

+
+ + +

+ +

+
+ + + +

+ +

+
+ + + +

+ +

+
+ + + + + +

+ + - + + +

+
+ + +

+ +

+
+ + +
+ +
+
+ + +
+ +
+
+ + +

+ +

+
+ + +
+ +
 
+
+ + +
+ +  
+
+ + +
+ +  
+
+ + +
+ +  
+
+ + +
+ + margin-left: em; + + +  
+
+ + +
+ +  
+
+ + +
+ +
+
+ + +
    + +
+
+ + +
  • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + „ + + ” + + + + + + + + + + + + + + + + + + + + +
  • + +
  • +
    + + + + + + +

     

    +
    + + +

    *

    +
    + + +
    +
    + + + + + + + + + + + + + + + [] + + + + + + + + + + + + + + + + + + + + + + + + + + . + + + + + diff --git a/librarian/epub/xsltScheme.xsl b/librarian/epub/xsltScheme.xsl index c3eb9fe..4060d62 100644 --- a/librarian/epub/xsltScheme.xsl +++ b/librarian/epub/xsltScheme.xsl @@ -1,5 +1,5 @@ - + @@ -10,7 +10,7 @@ - Future of Copyright + <xsl:value-of select="//dc:title"/> @@ -36,9 +36,11 @@ -
    - -
    + +
    + +
    +
    @@ -87,9 +89,7 @@ - - - +

    @@ -140,17 +140,27 @@

    + + + +
    + +
    +
    + -

    - - - - +

    +
    + + + +
    @@ -343,6 +353,8 @@ + + @@ -355,4 +367,14 @@ + + + + + + . + + + +
    diff --git a/librarian/pdf.py b/librarian/pdf.py index eb3f942..ead91be 100644 --- a/librarian/pdf.py +++ b/librarian/pdf.py @@ -185,7 +185,7 @@ def package_available(package, args='', verbose=False): def transform(wldoc, verbose=False, save_tex=None, save_texml=None, morefloats=None, - cover=None, flags=None, customizations=None, documentclass='wl', resources=None): + cover=None, cover_file=None, flags=None, customizations=None, documentclass='wl', resources=None): """ produces a PDF file with XeLaTeX wldoc: a WLDocument diff --git a/librarian/pdf/wl.cls b/librarian/pdf/wl.cls index 05f2aba..55813dc 100644 --- a/librarian/pdf/wl.cls +++ b/librarian/pdf/wl.cls @@ -648,3 +648,11 @@ Mapping=tex-text \fi } +\newcommand{\spistresci}{ + \tableofcontents + \newpage +} + +\newcommand{\link}[2]{\href{#1}{\uline{#2}\footnote{#1}}} + +\newcommand{\www}[1]{{\normalfont\href{#1}{#1}}} diff --git a/scripts/book2epub b/scripts/book2epub index ff2c668..f87bf05 100755 --- a/scripts/book2epub +++ b/scripts/book2epub @@ -16,8 +16,14 @@ class Book2Epub(Book2Anything): Option('-w', '--working-copy', dest='working-copy', action='store_true', default=False, help='mark the output as a working copy'), + ] + transform_options = [ Option('-R', '--resources', dest='resources', metavar='DIR', help='a directory with additional resources'), + Option('-I', '--intro', dest='intro_file', metavar='FILE', + help='HTML file containing intro'), + Option('-e', '--cover-file', dest='cover_file', metavar='FILE', + help='Cover image file'), ] diff --git a/scripts/book2pdf b/scripts/book2pdf index 87afc1e..cda7381 100755 --- a/scripts/book2pdf +++ b/scripts/book2pdf @@ -26,8 +26,9 @@ class Book2Pdf(Book2Anything): Option('-D', '--documentclass', dest='documentclass', help='LaTeX document class, defaults to "wl"'), Option('-s', '--customizations', dest='customizations', action='append', - help='LaTeX document class options') - + help='LaTeX document class options'), + Option('-e', '--cover-file', dest='cover_file', metavar='FILE', + help='Cover image file'), ] -- 2.20.1