From fb86204423aac5a1f65a1d9f24f54dcfa0649d4e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 21 Jun 2009 11:00:48 -0700 Subject: [PATCH] Add support for author and description when parsing NCX files --- src/calibre/ebooks/oeb/base.py | 17 ++++++++++++----- src/calibre/ebooks/oeb/reader.py | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index e9f82715cb..26bce4e912 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -41,11 +41,14 @@ SVG_NS = 'http://www.w3.org/2000/svg' XLINK_NS = 'http://www.w3.org/1999/xlink' CALIBRE_NS = 'http://calibre.kovidgoyal.net/2009/metadata' RE_NS = 'http://exslt.org/regular-expressions' +MBP_NS = 'http://www.mobipocket.com' 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} + 'svg': SVG_NS, 'xl' : XLINK_NS, 're': RE_NS, + 'mbp': MBP_NS } + OPF1_NSMAP = {'dc': DC11_NS, 'oebpackage': OPF1_NS} OPF2_NSMAP = {'opf': OPF2_NS, 'dc': DC11_NS, 'dcterms': DCTERMS_NS, 'xsi': XSI_NS, 'calibre': CALIBRE_NS} @@ -1373,9 +1376,11 @@ class TOC(object): :attr:`href`: Book-internal URL referenced by this node. :attr:`klass`: Optional semantic class referenced by this node. :attr:`id`: Option unique identifier for this node. + :attr:`author`: Optional author attribution for periodicals + :attr:`description`: Optional description attribute for periodicals """ def __init__(self, title=None, href=None, klass=None, id=None, - play_order=None): + play_order=None, author=None, description=None): self.title = title self.href = urlnormalize(href) if href else href self.klass = klass @@ -1385,10 +1390,12 @@ class TOC(object): if play_order is None: play_order = self.next_play_order() self.play_order = play_order - - def add(self, title, href, klass=None, id=None, play_order=0): + self.author = author + self.description = description + + def add(self, title, href, klass=None, id=None, play_order=0, author=None, description=None): """Create and return a new sub-node of this node.""" - node = TOC(title, href, klass, id, play_order) + node = TOC(title, href, klass, id, play_order, author, description) self.nodes.append(node) return node diff --git a/src/calibre/ebooks/oeb/reader.py b/src/calibre/ebooks/oeb/reader.py index 4ccc1eeed1..75d92f1815 100644 --- a/src/calibre/ebooks/oeb/reader.py +++ b/src/calibre/ebooks/oeb/reader.py @@ -351,9 +351,27 @@ class OEBReader(object): self.logger.warn('TOC reference %r not found' % href) continue id = child.get('id') - klass = child.get('class') + klass = child.get('class', 'chapter') + po = int(child.get('playOrder', self.oeb.toc.next_play_order())) - node = toc.add(title, href, id=id, klass=klass, play_order=po) + + authorElement = xpath(child, + 'descendant::mbp:meta[@name = "author"]') + if authorElement : + author = authorElement[0].text + else : + author = None + + descriptionElement = xpath(child, + 'descendant::mbp:meta[@name = "description"]') + if descriptionElement : + description = descriptionElement[0].text + else : + description = None + + node = toc.add(title, href, id=id, klass=klass, + play_order=po, description=description, author=author) + self._toc_from_navpoint(item, node, child) def _toc_from_ncx(self, item):