|
74 | 74 | "class FT(list):\n",
|
75 | 75 | " \"A 'Fast Tag' structure, which is a `list` of `[tag,children,attrs]`\"\n",
|
76 | 76 | " def __init__(self, tag, cs, attrs=None, void_=False, **kwargs):\n",
|
| 77 | + " assert isinstance(cs, tuple)\n", |
77 | 78 | " super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n",
|
78 | 79 | " self.void_ = void_\n",
|
79 | 80 | "\n",
|
|
90 | 91 | "\n",
|
91 | 92 | " def __getattr__(self, k):\n",
|
92 | 93 | " 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" |
94 | 99 | ]
|
95 | 100 | },
|
96 | 101 | {
|
|
396 | 401 | " return _f"
|
397 | 402 | ]
|
398 | 403 | },
|
| 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 | + }, |
399 | 460 | {
|
400 | 461 | "cell_type": "markdown",
|
401 | 462 | "id": "df973d4e",
|
|
0 commit comments