mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Minor namespace clean-ups for mobi-generation.
This commit is contained in:
parent
7ff96f540f
commit
c9124017af
@ -66,6 +66,12 @@ def barename(name):
|
|||||||
return name.split('}', 1)[1]
|
return name.split('}', 1)[1]
|
||||||
return name
|
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):
|
def xpath(elem, expr):
|
||||||
return elem.xpath(expr, namespaces=XPNSMAP)
|
return elem.xpath(expr, namespaces=XPNSMAP)
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ import calibre.ebooks.lit.maps as maps
|
|||||||
from calibre.ebooks.lit.oeb import OEB_DOCS, OEB_STYLES, OEB_CSS_MIME, \
|
from calibre.ebooks.lit.oeb import OEB_DOCS, OEB_STYLES, OEB_CSS_MIME, \
|
||||||
CSS_MIME, OPF_MIME, XML_NS, XML
|
CSS_MIME, OPF_MIME, XML_NS, XML
|
||||||
from calibre.ebooks.lit.oeb import namespace, barename, urlnormalize, xpath
|
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.stylizer import Stylizer
|
||||||
from calibre.ebooks.lit.lzx import Compressor
|
from calibre.ebooks.lit.lzx import Compressor
|
||||||
import calibre
|
import calibre
|
||||||
@ -116,12 +116,6 @@ LZXC_CONTROL = \
|
|||||||
|
|
||||||
COLLAPSE = re.compile(r'[ \t\r\n\v]+')
|
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):
|
def decint(value):
|
||||||
bytes = []
|
bytes = []
|
||||||
while True:
|
while True:
|
||||||
|
@ -20,11 +20,11 @@ from urlparse import urldefrag
|
|||||||
from lxml import etree
|
from lxml import etree
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from calibre.ebooks.mobi.palmdoc import compress_doc
|
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 XML_NS, XHTML, XHTML_NS, OEB_DOCS
|
||||||
from calibre.ebooks.lit.oeb import xpath, barename, namespace
|
from calibre.ebooks.lit.oeb import xpath, barename, namespace, prefixname
|
||||||
from calibre.ebooks.lit.oeb import FauxLogger, OEBBook
|
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)
|
def MBP(name): return '{%s}%s' % (MBP_NS, name)
|
||||||
|
|
||||||
EXTH_CODES = {
|
EXTH_CODES = {
|
||||||
@ -50,7 +50,10 @@ def encode(data):
|
|||||||
return data.encode('ascii', 'xmlcharrefreplace')
|
return data.encode('ascii', 'xmlcharrefreplace')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Serializer(object):
|
class Serializer(object):
|
||||||
|
NSRMAP = {'': None, XML_NS: 'xml', XHTML_NS: '', MBP_NS: 'mbp'}
|
||||||
|
|
||||||
def __init__(self, oeb, images):
|
def __init__(self, oeb, images):
|
||||||
self.oeb = oeb
|
self.oeb = oeb
|
||||||
self.images = images
|
self.images = images
|
||||||
@ -118,14 +121,12 @@ class Serializer(object):
|
|||||||
for elem in item.data.find(XHTML('body')):
|
for elem in item.data.find(XHTML('body')):
|
||||||
self.serialize_elem(elem, item)
|
self.serialize_elem(elem, item)
|
||||||
|
|
||||||
def serialize_elem(self, elem, item):
|
def serialize_elem(self, elem, item, nsrmap=NSRMAP):
|
||||||
ns = namespace(elem.tag)
|
if namespace(elem.tag) not in nsrmap:
|
||||||
if ns not in (XHTML_NS, MBP_NS):
|
|
||||||
return
|
return
|
||||||
buffer = self.buffer
|
buffer = self.buffer
|
||||||
hrefs = self.oeb.manifest.hrefs
|
hrefs = self.oeb.manifest.hrefs
|
||||||
tag = barename(elem.tag)
|
tag = prefixname(elem.tag, nsrmap)
|
||||||
if ns == MBP_NS: tag = 'mbp:' + tag
|
|
||||||
for attr in ('name', 'id'):
|
for attr in ('name', 'id'):
|
||||||
if attr in elem.attrib:
|
if attr in elem.attrib:
|
||||||
id = '_'.join((item.id, elem.attrib[attr]))
|
id = '_'.join((item.id, elem.attrib[attr]))
|
||||||
@ -135,6 +136,9 @@ class Serializer(object):
|
|||||||
buffer.write(tag)
|
buffer.write(tag)
|
||||||
if elem.attrib:
|
if elem.attrib:
|
||||||
for attr, val in elem.attrib.items():
|
for attr, val in elem.attrib.items():
|
||||||
|
if namespace(attr) not in nsrmap:
|
||||||
|
continue
|
||||||
|
attr = prefixname(attr, nsrmap)
|
||||||
buffer.write(' ')
|
buffer.write(' ')
|
||||||
if attr == 'href':
|
if attr == 'href':
|
||||||
if self.serialize_href(val, item.id):
|
if self.serialize_href(val, item.id):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user