mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Compactify the JSON repr of HTML a bit
This commit is contained in:
parent
98eacb55cf
commit
68371e6294
@ -177,26 +177,33 @@ def split_name(name):
|
||||
return l[1:], r
|
||||
return None, l
|
||||
|
||||
boolean_attributes = frozenset('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,defaultchecked,defaultmuted,defaultselected,defer,disabled,enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,truespeed,typemustmatch,visible'.split(',')) # noqa
|
||||
|
||||
def serialize_elem(elem, nsmap):
|
||||
ns, name = split_name(elem.tag)
|
||||
attribs = []
|
||||
ans = {'n':name}
|
||||
if elem.text:
|
||||
ans['te'] = elem.text
|
||||
ans['x'] = elem.text
|
||||
if elem.tail:
|
||||
ans['ta'] = elem.tail
|
||||
ans['l'] = elem.tail
|
||||
if ns:
|
||||
ns = nsmap[ns]
|
||||
if ns:
|
||||
ans['ns'] = ns
|
||||
ans['s'] = ns
|
||||
attribs = []
|
||||
for attr, val in elem.items():
|
||||
attr_ns, aname = split_name(attr)
|
||||
s = {'n':aname, 'v':val}
|
||||
al = attr.lower()
|
||||
if not attr_ns and al in boolean_attributes:
|
||||
if val and val.lower() in (al, ''):
|
||||
attribs.append([al, al])
|
||||
continue
|
||||
attrib = [attr, val]
|
||||
if attr_ns:
|
||||
attr_ns = nsmap[attr_ns]
|
||||
if attr_ns:
|
||||
s['ns'] = attr_ns
|
||||
attribs.append(s)
|
||||
attrib.append(attr_ns)
|
||||
attribs.append(attrib)
|
||||
if attribs:
|
||||
ans['a'] = attribs
|
||||
return ans
|
||||
|
Loading…
x
Reference in New Issue
Block a user