Skip to content

Commit

Permalink
fixes #557
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 27, 2024
1 parent fb2d913 commit 1b69956
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@
'fastcore.xtras.dumps': ('xtras.html#dumps', 'fastcore/xtras.py'),
'fastcore.xtras.get_source_link': ('xtras.html#get_source_link', 'fastcore/xtras.py'),
'fastcore.xtras.globtastic': ('xtras.html#globtastic', 'fastcore/xtras.py'),
'fastcore.xtras.hl_md': ('xtras.html#hl_md', 'fastcore/xtras.py'),
'fastcore.xtras.image_size': ('xtras.html#image_size', 'fastcore/xtras.py'),
'fastcore.xtras.is_listy': ('xtras.html#is_listy', 'fastcore/xtras.py'),
'fastcore.xtras.join_path_file': ('xtras.html#join_path_file', 'fastcore/xtras.py'),
Expand Down
12 changes: 11 additions & 1 deletion fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'repr_dict', 'is_listy', 'mapped', 'IterLen', 'ReindexCollection', 'get_source_link', 'truncstr',
'sparkline', 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file', 'autostart',
'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace',
'modified_env', 'ContextManagers', 'shufflish', 'console_help']
'modified_env', 'ContextManagers', 'shufflish', 'console_help', 'hl_md']

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


# %% ../nbs/03_xtras.ipynb 159
def hl_md(s, lang='xml', show=True):
"Syntax highlight `s` using `lang`."
md = f'```{lang}\n{s}\n```'
if not show: return md
try:
from IPython import display
return display.Markdown(md)
except ImportError: print(s)
49 changes: 49 additions & 0 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,55 @@
" print(f'{nm:45}{e.load().__doc__}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def hl_md(s, lang='xml', show=True):\n",
" \"Syntax highlight `s` using `lang`.\"\n",
" md = f'```{lang}\\n{s}\\n```'\n",
" if not show: return md\n",
" try:\n",
" from IPython import display\n",
" return display.Markdown(md)\n",
" except ImportError: print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we display code in a notebook, it's nice to highlight it, so we create a function to simplify that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```xml\n",
"<test><xml foo=\"bar\">a child</xml></test>\n",
"```"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hl_md('<test><xml foo=\"bar\">a child</xml></test>')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 1b69956

Please sign in to comment.