Skip to content

Commit 1b69956

Browse files
committed
fixes #557
1 parent fb2d913 commit 1b69956

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@
616616
'fastcore.xtras.dumps': ('xtras.html#dumps', 'fastcore/xtras.py'),
617617
'fastcore.xtras.get_source_link': ('xtras.html#get_source_link', 'fastcore/xtras.py'),
618618
'fastcore.xtras.globtastic': ('xtras.html#globtastic', 'fastcore/xtras.py'),
619+
'fastcore.xtras.hl_md': ('xtras.html#hl_md', 'fastcore/xtras.py'),
619620
'fastcore.xtras.image_size': ('xtras.html#image_size', 'fastcore/xtras.py'),
620621
'fastcore.xtras.is_listy': ('xtras.html#is_listy', 'fastcore/xtras.py'),
621622
'fastcore.xtras.join_path_file': ('xtras.html#join_path_file', 'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'repr_dict', 'is_listy', 'mapped', 'IterLen', 'ReindexCollection', 'get_source_link', 'truncstr',
1010
'sparkline', 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file', 'autostart',
1111
'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace',
12-
'modified_env', 'ContextManagers', 'shufflish', 'console_help']
12+
'modified_env', 'ContextManagers', 'shufflish', 'console_help', 'hl_md']
1313

1414
# %% ../nbs/03_xtras.ipynb 2
1515
from .imports import *
@@ -566,3 +566,13 @@ def console_help(
566566
nm = S.bold.light_blue(e.name)
567567
print(f'{nm:45}{e.load().__doc__}')
568568

569+
570+
# %% ../nbs/03_xtras.ipynb 159
571+
def hl_md(s, lang='xml', show=True):
572+
"Syntax highlight `s` using `lang`."
573+
md = f'```{lang}\n{s}\n```'
574+
if not show: return md
575+
try:
576+
from IPython import display
577+
return display.Markdown(md)
578+
except ImportError: print(s)

nbs/03_xtras.ipynb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,55 @@
23582358
" print(f'{nm:45}{e.load().__doc__}')\n"
23592359
]
23602360
},
2361+
{
2362+
"cell_type": "code",
2363+
"execution_count": null,
2364+
"metadata": {},
2365+
"outputs": [],
2366+
"source": [
2367+
"#| export\n",
2368+
"def hl_md(s, lang='xml', show=True):\n",
2369+
" \"Syntax highlight `s` using `lang`.\"\n",
2370+
" md = f'```{lang}\\n{s}\\n```'\n",
2371+
" if not show: return md\n",
2372+
" try:\n",
2373+
" from IPython import display\n",
2374+
" return display.Markdown(md)\n",
2375+
" except ImportError: print(s)"
2376+
]
2377+
},
2378+
{
2379+
"cell_type": "markdown",
2380+
"metadata": {},
2381+
"source": [
2382+
"When we display code in a notebook, it's nice to highlight it, so we create a function to simplify that:"
2383+
]
2384+
},
2385+
{
2386+
"cell_type": "code",
2387+
"execution_count": null,
2388+
"metadata": {},
2389+
"outputs": [
2390+
{
2391+
"data": {
2392+
"text/markdown": [
2393+
"```xml\n",
2394+
"<test><xml foo=\"bar\">a child</xml></test>\n",
2395+
"```"
2396+
],
2397+
"text/plain": [
2398+
"<IPython.core.display.Markdown object>"
2399+
]
2400+
},
2401+
"execution_count": null,
2402+
"metadata": {},
2403+
"output_type": "execute_result"
2404+
}
2405+
],
2406+
"source": [
2407+
"hl_md('<test><xml foo=\"bar\">a child</xml></test>')"
2408+
]
2409+
},
23612410
{
23622411
"cell_type": "markdown",
23632412
"metadata": {},

0 commit comments

Comments
 (0)