|
198 | 198 | " (['head', (['title', ('Some page',), {}],), {}],\n", |
199 | 199 | " ['body',\n", |
200 | 200 | " (['div',\n", |
201 | | - " ('Some text',\n", |
| 201 | + " ('Some text\\nanother line',\n", |
202 | 202 | " ['input', (), {'name': 'me'}],\n", |
203 | 203 | " ['img', (), {'data': 1, 'src': 'filename'}]),\n", |
204 | 204 | " {'class': 'myclass another', 'style': 'padding:1; margin:2'}],),\n", |
|
210 | 210 | "source": [ |
211 | 211 | "samp = Html(\n", |
212 | 212 | " Head(Title('Some page')),\n", |
213 | | - " Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data=1),\n", |
| 213 | + " Body(Div('Some text\\nanother line', Input(name='me'), Img(src=\"filename\", data=1),\n", |
214 | 214 | " cls=['myclass', 'another'],\n", |
215 | 215 | " style={'padding':1, 'margin':2}))\n", |
216 | 216 | ")\n", |
|
325 | 325 | "outputs": [], |
326 | 326 | "source": [ |
327 | 327 | "#| export\n", |
328 | | - "def to_xml(elm, lvl=0):\n", |
| 328 | + "def to_xml(elm, lvl=0, indent:bool=True):\n", |
329 | 329 | " \"Convert `ft` element tree into an XML string\"\n", |
| 330 | + " nl = '\\n'\n", |
| 331 | + " if not indent: lvl,nl = 0,''\n", |
330 | 332 | " if elm is None: return ''\n", |
331 | | - " if isinstance(elm, tuple): return '\\n'.join(to_xml(o) for o in elm)\n", |
| 333 | + " if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)\n", |
332 | 334 | " if hasattr(elm, '__ft__'): elm = elm.__ft__()\n", |
333 | | - " if isinstance(elm, bytes): return elm.decode('utf-8')\n", |
334 | 335 | " sp = ' ' * lvl\n", |
335 | | - " if not isinstance(elm, list): return f'{_escape(elm)}\\n'\n", |
| 336 | + " if not isinstance(elm, list): return f'{_escape(elm)}{nl}'\n", |
336 | 337 | "\n", |
337 | 338 | " tag,cs,attrs = elm\n", |
338 | 339 | " stag = tag\n", |
|
342 | 343 | "\n", |
343 | 344 | " isvoid = getattr(elm, 'void_', False)\n", |
344 | 345 | " cltag = '' if isvoid else f'</{tag}>'\n", |
345 | | - " if not cs: return f'{sp}<{stag}>{cltag}\\n'\n", |
| 346 | + " if not cs: return f'{sp}<{stag}>{cltag}{nl}'\n", |
346 | 347 | " if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__ft__'):\n", |
347 | | - " return f'{sp}<{stag}>{_escape(cs[0])}{cltag}\\n'\n", |
348 | | - " res = f'{sp}<{stag}>\\n'\n", |
349 | | - " res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)\n", |
350 | | - " if not isvoid: res += f'{sp}{cltag}\\n'\n", |
| 348 | + " return f'{sp}<{stag}>{_escape(cs[0])}{cltag}{nl}'\n", |
| 349 | + " res = f'{sp}<{stag}>{nl}'\n", |
| 350 | + " res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)\n", |
| 351 | + " if not isvoid: res += f'{sp}{cltag}{nl}'\n", |
351 | 352 | " return res\n", |
352 | 353 | "\n", |
353 | 354 | "FT.__html__ = to_xml" |
|
372 | 373 | " <body>\n", |
373 | 374 | " <div class=\"myclass another\" style=\"padding:1; margin:2\">\n", |
374 | 375 | "Some text\n", |
| 376 | + "another line\n", |
375 | 377 | " <input name=\"me\">\n", |
376 | 378 | " <img src=\"filename\" data=\"1\">\n", |
377 | 379 | " </div>\n", |
|
386 | 388 | "print(h)" |
387 | 389 | ] |
388 | 390 | }, |
| 391 | + { |
| 392 | + "cell_type": "code", |
| 393 | + "execution_count": null, |
| 394 | + "id": "51e58821", |
| 395 | + "metadata": {}, |
| 396 | + "outputs": [ |
| 397 | + { |
| 398 | + "name": "stdout", |
| 399 | + "output_type": "stream", |
| 400 | + "text": [ |
| 401 | + "<!doctype html><html><head><title>Some page</title></head><body><div class=\"myclass another\" style=\"padding:1; margin:2\">Some text\n", |
| 402 | + "another line<input name=\"me\"><img src=\"filename\" data=\"1\"></div></body></html>\n" |
| 403 | + ] |
| 404 | + } |
| 405 | + ], |
| 406 | + "source": [ |
| 407 | + "h = to_xml(samp, indent=False)\n", |
| 408 | + "print(h)" |
| 409 | + ] |
| 410 | + }, |
389 | 411 | { |
390 | 412 | "cell_type": "markdown", |
391 | 413 | "id": "4713bd8d", |
|
421 | 443 | "\n", |
422 | 444 | "r = _SafeString('<b>Hello from Django</b>')\n", |
423 | 445 | "print(to_xml(Div(r)))\n", |
424 | | - "\n", |
425 | 446 | "print(_escape(Div(P('Hello from fastcore <3'))))" |
426 | 447 | ] |
427 | 448 | }, |
|
0 commit comments