This commit is contained in:
Kovid Goyal 2013-02-04 11:26:17 +05:30
parent d2e8ec09fa
commit d7a26cfe54
2 changed files with 29 additions and 4 deletions

View File

@ -205,17 +205,20 @@ class Container(object):
if id_ is not None: if id_ is not None:
removed.add(id_) removed.add(id_)
elem.getparent().remove(elem) elem.getparent().remove(elem)
self.dirty(self.opf_name)
if removed: if removed:
for item in self.opf.xpath('//opf:spine/opf:itemref[@idref]', for item in self.opf.xpath('//opf:spine/opf:itemref[@idref]',
namespaces={'opf':OPF2_NS}): namespaces={'opf':OPF2_NS}):
idref = item.get('idref') idref = item.get('idref')
if idref in removed: if idref in removed:
item.getparent().remove(item) item.getparent().remove(item)
self.dirty(self.opf_name)
for item in self.opf.xpath('//opf:guide/opf:reference[@href]', for item in self.opf.xpath('//opf:guide/opf:reference[@href]',
namespaces={'opf':OPF2_NS}): namespaces={'opf':OPF2_NS}):
if self.href_to_name(item.get('href')) == name: if self.href_to_name(item.get('href')) == name:
item.getparent().remove(item) item.getparent().remove(item)
self.dirty(self.opf_name)
path = self.name_path_map.pop(name) path = self.name_path_map.pop(name)
if os.path.exists(path): if os.path.exists(path):
@ -228,10 +231,10 @@ class Container(object):
self.dirtied.add(name) self.dirtied.add(name)
def commit(self, outpath=None): def commit(self, outpath=None):
for name in self.dirtied: for name in tuple(self.dirtied):
self.dirtied.remove(name) self.dirtied.remove(name)
data = self.parsed_cache.pop(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: with open(self.name_path_map[name], 'wb') as f:
f.write(data) f.write(data)
@ -365,6 +368,8 @@ class EpubContainer(Container):
if outpath is None: if outpath is None:
outpath = self.pathtoepub outpath = self.pathtoepub
from calibre.ebooks.tweak import zip_rebuilder 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) zip_rebuilder(self.root, outpath)
# }}} # }}}

View File

@ -7,10 +7,11 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __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.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 from calibre.utils.fonts.sfnt.subset import subset
def remove_font_face_rules(container, sheet, remove_names): 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'%( report('Reduced total font size to %.1f%% of original'%(
total_new/total_old*100)) 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)