Skip to content

Commit

Permalink
fixes #570
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jun 9, 2024
1 parent fd98612 commit efb4841
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@
'fastcore.xml.XT.children': ('xml.html#xt.children', 'fastcore/xml.py'),
'fastcore.xml.XT.tag': ('xml.html#xt.tag', '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'),
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
Expand Down
18 changes: 11 additions & 7 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ def xt(tag:str, *c, **kw):
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())

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

# %% ../nbs/11_xml.ipynb 14
def _to_attr(k,v):
if isinstance(v,bool):
if v==True : return str(k)
if v==False: return ''
return f'{k}="{escape(str(v), quote=False)}"'
return f'{k}="{escape(str(v), quote=True)}"'

# %% ../nbs/11_xml.ipynb 14
# %% ../nbs/11_xml.ipynb 15
def to_xml(elm, lvl=0):
"Convert `xt` element tree into an XML string"
if elm is None: return ''
if isinstance(elm, tuple): return '\n'.join(to_xml(o) for o in elm)
if hasattr(elm, '__xt__'): elm = elm.__xt__()
sp = ' ' * lvl
if not isinstance(elm, list):
if isinstance(elm, str): elm = escape(elm)
return f'{elm}\n'
if not isinstance(elm, list): return f'{_escape(elm)}\n'

tag,cs,attrs = elm
stag = tag
Expand All @@ -82,17 +84,19 @@ def to_xml(elm, lvl=0):

cltag = '' if tag in voids 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'
return res

# %% ../nbs/11_xml.ipynb 16
# %% ../nbs/11_xml.ipynb 17
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 17
# %% ../nbs/11_xml.ipynb 18
def showtags(s):
return f"""<code><pre>
{escape(to_xml(s))}
Expand Down
26 changes: 18 additions & 8 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,18 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e89496fb",
"id": "254c8ff3",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _escape(s): return '' if s is None else escape(s) if isinstance(s, str) else s"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0255b96f",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -211,7 +222,7 @@
" if isinstance(v,bool):\n",
" if v==True : return str(k)\n",
" if v==False: return ''\n",
" return f'{k}=\"{escape(str(v), quote=False)}\"'"
" return f'{k}=\"{escape(str(v), quote=True)}\"'"
]
},
{
Expand All @@ -224,12 +235,11 @@
"#| export\n",
"def to_xml(elm, lvl=0):\n",
" \"Convert `xt` element tree into an XML string\"\n",
" if elm is None: return ''\n",
" if isinstance(elm, tuple): return '\\n'.join(to_xml(o) for o in elm)\n",
" if hasattr(elm, '__xt__'): elm = elm.__xt__()\n",
" sp = ' ' * lvl\n",
" if not isinstance(elm, list):\n",
" if isinstance(elm, str): elm = escape(elm)\n",
" return f'{elm}\\n'\n",
" if not isinstance(elm, list): return f'{_escape(elm)}\\n'\n",
"\n",
" tag,cs,attrs = elm\n",
" stag = tag\n",
Expand All @@ -239,6 +249,8 @@
"\n",
" cltag = '' if tag in voids 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",
Expand All @@ -257,9 +269,7 @@
"text": [
"<html>\n",
" <head>\n",
" <title>\n",
"Some page\n",
" </title>\n",
" <title>Some page</title>\n",
" </head>\n",
" <body>\n",
" <div class=\"myclass\">\n",
Expand Down

0 comments on commit efb4841

Please sign in to comment.