From 5ab558aca6430528e69aafb34fc53218024fda5a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Aug 2009 09:10:28 -0600 Subject: [PATCH 1/5] IGN:Prefer name to id when they differ during splitting. Also use first rather than last occurrence of an id. --- src/calibre/ebooks/oeb/transforms/split.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/oeb/transforms/split.py b/src/calibre/ebooks/oeb/transforms/split.py index d8d750eade..d639d895bf 100644 --- a/src/calibre/ebooks/oeb/transforms/split.py +++ b/src/calibre/ebooks/oeb/transforms/split.py @@ -457,10 +457,9 @@ class FlowSplitter(object): root = tree.getroot() self.files.append(self.base%i) for elem in root.xpath('//*[@id or @name]'): - anchor = elem.get('id', '') - if not anchor: - anchor = elem.get('name') - self.anchor_map[anchor] = self.files[-1] + for anchor in elem.get('id', ''), elem.get('name', ''): + if anchor != '' and anchor not in self.anchor_map: + self.anchor_map[anchor] = self.files[-1] for elem in root.xpath('//*[@%s]'%SPLIT_POINT_ATTR): elem.attrib.pop(SPLIT_POINT_ATTR, '0') From 83acd63c45d6929c68e9b17a7baf1353636f4930 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Aug 2009 09:13:34 -0600 Subject: [PATCH 2/5] Implement #2517 (Remove the #HREF syntax from the Chapter URL in the toc.ncx file for an ePUB) --- src/calibre/ebooks/epub/output.py | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/calibre/ebooks/epub/output.py b/src/calibre/ebooks/epub/output.py index 986d1bff24..d76e3cb20c 100644 --- a/src/calibre/ebooks/epub/output.py +++ b/src/calibre/ebooks/epub/output.py @@ -163,6 +163,7 @@ class EPUBOutput(OutputFormatPlugin): self.workaround_ade_quirks() self.workaround_webkit_quirks() + self.workaround_sony_quirks() from calibre.ebooks.oeb.transforms.rescale import RescaleImages RescaleImages()(oeb, opts) self.insert_cover() @@ -346,5 +347,48 @@ class EPUBOutput(OutputFormatPlugin): else: self.oeb.log.warn('No stylesheet found') + def workaround_sony_quirks(self): + ''' + Perform toc link transforms to alleviate slow loading. + ''' + from calibre.ebooks.oeb.base import urldefrag, XPath + def frag_is_at_top(root, frag): + body = XPath('//h:body')(root) + if body: + body = body[0] + else: + return 0 # Impossible? + tree = body.getroottree() + elem = XPath('//*[@id="%s" or @name="%s"]'%(frag, frag))(root) + if elem: + elem = elem[0] + else: + return 0 + path = tree.getpath(elem) + for el in body.iterdescendants(): + epath = tree.getpath(el) + if epath == path: + break + if el.text and el.text.strip(): + return 0 + if not path.startswith(epath): + # Only check tail of non-parent elements + if el.tail and el.tail.strip(): + return 0 + return 1 + def simplify_toc_entry(toc): + if toc.href: + href, frag = urldefrag(toc.href) + if frag: + for x in self.oeb.spine: + if x.href == href: + if frag_is_at_top(x.data, frag): + toc.href = href + break + for x in toc: + simplify_toc_entry(x) + + if self.oeb.toc: + simplify_toc_entry(self.oeb.toc) From 516e1ca9623ffb501ad6fe7d6b861012b1640a33 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Aug 2009 10:15:26 -0600 Subject: [PATCH 3/5] Fix #3218 (Adding a book in an existing format causes a temporary duplication of formats) --- src/calibre/ebooks/epub/output.py | 12 +++++++----- src/calibre/gui2/dialogs/metadata_single.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/epub/output.py b/src/calibre/ebooks/epub/output.py index d76e3cb20c..c03d103e3a 100644 --- a/src/calibre/ebooks/epub/output.py +++ b/src/calibre/ebooks/epub/output.py @@ -358,25 +358,25 @@ class EPUBOutput(OutputFormatPlugin): if body: body = body[0] else: - return 0 # Impossible? + return False tree = body.getroottree() elem = XPath('//*[@id="%s" or @name="%s"]'%(frag, frag))(root) if elem: elem = elem[0] else: - return 0 + return False path = tree.getpath(elem) for el in body.iterdescendants(): epath = tree.getpath(el) if epath == path: break if el.text and el.text.strip(): - return 0 + return False if not path.startswith(epath): # Only check tail of non-parent elements if el.tail and el.tail.strip(): - return 0 - return 1 + return False + return True def simplify_toc_entry(toc): if toc.href: @@ -385,6 +385,8 @@ class EPUBOutput(OutputFormatPlugin): for x in self.oeb.spine: if x.href == href: if frag_is_at_top(x.data, frag): + self.log.debug('Removing anchor from TOC href:', + href+'#'+frag) toc.href = href break for x in toc: diff --git a/src/calibre/gui2/dialogs/metadata_single.py b/src/calibre/gui2/dialogs/metadata_single.py index 42785e3f3e..ab86cc810d 100644 --- a/src/calibre/gui2/dialogs/metadata_single.py +++ b/src/calibre/gui2/dialogs/metadata_single.py @@ -144,7 +144,7 @@ class MetadataSingleDialog(ResizableDialog, Ui_MetadataSingleDialog): ext = os.path.splitext(_file)[1].lower().replace('.', '') for row in range(self.formats.count()): fmt = self.formats.item(row) - if fmt.ext == ext: + if fmt.ext.lower() == ext: self.formats.takeItem(row) break Format(self.formats, ext, size, path=_file) From d54b49ce78293f61586e9b5df890ad4cc3a6b536 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Aug 2009 11:16:23 -0600 Subject: [PATCH 4/5] IGN:... --- src/calibre/utils/filenames.py | 13 ++++++++++++- src/calibre/utils/sigil.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/calibre/utils/sigil.py diff --git a/src/calibre/utils/filenames.py b/src/calibre/utils/filenames.py index 0aab96522b..bf3cbe2f67 100644 --- a/src/calibre/utils/filenames.py +++ b/src/calibre/utils/filenames.py @@ -8,7 +8,7 @@ from math import ceil from calibre.ebooks.unidecode.unidecoder import Unidecoder from calibre import sanitize_file_name -from calibre.constants import preferred_encoding +from calibre.constants import preferred_encoding, iswindows udc = Unidecoder() def ascii_text(orig): @@ -65,3 +65,14 @@ def shorten_components_to(length, components): ans.append(r) return ans +def find_executable_in_path(name, path=None): + if path is None: + path = os.environ.get('PATH', '') + sep = ';' if iswindows else ':' + if iswindows and not name.endswith('.exe'): + name += '.exe' + path = path.split(sep) + for x in path: + q = os.path.abspath(os.path.join(x, name)) + if os.access(q, os.X_OK): + return q diff --git a/src/calibre/utils/sigil.py b/src/calibre/utils/sigil.py new file mode 100644 index 0000000000..c87cd5ea93 --- /dev/null +++ b/src/calibre/utils/sigil.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import with_statement + +__license__ = 'GPL v3' +__copyright__ = '2009, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +from calibre.utils.filenames import find_executable_in_path +from calibre.constants import iswindows + +def find_executable(): + name = 'sigil' + ('.exe' if iswindows else '') + path = find_executable_in_path(name) + #if path is None and iswindows: + # path = search_program_files() + From fb552eba06a3d24a03030ef87e8c4e0d46d93e87 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Aug 2009 11:33:39 -0600 Subject: [PATCH 5/5] IGN:News downloads now have dates set. Also use local timezone for default timestamp when converting --- src/calibre/ebooks/oeb/transforms/metadata.py | 2 +- src/calibre/web/feeds/news.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/oeb/transforms/metadata.py b/src/calibre/ebooks/oeb/transforms/metadata.py index 9733faf487..11509a1edd 100644 --- a/src/calibre/ebooks/oeb/transforms/metadata.py +++ b/src/calibre/ebooks/oeb/transforms/metadata.py @@ -71,7 +71,7 @@ def meta_info_to_oeb_metadata(mi, m, log): m.clear('publication_type') m.add('publication_type', mi.publication_type) if not m.timestamp: - m.add('timestamp', datetime.utcnow().isoformat()) + m.add('timestamp', datetime.now().isoformat()) class MergeMetadata(object): diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 80f8563771..a98e355274 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -11,6 +11,7 @@ import os, time, traceback, re, urlparse, sys from collections import defaultdict from functools import partial from contextlib import nested, closing +from datetime import datetime from PyQt4.Qt import QApplication, QFile, QIODevice @@ -872,6 +873,8 @@ class BasicNewsRecipe(Recipe): mi.publisher = __appname__ mi.author_sort = __appname__ mi.publication_type = 'periodical:'+self.publication_type + mi.timestamp = datetime.now() + mi.pubdate = datetime.now() opf_path = os.path.join(dir, 'index.opf') ncx_path = os.path.join(dir, 'index.ncx') opf = OPFCreator(dir, mi)