Skip to content

Commit 9ff049d

Browse files
committed
fixes #590
1 parent 69b8508 commit 9ff049d

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@
561561
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
562562
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
563563
'fastcore.xml': { 'fastcore.xml.FT': ('xml.html#ft', 'fastcore/xml.py'),
564+
'fastcore.xml.FT.__call__': ('xml.html#ft.__call__', 'fastcore/xml.py'),
564565
'fastcore.xml.FT.__getattr__': ('xml.html#ft.__getattr__', 'fastcore/xml.py'),
565566
'fastcore.xml.FT.__init__': ('xml.html#ft.__init__', 'fastcore/xml.py'),
566567
'fastcore.xml.FT.__setattr__': ('xml.html#ft.__setattr__', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def _attrmap(o):
2929
class FT(list):
3030
"A 'Fast Tag' structure, which is a `list` of `[tag,children,attrs]`"
3131
def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):
32+
assert isinstance(cs, tuple)
3233
super().__init__([tag, cs, {**(attrs or {}), **kwargs}])
3334
self.void_ = void_
3435

@@ -46,6 +47,10 @@ def __setattr__(self, k, v):
4647
def __getattr__(self, k):
4748
if k.startswith('__') or k not in self.attrs: raise AttributeError(k)
4849
return self.attrs[k.lstrip('_').replace('_', '-')]
50+
51+
def __call__(self, *c):
52+
self[1] = self[1]+c
53+
return self
4954

5055
# %% ../nbs/11_xml.ipynb
5156
def ft(tag:str, *c, void_=False, **kw):

nbs/11_xml.ipynb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"class FT(list):\n",
7575
" \"A 'Fast Tag' structure, which is a `list` of `[tag,children,attrs]`\"\n",
7676
" def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):\n",
77+
" assert isinstance(cs, tuple)\n",
7778
" super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n",
7879
" self.void_ = void_\n",
7980
"\n",
@@ -90,7 +91,11 @@
9091
"\n",
9192
" def __getattr__(self, k):\n",
9293
" if k.startswith('__') or k not in self.attrs: raise AttributeError(k)\n",
93-
" return self.attrs[k.lstrip('_').replace('_', '-')]"
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"
9499
]
95100
},
96101
{
@@ -396,6 +401,62 @@
396401
" return _f"
397402
]
398403
},
404+
{
405+
"cell_type": "markdown",
406+
"id": "9786e4d9",
407+
"metadata": {},
408+
"source": [
409+
"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)."
410+
]
411+
},
412+
{
413+
"cell_type": "code",
414+
"execution_count": null,
415+
"id": "efd647f8",
416+
"metadata": {},
417+
"outputs": [
418+
{
419+
"data": {
420+
"text/markdown": [
421+
"```xml\n",
422+
"<body class=\"myclass\">\n",
423+
" <div style=\"padding:3px\">\n",
424+
"Some text \n",
425+
" <i>in italics</i>\n",
426+
" <input name=\"me\"></input>\n",
427+
" <img src=\"filename\" data=\"1\"></img>\n",
428+
" </div>\n",
429+
"</body>\n",
430+
"\n",
431+
"```"
432+
],
433+
"text/plain": [
434+
"['body',\n",
435+
" (['div',\n",
436+
" ('Some text ',\n",
437+
" ['i', ('in italics',), {}],\n",
438+
" ['input', (), {'name': 'me'}],\n",
439+
" ['img', (), {'src': 'filename', 'data': 1}]),\n",
440+
" {'style': 'padding:3px'}],),\n",
441+
" {'class': 'myclass'}]"
442+
]
443+
},
444+
"execution_count": null,
445+
"metadata": {},
446+
"output_type": "execute_result"
447+
}
448+
],
449+
"source": [
450+
"Body(klass='myclass')(\n",
451+
" Div(style='padding:3px')(\n",
452+
" 'Some text ',\n",
453+
" I('in italics'),\n",
454+
" Input(name='me'),\n",
455+
" Img(src=\"filename\", data=1)\n",
456+
" )\n",
457+
")"
458+
]
459+
},
399460
{
400461
"cell_type": "markdown",
401462
"id": "df973d4e",

0 commit comments

Comments
 (0)