diff --git a/Makefile b/Makefile index c4b836b..bda8cc6 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,13 @@ help: @PYTHONPATH=. TEX_MAX_REPEAT=3 $(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) livehtml: - @PYTHONPATH=. sphinx-autobuild "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + @PYTHONPATH=. sphinx-autobuild "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS) -d _build/doctrees $(O) + +mobi: + ./po_ebook_convert.py _build/epub/PointsofOrder.epub _build/epub/PointsofOrder.mobi --mobi-file-type=both --remove-first-image --cover=_static/cover.jpg --chapter=/ --change-justification=left --mobi-ignore-margins --no-inline-toc upload: - cp -r _build/html '/home/runassudo/Documents/Work/School Cloud Data/unenc/public/www/pointsoforder' + cp -r _build/html/* '/home/runassudo/Documents/Work/School Cloud Data/unenc/public/www/pointsoforder' sha256sum _build/latex/PointsOfOrder_Print.pdf | awk '{print substr($$1,0,6);}' > _build/latex/PointsOfOrder_Print.pdf.sha256 rm '/home/runassudo/Documents/Work/School Cloud Data/unenc_transient/Lulu/PointsOfOrder_Print'*.pdf cp _build/latex/PointsOfOrder_Print.pdf '/home/runassudo/Documents/Work/School Cloud Data/unenc_transient/Lulu/PointsOfOrder_Print_'$$(sha256sum _build/latex/PointsOfOrder_Print.pdf | awk '{print substr($$1,0,6);}')'.pdf' diff --git a/_static/custom.css b/_static/custom.css index 65e9892..6ea7778 100644 --- a/_static/custom.css +++ b/_static/custom.css @@ -1,15 +1,11 @@ /* Footnote styling in text */ -a.footnote-reference { +a.footnote-reference.superscript { font-size: 0.6em; vertical-align: super; line-height: 0; } -a.footnote-reference.brackets::before, a.footnote-reference.brackets::after { - content: ''; -} - sup { line-height: 0; } diff --git a/_static/custom_epub.css b/_static/custom_epub.css index d54fde3..b1ce579 100644 --- a/_static/custom_epub.css +++ b/_static/custom_epub.css @@ -1,22 +1,15 @@ -/* Paragraph numbers */ - -span.paragraph-num a { - text-decoration: none; -} - -span.paragraph-num a::before { - content: '['; -} -span.paragraph-num a::after { - content: '] '; -} - /* Remove underlining from links */ a.footnote-reference { text-decoration: none; } +/* Footnote list styling */ + +dl.footnote.superscript > dt.label { /* Override epub.css */ + font-size: initial; +} + /* Table formatting */ table.docutils td, table.docutils th { diff --git a/docutils.conf b/docutils.conf new file mode 100644 index 0000000..d93945d --- /dev/null +++ b/docutils.conf @@ -0,0 +1,2 @@ +[html writers] +footnote_references: superscript diff --git a/extension.py b/extension.py index 2ad1dad..faf426e 100644 --- a/extension.py +++ b/extension.py @@ -38,7 +38,12 @@ class POHTML5Translator(HTML5Translator): if node.parent.tagname == 'section': # Add paragraph numbers self.paragraph_num += 1 - self.body.append('{0}'.format(self.paragraph_num)) + + if self.builder.name == 'html': + self.body.append('{0}'.format(self.paragraph_num)) + else: # epub (or mobi) + # Hardcode the brackets, as MOBI6 does not support CSS + self.body.append('[{0}] '.format(self.paragraph_num)) def visit_footnote_reference(self, node): super().visit_footnote_reference(node) @@ -66,10 +71,10 @@ __TocTree_resolve = TocTree.resolve def _TocTree_resolve(self, *args, **kwargs): result = __TocTree_resolve(self, *args, **kwargs) - if self.env.app.builder.name == 'html': + if self.env.app.builder.name == 'html' or self.env.app.builder.name == 'epub': # Add Index TOC entry bullet_list = result[1] - reference = docutils.nodes.reference('', '', internal=True, refuri='genindex.html', anchorname='', *[docutils.nodes.Text('Index')]) + reference = docutils.nodes.reference('', '', internal=True, refuri='genindex.xhtml' if self.env.app.builder.name == 'epub' else 'genindex.html', anchorname='', *[docutils.nodes.Text('Index')]) compact_paragraph = sphinx.addnodes.compact_paragraph('', '', reference) list_item = docutils.nodes.list_item('', classes=['toctree-l1'], *[compact_paragraph]) bullet_list.append(list_item) @@ -114,6 +119,16 @@ from sphinx.builders.epub3 import Epub3Builder class POEpub3Builder(Epub3Builder): default_translator_class = POHTML5Translator + + def toc_add_files(self, refnodes): + super().toc_add_files(refnodes) + + # Add Index TOC entry + refnodes.append({ + 'level': 1, + 'refuri': 'genindex.xhtml', + 'text': 'Index' + }) # :subref: Role diff --git a/po_ebook_convert.py b/po_ebook_convert.py new file mode 100755 index 0000000..82977a3 --- /dev/null +++ b/po_ebook_convert.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +# From /usr/bin/ebook-convert + +import sys, os + +path = os.environ.get('CALIBRE_PYTHON_PATH', '/usr/lib/calibre') +if path not in sys.path: + sys.path.insert(0, path) + +sys.resources_location = os.environ.get('CALIBRE_RESOURCES_PATH', '/usr/share/calibre') +sys.extensions_location = os.environ.get('CALIBRE_EXTENSIONS_PATH', '/usr/lib/calibre/calibre/plugins') +sys.executables_location = os.environ.get('CALIBRE_EXECUTABLES_PATH', '/usr/bin') + +# MONKEY PATCHING + +from calibre.ebooks.mobi.mobiml import MobiMLizer +from calibre.ebooks.oeb.base import barename, XHTML + +dt_content = None + +__MobiMLizer_mobimlize_elem = MobiMLizer.mobimlize_elem +def _MobiMLizer_mobimlize_elem(self, elem, stylizer, bstate, istates, ignore_valign=False): + global dt_content + tag = barename(elem.tag) + + # Fix rendering of footnote lists + if tag == 'dt': + # Save
content and ignore for now + span = elem[0] + dt_content = (elem.get('id'), span.text, list(span)) + return + if tag == 'dd': + # Prepend
content here + p = elem[0] + if dt_content[1]: # Has text + p.text = dt_content[1] + '. ' + (p.text or '') + else: # Has children + for i, child in enumerate(dt_content[2]): + p.insert(i, child) + child.tail = (child.tail or '') + '. ' + if p.text: # Move text to end + child.tail += p.text + p.text = '' + p.set('id', dt_content[0]) + dt_content = None + + return __MobiMLizer_mobimlize_elem(self, elem, stylizer, bstate, istates, ignore_valign) +MobiMLizer.mobimlize_elem = _MobiMLizer_mobimlize_elem + +__MobiMLizer_mobimlize_content = MobiMLizer.mobimlize_content +def _MobiMLizer_mobimlize_content(self, tag, text, bstate, istates): + if tag == 'ul': + # Override bstate.vmargin temporarily to prevent Calibre inserting an empty div (ugly spacing!) + tmp, bstate.vmargin = bstate.vmargin, 0 + result = __MobiMLizer_mobimlize_content(self, tag, text, bstate, istates) + bstate.vmargin = tmp + return result + return __MobiMLizer_mobimlize_content(self, tag, text, bstate, istates) +MobiMLizer.mobimlize_content = _MobiMLizer_mobimlize_content + +# From /usr/bin/ebook-convert + +from calibre.ebooks.conversion.cli import main +sys.exit(main())