add option to select cover class in metadata + kmlu cover
authorJan Szejko <janek37@gmail.com>
Mon, 7 Aug 2017 13:24:29 +0000 (15:24 +0200)
committerJan Szejko <janek37@gmail.com>
Mon, 7 Aug 2017 13:24:29 +0000 (15:24 +0200)
librarian/book2anything.py
librarian/cover.py
librarian/dcparser.py
librarian/epub.py
librarian/parser.py
librarian/pdf.py
librarian/res/kmlu-logo-white.png [new file with mode: 0644]

index 0dcfd33..0da3b61 100755 (executable)
@@ -9,7 +9,7 @@ import optparse
 
 from librarian import DirDocProvider, ParseError
 from librarian.parser import WLDocument
-from librarian.cover import DefaultEbookCover
+from librarian.cover import make_cover
 
 
 class Option(object):
@@ -92,11 +92,11 @@ class Book2Anything(object):
         # Add cover support, if any.
         if cls.uses_cover:
             if options.image_cache:
-                def cover_class(*args, **kwargs):
-                    return DefaultEbookCover(image_cache=options.image_cache, *args, **kwargs)
+                def cover_class(book_info, *args, **kwargs):
+                    return make_cover(book_info, image_cache=options.image_cache, *args, **kwargs)
                 transform_args['cover'] = cover_class
             elif not cls.cover_optional or options.with_cover:
-                transform_args['cover'] = DefaultEbookCover
+                transform_args['cover'] = make_cover
 
         # Do some real work
         try:
index 5093de3..9e21cf4 100644 (file)
@@ -369,7 +369,7 @@ class WLCover(Cover):
             dir_y = 1 if corner_y == 0 else -1
             for offset in (-1, 0, 1):
                 draw.line((corner_x, corner_y + dir_y * metr.bleed + offset,
-                           corner_x + dir_x * metr.bleed * line_ratio, corner_y + dir_y * metr.bleed +  offset),
+                           corner_x + dir_x * metr.bleed * line_ratio, corner_y + dir_y * metr.bleed + offset),
                           fill='black' if offset == 0 else 'white', width=1)
                 draw.line((corner_x + dir_x * metr.bleed + offset, corner_y,
                            corner_x + dir_x * metr.bleed + offset, corner_y + dir_y * metr.bleed * line_ratio),
@@ -539,4 +539,22 @@ class GandalfCover(Cover):
     format = 'PNG'
 
 
-DefaultEbookCover = LogoWLCover
+class KMLUCover(LogoWLCover):
+    gradient_logo_height = 58
+    gradient_logo_spacing = 25
+    gradient_logos = [
+        'res/kmlu-logo-white.png',
+        'res/wl-logo-white.png',
+        'res/fnp-logo-white.png',
+    ]
+
+
+COVER_CLASSES = {
+    'default': LogoWLCover,
+    'kmlu': KMLUCover,
+}
+
+
+def make_cover(book_info, *args, **kwargs):
+    cover_class = COVER_CLASSES[book_info.cover_class]
+    return cover_class(book_info, *args, **kwargs)
index 0a2822c..b5b1c22 100644 (file)
@@ -230,10 +230,10 @@ class WorkInfo(object):
         Field(DCNS('type'), 'type', required=False, multiple=True),
 
         Field(DCNS('contributor.editor'), 'editors',
-              as_person, salias='editor', multiple=True, default=[]),
+              as_person, salias='editor', multiple=True, required=False),
         Field(DCNS('contributor.technical_editor'), 'technical_editors',
-              as_person, salias='technical_editor', multiple=True, default=[]),
-        Field(DCNS('contributor.funding'), 'funders', salias='funder', multiple=True, default=[]),
+              as_person, salias='technical_editor', multiple=True, required=False),
+        Field(DCNS('contributor.funding'), 'funders', salias='funder', multiple=True, required=False),
         Field(DCNS('contributor.thanks'), 'thanks', required=False),
 
         Field(DCNS('date'), 'created_at'),
@@ -249,7 +249,7 @@ class WorkInfo(object):
         Field(DCNS('rights.license'), 'license', required=False),
         Field(DCNS('rights'), 'license_description'),
 
-        Field(PLMETNS('digitisationSponsor'), 'sponsors', multiple=True, default=[]),
+        Field(PLMETNS('digitisationSponsor'), 'sponsors', multiple=True, required=False),
         Field(WLNS('digitisationSponsorNote'), 'sponsor_note', required=False),
         Field(WLNS('developmentStage'), 'stage', required=False),
     )
@@ -452,7 +452,7 @@ class BookInfo(WorkInfo):
         Field(DCNS('subject.genre'), 'genres', salias='genre', multiple=True, required=False),
                 
         Field(DCNS('contributor.translator'), 'translators',
-              as_person,  salias='translator', multiple=True, default=[]),
+              as_person,  salias='translator', multiple=True, required=False),
         Field(DCNS('relation.hasPart'), 'parts', WLURI, strict=as_wluri_strict, multiple=True, required=False),
         Field(DCNS('relation.isVariantOf'), 'variant_of', WLURI, strict=as_wluri_strict, required=False),
 
@@ -462,6 +462,7 @@ class BookInfo(WorkInfo):
         # WLCover-specific.
         Field(WLNS('coverBarColor'), 'cover_bar_color', required=False),
         Field(WLNS('coverBoxPosition'), 'cover_box_position', required=False),
+        Field(WLNS('coverClass'), 'cover_class', default=['default']),
         Field('pdf-id',  'isbn_pdf',  required=False),
         Field('epub-id', 'isbn_epub', required=False),
         Field('mobi-id', 'isbn_mobi', required=False),
index 95e65f1..397dc51 100644 (file)
@@ -19,7 +19,7 @@ from tempfile import mkdtemp, NamedTemporaryFile
 from shutil import rmtree
 
 from librarian import RDFNS, WLNS, NCXNS, OPFNS, XHTMLNS, DCNS, OutputFile
-from librarian.cover import DefaultEbookCover
+from librarian.cover import make_cover
 
 from librarian import functions, get_resource
 
@@ -565,7 +565,7 @@ def transform(wldoc, verbose=False, style=None, html_toc=False,
 
     if cover:
         if cover is True:
-            cover = DefaultEbookCover
+            cover = make_cover
 
         cover_file = StringIO()
         bound_cover = cover(document.book_info)
index e4f1340..b312da8 100644 (file)
@@ -5,7 +5,7 @@
 #
 from librarian import ValidationError, NoDublinCore,  ParseError, NoProvider
 from librarian import RDFNS
-from librarian.cover import DefaultEbookCover
+from librarian.cover import make_cover
 from librarian import dcparser
 
 from xml.parsers.expat import ExpatError
@@ -207,7 +207,7 @@ class WLDocument(object):
 
     def as_cover(self, cover_class=None, *args, **kwargs):
         if cover_class is None:
-            cover_class = DefaultEbookCover
+            cover_class = make_cover
         return cover_class(self.book_info, *args, **kwargs).output_file()
 
     # for debugging only
index bbe3a92..652d4bf 100644 (file)
@@ -28,7 +28,7 @@ from librarian.dcparser import Person
 from librarian.parser import WLDocument
 from librarian import ParseError, DCNS, get_resource, OutputFile
 from librarian import functions
-from librarian.cover import DefaultEbookCover
+from librarian.cover import make_cover
 from .sponsor import sponsor_logo
 
 
@@ -220,7 +220,7 @@ def transform(wldoc, verbose=False, save_tex=None, morefloats=None,
 
         if cover:
             if cover is True:
-                cover = DefaultEbookCover
+                cover = make_cover
             bound_cover = cover(book_info, width=1200)
             root.set('data-cover-width', str(bound_cover.width))
             root.set('data-cover-height', str(bound_cover.height))
diff --git a/librarian/res/kmlu-logo-white.png b/librarian/res/kmlu-logo-white.png
new file mode 100644 (file)
index 0000000..e595e71
Binary files /dev/null and b/librarian/res/kmlu-logo-white.png differ