Skip to content

Commit f05f425

Browse files
committed
fixes #599
1 parent 44e831e commit f05f425

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

fastcore/_modidx.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,13 @@
570570
'fastcore.xml.FT.children': ('xml.html#ft.children', 'fastcore/xml.py'),
571571
'fastcore.xml.FT.tag': ('xml.html#ft.tag', 'fastcore/xml.py'),
572572
'fastcore.xml.Html': ('xml.html#html', 'fastcore/xml.py'),
573+
'fastcore.xml.Safe': ('xml.html#safe', 'fastcore/xml.py'),
574+
'fastcore.xml.Safe.__html__': ('xml.html#safe.__html__', 'fastcore/xml.py'),
573575
'fastcore.xml.__getattr__': ('xml.html#__getattr__', 'fastcore/xml.py'),
574576
'fastcore.xml._escape': ('xml.html#_escape', 'fastcore/xml.py'),
575577
'fastcore.xml._preproc': ('xml.html#_preproc', 'fastcore/xml.py'),
576578
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
579+
'fastcore.xml._to_xml': ('xml.html#_to_xml', 'fastcore/xml.py'),
577580
'fastcore.xml.attrmap': ('xml.html#attrmap', 'fastcore/xml.py'),
578581
'fastcore.xml.ft': ('xml.html#ft', 'fastcore/xml.py'),
579582
'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
@@ -1,12 +1,12 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22

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

@@ -88,6 +88,10 @@ def Html(*c, doctype=True, **kwargs)->FT:
8888
if not doctype: return res
8989
return (ft('!DOCTYPE', html=True, void_=True), res)
9090

91+
# %% ../nbs/11_xml.ipynb
92+
class Safe(str):
93+
def __html__(self): return self
94+
9195
# %% ../nbs/11_xml.ipynb
9296
def _escape(s): return '' if s is None else s.__html__() if hasattr(s, '__html__') else escape(s) if isinstance(s, str) else s
9397

@@ -104,8 +108,7 @@ def _to_attr(k,v):
104108
return f'{k}={qt}{v}{qt}'
105109

106110
# %% ../nbs/11_xml.ipynb
107-
def to_xml(elm, lvl=0, indent:bool=True):
108-
"Convert `ft` element tree into an XML string"
111+
def _to_xml(elm, lvl, indent:bool):
109112
nl = '\n'
110113
if not indent: lvl,nl = 0,''
111114
if elm is None: return ''
@@ -128,7 +131,11 @@ def to_xml(elm, lvl=0, indent:bool=True):
128131
res = f'{sp}<{stag}>{nl}'
129132
res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)
130133
if not isvoid: res += f'{sp}{cltag}{nl}'
131-
return res
134+
return Safe(res)
135+
136+
def to_xml(elm, lvl=0, indent:bool=True):
137+
"Convert `ft` element tree into an XML string"
138+
return Safe(_to_xml(elm, lvl, indent))
132139

133140
FT.__html__ = to_xml
134141

nbs/11_xml.ipynb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@
286286
"elem"
287287
]
288288
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": null,
292+
"id": "116c886e",
293+
"metadata": {},
294+
"outputs": [],
295+
"source": [
296+
"#| export\n",
297+
"class Safe(str):\n",
298+
" def __html__(self): return self"
299+
]
300+
},
289301
{
290302
"cell_type": "code",
291303
"execution_count": null,
@@ -325,8 +337,7 @@
325337
"outputs": [],
326338
"source": [
327339
"#| export\n",
328-
"def to_xml(elm, lvl=0, indent:bool=True):\n",
329-
" \"Convert `ft` element tree into an XML string\"\n",
340+
"def _to_xml(elm, lvl, indent:bool):\n",
330341
" nl = '\\n'\n",
331342
" if not indent: lvl,nl = 0,''\n",
332343
" if elm is None: return ''\n",
@@ -349,7 +360,11 @@
349360
" res = f'{sp}<{stag}>{nl}'\n",
350361
" res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)\n",
351362
" if not isvoid: res += f'{sp}{cltag}{nl}'\n",
352-
" return res\n",
363+
" return Safe(res)\n",
364+
"\n",
365+
"def to_xml(elm, lvl=0, indent:bool=True):\n",
366+
" \"Convert `ft` element tree into an XML string\"\n",
367+
" return Safe(_to_xml(elm, lvl, indent))\n",
353368
"\n",
354369
"FT.__html__ = to_xml"
355370
]
@@ -413,7 +428,7 @@
413428
"id": "4713bd8d",
414429
"metadata": {},
415430
"source": [
416-
"Interoperability both directions with Django and Jinja using the [__html__() protocol](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.escape)."
431+
"Interoperability both directions with Django and Jinja using the [__html__() protocol](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.escape):"
417432
]
418433
},
419434
{
@@ -436,14 +451,11 @@
436451
}
437452
],
438453
"source": [
439-
"class _SafeString(str):\n",
440-
" def __html__(self): return self\n",
441-
"\n",
442-
"def _escape(s): return s.__html__() if hasattr(s, '__html__') else _SafeString(escape(s))\n",
454+
"def _esc(s): return s.__html__() if hasattr(s, '__html__') else Safe(escape(s))\n",
443455
"\n",
444-
"r = _SafeString('<b>Hello from Django</b>')\n",
456+
"r = Safe('<b>Hello from Django</b>')\n",
445457
"print(to_xml(Div(r)))\n",
446-
"print(_escape(Div(P('Hello from fastcore <3'))))"
458+
"print(_esc(Div(P('Hello from fastcore <3'))))"
447459
]
448460
},
449461
{

0 commit comments

Comments
 (0)