From 68371e6294bd0d0e0382d0a90bc2ae4ed85f6f1b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 27 Mar 2016 11:32:20 +0530 Subject: [PATCH] Compactify the JSON repr of HTML a bit --- src/calibre/srv/render_book.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index 6db12fc588..29178e2e05 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -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