Minor namespace clean-ups for mobi-generation.

This commit is contained in:
Marshall T. Vandegrift 2008-12-30 00:39:54 -05:00
parent 7ff96f540f
commit c9124017af
3 changed files with 19 additions and 15 deletions

View File

@ -66,6 +66,12 @@ def barename(name):
return name.split('}', 1)[1]
return name
def prefixname(name, nsrmap):
prefix = nsrmap[namespace(name)]
if not prefix:
return barename(name)
return ':'.join((prefix, barename(name)))
def xpath(elem, expr):
return elem.xpath(expr, namespaces=XPNSMAP)

View File

@ -26,7 +26,7 @@ import calibre.ebooks.lit.maps as maps
from calibre.ebooks.lit.oeb import OEB_DOCS, OEB_STYLES, OEB_CSS_MIME, \
CSS_MIME, OPF_MIME, XML_NS, XML
from calibre.ebooks.lit.oeb import namespace, barename, urlnormalize, xpath
from calibre.ebooks.lit.oeb import FauxLogger, OEBBook
from calibre.ebooks.lit.oeb import prefixname, FauxLogger, OEBBook
from calibre.ebooks.lit.stylizer import Stylizer
from calibre.ebooks.lit.lzx import Compressor
import calibre
@ -116,12 +116,6 @@ LZXC_CONTROL = \
COLLAPSE = re.compile(r'[ \t\r\n\v]+')
def prefixname(name, nsrmap):
prefix = nsrmap[namespace(name)]
if not prefix:
return barename(name)
return ':'.join((prefix, barename(name)))
def decint(value):
bytes = []
while True:

View File

@ -20,11 +20,11 @@ from urlparse import urldefrag
from lxml import etree
from PIL import Image
from calibre.ebooks.mobi.palmdoc import compress_doc
from calibre.ebooks.lit.oeb import XHTML, XHTML_NS, OEB_DOCS
from calibre.ebooks.lit.oeb import xpath, barename, namespace
from calibre.ebooks.lit.oeb import XML_NS, XHTML, XHTML_NS, OEB_DOCS
from calibre.ebooks.lit.oeb import xpath, barename, namespace, prefixname
from calibre.ebooks.lit.oeb import FauxLogger, OEBBook
MBP_NS = 'http://mobipocket.cam/ns/mbp'
MBP_NS = 'http://mobipocket.com/ns/mbp'
def MBP(name): return '{%s}%s' % (MBP_NS, name)
EXTH_CODES = {
@ -50,7 +50,10 @@ def encode(data):
return data.encode('ascii', 'xmlcharrefreplace')
class Serializer(object):
NSRMAP = {'': None, XML_NS: 'xml', XHTML_NS: '', MBP_NS: 'mbp'}
def __init__(self, oeb, images):
self.oeb = oeb
self.images = images
@ -118,14 +121,12 @@ class Serializer(object):
for elem in item.data.find(XHTML('body')):
self.serialize_elem(elem, item)
def serialize_elem(self, elem, item):
ns = namespace(elem.tag)
if ns not in (XHTML_NS, MBP_NS):
def serialize_elem(self, elem, item, nsrmap=NSRMAP):
if namespace(elem.tag) not in nsrmap:
return
buffer = self.buffer
hrefs = self.oeb.manifest.hrefs
tag = barename(elem.tag)
if ns == MBP_NS: tag = 'mbp:' + tag
tag = prefixname(elem.tag, nsrmap)
for attr in ('name', 'id'):
if attr in elem.attrib:
id = '_'.join((item.id, elem.attrib[attr]))
@ -135,6 +136,9 @@ class Serializer(object):
buffer.write(tag)
if elem.attrib:
for attr, val in elem.attrib.items():
if namespace(attr) not in nsrmap:
continue
attr = prefixname(attr, nsrmap)
buffer.write(' ')
if attr == 'href':
if self.serialize_href(val, item.id):