PointsOfOrder/po_ebook_convert.py

66 lines
2.1 KiB
Python
Executable File

#!/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())