mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Remove unused code
This commit is contained in:
parent
cb6b93ef30
commit
ce81764353
@ -52,7 +52,7 @@ from polyglot.binary import (
|
|||||||
as_base64_unicode as encode_component, from_base64_bytes,
|
as_base64_unicode as encode_component, from_base64_bytes,
|
||||||
from_base64_unicode as decode_component
|
from_base64_unicode as decode_component
|
||||||
)
|
)
|
||||||
from polyglot.builtins import as_bytes, is_py3, iteritems, map, unicode_type
|
from polyglot.builtins import as_bytes, iteritems, map, unicode_type
|
||||||
from polyglot.urllib import quote, urlparse
|
from polyglot.urllib import quote, urlparse
|
||||||
|
|
||||||
RENDER_VERSION = 1
|
RENDER_VERSION = 1
|
||||||
@ -705,38 +705,6 @@ known_tags = ('img', 'script', 'link', 'image', 'style')
|
|||||||
discarded_tags = ('meta', 'base')
|
discarded_tags = ('meta', 'base')
|
||||||
|
|
||||||
|
|
||||||
def serialize_elem(elem, nsmap):
|
|
||||||
ns, name = split_name(elem.tag)
|
|
||||||
nl = name.lower()
|
|
||||||
if nl in discarded_tags:
|
|
||||||
# Filter out <meta> tags as they have unknown side-effects
|
|
||||||
# Filter out <base> tags as the viewer uses <base> for URL resolution
|
|
||||||
return
|
|
||||||
if nl in known_tags:
|
|
||||||
name = nl
|
|
||||||
ans = {'n':name}
|
|
||||||
if elem.text:
|
|
||||||
ans['x'] = elem.text
|
|
||||||
if elem.tail:
|
|
||||||
ans['l'] = elem.tail
|
|
||||||
if ns:
|
|
||||||
ns = nsmap[ns]
|
|
||||||
if ns:
|
|
||||||
ans['s'] = ns
|
|
||||||
attribs = []
|
|
||||||
for attr, val in elem.items():
|
|
||||||
attr_ns, aname = split_name(attr)
|
|
||||||
attrib = aname, val
|
|
||||||
if attr_ns:
|
|
||||||
attr_ns = nsmap[attr_ns]
|
|
||||||
if attr_ns:
|
|
||||||
attrib = aname, val, attr_ns
|
|
||||||
attribs.append(attrib)
|
|
||||||
if attribs:
|
|
||||||
ans['a'] = attribs
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_body(root):
|
def ensure_body(root):
|
||||||
# Make sure we have only a single <body>
|
# Make sure we have only a single <body>
|
||||||
bodies = list(root.iterchildren(XHTML('body')))
|
bodies = list(root.iterchildren(XHTML('body')))
|
||||||
@ -759,10 +727,13 @@ def html_as_json(root):
|
|||||||
if ns not in (None, XHTML_NS):
|
if ns not in (None, XHTML_NS):
|
||||||
raise ValueError('HTML tag must be in empty or XHTML namespace')
|
raise ValueError('HTML tag must be in empty or XHTML namespace')
|
||||||
ensure_body(root)
|
ensure_body(root)
|
||||||
|
pl, err = plugins['html_as_json']
|
||||||
|
if err:
|
||||||
|
raise SystemExit('Failed to load html_as_json plugin with error: {}'.format(err))
|
||||||
try:
|
try:
|
||||||
serialize = plugins['html_as_json'][0].serialize
|
serialize = pl.serialize
|
||||||
except (KeyError, AttributeError):
|
except AttributeError:
|
||||||
return as_bytes(json.dumps(html_as_dict(root), ensure_ascii=False, separators=(',', ':')))
|
raise SystemExit('You are running calibre from source, you need to also update the main calibre installation to version >=4.3')
|
||||||
for child in tuple(root.iterchildren('*')):
|
for child in tuple(root.iterchildren('*')):
|
||||||
if child.tag.partition('}')[-1] not in ('head', 'body'):
|
if child.tag.partition('}')[-1] not in ('head', 'body'):
|
||||||
root.remove(child)
|
root.remove(child)
|
||||||
@ -770,45 +741,6 @@ def html_as_json(root):
|
|||||||
return serialize(root, Comment)
|
return serialize(root, Comment)
|
||||||
|
|
||||||
|
|
||||||
def html_as_dict(root):
|
|
||||||
for child in tuple(root.iterchildren('*')):
|
|
||||||
if child.tag.partition('}')[-1] not in ('head', 'body'):
|
|
||||||
root.remove(child)
|
|
||||||
root.text = root.tail = None
|
|
||||||
if is_py3:
|
|
||||||
nsmap = defaultdict(count().__next__)
|
|
||||||
else:
|
|
||||||
nsmap = defaultdict(count().next)
|
|
||||||
nsmap[XHTML_NS]
|
|
||||||
tags = [serialize_elem(root, nsmap)]
|
|
||||||
tree = [0]
|
|
||||||
stack = [(root, tree)]
|
|
||||||
while stack:
|
|
||||||
elem, node = stack.pop()
|
|
||||||
prev_child_node = None
|
|
||||||
for child in elem.iterchildren():
|
|
||||||
tag = getattr(child, 'tag', None)
|
|
||||||
if tag is None or callable(tag):
|
|
||||||
tail = getattr(child, 'tail', None)
|
|
||||||
if tail:
|
|
||||||
if prev_child_node is None:
|
|
||||||
parent_node = node[-1]
|
|
||||||
parent_node = tags[parent_node]
|
|
||||||
parent_node['x'] = parent_node.get('x', '') + tail
|
|
||||||
else:
|
|
||||||
prev_child_node['l'] = prev_child_node.get('l', '') + tail
|
|
||||||
else:
|
|
||||||
cnode = serialize_elem(child, nsmap)
|
|
||||||
if cnode is not None:
|
|
||||||
tags.append(cnode)
|
|
||||||
child_tree_node = [len(tags)-1]
|
|
||||||
node.append(child_tree_node)
|
|
||||||
stack.append((child, child_tree_node))
|
|
||||||
prev_child_node = cnode
|
|
||||||
ns_map = [ns for ns, nsnum in sorted(iteritems(nsmap), key=lambda x: x[1])]
|
|
||||||
return {'ns_map':ns_map, 'tag_map':tags, 'tree':tree}
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_datetimes(d):
|
def serialize_datetimes(d):
|
||||||
for k in tuple(d):
|
for k in tuple(d):
|
||||||
v = d[k]
|
v = d[k]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user