Skip to content

Commit d132dd9

Browse files
committed
fixes #591
1 parent dd148cf commit d132dd9

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@
572572
'fastcore.xml.__getattr__': ('xml.html#__getattr__', 'fastcore/xml.py'),
573573
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
574574
'fastcore.xml._escape': ('xml.html#_escape', 'fastcore/xml.py'),
575+
'fastcore.xml._preproc': ('xml.html#_preproc', 'fastcore/xml.py'),
575576
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
576577
'fastcore.xml.ft': ('xml.html#ft', 'fastcore/xml.py'),
577578
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22

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

1212
# %% ../nbs/11_xml.ipynb
1313
from .utils import *
@@ -47,19 +47,19 @@ def __setattr__(self, k, v):
4747
def __getattr__(self, k):
4848
if k.startswith('__') or k not in self.attrs: raise AttributeError(k)
4949
return self.attrs[k.lstrip('_').replace('_', '-')]
50-
51-
def __call__(self, *c):
52-
self[1] = self[1]+c
53-
return self
50+
51+
# %% ../nbs/11_xml.ipynb
52+
def _preproc(c, kw):
53+
if len(c)==1 and isinstance(c[0], (types.GeneratorType, map, filter)): c = tuple(c[0])
54+
return c,{_attrmap(k):v for k,v in kw.items() if v is not None}
5455

5556
# %% ../nbs/11_xml.ipynb
5657
def ft(tag:str, *c, void_=False, **kw):
5758
"Create an `FT` structure for `to_xml()`"
58-
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
59-
kw = {_attrmap(k):v for k,v in kw.items() if v is not None}
60-
return FT(tag.lower(),c,kw, void_=void_)
59+
return FT(tag.lower(),*_preproc(c,kw), void_=void_)
6160

6261
# %% ../nbs/11_xml.ipynb
62+
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())
6363
_g = globals()
6464
_all_ = ['Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code',
6565
'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B',
@@ -71,7 +71,7 @@ def ft(tag:str, *c, void_=False, **kw):
7171
'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video',
7272
'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']
7373

74-
for o in _all_: _g[o] = partial(ft, o.lower())
74+
for o in _all_: _g[o] = partial(ft, o.lower(), void_=o.lower() in voids)
7575

7676
# %% ../nbs/11_xml.ipynb
7777
def Html(*c, doctype=True, **kwargs)->FT:
@@ -123,7 +123,7 @@ def to_xml(elm, lvl=0):
123123
FT.__html__ = to_xml
124124

125125
# %% ../nbs/11_xml.ipynb
126-
def highlight(s, lang='xml'):
126+
def highlight(s, lang='html'):
127127
"Markdown to syntax-highlight `s` in language `lang`"
128128
return f'```{lang}\n{to_xml(s)}\n```'
129129

@@ -140,3 +140,11 @@ def __getattr__(tag):
140140
if tag.startswith('_') or tag[0].islower(): raise AttributeError
141141
def _f(*c, target_id=None, **kwargs): return ft(tag, *c, target_id=target_id, **kwargs)
142142
return _f
143+
144+
# %% ../nbs/11_xml.ipynb
145+
@patch
146+
def __call__(self:FT, *c, **kw):
147+
c,kw = _preproc(c,kw)
148+
if c: self[1] = self[1]+c
149+
if kw: self[2] = {**self[2], **kw}
150+
return self

nbs/11_xml.ipynb

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
{
6767
"cell_type": "code",
6868
"execution_count": null,
69-
"id": "149067dd",
69+
"id": "b06c10f6",
7070
"metadata": {},
7171
"outputs": [],
7272
"source": [
@@ -91,11 +91,20 @@
9191
"\n",
9292
" def __getattr__(self, k):\n",
9393
" if k.startswith('__') or k not in self.attrs: raise AttributeError(k)\n",
94-
" return self.attrs[k.lstrip('_').replace('_', '-')]\n",
95-
" \n",
96-
" def __call__(self, *c):\n",
97-
" self[1] = self[1]+c\n",
98-
" return self"
94+
" return self.attrs[k.lstrip('_').replace('_', '-')]"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "149067dd",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"#|export\n",
105+
"def _preproc(c, kw):\n",
106+
" if len(c)==1 and isinstance(c[0], (types.GeneratorType, map, filter)): c = tuple(c[0])\n",
107+
" return c,{_attrmap(k):v for k,v in kw.items() if v is not None}"
99108
]
100109
},
101110
{
@@ -108,9 +117,7 @@
108117
"#| export\n",
109118
"def ft(tag:str, *c, void_=False, **kw):\n",
110119
" \"Create an `FT` structure for `to_xml()`\"\n",
111-
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
112-
" kw = {_attrmap(k):v for k,v in kw.items() if v is not None}\n",
113-
" return FT(tag.lower(),c,kw, void_=void_)"
120+
" return FT(tag.lower(),*_preproc(c,kw), void_=void_)"
114121
]
115122
},
116123
{
@@ -121,6 +128,7 @@
121128
"outputs": [],
122129
"source": [
123130
"#| export\n",
131+
"voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())\n",
124132
"_g = globals()\n",
125133
"_all_ = ['Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code',\n",
126134
" 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B',\n",
@@ -132,7 +140,7 @@
132140
" 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video',\n",
133141
" 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']\n",
134142
"\n",
135-
"for o in _all_: _g[o] = partial(ft, o.lower())"
143+
"for o in _all_: _g[o] = partial(ft, o.lower(), void_=o.lower() in voids)"
136144
]
137145
},
138146
{
@@ -346,8 +354,8 @@
346354
" <body>\n",
347355
" <div class=\"myclass\">\n",
348356
"Some text\n",
349-
" <input name=\"me\"></input>\n",
350-
" <img src=\"filename\" data=\"1\"></img>\n",
357+
" <input name=\"me\">\n",
358+
" <img src=\"filename\" data=\"1\">\n",
351359
" </div>\n",
352360
" </body>\n",
353361
"</html>\n",
@@ -370,7 +378,7 @@
370378
},
371379
{
372380
"cell_type": "code",
373-
"execution_count": 107,
381+
"execution_count": null,
374382
"id": "798ae1d2",
375383
"metadata": {},
376384
"outputs": [
@@ -403,13 +411,13 @@
403411
},
404412
{
405413
"cell_type": "code",
406-
"execution_count": 108,
414+
"execution_count": null,
407415
"id": "5f0e91e0",
408416
"metadata": {},
409417
"outputs": [],
410418
"source": [
411419
"#| export\n",
412-
"def highlight(s, lang='xml'):\n",
420+
"def highlight(s, lang='html'):\n",
413421
" \"Markdown to syntax-highlight `s` in language `lang`\"\n",
414422
" return f'```{lang}\\n{to_xml(s)}\\n```'"
415423
]
@@ -444,9 +452,25 @@
444452
" return _f"
445453
]
446454
},
455+
{
456+
"cell_type": "code",
457+
"execution_count": null,
458+
"id": "204c3900",
459+
"metadata": {},
460+
"outputs": [],
461+
"source": [
462+
"#|export\n",
463+
"@patch\n",
464+
"def __call__(self:FT, *c, **kw):\n",
465+
" c,kw = _preproc(c,kw)\n",
466+
" if c: self[1] = self[1]+c\n",
467+
" if kw: self[2] = {**self[2], **kw}\n",
468+
" return self"
469+
]
470+
},
447471
{
448472
"cell_type": "markdown",
449-
"id": "9786e4d9",
473+
"id": "32b75e87",
450474
"metadata": {},
451475
"source": [
452476
"You can also reorder the children to come *after* the attrs, if you use this alternative syntax for `FT` where the children are in a second pair of `()` (behind the scenes this is because `FT` implements `__call__` to add children)."
@@ -461,13 +485,13 @@
461485
{
462486
"data": {
463487
"text/markdown": [
464-
"```xml\n",
488+
"```html\n",
465489
"<body class=\"myclass\">\n",
466490
" <div style=\"padding:3px\">\n",
467491
"Some text \n",
468-
" <i>in italics</i>\n",
469-
" <input name=\"me\"></input>\n",
470-
" <img src=\"filename\" data=\"1\"></img>\n",
492+
" <i spurious>in italics</i>\n",
493+
" <input name=\"me\">\n",
494+
" <img src=\"filename\" data=\"1\">\n",
471495
" </div>\n",
472496
"</body>\n",
473497
"\n",
@@ -477,7 +501,7 @@
477501
"['body',\n",
478502
" (['div',\n",
479503
" ('Some text ',\n",
480-
" ['i', ('in italics',), {}],\n",
504+
" ['i', ('in italics',), {'spurious': True}],\n",
481505
" ['input', (), {'name': 'me'}],\n",
482506
" ['img', (), {'src': 'filename', 'data': 1}]),\n",
483507
" {'style': 'padding:3px'}],),\n",
@@ -493,7 +517,7 @@
493517
"Body(klass='myclass')(\n",
494518
" Div(style='padding:3px')(\n",
495519
" 'Some text ',\n",
496-
" I('in italics'),\n",
520+
" I(spurious=True)('in italics'),\n",
497521
" Input(name='me'),\n",
498522
" Img(src=\"filename\", data=1)\n",
499523
" )\n",

0 commit comments

Comments
 (0)