Skip to content

Commit

Permalink
fixes #586
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jul 27, 2024
1 parent bbcc9a1 commit 750768f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
### New Features

- Add `process` param to `threaded` ([#550](https://github.com/fastai/fastcore/issues/550))
- Check for `__xt__` in `to_xml`
- Check for `__ft__` in `to_xml`


## 1.5.34
Expand Down
2 changes: 1 addition & 1 deletion fastcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.5.56"
__version__ = "1.6.0"
20 changes: 10 additions & 10 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,22 @@
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
'fastcore.xml': { 'fastcore.xml.Html': ('xml.html#html', 'fastcore/xml.py'),
'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
'fastcore.xml.XT.__getattr__': ('xml.html#xt.__getattr__', 'fastcore/xml.py'),
'fastcore.xml.XT.__init__': ('xml.html#xt.__init__', 'fastcore/xml.py'),
'fastcore.xml.XT.__setattr__': ('xml.html#xt.__setattr__', 'fastcore/xml.py'),
'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': { 'fastcore.xml.FT': ('xml.html#ft', 'fastcore/xml.py'),
'fastcore.xml.FT.__getattr__': ('xml.html#ft.__getattr__', 'fastcore/xml.py'),
'fastcore.xml.FT.__init__': ('xml.html#ft.__init__', 'fastcore/xml.py'),
'fastcore.xml.FT.__setattr__': ('xml.html#ft.__setattr__', 'fastcore/xml.py'),
'fastcore.xml.FT.attrs': ('xml.html#ft.attrs', 'fastcore/xml.py'),
'fastcore.xml.FT.children': ('xml.html#ft.children', 'fastcore/xml.py'),
'fastcore.xml.FT.tag': ('xml.html#ft.tag', 'fastcore/xml.py'),
'fastcore.xml.Html': ('xml.html#html', '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'),
'fastcore.xml.ft': ('xml.html#ft', 'fastcore/xml.py'),
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py')},
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
'fastcore.xtras.ContextManagers.__enter__': ('xtras.html#contextmanagers.__enter__', 'fastcore/xtras.py'),
'fastcore.xtras.ContextManagers.__exit__': ('xtras.html#contextmanagers.__exit__', 'fastcore/xtras.py'),
Expand Down
29 changes: 15 additions & 14 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.

# %% auto 0
__all__ = ['XT', 'xt', 'Html', 'to_xml', 'highlight', 'showtags', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code',
__all__ = ['FT', 'ft', 'Html', 'to_xml', 'highlight', 'showtags', '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',
Expand All @@ -26,7 +26,8 @@ def _attrmap(o):
return o.lstrip('_').replace('_', '-')

# %% ../nbs/11_xml.ipynb
class XT(list):
class FT(list):
"A 'Fast Tag' structure, which is a `list` of `[tag,children,attrs]`"
def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):
super().__init__([tag, cs, {**(attrs or {}), **kwargs}])
self.void_ = void_
Expand All @@ -47,11 +48,11 @@ def __getattr__(self, k):
return self.attrs[k.lstrip('_').replace('_', '-')]

# %% ../nbs/11_xml.ipynb
def xt(tag:str, *c, void_=False, **kw):
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
def ft(tag:str, *c, void_=False, **kw):
"Create an `FT` structure for `to_xml()`"
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, void_=void_)
return FT(tag.lower(),c,kw, void_=void_)

# %% ../nbs/11_xml.ipynb
_g = globals()
Expand All @@ -65,14 +66,14 @@ def xt(tag:str, *c, void_=False, **kw):
'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video',
'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']

for o in _all_: _g[o] = partial(xt, o.lower())
for o in _all_: _g[o] = partial(ft, o.lower())

# %% ../nbs/11_xml.ipynb
def Html(*c, doctype=True, **kwargs)->XT:
def Html(*c, doctype=True, **kwargs)->FT:
"An HTML tag, optionally preceeded by `!DOCTYPE HTML`"
res = xt('html', *c, **kwargs)
res = ft('html', *c, **kwargs)
if not doctype: return res
return (xt('!DOCTYPE', html=True, void_=True), res)
return (ft('!DOCTYPE', html=True, void_=True), res)

# %% ../nbs/11_xml.ipynb
def _escape(s): return '' if s is None else escape(s) if isinstance(s, str) else s
Expand All @@ -91,10 +92,10 @@ def _to_attr(k,v):

# %% ../nbs/11_xml.ipynb
def to_xml(elm, lvl=0):
"Convert `xt` element tree into an XML string"
"Convert `ft` 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__()
if hasattr(elm, '__ft__'): elm = elm.__ft__()
sp = ' ' * lvl
if not isinstance(elm, list): return f'{_escape(elm)}\n'

Expand All @@ -107,7 +108,7 @@ def to_xml(elm, lvl=0):
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__'):
if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__ft__'):
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)
Expand All @@ -125,10 +126,10 @@ def showtags(s):
{escape(to_xml(s))}
</code></pre>"""

XT._repr_markdown_ = highlight
FT._repr_markdown_ = highlight

# %% ../nbs/11_xml.ipynb
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)
def _f(*c, target_id=None, **kwargs): return ft(tag, *c, target_id=target_id, **kwargs)
return _f
29 changes: 15 additions & 14 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"outputs": [],
"source": [
"#|export\n",
"class XT(list):\n",
"class FT(list):\n",
" \"A 'Fast Tag' structure, which is a `list` of `[tag,children,attrs]`\"\n",
" def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):\n",
" super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n",
" self.void_ = void_\n",
Expand Down Expand Up @@ -100,11 +101,11 @@
"outputs": [],
"source": [
"#| export\n",
"def xt(tag:str, *c, void_=False, **kw):\n",
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
"def ft(tag:str, *c, void_=False, **kw):\n",
" \"Create an `FT` structure for `to_xml()`\"\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, void_=void_)"
" return FT(tag.lower(),c,kw, void_=void_)"
]
},
{
Expand All @@ -126,15 +127,15 @@
" 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video',\n",
" 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']\n",
"\n",
"for o in _all_: _g[o] = partial(xt, o.lower())"
"for o in _all_: _g[o] = partial(ft, o.lower())"
]
},
{
"cell_type": "markdown",
"id": "732e44ab",
"metadata": {},
"source": [
"The main HTML tags are exported as `xt` partials.\n",
"The main HTML tags are exported as `ft` partials.\n",
"\n",
"Attributes are passed as keywords. Use 'klass' and 'fr' instead of 'class' and 'for', to avoid Python reserved word clashes."
]
Expand All @@ -147,11 +148,11 @@
"outputs": [],
"source": [
"#| export\n",
"def Html(*c, doctype=True, **kwargs)->XT:\n",
"def Html(*c, doctype=True, **kwargs)->FT:\n",
" \"An HTML tag, optionally preceeded by `!DOCTYPE HTML`\"\n",
" res = xt('html', *c, **kwargs)\n",
" res = ft('html', *c, **kwargs)\n",
" if not doctype: return res\n",
" return (xt('!DOCTYPE', html=True, void_=True), res)"
" return (ft('!DOCTYPE', html=True, void_=True), res)"
]
},
{
Expand Down Expand Up @@ -295,10 +296,10 @@
"source": [
"#| export\n",
"def to_xml(elm, lvl=0):\n",
" \"Convert `xt` element tree into an XML string\"\n",
" \"Convert `ft` 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",
" if hasattr(elm, '__ft__'): elm = elm.__ft__()\n",
" sp = ' ' * lvl\n",
" if not isinstance(elm, list): return f'{_escape(elm)}\\n'\n",
"\n",
Expand All @@ -311,7 +312,7 @@
" 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",
" if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__ft__'):\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",
Expand Down Expand Up @@ -378,7 +379,7 @@
"{escape(to_xml(s))}\n",
"</code></pre>\"\"\"\n",
"\n",
"XT._repr_markdown_ = highlight"
"FT._repr_markdown_ = highlight"
]
},
{
Expand All @@ -391,7 +392,7 @@
"#| 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",
" def _f(*c, target_id=None, **kwargs): return ft(tag, *c, target_id=target_id, **kwargs)\n",
" return _f"
]
},
Expand Down
6 changes: 3 additions & 3 deletions settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ author = Jeremy Howard and Sylvain Gugger
author_email = [email protected]
copyright = fast.ai
branch = master
version = 1.5.56
min_python = 3.7
version = 1.6.0
min_python = 3.8
audience = Developers
language = English
custom_sidebar = False
license = apache2
status = 4
status = 5
nbs_path = nbs
doc_path = _docs
dev_requirements = numpy nbdev>=0.2.39 matplotlib pillow torch pandas nbclassic
Expand Down

0 comments on commit 750768f

Please sign in to comment.