Skip to content

Commit

Permalink
Merge pull request #193 from AndrewRPerkins/experimental_formatting
Browse files Browse the repository at this point in the history
Add experimental formatting to html2ft
  • Loading branch information
jph00 authored Aug 5, 2024
2 parents d80d3be + eb074e4 commit 3892b65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions fasthtml/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _f(*c, target_id=None, **kwargs): return ft_hx(tag, *c, target_id=target_id,

# %% ../nbs/api/01_components.ipynb
_re_h2x_attr_key = re.compile(r'^[A-Za-z_-][\w-]*$')
def html2ft(html):
def html2ft(html, style=None):
"""Convert HTML to an `ft` expression"""
rev_map = {'class': 'cls', 'for': 'fr'}

Expand All @@ -142,7 +142,10 @@ def _parse(elm, lvl=0, indent=4):
j = ', ' if onlychild else f',\n{spc}'
inner = j.join(filter(None, cs+attrs))
if onlychild: return f'{tag_name}({inner})'
return f'{tag_name}(\n{spc}{inner}\n{" "*(lvl-1)*indent})'
if style is None or not attrs: return f'{tag_name}(\n{spc}{inner}\n{" "*(lvl-1)*indent})'
inner_cs = j.join(filter(None, cs))
inner_attrs = ', '.join(filter(None, attrs))
return f'{tag_name}({inner_attrs})(\n{spc}{inner_cs}\n{" "*(lvl-1)*indent})'

soup = BeautifulSoup(html.strip(), 'html.parser')
for c in soup.find_all(string=risinstance(Comment)): c.extract()
Expand Down
39 changes: 37 additions & 2 deletions nbs/api/01_components.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
"source": [
"#| export\n",
"_re_h2x_attr_key = re.compile(r'^[A-Za-z_-][\\w-]*$')\n",
"def html2ft(html):\n",
"def html2ft(html, style=None):\n",
" \"\"\"Convert HTML to an `ft` expression\"\"\"\n",
" rev_map = {'class': 'cls', 'for': 'fr'}\n",
" \n",
Expand All @@ -443,7 +443,10 @@
" j = ', ' if onlychild else f',\\n{spc}'\n",
" inner = j.join(filter(None, cs+attrs))\n",
" if onlychild: return f'{tag_name}({inner})'\n",
" return f'{tag_name}(\\n{spc}{inner}\\n{\" \"*(lvl-1)*indent})'\n",
" if style is None or not attrs: return f'{tag_name}(\\n{spc}{inner}\\n{\" \"*(lvl-1)*indent})' \n",
" inner_cs = j.join(filter(None, cs))\n",
" inner_attrs = ', '.join(filter(None, attrs))\n",
" return f'{tag_name}({inner_attrs})(\\n{spc}{inner_cs}\\n{\" \"*(lvl-1)*indent})'\n",
"\n",
" soup = BeautifulSoup(html.strip(), 'html.parser')\n",
" for c in soup.find_all(string=risinstance(Comment)): c.extract()\n",
Expand Down Expand Up @@ -488,6 +491,38 @@
"hl_md(html2ft(h), 'python')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c3db4e841d2dbb1",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": "```python\nBody(cls='myclass')(\n Div(style='padding:3px')(\n Div(\n 'Some text',\n Input(name='me'),\n Img(src='filename', data='1')\n )\n )\n)\n```",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hl_md(html2ft('''\n",
"<body class=\"myclass\">\n",
" <div style=\"padding:3px\">\n",
" <div>\n",
" Some text\n",
" <input name=\"me\">\n",
" <img src=\"filename\" data=\"1\">\n",
" </div>\n",
" </div>\n",
"</body>\n",
"''', 'experimental_formatting'), 'python')"
]
},
{
"cell_type": "markdown",
"id": "474e14b4",
Expand Down

0 comments on commit 3892b65

Please sign in to comment.