New EPUB builder, other minor changes.
[librarian.git] / src / librarian / builders / epub.py
1 from datetime import date
2 import os
3 import tempfile
4 from ebooklib import epub
5 from lxml import etree
6 import six
7 from librarian import functions, OutputFile, get_resource, XHTMLNS
8 from librarian.cover import make_cover
9 from librarian.embeds.mathml import MathML
10 import librarian.epub
11 from librarian.fonts import strip_font
12 from librarian.fundraising import FUNDRAISING
13
14
15
16
17 class Xhtml:
18     def __init__(self):
19         self.element = etree.XML('''<html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" href="style.css" type="text/css"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>WolneLektury.pl</title></head><body/></html>''')
20
21     @property
22     def title(self):
23         return self.element.find('.//' + XHTMLNS('title'))
24         
25     @property
26     def body(self):
27         return self.element.find('.//' + XHTMLNS('body'))
28
29
30 class Builder:
31     file_extension = None
32
33     def __init__(self, base_url=None):
34         self._base_url = base_url or 'file:///home/rczajka/for/fnp/librarian/temp~/maly/img/'
35         self.footnotes = etree.Element('div', id='footnotes')
36
37         self.cursors = {
38 #            None: None,
39 #            'header': self.header,
40             'footnotes': self.footnotes,
41         }
42         self.current_cursors = []
43
44         self.toc_base = 0
45
46     @property
47     def cursor(self):
48         return self.current_cursors[-1]
49
50     def enter_fragment(self, fragment):
51         self.current_cursors.append(self.cursors[fragment])
52
53     def exit_fragment(self):
54         self.current_cursors.pop()
55
56     def create_fragment(self, name, element):
57         assert name not in self.cursors
58         self.cursors[name] = element
59
60     def forget_fragment(self, name):
61         del self.cursors[name]
62
63
64
65     @property
66     def base_url(self):
67         if self._base_url is not None:
68             return self._base_url
69         else:
70             return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
71
72
73     # Base URL should be on Document level, not builder.
74     def build(self, document, **kwargs):
75         """Should return an OutputFile with the output."""
76         raise NotImplementedError()
77
78
79 class EpubBuilder(Builder):
80     file_extension = 'epub'
81
82     def __init__(self, *args, **kwargs):
83         self.chars = set()
84         self.fundr = 0
85         super().__init__(*args, **kwargs)
86     
87     def build(self, document, **kwargs):
88         # replace_characters -- nie, robimy to na poziomie elementów
89         
90         # hyphenator (\00ad w odp. miejscach) -- jeśli już, to też powinno to się dziać na poziomie elementów
91         # spójniki (\u00a0 po)-- jeśli już, to na poziomie elementów
92         # trick na dywizy: &#xad;&#8288;-
93
94         # do toc trafia:
95         #   początek z KAŻDEGO PLIKU xml
96         
97         # zliczamy zbiór użytych znaków
98
99         # flagi:
100         # mieliśmy taką flagę less-advertising, używaną tylko dla Prestigio; już nie używamy.
101
102         # @editors = document.editors() (jako str)
103         # @funders = join(meta.funders)
104         # @thanks = meta.thanks
105
106
107         self.output = output = epub.EpubBook()
108         self.document = document
109
110         self.set_metadata()
111         
112
113         self.add_cover()
114         
115         self.add_title_page()
116         self.add_toc()
117
118
119
120         self.start_chunk()
121
122         self.add_toc_entry(
123             None,
124             'Początek utworu', # i18n
125             0
126         )
127         self.output.guide.append({
128             "type": "text",
129             "title": "Początek",
130             "href": "part1.xhtml"
131         })
132
133
134         self.build_document(self.document)
135
136         
137         self.close_chunk()
138
139         self.add_annotations()
140         self.add_support_page()
141         self.add_last_page()
142
143
144         e = len(self.output.spine) - 3 - 3
145         nfunds = len(FUNDRAISING)
146         if e > 16:
147             nfunds *= 2
148
149         # COUNTING CHARACTERS?
150         for f in range(nfunds):
151             spine_index = int(4 + (f / nfunds * e) + f)
152
153             h = Xhtml()
154             h.body.append(
155                 etree.XML('<div id="book-text"><div class="fundraising">' + FUNDRAISING[f % len(FUNDRAISING)] + '</div></div>')
156             )
157             self.add_html(h.element, file_name='fund%d.xhtml' % f, spine=spine_index)
158
159         self.add_fonts()
160
161         output_file = tempfile.NamedTemporaryFile(
162             prefix='librarian', suffix='.epub',
163             delete=False)
164         output_file.close()
165         epub.write_epub(output_file.name, output, {'epub3_landmark': False})
166         return OutputFile.from_filename(output_file.name)
167
168     def build_document(self, document):
169         self.toc_precedences = []
170
171         self.start_chunk()
172
173
174         document.tree.getroot().epub_build(self)
175         if document.meta.parts:
176             self.start_chunk()
177
178             self.start_element('div', {'class': 'title-page'})
179             self.start_element('h1', {'class': 'title'})
180             self.push_text(document.meta.title)
181             self.end_element()
182             self.end_element()
183
184             ######
185             # 160
186             # translators
187             # working copy?
188             # ta lektura
189             # tanks
190             # utwor opracowany
191             # isbn
192             # logo
193
194             for child in document.children:
195                 self.start_chunk()
196                 self.add_toc_entry(None, child.meta.title, 0)
197                 self.build_document(child)
198
199         self.shift_toc_base()
200             
201     
202     def add_title_page(self):
203         html = Xhtml()
204         html.title.text = "Strona tytułowa"
205         bt = etree.SubElement(html.body, 'div', **{'class': 'book-text'})
206         tp = etree.SubElement(bt, 'div', **{'class': 'title-page'})
207
208         # Tak jak jest teraz – czy może być jednocześnie
209         # no „autor_utworu”
210         # i „dzieło nadrzędne”
211         # wcześniej mogło być dzieło nadrzędne,
212
213         e = self.document.tree.find('//autor_utworu')
214         if e is not None:
215             etree.SubElement(tp, 'h2', **{'class': 'author'}).text = e.raw_printable_text()
216         e = self.document.tree.find('//nazwa_utworu')
217         if e is not None:
218             etree.SubElement(tp, 'h1', **{'class': 'title'}).text = e.raw_printable_text()
219
220         if not len(tp):
221             for author in self.document.meta.authors:
222                 etree.SubElement(tp, 'h2', **{'class': 'author'}).text = author.readable()
223             etree.SubElement(tp, 'h1', **{'class': 'title'}).text = self.document.meta.title
224
225 #                <xsl:apply-templates select="//nazwa_utworu | //podtytul | //dzielo_nadrzedne" mode="poczatek"/>
226 #        else:
227 #                            <xsl:apply-templates select="//dc:creator" mode="poczatek"/>
228 #                <xsl:apply-templates select="//dc:title | //podtytul | //dzielo_nadrzedne" mode="poczatek"/>
229
230         etree.SubElement(tp, 'p', **{"class": "info"}).text = '\u00a0'
231
232         if self.document.meta.translators:
233             p = etree.SubElement(tp, 'p', **{'class': 'info'})
234             p.text = 'tłum. ' + ', '.join(t.readable() for t in self.document.meta.translators)
235                 
236         #<p class="info">[Kopia robocza]</p>
237
238         p = etree.XML("""<p class="info">
239               <a>Ta lektura</a>, podobnie jak tysiące innych, jest dostępna on-line na stronie
240               <a href="http://www.wolnelektury.pl/">wolnelektury.pl</a>.
241             </p>""")
242         p[0].attrib['href'] = str(self.document.meta.url)
243         tp.append(p)
244
245         if self.document.meta.thanks:
246             etree.SubElement(tp, 'p', **{'class': 'info'}).text = self.document.meta.thanks
247         
248         tp.append(etree.XML("""
249           <p class="info">
250             Utwór opracowany został w&#160;ramach projektu<a href="http://www.wolnelektury.pl/"> Wolne Lektury</a> przez<a href="http://www.nowoczesnapolska.org.pl/"> fundację Nowoczesna Polska</a>.
251           </p>
252         """))
253
254         if self.document.meta.isbn_epub:
255             etree.SubElement(tp, 'p', **{"class": "info"}).text = self.document.meta.isbn_epub
256
257         tp.append(etree.XML("""<p class="footer info">
258             <a href="http://www.wolnelektury.pl/"><img src="logo_wolnelektury.png" alt="WolneLektury.pl" /></a>
259         </p>"""))
260
261         self.add_html(
262             html.element,
263             file_name='title.xhtml',
264             spine=True,
265             toc='Strona tytułowa' # TODO: i18n
266         )
267
268         self.add_file(
269             get_resource('res/wl-logo-small.png'),
270             file_name='logo_wolnelektury.png',
271             media_type='image/png'
272         )
273     
274     def set_metadata(self):
275         self.output.set_identifier(
276             str(self.document.meta.url))
277         self.output.set_language(
278             functions.lang_code_3to2(self.document.meta.language)
279         )
280         self.output.set_title(self.document.meta.title)
281
282         for i, author in enumerate(self.document.meta.authors):
283             self.output.add_author(
284                 author.readable(),
285                 file_as=six.text_type(author),
286                 uid='creator{}'.format(i)
287             )
288         for translator in self.document.meta.translators:
289             self.output.add_author(
290                 translator.readable(),
291                 file_as=six.text_type(translator),
292                 role='trl',
293                 uid='translator{}'.format(i)
294             )
295         for publisher in self.document.meta.publisher:
296             self.output.add_metadata("DC", "publisher", publisher)
297
298         self.output.add_metadata("DC", "date", self.document.meta.created_at)
299
300         
301
302
303     def add_toc(self):
304         item = epub.EpubNav()
305         self.output.add_item(item)
306         self.output.spine.append(item)
307         self.output.add_item(epub.EpubNcx())
308
309         self.output.toc.append(
310             epub.Link(
311                 "nav.xhtml",
312                 "Spis treści",
313                 "nav"
314             )
315         )
316
317     
318
319     def add_support_page(self):
320         self.add_file(
321             get_resource('epub/support.xhtml'),
322             spine=True,
323             toc='Wesprzyj Wolne Lektury'
324         )
325
326         self.add_file(
327             get_resource('res/jedenprocent.png'),
328             media_type='image/png'
329         )
330         self.add_file(
331             get_resource('epub/style.css'),
332             media_type='text/css'
333         )
334
335
336     def add_file(self, path=None, content=None,
337                  media_type='application/xhtml+xml',
338                  file_name=None, uid=None,
339                  spine=False, toc=None):
340
341         # update chars?
342         # jakieś tam ścieśnianie białych znaków?
343
344         if content is None:
345             with open(path, 'rb') as f:
346                 content = f.read()
347             if file_name is None:
348                 file_name = path.rsplit('/', 1)[-1]
349
350         if uid is None:
351             uid = file_name.split('.', 1)[0]
352
353         item = epub.EpubItem(
354             uid=uid,
355             file_name=file_name,
356             media_type=media_type,
357             content=content
358         )
359
360         self.output.add_item(item)
361         if spine:
362             if spine is True:
363                 self.output.spine.append(item)
364             else:
365                 self.output.spine.insert(spine, item)
366
367         if toc:
368             self.output.toc.append(
369                 epub.Link(
370                     file_name,
371                     toc,
372                     uid
373                 )
374             )
375
376     def add_html(self, html_tree, **kwargs):
377         html = etree.tostring(
378             html_tree, pretty_print=True, xml_declaration=True,
379             encoding="utf-8",
380             doctype='<!DOCTYPE html>'
381         )
382
383         html = librarian.epub.squeeze_whitespace(html)
384
385         self.add_file(
386             content=html,
387             **kwargs
388         )
389             
390         
391     def add_fonts(self):
392         print("".join(sorted(self.chars)))
393         # TODO: optimizer
394         for fname in ('DejaVuSerif.ttf', 'DejaVuSerif-Bold.ttf',
395                       'DejaVuSerif-Italic.ttf', 'DejaVuSerif-BoldItalic.ttf'):
396             self.add_file(
397                 content=strip_font(
398                     get_resource('fonts/' + fname),
399                     self.chars
400                 ),
401                 file_name=fname,
402                 media_type='font/ttf'
403             )
404
405     def start_chunk(self):
406         if getattr(self, 'current_chunk', None) is not None:
407             if not len(self.current_chunk):
408                 return
409             self.close_chunk()
410         self.current_chunk = etree.Element(
411             'div',
412             id="book-text"
413         )
414         self.cursors[None] = self.current_chunk
415         self.current_cursors.append(self.current_chunk)
416
417         self.section_number = 0
418         
419
420     def close_chunk(self):
421         assert self.cursor is self.current_chunk
422         ###### -- what if we're inside?
423
424         chunk_no = getattr(
425             self,
426             'chunk_counter',
427             1
428         )
429         self.chunk_counter = chunk_no + 1
430
431         html = Xhtml()
432         html.body.append(self.current_chunk)
433         
434         self.add_html(
435             ## html container from template.
436             #self.current_chunk,
437             html.element,
438             file_name='part%d.xhtml' % chunk_no,
439             spine=True,
440             
441         )
442         self.current_chunk = None
443         self.current_cursors.pop()
444
445     def start_element(self, tag, attr):
446         self.current_cursors.append(
447             etree.SubElement(self.cursor, tag, **attr)
448         )
449         
450     def end_element(self):
451         self.current_cursors.pop()
452         
453     def push_text(self, text):
454         self.chars.update(text)
455         if len(self.cursor):
456             self.cursor[-1].tail = (self.cursor[-1].tail or '') + text
457         else:
458             self.cursor.text = (self.cursor.text or '') + text
459
460
461     def assign_image_number(self):
462         image_number = getattr(self, 'image_number', 0)
463         self.image_number = image_number + 1
464         return image_number
465
466     def assign_footnote_number(self):
467         number = getattr(self, 'footnote_number', 1)
468         self.footnote_number = number + 1
469         return number
470
471     def assign_section_number(self):
472         number = getattr(self, 'section_number', 1)
473         self.section_number = number + 1
474         return number
475
476     def assign_mathml_number(self):
477         number = getattr(self, 'mathml_number', 0)
478         self.mathml_number = number + 1
479         return number
480
481     
482     def add_toc_entry(self, fragment, name, precedence):
483         if precedence:
484             while self.toc_precedences and self.toc_precedences[-1] >= precedence:
485                 self.toc_precedences.pop()
486         else:
487             self.toc_precedences = []
488
489         real_level = self.toc_base + len(self.toc_precedences)
490         if precedence:
491             self.toc_precedences.append(precedence)
492         else:
493             self.toc_base += 1
494         
495         part_number = getattr(
496             self,
497             'chunk_counter',
498             1
499         )
500         filename = 'part%d.xhtml' % part_number
501         uid = filename.split('.')[0]
502         if fragment:
503             filename += '#' + fragment
504             uid += '-' + fragment
505
506         toc = self.output.toc
507         for l in range(1, real_level):
508             if isinstance(toc[-1], epub.Link):
509                 toc[-1] = [toc[-1], []]
510             toc = toc[-1][1]
511
512         toc.append(
513             epub.Link(
514                 filename,
515                 name,
516                 uid
517             )
518         )
519
520     def shift_toc_base(self):
521         self.toc_base -= 1
522         
523
524     def add_last_page(self):
525         html = Xhtml()
526         m = self.document.meta
527         
528         html.title.text = 'Strona redakcyjna'
529         d = etree.SubElement(html.body, 'div', id='book-text')
530
531         newp = lambda: etree.SubElement(d, 'p', {'class': 'info'})
532
533         p = newp()
534         if m.license:
535             p.text = """
536                       Ten utwór jest udostępniony na licencji
537                       """
538             etree.SubElement(p, 'a', href=m.license).text = m.license_description
539         else:
540             p.text = """
541                     Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się w domenie
542                     publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
543                     i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami
544                     (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to
545                     te dodatkowe materiały udostępnione są na licencji
546                     """
547             a = etree.SubElement(p, "a", href="http://creativecommons.org/licenses/by-sa/3.0/")
548             a.text = """Creative Commons
549                     Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL"""
550             a.tail = "."
551
552
553         p = newp()
554         p.text = 'Źródło: '
555         etree.SubElement(
556             p, 'a', href=str(m.url),
557             title=', '.join((
558                 ', '.join(p.readable() for p in m.authors),
559                 m.title
560             ))
561         ).text = str(m.url)
562
563         newp().text = 'Tekst opracowany na podstawie: ' + m.source_name
564
565         newp().text = """
566               Wydawca:
567               """ + ", ".join(p for p in m.publisher)
568
569         if m.description:
570             newp().text = m.description
571
572
573         if m.editors:
574             newp().text = 'Opracowanie redakcyjne i przypisy: %s.' % (
575                 ', '.join(e.readable() for e in sorted(self.document.editors())))
576
577         if m.funders:
578             etree.SubElement(d, 'p', {'class': 'minor-info'}).text = '''Publikację wsparli i wsparły:
579             %s.''' % (', '.join(m.funders))
580
581         if m.cover_by:
582             p = newp()
583             p.text = 'Okładka na podstawie: '
584             if m.cover_source:
585                 etree.SubElement(
586                     p,
587                     'a',
588                     href=m.cover_source
589                 ).text = m.cover_by
590             else:
591                 p.text += m.cover_by
592             
593         if m.isbn_epub:
594             newp().text = m.isbn_epub
595
596         newp().text = '\u00a0'
597
598         p = newp()
599         p.attrib['class'] = 'minor-info'
600         p.text = '''
601               Plik wygenerowany dnia '''
602         span = etree.SubElement(p, 'span', id='file_date')
603         span.text = str(date.today())
604         span.tail = '''.
605           '''
606         
607         self.add_html(
608             html.element,
609             file_name='last.xhtml',
610             toc='Strona redakcyjna',
611             spine=True
612         )
613
614
615     def add_annotations(self):
616         if not len(self.footnotes):
617             return
618
619         html = Xhtml()
620         html.title.text = 'Przypisy'
621         d = etree.SubElement(
622             etree.SubElement(
623                 html.body,
624                 'div',
625                 id='book-text'
626             ),
627             'div',
628             id='footnotes'
629         )
630         
631         etree.SubElement(
632             d,
633             'h2',
634         ).text = 'Przypisy:'
635
636         d.extend(self.footnotes)
637         
638         self.add_html(
639             html.element,
640             file_name='annotations.xhtml',
641             spine=True,
642             toc='Przypisy'
643         )
644
645     def add_cover(self):
646         # TODO: allow other covers
647
648         cover_maker = make_cover
649
650         cover_file = six.BytesIO()
651         cover = cover_maker(self.document.meta)
652         cover.save(cover_file)
653         cover_name = 'cover.%s' % cover.ext()
654
655         self.output.set_cover(
656             file_name=cover_name,
657             content=cover_file.getvalue(),
658             create_page = False
659         )
660         ci = ('''<?xml version="1.0" encoding="UTF-8"?>
661 <!DOCTYPE html>
662 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en" xml:lang="en">
663  <head>
664   <title>Okładka</title>
665   <style>
666     body { margin: 0em; padding: 0em; }
667     img { width: 100%%; }
668   </style>
669  </head>
670  <body>
671    <img src="cover.%s" alt="Okładka" />
672  </body>
673 </html>''' % cover.ext()).encode('utf-8')
674         self.add_file(file_name='cover.xhtml', content=ci)
675
676         self.output.spine.append('cover')
677         self.output.guide.append({
678             'type': 'cover',
679             'href': 'cover.xhtml',
680             'title': 'Okładka'
681         })
682
683     def mathml(self, element):
684         name = "math%d.png" % self.assign_mathml_number()
685         self.add_file(
686             content=MathML(element).to_latex().to_png().data,
687             media_type='image/png',
688             file_name=name
689         )
690         return name