Skip to content

Commit

Permalink
Merge pull request #596 from Rahulbeniwal26119/fixing-to-xml-bug-whil…
Browse files Browse the repository at this point in the history
…e-converting-ft

fixing to_xml to parse Titled correctly for fasthtml
  • Loading branch information
jph00 authored Aug 11, 2024
2 parents 92932e6 + cde87a7 commit ba8bcd1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fastcore/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def _to_xml(elm, lvl, indent:bool):
nl = '\n'
if not indent: lvl,nl = 0,''
if elm is None: return ''
if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)
if hasattr(elm, '__ft__'): elm = elm.__ft__()
if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)
if isinstance(elm, bytes): return elm.decode('utf-8')
sp = ' ' * lvl
if not isinstance(elm, list): return f'{_escape(elm)}{nl}'
Expand Down
30 changes: 29 additions & 1 deletion nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@
" nl = '\\n'\n",
" if not indent: lvl,nl = 0,''\n",
" if elm is None: return ''\n",
" if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)\n",
" if hasattr(elm, '__ft__'): elm = elm.__ft__()\n",
" if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)\n",
" if isinstance(elm, bytes): return elm.decode('utf-8')\n",
" sp = ' ' * lvl\n",
" if not isinstance(elm, list): return f'{_escape(elm)}{nl}'\n",
Expand Down Expand Up @@ -408,6 +408,34 @@
"print(h)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a16341",
"metadata": {},
"outputs": [],
"source": [
"\n",
"class PageTitle:\n",
" \n",
" def __ft__(self):\n",
" return H1(\"Hello\")\n",
"\n",
"class HomePage:\n",
" def __ft__(self):\n",
" return Div(PageTitle(), Div('hello'))\n",
"\n",
"h = to_xml(Div(HomePage()))\n",
"expected_output = \"\"\"<div>\n",
" <div>\n",
" <h1>Hello</h1>\n",
" <div>hello</div>\n",
" </div>\n",
"</div>\n",
"\"\"\"\n",
"assert h == expected_output"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit ba8bcd1

Please sign in to comment.