You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: nbs/11_xml.ipynb
+22-10Lines changed: 22 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -286,6 +286,18 @@
286
286
"elem"
287
287
]
288
288
},
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
+
},
289
301
{
290
302
"cell_type": "code",
291
303
"execution_count": null,
@@ -325,8 +337,7 @@
325
337
"outputs": [],
326
338
"source": [
327
339
"#| 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",
330
341
" nl = '\\n'\n",
331
342
" if not indent: lvl,nl = 0,''\n",
332
343
" if elm is None: return ''\n",
@@ -349,7 +360,11 @@
349
360
" res = f'{sp}<{stag}>{nl}'\n",
350
361
" res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)\n",
351
362
" 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",
353
368
"\n",
354
369
"FT.__html__ = to_xml"
355
370
]
@@ -413,7 +428,7 @@
413
428
"id": "4713bd8d",
414
429
"metadata": {},
415
430
"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):"
417
432
]
418
433
},
419
434
{
@@ -436,14 +451,11 @@
436
451
}
437
452
],
438
453
"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",
443
455
"\n",
444
-
"r = _SafeString('<b>Hello from Django</b>')\n",
456
+
"r = Safe('<b>Hello from Django</b>')\n",
445
457
"print(to_xml(Div(r)))\n",
446
-
"print(_escape(Div(P('Hello from fastcore <3'))))"
0 commit comments