Skip to content

Commit

Permalink
fixes #1442
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 1, 2024
1 parent c5f06ac commit a4c9ed6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
'nbdev.processors.add_show_docs': ('api/processors.html#add_show_docs', 'nbdev/processors.py'),
'nbdev.processors.add_show_docs.begin': ( 'api/processors.html#add_show_docs.begin',
'nbdev/processors.py'),
'nbdev.processors.ai_magics': ('api/processors.html#ai_magics', 'nbdev/processors.py'),
'nbdev.processors.boxify': ('api/processors.html#boxify', 'nbdev/processors.py'),
'nbdev.processors.cell_lang': ('api/processors.html#cell_lang', 'nbdev/processors.py'),
'nbdev.processors.clean_magics': ('api/processors.html#clean_magics', 'nbdev/processors.py'),
Expand Down
16 changes: 13 additions & 3 deletions nbdev/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# %% auto 0
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'fdiv', 'boxify', 'mv_exports', 'add_links',
'add_fold', 'strip_ansi', 'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics',
'rm_header_dash', 'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']
'add_fold', 'strip_ansi', 'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'ai_magics',
'clean_magics', 'rm_header_dash', 'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']

# %% ../nbs/api/10_processors.ipynb
import ast
Expand Down Expand Up @@ -169,6 +169,15 @@ def filter_stream_(cell, *words):
if outp.output_type == 'stream':
outp['text'] = [l for l in outp.text if not re.search('|'.join(words), l)]

# %% ../nbs/api/10_processors.ipynb
_aimagics_pattern = re.compile(r'^\s*(%%ai.? |%%ai.?$)', re.MULTILINE)

def ai_magics(cell):
"A preprocessor to convert AI magics to markdown"
if cell.cell_type == 'code' and _aimagics_pattern.search(cell.source):
cell.cell_type ='markdown'
cell.source = '\n'.join(cell.source.splitlines()[1:])

# %% ../nbs/api/10_processors.ipynb
_magics_pattern = re.compile(r'^\s*(%%|%).*', re.MULTILINE)

Expand Down Expand Up @@ -268,7 +277,8 @@ def xtra_procs(self):
def base_procs(self):
return [FrontmatterProc, populate_language, add_show_docs, insert_warning,
strip_ansi, hide_line, filter_stream_, rm_header_dash,
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, mv_exports, strip_hidden_metadata]
clean_show_doc, exec_show_docs, rm_export, ai_magics, clean_magics, hide_, add_links,
add_fold, mv_exports, strip_hidden_metadata]

def procs(self):
"Processors for export"
Expand Down
31 changes: 30 additions & 1 deletion nbs/api/10_processors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,34 @@
"id": "848fd452-3d63-4c41-aaa6-e14cbeb9fdcd",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"_aimagics_pattern = re.compile(r'^\\s*(%%ai.? |%%ai.?$)', re.MULTILINE)\n",
"\n",
"def ai_magics(cell):\n",
" \"A preprocessor to convert AI magics to markdown\"\n",
" if cell.cell_type == 'code' and _aimagics_pattern.search(cell.source):\n",
" cell.cell_type ='markdown'\n",
" cell.source = '\\n'.join(cell.source.splitlines()[1:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39541a1e",
"metadata": {},
"outputs": [],
"source": [
"res = _run_procs(ai_magics)\n",
"assert \"'source': 'This is a test.'\" in res"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a9c8fd4",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"_magics_pattern = re.compile(r'^\\s*(%%|%).*', re.MULTILINE)\n",
Expand Down Expand Up @@ -746,7 +774,8 @@
" def base_procs(self):\n",
" return [FrontmatterProc, populate_language, add_show_docs, insert_warning,\n",
" strip_ansi, hide_line, filter_stream_, rm_header_dash,\n",
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, mv_exports, strip_hidden_metadata]\n",
" clean_show_doc, exec_show_docs, rm_export, ai_magics, clean_magics, hide_, add_links,\n",
" add_fold, mv_exports, strip_hidden_metadata]\n",
"\n",
" def procs(self):\n",
" \"Processors for export\"\n",
Expand Down
25 changes: 24 additions & 1 deletion tests/docs_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@
"b()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "516a8282",
"metadata": {},
"outputs": [],
"source": [
"%%ai 0\n",
"This is a test."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -474,9 +485,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit a4c9ed6

Please sign in to comment.