Skip to content

Commit

Permalink
fixes #553
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 20, 2024
1 parent 33e22e3 commit 0813357
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 84 deletions.
3 changes: 2 additions & 1 deletion fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,9 @@
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
'fastcore.xml.XT._repr_markdown_': ('xml.html#xt._repr_markdown_', 'fastcore/xml.py'),
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
Expand Down
30 changes: 18 additions & 12 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.

# %% auto 0
__all__ = ['voids', 'XT', 'xt', 'to_xml', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code', 'Div', 'Span',
'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr',
'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th',
'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button', 'Select', 'Option', 'Label',
'Fieldset', 'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer', 'Section', 'Article', 'Aside',
'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video', 'Audio', 'Source',
'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']
__all__ = ['voids', 'XT', 'xt', 'to_xml', 'highlight', 'showtags', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body',
'Pre', 'Code', 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S',
'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead',
'Tbody', 'Tfoot', 'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button',
'Select', 'Option', 'Label', 'Fieldset', 'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer',
'Section', 'Article', 'Aside', 'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param',
'Video', 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript', 'Template', 'Slot']

# %% ../nbs/11_xml.ipynb 2
from .utils import *
Expand Down Expand Up @@ -73,8 +73,14 @@ def to_xml(elm, lvl=0):
return res

# %% ../nbs/11_xml.ipynb 13
@patch
def _repr_markdown_(self:XT):
try: from IPython import display
except ImportError: return repr(self)
return f'```html\n{to_xml(self)}\n```'
def highlight(s, lang='html'):
"Markdown to syntax-highlight `s` in language `lang`"
return f'```{lang}\n{to_xml(s)}\n```'

# %% ../nbs/11_xml.ipynb 14
def showtags(s):
return f"""<code><pre>
{escape(to_xml(s))}
</code></pre>"""

XT._repr_html_ = showtags
97 changes: 26 additions & 71 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 1,
"id": "d0c84aa5",
"metadata": {},
"outputs": [],
Expand All @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 2,
"id": "55944805",
"metadata": {},
"outputs": [],
Expand All @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 3,
"id": "42d18e5c",
"metadata": {},
"outputs": [],
Expand All @@ -49,7 +49,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 4,
"id": "4dbb9a59",
"metadata": {},
"outputs": [],
Expand All @@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 5,
"id": "149067dd",
"metadata": {},
"outputs": [],
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 6,
"id": "06718948",
"metadata": {},
"outputs": [],
Expand All @@ -88,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 7,
"id": "45489975",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -120,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 8,
"id": "9a8b4ddb",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -151,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 9,
"id": "c7de63a4",
"metadata": {},
"outputs": [],
Expand All @@ -162,7 +162,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 10,
"id": "b89d088a",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -193,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 11,
"id": "d3d23c48",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -227,76 +227,31 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 12,
"id": "798ae1d2",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@patch\n",
"def _repr_markdown_(self:XT):\n",
" try: from IPython import display\n",
" except ImportError: return repr(self)\n",
" return f'```html\\n{to_xml(self)}\\n```'"
]
},
{
"cell_type": "markdown",
"id": "107d5912",
"metadata": {},
"source": [
"Automatic syntax highlighted output in notebooks:"
"def highlight(s, lang='html'):\n",
" \"Markdown to syntax-highlight `s` in language `lang`\"\n",
" return f'```{lang}\\n{to_xml(s)}\\n```'"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "b94a79b9",
"execution_count": 16,
"id": "39fab735",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```html\n",
"<html>\n",
" <head>\n",
" <title>\n",
"Some page\n",
" </title>\n",
" </head>\n",
" <body>\n",
" <div class=\"myclass\">\n",
" <p>\n",
"Some text\n",
" </p>\n",
" <input name=\"me\">\n",
" <img src=\"filename\">\n",
" </div>\n",
" </body>\n",
"</html>\n",
"\n",
"```"
],
"text/plain": [
"['html',\n",
" (['head', (['title', ('Some page',), {}],), {}],\n",
" ['body',\n",
" (['div',\n",
" (['p', ('Some text',), {}],\n",
" ['input', (), {'name': 'me'}],\n",
" ['img', (), {'src': 'filename'}]),\n",
" {'class': 'myclass'}],),\n",
" {}]),\n",
" {}]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"samp"
"#| export\n",
"def showtags(s):\n",
" return f\"\"\"<code><pre>\n",
"{escape(to_xml(s))}\n",
"</code></pre>\"\"\"\n",
"\n",
"XT._repr_html_ = showtags"
]
},
{
Expand All @@ -309,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 17,
"id": "ad32b076",
"metadata": {},
"outputs": [],
Expand Down

0 comments on commit 0813357

Please sign in to comment.