Skip to content

Commit 0813357

Browse files
committed
fixes #553
1 parent 33e22e3 commit 0813357

File tree

3 files changed

+46
-84
lines changed

3 files changed

+46
-84
lines changed

fastcore/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@
535535
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
536536
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
537537
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
538-
'fastcore.xml.XT._repr_markdown_': ('xml.html#xt._repr_markdown_', 'fastcore/xml.py'),
539538
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
539+
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
540+
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
540541
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
541542
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
542543
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),

fastcore/xml.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22

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

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

7575
# %% ../nbs/11_xml.ipynb 13
76-
@patch
77-
def _repr_markdown_(self:XT):
78-
try: from IPython import display
79-
except ImportError: return repr(self)
80-
return f'```html\n{to_xml(self)}\n```'
76+
def highlight(s, lang='html'):
77+
"Markdown to syntax-highlight `s` in language `lang`"
78+
return f'```{lang}\n{to_xml(s)}\n```'
79+
80+
# %% ../nbs/11_xml.ipynb 14
81+
def showtags(s):
82+
return f"""<code><pre>
83+
{escape(to_xml(s))}
84+
</code></pre>"""
85+
86+
XT._repr_html_ = showtags

nbs/11_xml.ipynb

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 37,
5+
"execution_count": 1,
66
"id": "d0c84aa5",
77
"metadata": {},
88
"outputs": [],
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 38,
25+
"execution_count": 2,
2626
"id": "55944805",
2727
"metadata": {},
2828
"outputs": [],
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"cell_type": "code",
41-
"execution_count": 39,
41+
"execution_count": 3,
4242
"id": "42d18e5c",
4343
"metadata": {},
4444
"outputs": [],
@@ -49,7 +49,7 @@
4949
},
5050
{
5151
"cell_type": "code",
52-
"execution_count": 40,
52+
"execution_count": 4,
5353
"id": "4dbb9a59",
5454
"metadata": {},
5555
"outputs": [],
@@ -62,7 +62,7 @@
6262
},
6363
{
6464
"cell_type": "code",
65-
"execution_count": 41,
65+
"execution_count": 5,
6666
"id": "149067dd",
6767
"metadata": {},
6868
"outputs": [],
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 42,
76+
"execution_count": 6,
7777
"id": "06718948",
7878
"metadata": {},
7979
"outputs": [],
@@ -88,7 +88,7 @@
8888
},
8989
{
9090
"cell_type": "code",
91-
"execution_count": 43,
91+
"execution_count": 7,
9292
"id": "45489975",
9393
"metadata": {},
9494
"outputs": [],
@@ -120,7 +120,7 @@
120120
},
121121
{
122122
"cell_type": "code",
123-
"execution_count": 44,
123+
"execution_count": 8,
124124
"id": "9a8b4ddb",
125125
"metadata": {},
126126
"outputs": [
@@ -151,7 +151,7 @@
151151
},
152152
{
153153
"cell_type": "code",
154-
"execution_count": 45,
154+
"execution_count": 9,
155155
"id": "c7de63a4",
156156
"metadata": {},
157157
"outputs": [],
@@ -162,7 +162,7 @@
162162
},
163163
{
164164
"cell_type": "code",
165-
"execution_count": 46,
165+
"execution_count": 10,
166166
"id": "b89d088a",
167167
"metadata": {},
168168
"outputs": [],
@@ -193,7 +193,7 @@
193193
},
194194
{
195195
"cell_type": "code",
196-
"execution_count": 47,
196+
"execution_count": 11,
197197
"id": "d3d23c48",
198198
"metadata": {},
199199
"outputs": [
@@ -227,76 +227,31 @@
227227
},
228228
{
229229
"cell_type": "code",
230-
"execution_count": 48,
230+
"execution_count": 12,
231231
"id": "798ae1d2",
232232
"metadata": {},
233233
"outputs": [],
234234
"source": [
235235
"#| export\n",
236-
"@patch\n",
237-
"def _repr_markdown_(self:XT):\n",
238-
" try: from IPython import display\n",
239-
" except ImportError: return repr(self)\n",
240-
" return f'```html\\n{to_xml(self)}\\n```'"
241-
]
242-
},
243-
{
244-
"cell_type": "markdown",
245-
"id": "107d5912",
246-
"metadata": {},
247-
"source": [
248-
"Automatic syntax highlighted output in notebooks:"
236+
"def highlight(s, lang='html'):\n",
237+
" \"Markdown to syntax-highlight `s` in language `lang`\"\n",
238+
" return f'```{lang}\\n{to_xml(s)}\\n```'"
249239
]
250240
},
251241
{
252242
"cell_type": "code",
253-
"execution_count": 49,
254-
"id": "b94a79b9",
243+
"execution_count": 16,
244+
"id": "39fab735",
255245
"metadata": {},
256-
"outputs": [
257-
{
258-
"data": {
259-
"text/markdown": [
260-
"```html\n",
261-
"<html>\n",
262-
" <head>\n",
263-
" <title>\n",
264-
"Some page\n",
265-
" </title>\n",
266-
" </head>\n",
267-
" <body>\n",
268-
" <div class=\"myclass\">\n",
269-
" <p>\n",
270-
"Some text\n",
271-
" </p>\n",
272-
" <input name=\"me\">\n",
273-
" <img src=\"filename\">\n",
274-
" </div>\n",
275-
" </body>\n",
276-
"</html>\n",
277-
"\n",
278-
"```"
279-
],
280-
"text/plain": [
281-
"['html',\n",
282-
" (['head', (['title', ('Some page',), {}],), {}],\n",
283-
" ['body',\n",
284-
" (['div',\n",
285-
" (['p', ('Some text',), {}],\n",
286-
" ['input', (), {'name': 'me'}],\n",
287-
" ['img', (), {'src': 'filename'}]),\n",
288-
" {'class': 'myclass'}],),\n",
289-
" {}]),\n",
290-
" {}]"
291-
]
292-
},
293-
"execution_count": 49,
294-
"metadata": {},
295-
"output_type": "execute_result"
296-
}
297-
],
246+
"outputs": [],
298247
"source": [
299-
"samp"
248+
"#| export\n",
249+
"def showtags(s):\n",
250+
" return f\"\"\"<code><pre>\n",
251+
"{escape(to_xml(s))}\n",
252+
"</code></pre>\"\"\"\n",
253+
"\n",
254+
"XT._repr_html_ = showtags"
300255
]
301256
},
302257
{
@@ -309,7 +264,7 @@
309264
},
310265
{
311266
"cell_type": "code",
312-
"execution_count": 51,
267+
"execution_count": 17,
313268
"id": "ad32b076",
314269
"metadata": {},
315270
"outputs": [],

0 commit comments

Comments
 (0)