From b604aa7acc9f12c970c868655a0022c92dddcf6d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 13 Apr 2018 11:16:47 +0530 Subject: [PATCH] Apply content based manifest properties --- src/calibre/ebooks/oeb/base.py | 13 +++++---- src/calibre/ebooks/oeb/polish/upgrade.py | 34 +++++++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 9f4aa56911..d457fa04b4 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -41,12 +41,15 @@ CALIBRE_NS = 'http://calibre.kovidgoyal.net/2009/metadata' RE_NS = 'http://exslt.org/regular-expressions' MBP_NS = 'http://www.mobipocket.com' EPUB_NS = 'http://www.idpf.org/2007/ops' +MATHML_NS = 'http://www.w3.org/1998/Math/MathML' -XPNSMAP = {'h': XHTML_NS, 'o1': OPF1_NS, 'o2': OPF2_NS, - 'd09': DC09_NS, 'd10': DC10_NS, 'd11': DC11_NS, - 'xsi': XSI_NS, 'dt': DCTERMS_NS, 'ncx': NCX_NS, - 'svg': SVG_NS, 'xl': XLINK_NS, 're': RE_NS, - 'mbp': MBP_NS, 'calibre': CALIBRE_NS, 'epub':EPUB_NS} +XPNSMAP = { + 'h': XHTML_NS, 'o1': OPF1_NS, 'o2': OPF2_NS, 'd09': DC09_NS, + 'd10': DC10_NS, 'd11': DC11_NS, 'xsi': XSI_NS, 'dt': DCTERMS_NS, + 'ncx': NCX_NS, 'svg': SVG_NS, 'xl': XLINK_NS, 're': RE_NS, + 'mathml': MATHML_NS, 'mbp': MBP_NS, 'calibre': CALIBRE_NS, + 'epub':EPUB_NS +} OPF1_NSMAP = {'dc': DC11_NS, 'oebpackage': OPF1_NS} OPF2_NSMAP = {'opf': OPF2_NS, 'dc': DC11_NS, 'dcterms': DCTERMS_NS, diff --git a/src/calibre/ebooks/oeb/polish/upgrade.py b/src/calibre/ebooks/oeb/polish/upgrade.py index 76fa409d96..9757807cd1 100644 --- a/src/calibre/ebooks/oeb/polish/upgrade.py +++ b/src/calibre/ebooks/oeb/polish/upgrade.py @@ -2,16 +2,44 @@ # vim:fileencoding=utf-8 # License: GPLv3 Copyright: 2018, Kovid Goyal -from __future__ import (absolute_import, division, print_function, - unicode_literals) +from __future__ import absolute_import, division, print_function, unicode_literals from calibre.ebooks.metadata.opf_2_to_3 import upgrade_metadata +from calibre.ebooks.oeb.base import OEB_DOCS, xpath + + +def add_properties(item, *props): + existing = set((item.get('properties') or '').split()) + existing |= set(props) + item.set('properties', ' '.join(sorted(existing))) + + +def collect_properties(container): + for item in container.opf_xpath('//opf:manifest/opf:item[@href and @media-type]'): + mt = item.get('media-type') or '' + if mt.lower() not in OEB_DOCS: + continue + name = container.href_to_name(item.get('href'), container.opf_name) + root = container.parsed(name) + properties = set() + container.dirty(name) # Ensure entities are converted + if xpath(root, '//svg:svg'): + properties.add('svg') + if xpath(root, '//h:script'): + properties.add('scripted') + if xpath(root, '//mathml:math'): + properties.add('mathml') + if xpath(root, '//epub:switch'): + properties.add('switch') + if properties: + add_properties(item, *tuple(properties)) def epub_2_to_3(container, report): upgrade_metadata(container.opf) - container.dirty(container.opf_name) + collect_properties(container) container.opf.set('version', '3.0') + container.dirty(container.opf_name) def upgrade_book(container, report):