Skip to content

Commit 954233c

Browse files
committed
fixes #561
1 parent 678806d commit 954233c

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

fastcore/_modidx.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@
554554
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
555555
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
556556
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
557+
'fastcore.xml.XT.__init__': ('xml.html#xt.__init__', 'fastcore/xml.py'),
558+
'fastcore.xml.XT.attrs': ('xml.html#xt.attrs', 'fastcore/xml.py'),
559+
'fastcore.xml.XT.children': ('xml.html#xt.children', 'fastcore/xml.py'),
560+
'fastcore.xml.XT.tag': ('xml.html#xt.tag', 'fastcore/xml.py'),
557561
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
558562
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
559563
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ def _attrmap(o):
2323
return o.lstrip('_').replace('_', '-')
2424

2525
# %% ../nbs/11_xml.ipynb 5
26-
class XT(list): pass
26+
class XT(list):
27+
def __init__(self, tag, cs, attrs): super().__init__([tag, cs, attrs])
28+
@property
29+
def tag(self): return self[0]
30+
@property
31+
def children(self): return self[1]
32+
@property
33+
def attrs(self): return self[2]
2734

2835
# %% ../nbs/11_xml.ipynb 6
2936
def xt(tag:str, *c, **kw):
3037
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
3138
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
3239
kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}
33-
return XT([tag.lower(),c,kw])
40+
return XT(tag.lower(),c,kw)
3441

3542
# %% ../nbs/11_xml.ipynb 7
3643
_g = globals()
@@ -46,16 +53,16 @@ def xt(tag:str, *c, **kw):
4653

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

49-
# %% ../nbs/11_xml.ipynb 10
56+
# %% ../nbs/11_xml.ipynb 12
5057
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())
5158

52-
# %% ../nbs/11_xml.ipynb 11
59+
# %% ../nbs/11_xml.ipynb 13
5360
def _to_attr(k,v):
5461
if v==True: return str(k)
5562
if v==False: return ''
5663
return f'{k}="{escape(str(v), quote=False)}"'
5764

58-
# %% ../nbs/11_xml.ipynb 12
65+
# %% ../nbs/11_xml.ipynb 14
5966
def to_xml(elm, lvl=0):
6067
"Convert `xt` element tree into an XML string"
6168
if isinstance(elm, tuple): return '\n'.join(to_xml(o) for o in elm)
@@ -78,15 +85,15 @@ def to_xml(elm, lvl=0):
7885
if tag not in voids: res += f'{sp}{cltag}\n'
7986
return res
8087

81-
# %% ../nbs/11_xml.ipynb 14
82-
def highlight(s, lang='html'):
88+
# %% ../nbs/11_xml.ipynb 16
89+
def highlight(s, lang='xml'):
8390
"Markdown to syntax-highlight `s` in language `lang`"
8491
return f'```{lang}\n{to_xml(s)}\n```'
8592

86-
# %% ../nbs/11_xml.ipynb 15
93+
# %% ../nbs/11_xml.ipynb 17
8794
def showtags(s):
8895
return f"""<code><pre>
8996
{escape(to_xml(s))}
9097
</code></pre>"""
9198

92-
XT._repr_html_ = showtags
99+
XT._repr_markdown_ = highlight

nbs/01_basics.ipynb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"from __future__ import annotations\n",
3434
"from fastcore.test import *\n",
3535
"from nbdev.showdoc import *\n",
36-
"from fastcore.nb_imports import *\n",
37-
"from inspect import get_annotations"
36+
"from fastcore.nb_imports import *"
3837
]
3938
},
4039
{
@@ -652,7 +651,7 @@
652651
"test_eq(t.a, 1)\n",
653652
"test_eq(t.b, 3)\n",
654653
"test_eq(t, pickle.loads(pickle.dumps(t)))\n",
655-
"test_eq(get_annotations(_t), {'b':int, 'a':typing.Any})\n",
654+
"test_eq(_t.__annotations__, {'b':int, 'a':typing.Any})\n",
656655
"repr(t)"
657656
]
658657
},

nbs/11_xml.ipynb

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@
6868
"outputs": [],
6969
"source": [
7070
"#|export\n",
71-
"class XT(list): pass"
71+
"class XT(list):\n",
72+
" def __init__(self, tag, cs, attrs): super().__init__([tag, cs, attrs])\n",
73+
" @property\n",
74+
" def tag(self): return self[0]\n",
75+
" @property\n",
76+
" def children(self): return self[1]\n",
77+
" @property\n",
78+
" def attrs(self): return self[2]"
7279
]
7380
},
7481
{
@@ -83,7 +90,7 @@
8390
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
8491
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
8592
" kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}\n",
86-
" return XT([tag.lower(),c,kw])"
93+
" return XT(tag.lower(),c,kw)"
8794
]
8895
},
8996
{
@@ -149,6 +156,37 @@
149156
"pprint(samp)"
150157
]
151158
},
159+
{
160+
"cell_type": "markdown",
161+
"id": "314eb4bc",
162+
"metadata": {},
163+
"source": [
164+
"The three elements of the list can also be accessed with property names, so you don't have to remember their order."
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": null,
170+
"id": "5c6c57e9",
171+
"metadata": {},
172+
"outputs": [
173+
{
174+
"name": "stdout",
175+
"output_type": "stream",
176+
"text": [
177+
"p\n",
178+
"('Some text',)\n",
179+
"{'id': 'myid'}\n"
180+
]
181+
}
182+
],
183+
"source": [
184+
"elem = P('Some text', id=\"myid\")\n",
185+
"print(elem.tag)\n",
186+
"print(elem.children)\n",
187+
"print(elem.attrs)"
188+
]
189+
},
152190
{
153191
"cell_type": "code",
154192
"execution_count": null,
@@ -247,7 +285,7 @@
247285
"outputs": [],
248286
"source": [
249287
"#| export\n",
250-
"def highlight(s, lang='html'):\n",
288+
"def highlight(s, lang='xml'):\n",
251289
" \"Markdown to syntax-highlight `s` in language `lang`\"\n",
252290
" return f'```{lang}\\n{to_xml(s)}\\n```'"
253291
]
@@ -265,7 +303,7 @@
265303
"{escape(to_xml(s))}\n",
266304
"</code></pre>\"\"\"\n",
267305
"\n",
268-
"XT._repr_html_ = showtags"
306+
"XT._repr_markdown_ = highlight"
269307
]
270308
},
271309
{

0 commit comments

Comments
 (0)