diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index e82bffe5dd..0591117a44 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -205,17 +205,20 @@ class Container(object): if id_ is not None: removed.add(id_) elem.getparent().remove(elem) + self.dirty(self.opf_name) if removed: for item in self.opf.xpath('//opf:spine/opf:itemref[@idref]', namespaces={'opf':OPF2_NS}): idref = item.get('idref') if idref in removed: item.getparent().remove(item) + self.dirty(self.opf_name) for item in self.opf.xpath('//opf:guide/opf:reference[@href]', namespaces={'opf':OPF2_NS}): if self.href_to_name(item.get('href')) == name: item.getparent().remove(item) + self.dirty(self.opf_name) path = self.name_path_map.pop(name) if os.path.exists(path): @@ -228,10 +231,10 @@ class Container(object): self.dirtied.add(name) def commit(self, outpath=None): - for name in self.dirtied: + for name in tuple(self.dirtied): self.dirtied.remove(name) data = self.parsed_cache.pop(name) - data = serialize(data) + data = serialize(data, self.mime_map[name]) with open(self.name_path_map[name], 'wb') as f: f.write(data) @@ -365,6 +368,8 @@ class EpubContainer(Container): if outpath is None: outpath = self.pathtoepub from calibre.ebooks.tweak import zip_rebuilder + with open(join(self.root, 'mimetype'), 'wb') as f: + f.write(guess_type('a.epub')[0]) zip_rebuilder(self.root, outpath) # }}} diff --git a/src/calibre/ebooks/oeb/polish/subset.py b/src/calibre/ebooks/oeb/polish/subset.py index 76906ed3ca..c13bdb860f 100644 --- a/src/calibre/ebooks/oeb/polish/subset.py +++ b/src/calibre/ebooks/oeb/polish/subset.py @@ -7,10 +7,11 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os +import os, sys +from calibre import prints from calibre.ebooks.oeb.base import OEB_STYLES, OEB_DOCS, XPath -from calibre.ebooks.oeb.polish import OEB_FONTS +from calibre.ebooks.oeb.polish.container import OEB_FONTS from calibre.utils.fonts.sfnt.subset import subset def remove_font_face_rules(container, sheet, remove_names): @@ -74,3 +75,22 @@ def subset_all_fonts(container, font_stats, report): report('Reduced total font size to %.1f%% of original'%( total_new/total_old*100)) +if __name__ == '__main__': + from calibre.ebooks.oeb.polish.container import get_container + from calibre.ebooks.oeb.polish.stats import StatsCollector + from calibre.utils.logging import default_log + default_log.filter_level = default_log.DEBUG + inbook = sys.argv[-1] + ebook = get_container(inbook, default_log) + report = [] + stats = StatsCollector(ebook).font_stats + subset_all_fonts(ebook, stats, report.append) + outbook, ext = inbook.rpartition('.')[0::2] + outbook += '_subset.'+ext + ebook.commit(outbook) + prints('\nReport:') + for msg in report: + prints(msg) + print() + prints('Output written to:', outbook) +