Skip to content

Commit a4c9ed6

Browse files
committed
fixes #1442
1 parent c5f06ac commit a4c9ed6

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

nbdev/_modidx.py

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
'nbdev.processors.add_show_docs': ('api/processors.html#add_show_docs', 'nbdev/processors.py'),
205205
'nbdev.processors.add_show_docs.begin': ( 'api/processors.html#add_show_docs.begin',
206206
'nbdev/processors.py'),
207+
'nbdev.processors.ai_magics': ('api/processors.html#ai_magics', 'nbdev/processors.py'),
207208
'nbdev.processors.boxify': ('api/processors.html#boxify', 'nbdev/processors.py'),
208209
'nbdev.processors.cell_lang': ('api/processors.html#cell_lang', 'nbdev/processors.py'),
209210
'nbdev.processors.clean_magics': ('api/processors.html#clean_magics', 'nbdev/processors.py'),

nbdev/processors.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

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

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

172+
# %% ../nbs/api/10_processors.ipynb
173+
_aimagics_pattern = re.compile(r'^\s*(%%ai.? |%%ai.?$)', re.MULTILINE)
174+
175+
def ai_magics(cell):
176+
"A preprocessor to convert AI magics to markdown"
177+
if cell.cell_type == 'code' and _aimagics_pattern.search(cell.source):
178+
cell.cell_type ='markdown'
179+
cell.source = '\n'.join(cell.source.splitlines()[1:])
180+
172181
# %% ../nbs/api/10_processors.ipynb
173182
_magics_pattern = re.compile(r'^\s*(%%|%).*', re.MULTILINE)
174183

@@ -268,7 +277,8 @@ def xtra_procs(self):
268277
def base_procs(self):
269278
return [FrontmatterProc, populate_language, add_show_docs, insert_warning,
270279
strip_ansi, hide_line, filter_stream_, rm_header_dash,
271-
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, mv_exports, strip_hidden_metadata]
280+
clean_show_doc, exec_show_docs, rm_export, ai_magics, clean_magics, hide_, add_links,
281+
add_fold, mv_exports, strip_hidden_metadata]
272282

273283
def procs(self):
274284
"Processors for export"

nbs/api/10_processors.ipynb

+30-1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,34 @@
544544
"id": "848fd452-3d63-4c41-aaa6-e14cbeb9fdcd",
545545
"metadata": {},
546546
"outputs": [],
547+
"source": [
548+
"#| export\n",
549+
"_aimagics_pattern = re.compile(r'^\\s*(%%ai.? |%%ai.?$)', re.MULTILINE)\n",
550+
"\n",
551+
"def ai_magics(cell):\n",
552+
" \"A preprocessor to convert AI magics to markdown\"\n",
553+
" if cell.cell_type == 'code' and _aimagics_pattern.search(cell.source):\n",
554+
" cell.cell_type ='markdown'\n",
555+
" cell.source = '\\n'.join(cell.source.splitlines()[1:])"
556+
]
557+
},
558+
{
559+
"cell_type": "code",
560+
"execution_count": null,
561+
"id": "39541a1e",
562+
"metadata": {},
563+
"outputs": [],
564+
"source": [
565+
"res = _run_procs(ai_magics)\n",
566+
"assert \"'source': 'This is a test.'\" in res"
567+
]
568+
},
569+
{
570+
"cell_type": "code",
571+
"execution_count": null,
572+
"id": "5a9c8fd4",
573+
"metadata": {},
574+
"outputs": [],
547575
"source": [
548576
"#| export\n",
549577
"_magics_pattern = re.compile(r'^\\s*(%%|%).*', re.MULTILINE)\n",
@@ -746,7 +774,8 @@
746774
" def base_procs(self):\n",
747775
" return [FrontmatterProc, populate_language, add_show_docs, insert_warning,\n",
748776
" strip_ansi, hide_line, filter_stream_, rm_header_dash,\n",
749-
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, mv_exports, strip_hidden_metadata]\n",
777+
" clean_show_doc, exec_show_docs, rm_export, ai_magics, clean_magics, hide_, add_links,\n",
778+
" add_fold, mv_exports, strip_hidden_metadata]\n",
750779
"\n",
751780
" def procs(self):\n",
752781
" \"Processors for export\"\n",

tests/docs_test.ipynb

+24-1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,17 @@
463463
"b()"
464464
]
465465
},
466+
{
467+
"cell_type": "code",
468+
"execution_count": 1,
469+
"id": "516a8282",
470+
"metadata": {},
471+
"outputs": [],
472+
"source": [
473+
"%%ai 0\n",
474+
"This is a test."
475+
]
476+
},
466477
{
467478
"cell_type": "code",
468479
"execution_count": null,
@@ -474,9 +485,21 @@
474485
],
475486
"metadata": {
476487
"kernelspec": {
477-
"display_name": "python3",
488+
"display_name": "Python 3 (ipykernel)",
478489
"language": "python",
479490
"name": "python3"
491+
},
492+
"language_info": {
493+
"codemirror_mode": {
494+
"name": "ipython",
495+
"version": 3
496+
},
497+
"file_extension": ".py",
498+
"mimetype": "text/x-python",
499+
"name": "python",
500+
"nbconvert_exporter": "python",
501+
"pygments_lexer": "ipython3",
502+
"version": "3.11.8"
480503
}
481504
},
482505
"nbformat": 4,

0 commit comments

Comments
 (0)