|
1120 | 1120 | "def html2ft(html, attr1st=False):\n", |
1121 | 1121 | " \"\"\"Convert HTML to an `ft` expression\"\"\"\n", |
1122 | 1122 | " rev_map = {'class': 'cls', 'for': 'fr'}\n", |
1123 | | - " \n", |
| 1123 | + "\n", |
1124 | 1124 | " def _parse(elm, lvl=0, indent=4):\n", |
1125 | | - " if isinstance(elm, str): return repr(elm.strip()) if elm.strip() else ''\n", |
| 1125 | + " if isinstance(elm, str): return repr(elm.strip(\"\\n\")) if elm.strip() else ''\n", |
1126 | 1126 | " if isinstance(elm, list): return '\\n'.join(_parse(o, lvl) for o in elm)\n", |
1127 | 1127 | " tag_name = elm.name.capitalize().replace(\"-\", \"_\")\n", |
1128 | 1128 | " if tag_name=='[document]': return _parse(list(elm.children), lvl)\n", |
1129 | 1129 | " cts = elm.contents\n", |
1130 | | - " cs = [repr(c.strip()) if isinstance(c, str) else _parse(c, lvl+1)\n", |
| 1130 | + " cs = [repr(c.strip(\"\\n\")) if isinstance(c, str) else _parse(c, lvl+1)\n", |
1131 | 1131 | " for c in cts if str(c).strip()]\n", |
1132 | 1132 | " attrs, exotic_attrs = [], {}\n", |
1133 | 1133 | " for key, value in sorted(elm.attrs.items(), key=lambda x: x[0]=='class'):\n", |
|
1299 | 1299 | "#|hide\n", |
1300 | 1300 | "def test_html2ft(html: str, attr1st=False):\n", |
1301 | 1301 | " # html -> ft -> html\n", |
1302 | | - " assert html == to_xml(eval(html2ft(html, attr1st))).strip()" |
| 1302 | + " soup1 = BeautifulSoup(html, 'html.parser')\n", |
| 1303 | + " soup2 = BeautifulSoup(to_xml(eval(html2ft(html, attr1st))).strip(), 'html.parser')\n", |
| 1304 | + " assert soup1.prettify() == soup2.prettify()\n" |
1303 | 1305 | ] |
1304 | 1306 | }, |
1305 | 1307 | { |
|
1313 | 1315 | "test_html2ft('<input value=\"Profit\" name=\"title\" id=\"title\" class=\"char\">')\n", |
1314 | 1316 | "test_html2ft('<div id=\"foo\"></div>')\n", |
1315 | 1317 | "test_html2ft('<div id=\"foo\">hi</div>')\n", |
| 1318 | + "test_html2ft('<div>Howdy <a href=\"https://answer.ai\">answer</a> how are you?</div>')\n", |
1316 | 1319 | "test_html2ft('<div x-show=\"open\" x-transition:enter=\"transition duration-300\" x-transition:enter-start=\"opacity-0 scale-90\">Hello 👋</div>')\n", |
1317 | 1320 | "test_html2ft('<div x-transition:enter.scale.80 x-transition:leave.scale.90>hello</div>')" |
1318 | 1321 | ] |
|
1361 | 1364 | "source": [] |
1362 | 1365 | } |
1363 | 1366 | ], |
1364 | | - "metadata": { |
1365 | | - "kernelspec": { |
1366 | | - "display_name": "python3", |
1367 | | - "language": "python", |
1368 | | - "name": "python3" |
1369 | | - } |
1370 | | - }, |
| 1367 | + "metadata": {}, |
1371 | 1368 | "nbformat": 4, |
1372 | 1369 | "nbformat_minor": 5 |
1373 | 1370 | } |
0 commit comments