| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2009, Kovid Goyal <[email protected]>' |
| 7 |
__docformat__ = 'restructuredtext en' |
| 8 |
|
| 9 |
import os |
| 10 |
|
| 11 |
from sphinx.builders.epub3 import Epub3Builder as EpubBuilder |
| 12 |
|
| 13 |
from calibre.ebooks.oeb.base import OPF |
| 14 |
from calibre.ebooks.oeb.polish.check.links import UnreferencedResource, check_links |
| 15 |
from calibre.ebooks.oeb.polish.container import OEB_DOCS, get_container |
| 16 |
from calibre.ebooks.oeb.polish.pretty import pretty_html_tree, pretty_opf |
| 17 |
from calibre.utils.imghdr import identify |
| 18 |
|
| 19 |
|
| 20 |
class EPUBHelpBuilder(EpubBuilder): |
| 21 |
name = 'myepub' |
| 22 |
|
| 23 |
def build_epub(self, outdir=None, outname=None): |
| 24 |
if outdir: |
| 25 |
EpubBuilder.build_epub(self, outdir, outname) |
| 26 |
else: |
| 27 |
EpubBuilder.build_epub(self) |
| 28 |
outdir = self.outdir |
| 29 |
outname = self.config.epub_basename + '.epub' |
| 30 |
container = get_container(os.path.join(outdir, outname)) |
| 31 |
self.fix_epub(container) |
| 32 |
container.commit() |
| 33 |
|
| 34 |
def fix_epub(self, container): |
| 35 |
"Fix all the brokenness that sphinx's epub builder creates" |
| 36 |
for name, mt in container.mime_map.items(): |
| 37 |
if mt in OEB_DOCS: |
| 38 |
self.workaround_ade_quirks(container, name) |
| 39 |
pretty_html_tree(container, container.parsed(name)) |
| 40 |
container.dirty(name) |
| 41 |
self.fix_opf(container) |
| 42 |
|
| 43 |
def workaround_ade_quirks(self, container, name): |
| 44 |
root = container.parsed(name) |
| 45 |
# ADE blows up floating images if their sizes are not specified |
| 46 |
for img in root.xpath('//*[local-name() = "img" and (@class = "float-right-img" or @class = "float-left-img")]'): |
| 47 |
if 'style' not in img.attrib: |
| 48 |
imgname = container.href_to_name(img.get('src'), name) |
| 49 |
fmt, width, height = identify(container.raw_data(imgname)) |
| 50 |
if width == -1: |
| 51 |
raise ValueError(f'Failed to read size of: {imgname}') |
| 52 |
img.set('style', f'width: {width}px; height: {height}px') |
| 53 |
|
| 54 |
def fix_opf(self, container): |
| 55 |
spine_names = {n for n, l in container.spine_names} |
| 56 |
spine = container.opf_xpath('//opf:spine')[0] |
| 57 |
rmap = {v: k for k, v in container.manifest_id_map.items()} |
| 58 |
# Add unreferenced text files to the spine |
| 59 |
for name, mt in container.mime_map.items(): |
| 60 |
if mt in OEB_DOCS and name not in spine_names: |
| 61 |
spine_names.add(name) |
| 62 |
container.insert_into_xml(spine, spine.makeelement(OPF('itemref'), idref=rmap[name])) |
| 63 |
|
| 64 |
# Remove duplicate entries from spine |
| 65 |
seen = set() |
| 66 |
for item, name, linear in container.spine_iter: |
| 67 |
if name in seen: |
| 68 |
container.remove_from_xml(item) |
| 69 |
seen.add(name) |
| 70 |
|
| 71 |
# Remove the <guide> which is not needed in EPUB 3 |
| 72 |
for guide in container.opf_xpath('//*[local-name()="guide"]'): |
| 73 |
guide.getparent().remove(guide) |
| 74 |
|
| 75 |
# Ensure that the cover-image property is set |
| 76 |
cover_id = rmap['_static/' + self.config.epub_cover[0]] |
| 77 |
for item in container.opf_xpath(f'//opf:item[@id="{cover_id}"]'): |
| 78 |
item.set('properties', 'cover-image') |
| 79 |
for item in container.opf_xpath('//opf:item[@href="epub-cover.xhtml"]'): |
| 80 |
item.set('properties', 'svg calibre:title-page') |
| 81 |
for item in container.opf_xpath('//opf:package'): |
| 82 |
prefix = item.get('prefix') or '' |
| 83 |
if prefix: |
| 84 |
prefix += ' ' |
| 85 |
item.set('prefix', prefix + 'calibre: https://calibre-ebook.com') |
| 86 |
|
| 87 |
# Remove any <meta cover> tag as it is not needed in epub 3 |
| 88 |
for meta in container.opf_xpath('//opf:meta[@name="cover"]'): |
| 89 |
meta.getparent().remove(meta) |
| 90 |
|
| 91 |
# Remove unreferenced files |
| 92 |
for error in check_links(container): |
| 93 |
if error.__class__ is UnreferencedResource: |
| 94 |
container.remove_item(error.name) |
| 95 |
|
| 96 |
# Pretty print the OPF |
| 97 |
pretty_opf(container.parsed(container.opf_name)) |
| 98 |
container.dirty(container.opf_name) |