Add DAISY HTML builder.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 16 Oct 2020 10:36:13 +0000 (12:36 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 16 Oct 2020 10:53:43 +0000 (12:53 +0200)
src/librarian/builders/__init__.py
src/librarian/builders/html.py
src/librarian/elements/footnotes/__init__.py
src/librarian/elements/themes/motyw.py

index e9afc56..b1afe94 100644 (file)
@@ -1,6 +1,6 @@
 from collections import OrderedDict
 from .txt import TxtBuilder
-from .html import HtmlBuilder, StandaloneHtmlBuilder
+from .html import HtmlBuilder, StandaloneHtmlBuilder, DaisyHtmlBuilder
 from .sanitize import Sanitizer
 
 
@@ -8,5 +8,6 @@ builders = OrderedDict([
     ("txt", TxtBuilder),
     ("html", HtmlBuilder),
     ("html-standalone", StandaloneHtmlBuilder),
+    ("html-daisy", DaisyHtmlBuilder),
     ("sanitizer", Sanitizer),
 ])
index 40d7777..165a265 100644 (file)
@@ -1,6 +1,10 @@
 # coding: utf-8
 from __future__ import unicode_literals
 
+try:
+    from urllib.request import urlopen
+except ImportError:
+    from urllib2 import urlopen
 from lxml import etree
 from librarian.html import add_anchors, add_table_of_contents, add_table_of_themes
 from librarian import OutputFile
@@ -8,7 +12,12 @@ from librarian import OutputFile
 
 class HtmlBuilder:
     file_extension = "html"
-    identifier = "html"
+    with_anchors = True
+    with_themes = True
+    with_toc = True
+    with_footnotes = True
+    with_nota_red = True
+    no_externalities = False
 
     def __init__(self, image_location='https://wolnelektury.pl/media/book/pictures/marcos-historia-kolorow/'):
         self.image_location = image_location
@@ -58,7 +67,9 @@ class HtmlBuilder:
         self.preprocess(document)
         document.tree.getroot().html_build(self)
         self.postprocess(document)
+        return self.output()
 
+    def output(self):
         return OutputFile.from_bytes(
             etree.tostring(
                 self.tree,
@@ -83,11 +94,14 @@ class HtmlBuilder:
             )
             self.exit_fragment()
 
-        add_anchors(self.tree)
-        if len(self.nota_red):
+        if self.with_anchors:
+            add_anchors(self.tree)
+        if self.with_nota_red and len(self.nota_red):
             self.tree.append(self.nota_red)
-        add_table_of_themes(self.tree)
-        add_table_of_contents(self.tree)
+        if self.with_themes:
+            add_table_of_themes(self.tree)
+        if self.with_toc:
+            add_table_of_contents(self.tree)
 
         if self.footnote_counter:
             fnheader = etree.Element("h3")
@@ -117,6 +131,8 @@ class HtmlBuilder:
 
 
 class StandaloneHtmlBuilder(HtmlBuilder):
+    css_url = "https://static.wolnelektury.pl/css/compressed/book_text.css"
+
     def postprocess(self, document):
         super(StandaloneHtmlBuilder, self).postprocess(document)
 
@@ -139,21 +155,51 @@ class StandaloneHtmlBuilder(HtmlBuilder):
             content="width=device-width, initial-scale=1, maximum-scale=1"
         )
 
-        etree.SubElement(
-            head,
-            'link',
-            href="https://static.wolnelektury.pl/css/compressed/book_text.css",
-            rel="stylesheet",
-            type="text/css",
-        )
+        if self.no_externalities:
+            etree.SubElement(
+                head, 'style',
+            ).text = urlopen(self.css_url).read().decode('utf-8')
+        else:
+            etree.SubElement(
+                head,
+                'link',
+                href=self.css_url,
+                rel="stylesheet",
+                type="text/css",
+            )
 
-        etree.SubElement(
-            body, 'script',
-            src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
-        )
+            etree.SubElement(
+                body, 'script',
+                src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
+            )
 
-        etree.SubElement(
-            body,
-            "script",
-            src="http://malsup.github.io/min/jquery.cycle2.min.js"
+            etree.SubElement(
+                body,
+                "script",
+                src="http://malsup.github.io/min/jquery.cycle2.min.js"
+            )
+
+
+class DaisyHtmlBuilder(StandaloneHtmlBuilder):
+    file_extension = 'xhtml'
+    with_anchors = False
+    with_themes = False
+    with_toc = False
+    with_footnotes = False
+    with_nota_red = False
+    with_deep_identifiers = False
+    no_externalities = True
+
+    def output(self):
+        tree = etree.ElementTree(self.tree)
+        tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
+        tree.docinfo.system_url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
+        return OutputFile.from_bytes(
+            etree.tostring(
+                tree,
+                encoding='utf-8',
+                pretty_print=True,
+                xml_declaration=True
+            )
         )
+
index 82f6497..6e00385 100644 (file)
@@ -6,6 +6,9 @@ class Footnote(WLElement):
         pass
 
     def html_build(self, builder):
+        if not builder.with_footnotes:
+            return
+
         builder.footnote_counter += 1
         fn_no = builder.footnote_counter
         footnote_id = 'footnote-idm{}'.format(self.attrib['_compat_ordered_id'])
index 6ec6c53..3b68c8b 100644 (file)
@@ -7,6 +7,10 @@ class Motyw(WLElement):
     def txt_build(self, builder):
         pass
 
+    def html_build(self, builder):
+        if builder.with_themes:
+            super(Motyw, self).html_build(builder)
+
     def get_html_attr(self, builder):
         fid = self.attrib['id'][1:]
         return {