Remove DateValue, drop Py<3.6, fix tests.
[librarian.git] / src / librarian / builders / epub.py
index 91405c3..8544846 100644 (file)
@@ -9,9 +9,6 @@ from librarian.cover import make_cover
 from librarian.embeds.mathml import MathML
 import librarian.epub
 from librarian.fonts import strip_font
-from librarian.fundraising import FUNDRAISING
-
-
 
 
 class Xhtml:
@@ -30,9 +27,11 @@ class Xhtml:
 class Builder:
     file_extension = None
 
-    def __init__(self, base_url=None):
+    def __init__(self, base_url=None, fundraising=None, cover=None):
         self._base_url = base_url or 'file:///home/rczajka/for/fnp/librarian/temp~/maly/img/'
+        self.fundraising = fundraising
         self.footnotes = etree.Element('div', id='footnotes')
+        self.make_cover = cover or make_cover
 
         self.cursors = {
 #            None: None,
@@ -60,8 +59,6 @@ class Builder:
     def forget_fragment(self, name):
         del self.cursors[name]
 
-
-
     @property
     def base_url(self):
         if self._base_url is not None:
@@ -78,6 +75,8 @@ class Builder:
 
 class EpubBuilder(Builder):
     file_extension = 'epub'
+    isbn_field = 'isbn_epub'
+    orphans = True
 
     def __init__(self, *args, **kwargs):
         self.chars = set()
@@ -109,7 +108,6 @@ class EpubBuilder(Builder):
 
         self.set_metadata()
         
-
         self.add_cover()
         
         self.add_title_page()
@@ -140,21 +138,21 @@ class EpubBuilder(Builder):
         self.add_support_page()
         self.add_last_page()
 
+        if self.fundraising:
+            e = len(self.output.spine) - 3 - 3
+            nfunds = len(self.fundraising)
+            if e > 4 * nfunds:
+                nfunds *= 2
 
-        e = len(self.output.spine) - 3 - 3
-        nfunds = len(FUNDRAISING)
-        if e > 16:
-            nfunds *= 2
-
-        # COUNTING CHARACTERS?
-        for f in range(nfunds):
-            spine_index = int(4 + (f / nfunds * e) + f)
+            # COUNTING CHARACTERS?
+            for f in range(nfunds):
+                spine_index = int(4 + (f / nfunds * e) + f)
 
-            h = Xhtml()
-            h.body.append(
-                etree.XML('<div id="book-text"><div class="fundraising">' + FUNDRAISING[f % len(FUNDRAISING)] + '</div></div>')
-            )
-            self.add_html(h.element, file_name='fund%d.xhtml' % f, spine=spine_index)
+                h = Xhtml()
+                h.body.append(
+                    etree.XML('<div id="book-text"><div class="fundraising">' + self.fundraising[f % len(self.fundraising)] + '</div></div>')
+                )
+                self.add_html(h.element, file_name='fund%d.xhtml' % f, spine=spine_index)
 
         self.add_fonts()
 
@@ -202,7 +200,7 @@ class EpubBuilder(Builder):
     def add_title_page(self):
         html = Xhtml()
         html.title.text = "Strona tytułowa"
-        bt = etree.SubElement(html.body, 'div', **{'class': 'book-text'})
+        bt = etree.SubElement(html.body, 'div', **{'id': 'book-text'})
         tp = etree.SubElement(bt, 'div', **{'class': 'title-page'})
 
         # Tak jak jest teraz – czy może być jednocześnie
@@ -212,10 +210,10 @@ class EpubBuilder(Builder):
 
         e = self.document.tree.find('//autor_utworu')
         if e is not None:
-            etree.SubElement(tp, 'h2', **{'class': 'author'}).text = e.raw_printable_text()
+            etree.SubElement(tp, 'h2', **{'class': 'author'}).text = e.raw_printable_text(self)
         e = self.document.tree.find('//nazwa_utworu')
         if e is not None:
-            etree.SubElement(tp, 'h1', **{'class': 'title'}).text = e.raw_printable_text()
+            etree.SubElement(tp, 'h1', **{'class': 'title'}).text = e.raw_printable_text(self)
 
         if not len(tp):
             for author in self.document.meta.authors:
@@ -251,8 +249,8 @@ class EpubBuilder(Builder):
           </p>
         """))
 
-        if self.document.meta.isbn_epub:
-            etree.SubElement(tp, 'p', **{"class": "info"}).text = self.document.meta.isbn_epub
+        if getattr(self.document.meta, self.isbn_field):
+            etree.SubElement(tp, 'p', **{"class": "info"}).text = getattr(self.document.meta, self.isbn_field)
 
         tp.append(etree.XML("""<p class="footer info">
             <a href="http://www.wolnelektury.pl/"><img src="logo_wolnelektury.png" alt="WolneLektury.pl" /></a>
@@ -302,6 +300,7 @@ class EpubBuilder(Builder):
 
     def add_toc(self):
         item = epub.EpubNav()
+        item.add_link(href='style.css', rel='stylesheet', type='text/css')
         self.output.add_item(item)
         self.output.spine.append(item)
         self.output.add_item(epub.EpubNcx())
@@ -389,8 +388,6 @@ class EpubBuilder(Builder):
             
         
     def add_fonts(self):
-        print("".join(sorted(self.chars)))
-        # TODO: optimizer
         for fname in ('DejaVuSerif.ttf', 'DejaVuSerif-Bold.ttf',
                       'DejaVuSerif-Italic.ttf', 'DejaVuSerif-BoldItalic.ttf'):
             self.add_file(
@@ -531,27 +528,46 @@ class EpubBuilder(Builder):
         newp = lambda: etree.SubElement(d, 'p', {'class': 'info'})
 
         p = newp()
+        p.text = (
+            "Wszystkie zasoby Wolnych Lektur możesz swobodnie wykorzystywać, "
+            "publikować i rozpowszechniać pod warunkiem zachowania warunków "
+            "licencji i zgodnie z "
+        )
+        a = etree.SubElement(p, "a", href="https://wolnelektury.pl/info/zasady-wykorzystania/")
+        a.text = "Zasadami wykorzystania Wolnych Lektur"
+        a.tail = "."
+
+        etree.SubElement(p, "br")
+        
+
         if m.license:
-            p.text = """
-                      Ten utwór jest udostępniony na licencji
-                      """
+            p[-1].tail = "Ten utwór jest udostępniony na licencji "
             etree.SubElement(p, 'a', href=m.license).text = m.license_description
         else:
-            p.text = """
-                    Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się w domenie
-                    publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
-                    i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami
-                    (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to
-                    te dodatkowe materiały udostępnione są na licencji
-                    """
-            a = etree.SubElement(p, "a", href="http://creativecommons.org/licenses/by-sa/3.0/")
-            a.text = """Creative Commons
-                    Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL"""
-            a.tail = "."
+            p[-1].tail = 'Ten utwór jest w domenie publicznej.'
+
+        etree.SubElement(p, "br")
+        
+        p[-1].tail = (
+            "Wszystkie materiały dodatkowe (przypisy, motywy literackie) są "
+            "udostępnione na "
+            )
+        etree.SubElement(p, 'a', href='https://artlibre.org/licence/lal/pl/').text = 'Licencji Wolnej Sztuki 1.3'
+        p[-1].tail = '.'
+        etree.SubElement(p, "br")
+        p[-1].tail = (
+            "Fundacja Nowoczesna Polska zastrzega sobie prawa do wydania "
+            "krytycznego zgodnie z art. Art.99(2) Ustawy o prawach autorskich "
+            "i prawach pokrewnych. Wykorzystując zasoby z Wolnych Lektur, "
+            "należy pamiętać o zapisach licencji oraz zasadach, które "
+            "spisaliśmy w "
+        )
 
+        etree.SubElement(p, 'a', href='https://wolnelektury.pl/info/zasady-wykorzystania/').text = 'Zasadach wykorzystania Wolnych Lektur'
+        p[-1].tail = '. Zapoznaj się z nimi, zanim udostępnisz dalej nasze książki.'
 
         p = newp()
-        p.text = 'Źródło: '
+        p.text = 'E-book można pobrać ze strony: '
         etree.SubElement(
             p, 'a', href=str(m.url),
             title=', '.join((
@@ -560,7 +576,8 @@ class EpubBuilder(Builder):
             ))
         ).text = str(m.url)
 
-        newp().text = 'Tekst opracowany na podstawie: ' + m.source_name
+        if m.source_name:
+            newp().text = 'Tekst opracowany na podstawie: ' + m.source_name
 
         newp().text = """
               Wydawca:
@@ -590,8 +607,8 @@ class EpubBuilder(Builder):
             else:
                 p.text += m.cover_by
             
-        if m.isbn_epub:
-            newp().text = m.isbn_epub
+        if getattr(m, self.isbn_field):
+            newp().text = getattr(m, self.isbn_field)
 
         newp().text = '\u00a0'
 
@@ -645,10 +662,10 @@ class EpubBuilder(Builder):
     def add_cover(self):
         # TODO: allow other covers
 
-        cover_maker = make_cover
+        cover_maker = self.make_cover
 
         cover_file = six.BytesIO()
-        cover = cover_maker(self.document.meta)
+        cover = cover_maker(self.document.meta, width=600)
         cover.save(cover_file)
         cover_name = 'cover.%s' % cover.ext()
 
@@ -673,7 +690,7 @@ class EpubBuilder(Builder):
 </html>''' % cover.ext()).encode('utf-8')
         self.add_file(file_name='cover.xhtml', content=ci)
 
-        self.output.spine.append('cover')
+        self.output.spine.append(('cover', 'no'))
         self.output.guide.append({
             'type': 'cover',
             'href': 'cover.xhtml',