Skip to content

Commit e727da9

Browse files
committed
fixes #598
1 parent a1e3336 commit e727da9

File tree

3 files changed

+75
-66
lines changed

3 files changed

+75
-66
lines changed

fastcore/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@
578578
'fastcore.xml.ft': ('xml.html#ft', 'fastcore/xml.py'),
579579
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
580580
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
581-
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py')},
581+
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
582+
'fastcore.xml.valmap': ('xml.html#valmap', 'fastcore/xml.py')},
582583
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
583584
'fastcore.xtras.ContextManagers.__enter__': ('xtras.html#contextmanagers.__enter__', 'fastcore/xtras.py'),
584585
'fastcore.xtras.ContextManagers.__exit__': ('xtras.html#contextmanagers.__exit__', 'fastcore/xtras.py'),

fastcore/xml.py

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

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

1313
# %% ../nbs/11_xml.ipynb
1414
from .utils import *
@@ -51,14 +51,20 @@ def attrmap(o):
5151
return o.lstrip('_').replace('_', '-')
5252

5353
# %% ../nbs/11_xml.ipynb
54-
def _preproc(c, kw, attrmap=attrmap):
54+
def valmap(o):
55+
if is_listy(o): return ' '.join(o)
56+
if isinstance(o, dict): return '; '.join(f"{k}:{v}" for k,v in o.items())
57+
return o
58+
59+
# %% ../nbs/11_xml.ipynb
60+
def _preproc(c, kw, attrmap=attrmap, valmap=valmap):
5561
if len(c)==1 and isinstance(c[0], (types.GeneratorType, map, filter)): c = tuple(c[0])
56-
return c,{attrmap(k.lower()):v for k,v in kw.items() if v is not None}
62+
return c,{attrmap(k.lower()):valmap(v) for k,v in kw.items() if v is not None}
5763

5864
# %% ../nbs/11_xml.ipynb
59-
def ft(tag:str, *c, void_=False, attrmap=attrmap, **kw):
65+
def ft(tag:str, *c, void_=False, attrmap=attrmap, valmap=valmap, **kw):
6066
"Create an `FT` structure for `to_xml()`"
61-
return FT(tag.lower(),*_preproc(c,kw,attrmap=attrmap), void_=void_)
67+
return FT(tag.lower(),*_preproc(c,kw,attrmap=attrmap, valmap=valmap), void_=void_)
6268

6369
# %% ../nbs/11_xml.ipynb
6470
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())

nbs/11_xml.ipynb

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"id": "d0c84aa5",
77
"metadata": {},
88
"outputs": [],
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 2,
25+
"execution_count": null,
2626
"id": "55944805",
2727
"metadata": {},
2828
"outputs": [],
@@ -40,7 +40,7 @@
4040
},
4141
{
4242
"cell_type": "code",
43-
"execution_count": 3,
43+
"execution_count": null,
4444
"id": "42d18e5c",
4545
"metadata": {},
4646
"outputs": [],
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "code",
54-
"execution_count": 4,
54+
"execution_count": null,
5555
"id": "b06c10f6",
5656
"metadata": {},
5757
"outputs": [],
@@ -82,7 +82,7 @@
8282
},
8383
{
8484
"cell_type": "code",
85-
"execution_count": 5,
85+
"execution_count": null,
8686
"id": "d6069e01",
8787
"metadata": {},
8888
"outputs": [],
@@ -97,33 +97,47 @@
9797
},
9898
{
9999
"cell_type": "code",
100-
"execution_count": 6,
100+
"execution_count": null,
101+
"id": "9b2adafa",
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"#| export\n",
106+
"def valmap(o):\n",
107+
" if is_listy(o): return ' '.join(o)\n",
108+
" if isinstance(o, dict): return '; '.join(f\"{k}:{v}\" for k,v in o.items())\n",
109+
" return o"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
101115
"id": "149067dd",
102116
"metadata": {},
103117
"outputs": [],
104118
"source": [
105119
"#|export\n",
106-
"def _preproc(c, kw, attrmap=attrmap):\n",
120+
"def _preproc(c, kw, attrmap=attrmap, valmap=valmap):\n",
107121
" if len(c)==1 and isinstance(c[0], (types.GeneratorType, map, filter)): c = tuple(c[0])\n",
108-
" return c,{attrmap(k.lower()):v for k,v in kw.items() if v is not None}"
122+
" return c,{attrmap(k.lower()):valmap(v) for k,v in kw.items() if v is not None}"
109123
]
110124
},
111125
{
112126
"cell_type": "code",
113-
"execution_count": 7,
127+
"execution_count": null,
114128
"id": "06718948",
115129
"metadata": {},
116130
"outputs": [],
117131
"source": [
118132
"#| export\n",
119-
"def ft(tag:str, *c, void_=False, attrmap=attrmap, **kw):\n",
133+
"def ft(tag:str, *c, void_=False, attrmap=attrmap, valmap=valmap, **kw):\n",
120134
" \"Create an `FT` structure for `to_xml()`\"\n",
121-
" return FT(tag.lower(),*_preproc(c,kw,attrmap=attrmap), void_=void_)"
135+
" return FT(tag.lower(),*_preproc(c,kw,attrmap=attrmap, valmap=valmap), void_=void_)"
122136
]
123137
},
124138
{
125139
"cell_type": "code",
126-
"execution_count": 8,
140+
"execution_count": null,
127141
"id": "45489975",
128142
"metadata": {},
129143
"outputs": [],
@@ -156,7 +170,7 @@
156170
},
157171
{
158172
"cell_type": "code",
159-
"execution_count": 9,
173+
"execution_count": null,
160174
"id": "39834fcb",
161175
"metadata": {},
162176
"outputs": [],
@@ -171,7 +185,7 @@
171185
},
172186
{
173187
"cell_type": "code",
174-
"execution_count": 10,
188+
"execution_count": null,
175189
"id": "9a8b4ddb",
176190
"metadata": {},
177191
"outputs": [
@@ -187,7 +201,7 @@
187201
" ('Some text',\n",
188202
" ['input', (), {'name': 'me'}],\n",
189203
" ['img', (), {'data': 1, 'src': 'filename'}]),\n",
190-
" {'class': 'myclass'}],),\n",
204+
" {'class': 'myclass another', 'style': 'padding:1; margin:2'}],),\n",
191205
" {}]),\n",
192206
" {}])\n"
193207
]
@@ -196,7 +210,9 @@
196210
"source": [
197211
"samp = Html(\n",
198212
" Head(Title('Some page')),\n",
199-
" Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data=1), klass='myclass'))\n",
213+
" Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data=1),\n",
214+
" cls=['myclass', 'another'],\n",
215+
" style={'padding':1, 'margin':2}))\n",
200216
")\n",
201217
"pprint(samp)"
202218
]
@@ -211,7 +227,7 @@
211227
},
212228
{
213229
"cell_type": "code",
214-
"execution_count": 11,
230+
"execution_count": null,
215231
"id": "5c6c57e9",
216232
"metadata": {},
217233
"outputs": [
@@ -242,7 +258,7 @@
242258
},
243259
{
244260
"cell_type": "code",
245-
"execution_count": 12,
261+
"execution_count": null,
246262
"id": "5c7f175d",
247263
"metadata": {},
248264
"outputs": [
@@ -259,7 +275,7 @@
259275
"['p', ('Some text',), {'id': 'newid'}]"
260276
]
261277
},
262-
"execution_count": 12,
278+
"execution_count": null,
263279
"metadata": {},
264280
"output_type": "execute_result"
265281
}
@@ -272,7 +288,7 @@
272288
},
273289
{
274290
"cell_type": "code",
275-
"execution_count": 13,
291+
"execution_count": null,
276292
"id": "254c8ff3",
277293
"metadata": {},
278294
"outputs": [],
@@ -283,7 +299,7 @@
283299
},
284300
{
285301
"cell_type": "code",
286-
"execution_count": 14,
302+
"execution_count": null,
287303
"id": "0255b96f",
288304
"metadata": {},
289305
"outputs": [],
@@ -303,7 +319,7 @@
303319
},
304320
{
305321
"cell_type": "code",
306-
"execution_count": 15,
322+
"execution_count": null,
307323
"id": "b89d088a",
308324
"metadata": {},
309325
"outputs": [],
@@ -338,7 +354,7 @@
338354
},
339355
{
340356
"cell_type": "code",
341-
"execution_count": 16,
357+
"execution_count": null,
342358
"id": "d3d23c48",
343359
"metadata": {},
344360
"outputs": [
@@ -353,7 +369,7 @@
353369
" <title>Some page</title>\n",
354370
" </head>\n",
355371
" <body>\n",
356-
" <div class=\"myclass\">\n",
372+
" <div class=\"myclass another\" style=\"padding:1; margin:2\">\n",
357373
"Some text\n",
358374
" <input name=\"me\">\n",
359375
" <img src=\"filename\" data=\"1\">\n",
@@ -379,7 +395,7 @@
379395
},
380396
{
381397
"cell_type": "code",
382-
"execution_count": 17,
398+
"execution_count": null,
383399
"id": "798ae1d2",
384400
"metadata": {},
385401
"outputs": [
@@ -397,22 +413,20 @@
397413
}
398414
],
399415
"source": [
400-
"class MockDjangoSafeString(str):\n",
401-
" def __html__(self):\n",
402-
" return self\n",
416+
"class _SafeString(str):\n",
417+
" def __html__(self): return self\n",
403418
"\n",
404-
"def mock_django_conditional_escape(s):\n",
405-
" return s.__html__() if hasattr(s, '__html__') else MockDjangoSafeString(escape(s))\n",
419+
"def _escape(s): return s.__html__() if hasattr(s, '__html__') else _SafeString(escape(s))\n",
406420
"\n",
407-
"html_string_coming_from_django = MockDjangoSafeString('<b>Hello from Django</b>')\n",
408-
"print(to_xml(Div(html_string_coming_from_django)))\n",
421+
"r = _SafeString('<b>Hello from Django</b>')\n",
422+
"print(to_xml(Div(r)))\n",
409423
"\n",
410-
"print(mock_django_conditional_escape(Div(P('Hello from fastcore <3'))))"
424+
"print(_escape(Div(P('Hello from fastcore <3'))))"
411425
]
412426
},
413427
{
414428
"cell_type": "code",
415-
"execution_count": 18,
429+
"execution_count": null,
416430
"id": "5f0e91e0",
417431
"metadata": {},
418432
"outputs": [],
@@ -425,7 +439,7 @@
425439
},
426440
{
427441
"cell_type": "code",
428-
"execution_count": 19,
442+
"execution_count": null,
429443
"id": "39fab735",
430444
"metadata": {},
431445
"outputs": [],
@@ -441,7 +455,7 @@
441455
},
442456
{
443457
"cell_type": "code",
444-
"execution_count": 20,
458+
"execution_count": null,
445459
"id": "530666f8",
446460
"metadata": {},
447461
"outputs": [],
@@ -455,7 +469,7 @@
455469
},
456470
{
457471
"cell_type": "code",
458-
"execution_count": 21,
472+
"execution_count": null,
459473
"id": "204c3900",
460474
"metadata": {},
461475
"outputs": [],
@@ -479,7 +493,7 @@
479493
},
480494
{
481495
"cell_type": "code",
482-
"execution_count": 22,
496+
"execution_count": null,
483497
"id": "efd647f8",
484498
"metadata": {},
485499
"outputs": [
@@ -509,7 +523,7 @@
509523
" {'class': 'myclass'}]"
510524
]
511525
},
512-
"execution_count": 22,
526+
"execution_count": null,
513527
"metadata": {},
514528
"output_type": "execute_result"
515529
}
@@ -535,7 +549,7 @@
535549
},
536550
{
537551
"cell_type": "code",
538-
"execution_count": 23,
552+
"execution_count": null,
539553
"id": "ad32b076",
540554
"metadata": {},
541555
"outputs": [],
@@ -555,21 +569,9 @@
555569
],
556570
"metadata": {
557571
"kernelspec": {
558-
"display_name": "Python 3 (ipykernel)",
572+
"display_name": "python3",
559573
"language": "python",
560574
"name": "python3"
561-
},
562-
"language_info": {
563-
"codemirror_mode": {
564-
"name": "ipython",
565-
"version": 3
566-
},
567-
"file_extension": ".py",
568-
"mimetype": "text/x-python",
569-
"name": "python",
570-
"nbconvert_exporter": "python",
571-
"pygments_lexer": "ipython3",
572-
"version": "3.11.8"
573575
}
574576
},
575577
"nbformat": 4,

0 commit comments

Comments
 (0)