Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions fasthtml/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ def _f(*c, target_id=None, **kwargs): return ft_hx(tag, *c, target_id=target_id,
def html2ft(html, attr1st=False):
"""Convert HTML to an `ft` expression"""
rev_map = {'class': 'cls', 'for': 'fr'}

def _parse(elm, lvl=0, indent=4):
if isinstance(elm, str): return repr(elm.strip()) if elm.strip() else ''
if isinstance(elm, str): return repr(elm.strip("\n")) if elm.strip() else ''
if isinstance(elm, list): return '\n'.join(_parse(o, lvl) for o in elm)
tag_name = elm.name.capitalize().replace("-", "_")
if tag_name=='[document]': return _parse(list(elm.children), lvl)
cts = elm.contents
cs = [repr(c.strip()) if isinstance(c, str) else _parse(c, lvl+1)
cs = [repr(c.strip("\n")) if isinstance(c, str) else _parse(c, lvl+1)
for c in cts if str(c).strip()]
attrs, exotic_attrs = [], {}
for key, value in sorted(elm.attrs.items(), key=lambda x: x[0]=='class'):
Expand Down
19 changes: 8 additions & 11 deletions nbs/api/01_components.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,14 @@
"def html2ft(html, attr1st=False):\n",
" \"\"\"Convert HTML to an `ft` expression\"\"\"\n",
" rev_map = {'class': 'cls', 'for': 'fr'}\n",
" \n",
"\n",
" def _parse(elm, lvl=0, indent=4):\n",
" if isinstance(elm, str): return repr(elm.strip()) if elm.strip() else ''\n",
" if isinstance(elm, str): return repr(elm.strip(\"\\n\")) if elm.strip() else ''\n",
" if isinstance(elm, list): return '\\n'.join(_parse(o, lvl) for o in elm)\n",
" tag_name = elm.name.capitalize().replace(\"-\", \"_\")\n",
" if tag_name=='[document]': return _parse(list(elm.children), lvl)\n",
" cts = elm.contents\n",
" cs = [repr(c.strip()) if isinstance(c, str) else _parse(c, lvl+1)\n",
" cs = [repr(c.strip(\"\\n\")) if isinstance(c, str) else _parse(c, lvl+1)\n",
" for c in cts if str(c).strip()]\n",
" attrs, exotic_attrs = [], {}\n",
" for key, value in sorted(elm.attrs.items(), key=lambda x: x[0]=='class'):\n",
Expand Down Expand Up @@ -1299,7 +1299,9 @@
"#|hide\n",
"def test_html2ft(html: str, attr1st=False):\n",
" # html -> ft -> html\n",
" assert html == to_xml(eval(html2ft(html, attr1st))).strip()"
" soup1 = BeautifulSoup(html, 'html.parser')\n",
" soup2 = BeautifulSoup(to_xml(eval(html2ft(html, attr1st))).strip(), 'html.parser')\n",
" assert soup1.prettify() == soup2.prettify()\n"
]
},
{
Expand All @@ -1313,6 +1315,7 @@
"test_html2ft('<input value=\"Profit\" name=\"title\" id=\"title\" class=\"char\">')\n",
"test_html2ft('<div id=\"foo\"></div>')\n",
"test_html2ft('<div id=\"foo\">hi</div>')\n",
"test_html2ft('<div>Howdy <a href=\"https://answer.ai\">answer</a> how are you?</div>')\n",
"test_html2ft('<div x-show=\"open\" x-transition:enter=\"transition duration-300\" x-transition:enter-start=\"opacity-0 scale-90\">Hello 👋</div>')\n",
"test_html2ft('<div x-transition:enter.scale.80 x-transition:leave.scale.90>hello</div>')"
]
Expand Down Expand Up @@ -1361,13 +1364,7 @@
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}