Build process adjustments

Support building MOBI files
Add Index to EPUB/MOBI TOC
This commit is contained in:
RunasSudo 2021-02-10 22:24:59 +11:00
parent c33b6d931a
commit 3813aafecf
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 97 additions and 23 deletions

View File

@ -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'

View File

@ -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;
}

View File

@ -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 {

2
docutils.conf Normal file
View File

@ -0,0 +1,2 @@
[html writers]
footnote_references: superscript

View File

@ -38,7 +38,12 @@ class POHTML5Translator(HTML5Translator):
if node.parent.tagname == 'section':
# Add paragraph numbers
self.paragraph_num += 1
if self.builder.name == 'html':
self.body.append('<span class="paragraph-num"><a id="para{0}" href="#para{0}">{0}</a></span>'.format(self.paragraph_num))
else: # epub (or mobi)
# Hardcode the brackets, as MOBI6 does not support CSS
self.body.append('<span class="paragraph-num">[{0}] </span>'.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)
@ -115,6 +120,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
from sphinx.roles import XRefRole

65
po_ebook_convert.py Executable file
View File

@ -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 <dt> content and ignore for now
span = elem[0]
dt_content = (elem.get('id'), span.text, list(span))
return
if tag == 'dd':
# Prepend <dt> 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())