diff --git a/src/calibre/ebooks/conversion/plugins/mobi_output.py b/src/calibre/ebooks/conversion/plugins/mobi_output.py index b5dc6f17d5..0bdb26162c 100644 --- a/src/calibre/ebooks/conversion/plugins/mobi_output.py +++ b/src/calibre/ebooks/conversion/plugins/mobi_output.py @@ -148,7 +148,6 @@ class MOBIOutput(OutputFormatPlugin): self.oeb.manifest.remove(self.oeb.manifest.hrefs[x.href]) x.href = articles_[0].href - for sec in sections: articles[id(sec)] = [] for a in list(sec): @@ -179,7 +178,7 @@ class MOBIOutput(OutputFormatPlugin): mobi_type = opts.mobi_file_type if self.is_periodical: - mobi_type = 'old' # Amazon does not support KF8 periodicals + mobi_type = 'old' # Amazon does not support KF8 periodicals create_kf8 = mobi_type in ('new', 'both') remove_html_cover(self.oeb, self.log) @@ -188,12 +187,10 @@ class MOBIOutput(OutputFormatPlugin): self.check_for_periodical() if create_kf8: - # Split on pagebreaks so that the resulting KF8 works better with - # calibre's viewer, which does not support CSS page breaks + # Split on pagebreaks so that the resulting KF8 is faster to load from calibre.ebooks.oeb.transforms.split import Split Split()(self.oeb, self.opts) - kf8 = self.create_kf8(resources, for_joint=mobi_type=='both' ) if create_kf8 else None if mobi_type == 'new': diff --git a/src/calibre/ebooks/conversion/plugins/oeb_output.py b/src/calibre/ebooks/conversion/plugins/oeb_output.py index 86d3b76a8c..d63be32fd9 100644 --- a/src/calibre/ebooks/conversion/plugins/oeb_output.py +++ b/src/calibre/ebooks/conversion/plugins/oeb_output.py @@ -18,7 +18,6 @@ class OEBOutput(OutputFormatPlugin): recommendations = set([('pretty_print', True, OptionRecommendation.HIGH)]) - def convert(self, oeb_book, output_path, input_plugin, opts, log): from urllib import unquote from lxml import etree @@ -26,7 +25,8 @@ class OEBOutput(OutputFormatPlugin): self.log, self.opts = log, opts if not os.path.exists(output_path): os.makedirs(output_path) - from calibre.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME + from calibre.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME, OEB_STYLES + from calibre.ebooks.oeb.normalize_css import condense_sheet with CurrentDir(output_path): results = oeb_book.to_opf2(page_map=True) for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME): @@ -54,6 +54,8 @@ class OEBOutput(OutputFormatPlugin): f.write(raw) for item in oeb_book.manifest: + if item.media_type in OEB_STYLES and hasattr(item.data, 'cssText'): + condense_sheet(item.data) path = os.path.abspath(unquote(item.href)) dir = os.path.dirname(path) if not os.path.exists(dir): @@ -62,7 +64,7 @@ class OEBOutput(OutputFormatPlugin): f.write(str(item)) item.unload_data_from_memory(memory=path) - def workaround_nook_cover_bug(self, root): # {{{ + def workaround_nook_cover_bug(self, root): # {{{ cov = root.xpath('//*[local-name() = "meta" and @name="cover" and' ' @content != "cover"]') @@ -96,7 +98,7 @@ class OEBOutput(OutputFormatPlugin): cov.set('content', 'cover') # }}} - def workaround_pocketbook_cover_bug(self, root): # {{{ + def workaround_pocketbook_cover_bug(self, root): # {{{ m = root.xpath('//*[local-name() = "manifest"]/*[local-name() = "item" ' ' and @id="cover"]') if len(m) == 1: @@ -106,7 +108,7 @@ class OEBOutput(OutputFormatPlugin): p.insert(0, m) # }}} - def migrate_lang_code(self, root): # {{{ + def migrate_lang_code(self, root): # {{{ from calibre.utils.localization import lang_as_iso639_1 for lang in root.xpath('//*[local-name() = "language"]'): clc = lang_as_iso639_1(lang.text) diff --git a/src/calibre/ebooks/mobi/writer8/main.py b/src/calibre/ebooks/mobi/writer8/main.py index 564dc5521c..e1e0981af1 100644 --- a/src/calibre/ebooks/mobi/writer8/main.py +++ b/src/calibre/ebooks/mobi/writer8/main.py @@ -22,6 +22,7 @@ from calibre.ebooks.mobi.utils import (create_text_record, to_base, from calibre.ebooks.compression.palmdoc import compress_doc from calibre.ebooks.oeb.base import (OEB_DOCS, OEB_STYLES, SVG_MIME, XPath, extract, XHTML, urlnormalize) +from calibre.ebooks.oeb.normalize_css import condense_sheet from calibre.ebooks.oeb.parse_utils import barename from calibre.ebooks.mobi.writer8.skeleton import Chunker, aid_able_tags, to_href from calibre.ebooks.mobi.writer8.index import (NCXIndex, SkelIndex, @@ -48,8 +49,8 @@ class KF8Writer(object): self.toc_adder = TOCAdder(oeb, opts) self.used_images = set() self.resources = resources - self.flows = [None] # First flow item is reserved for the text - self.records = [None] # Placeholder for zeroth record + self.flows = [None] # First flow item is reserved for the text + self.records = [None] # Placeholder for zeroth record self.log('\tGenerating KF8 markup...') self.dup_data() @@ -145,11 +146,13 @@ class KF8Writer(object): cssutils.replaceUrls(sheet, replacer, ignoreImportRules=True) def extract_css_into_flows(self): - inlines = defaultdict(list) # Ensure identical