Skip to content

Commit

Permalink
fixes #575
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jun 21, 2024
1 parent 855a7be commit fde17fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
1 change: 1 addition & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@
'fastcore.xml.XT.attrs': ('xml.html#xt.attrs', 'fastcore/xml.py'),
'fastcore.xml.XT.children': ('xml.html#xt.children', 'fastcore/xml.py'),
'fastcore.xml.XT.tag': ('xml.html#xt.tag', 'fastcore/xml.py'),
'fastcore.xml.__getattr__': ('xml.html#__getattr__', 'fastcore/xml.py'),
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
'fastcore.xml._escape': ('xml.html#_escape', 'fastcore/xml.py'),
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
Expand Down
47 changes: 27 additions & 20 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.

# %% auto 0
__all__ = ['voids', 'XT', 'xt', 'to_xml', 'highlight', 'showtags', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body',
'Pre', 'Code', 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S',
'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead',
'Tbody', 'Tfoot', 'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button',
'Select', 'Option', 'Label', 'Fieldset', 'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer',
'Section', 'Article', 'Aside', 'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param',
'Video', 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']
__all__ = ['XT', 'xt', 'to_xml', 'highlight', 'showtags', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code',
'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub',
'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot',
'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button', 'Select', 'Option',
'Label', 'Fieldset', 'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer', 'Section', 'Article',
'Aside', 'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video', 'Audio',
'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']

# %% ../nbs/11_xml.ipynb 2
from .utils import *
Expand All @@ -27,7 +27,10 @@ def _attrmap(o):

# %% ../nbs/11_xml.ipynb 5
class XT(list):
def __init__(self, tag, cs, attrs=None, **kwargs): super().__init__([tag, cs, {**(attrs or {}), **kwargs}])
def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):
super().__init__([tag, cs, {**(attrs or {}), **kwargs}])
self.void_ = void_

@property
def tag(self): return self[0]
@property
Expand All @@ -36,19 +39,19 @@ def children(self): return self[1]
def attrs(self): return self[2]

def __setattr__(self, k, v):
if k.startswith('__') or k in ('tag','cs','attrs'): return super().__setattr__(k,v)
if k.startswith('__') or k in ('tag','cs','attrs','void_'): return super().__setattr__(k,v)
self.attrs[k.lstrip('_').replace('_', '-')] = v

def __getattr__(self, k):
if k.startswith('__') or k not in self.attrs: raise AttributeError(k)
return self.attrs[k.lstrip('_').replace('_', '-')]

# %% ../nbs/11_xml.ipynb 6
def xt(tag:str, *c, **kw):
def xt(tag:str, *c, void_=False, **kw):
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
kw = {_attrmap(k):v for k,v in kw.items() if v is not None}
return XT(tag.lower(),c,kw)
return XT(tag.lower(),c,kw, void_=void_)

# %% ../nbs/11_xml.ipynb 7
_g = globals()
Expand All @@ -65,12 +68,9 @@ def xt(tag:str, *c, **kw):
for o in _all_: _g[o] = partial(xt, o.lower())

# %% ../nbs/11_xml.ipynb 14
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())

# %% ../nbs/11_xml.ipynb 15
def _escape(s): return '' if s is None else escape(s) if isinstance(s, str) else s

# %% ../nbs/11_xml.ipynb 16
# %% ../nbs/11_xml.ipynb 15
def _to_attr(k,v):
if isinstance(v,bool):
if v==True : return str(k)
Expand All @@ -82,7 +82,7 @@ def _to_attr(k,v):
if qt in v: qt = "'"
return f'{k}={qt}{v}{qt}'

# %% ../nbs/11_xml.ipynb 17
# %% ../nbs/11_xml.ipynb 16
def to_xml(elm, lvl=0):
"Convert `xt` element tree into an XML string"
if elm is None: return ''
Expand All @@ -97,24 +97,31 @@ def to_xml(elm, lvl=0):
sattrs = (_to_attr(k,v) for k,v in attrs.items())
stag += ' ' + ' '.join(sattrs)

cltag = '' if tag in voids else f'</{tag}>'
isvoid = getattr(elm, 'void_', False)
cltag = '' if isvoid else f'</{tag}>'
if not cs: return f'{sp}<{stag}>{cltag}\n'
if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__xt__'):
return f'{sp}<{stag}>{_escape(cs[0])}{cltag}\n'
res = f'{sp}<{stag}>\n'
res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)
if tag not in voids: res += f'{sp}{cltag}\n'
if not isvoid: res += f'{sp}{cltag}\n'
return res

# %% ../nbs/11_xml.ipynb 19
# %% ../nbs/11_xml.ipynb 18
def highlight(s, lang='xml'):
"Markdown to syntax-highlight `s` in language `lang`"
return f'```{lang}\n{to_xml(s)}\n```'

# %% ../nbs/11_xml.ipynb 20
# %% ../nbs/11_xml.ipynb 19
def showtags(s):
return f"""<code><pre>
{escape(to_xml(s))}
</code></pre>"""

XT._repr_markdown_ = highlight

# %% ../nbs/11_xml.ipynb 20
def __getattr__(tag):
if tag.startswith('_') or tag[0].islower(): raise AttributeError
def _f(*c, target_id=None, **kwargs): return xt(tag, *c, target_id=target_id, **kwargs)
return _f
45 changes: 26 additions & 19 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@
"source": [
"#|export\n",
"class XT(list):\n",
" def __init__(self, tag, cs, attrs=None, **kwargs): super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n",
" def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):\n",
" super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n",
" self.void_ = void_\n",
"\n",
" @property\n",
" def tag(self): return self[0]\n",
" @property\n",
Expand All @@ -81,7 +84,7 @@
" def attrs(self): return self[2]\n",
"\n",
" def __setattr__(self, k, v):\n",
" if k.startswith('__') or k in ('tag','cs','attrs'): return super().__setattr__(k,v)\n",
" if k.startswith('__') or k in ('tag','cs','attrs','void_'): return super().__setattr__(k,v)\n",
" self.attrs[k.lstrip('_').replace('_', '-')] = v\n",
"\n",
" def __getattr__(self, k):\n",
Expand All @@ -97,11 +100,11 @@
"outputs": [],
"source": [
"#| export\n",
"def xt(tag:str, *c, **kw):\n",
"def xt(tag:str, *c, void_=False, **kw):\n",
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
" kw = {_attrmap(k):v for k,v in kw.items() if v is not None}\n",
" return XT(tag.lower(),c,kw)"
" return XT(tag.lower(),c,kw, void_=void_)"
]
},
{
Expand Down Expand Up @@ -236,17 +239,6 @@
"elem"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7de63a4",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -300,13 +292,14 @@
" sattrs = (_to_attr(k,v) for k,v in attrs.items())\n",
" stag += ' ' + ' '.join(sattrs)\n",
"\n",
" cltag = '' if tag in voids else f'</{tag}>'\n",
" isvoid = getattr(elm, 'void_', False)\n",
" cltag = '' if isvoid else f'</{tag}>'\n",
" if not cs: return f'{sp}<{stag}>{cltag}\\n'\n",
" if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__xt__'):\n",
" return f'{sp}<{stag}>{_escape(cs[0])}{cltag}\\n'\n",
" res = f'{sp}<{stag}>\\n'\n",
" res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)\n",
" if tag not in voids: res += f'{sp}{cltag}\\n'\n",
" if not isvoid: res += f'{sp}{cltag}\\n'\n",
" return res"
]
},
Expand All @@ -327,8 +320,8 @@
" <body>\n",
" <div class=\"myclass\">\n",
"Some text\n",
" <input name=\"me\">\n",
" <img src=\"filename\" data=\"1\">\n",
" <input name=\"me\"></input>\n",
" <img src=\"filename\" data=\"1\"></img>\n",
" </div>\n",
" </body>\n",
"</html>\n",
Expand Down Expand Up @@ -370,6 +363,20 @@
"XT._repr_markdown_ = highlight"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "530666f8",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def __getattr__(tag):\n",
" if tag.startswith('_') or tag[0].islower(): raise AttributeError\n",
" def _f(*c, target_id=None, **kwargs): return xt(tag, *c, target_id=target_id, **kwargs)\n",
" return _f"
]
},
{
"cell_type": "markdown",
"id": "df973d4e",
Expand Down

0 comments on commit fde17fc

Please sign in to comment.