11# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/10_processors.ipynb.
22
33# %% auto 0
4- __all__ = ['populate_language' , 'insert_warning' , 'cell_lang' , 'add_show_docs' , 'add_links' , 'strip_ansi' ,
4+ __all__ = ['populate_language' , 'insert_warning' , 'cell_lang' , 'add_show_docs' , 'add_links' , 'add_fold' , ' strip_ansi' ,
55 'strip_hidden_metadata' , 'hide_' , 'hide_line' , 'filter_stream_' , 'clean_magics' , 'rm_header_dash' ,
66 'rm_export' , 'clean_show_doc' , 'exec_show_docs' , 'FilterDefaults' ]
77
@@ -75,7 +75,8 @@ def begin(self):
7575 shown_docs = {_get_nm (t ) for t in _show_docs (trees )}
7676 for cell in reversed (exports ):
7777 if cell_lang (cell ) != 'python' : raise ValueError (f"{ cell .metadata .language } can't export:\n { cell .source } " )
78- for nm in _def_names (cell , shown_docs ): nb .cells .insert (cell .idx_ + 1 , mk_cell (f'show_doc({ nm } )' ))
78+ nms = _def_names (cell , shown_docs )
79+ for nm in nms : nb .cells .insert (cell .idx_ + 1 , mk_cell (f'show_doc({ nm } )' ))
7980 nb .has_docs_ = shown_docs or exports
8081
8182# %% ../nbs/api/10_processors.ipynb 17
@@ -96,25 +97,31 @@ def add_links(cell):
9697 if hasattr (o , 'data' ) and hasattr (o ['data' ], 'text/markdown' ):
9798 o .data ['text/markdown' ] = [nl .link_line (s ) for s in o .data ['text/markdown' ]]
9899
99- # %% ../nbs/api/10_processors.ipynb 22
100+ # %% ../nbs/api/10_processors.ipynb 21
101+ def add_fold (cell ):
102+ "Add `code-fold` to `exports` cells"
103+ if cell .cell_type != 'code' or 'exports' not in cell .directives_ : return
104+ cell .source = f'#| code-fold: show\n #| code-summary: "Exported source"\n { cell .source } '
105+
106+ # %% ../nbs/api/10_processors.ipynb 24
100107_re_ansi_escape = re .compile (r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])' )
101108
102109def strip_ansi (cell ):
103110 "Strip Ansi Characters."
104111 for outp in cell .get ('outputs' , []):
105112 if outp .get ('name' )== 'stdout' : outp ['text' ] = [_re_ansi_escape .sub ('' , o ) for o in outp .text ]
106113
107- # %% ../nbs/api/10_processors.ipynb 24
114+ # %% ../nbs/api/10_processors.ipynb 26
108115def strip_hidden_metadata (cell ):
109116 '''Strips "hidden" metadata property from code cells so it doesn't interfere with docs rendering'''
110117 if cell .cell_type == 'code' and 'metadata' in cell : cell .metadata .pop ('hidden' ,None )
111118
112- # %% ../nbs/api/10_processors.ipynb 25
119+ # %% ../nbs/api/10_processors.ipynb 27
113120def hide_ (cell ):
114121 "Hide cell from output"
115122 del (cell ['source' ])
116123
117- # %% ../nbs/api/10_processors.ipynb 27
124+ # %% ../nbs/api/10_processors.ipynb 29
118125def _re_hideline (lang = None ): return re .compile (fr'{ langs [lang ]} \|\s*hide_line\s*$' , re .MULTILINE )
119126
120127def hide_line (cell ):
@@ -123,22 +130,22 @@ def hide_line(cell):
123130 if cell .cell_type == 'code' and _re_hideline (lang ).search (cell .source ):
124131 cell .source = '\n ' .join ([c for c in cell .source .splitlines () if not _re_hideline (lang ).search (c )])
125132
126- # %% ../nbs/api/10_processors.ipynb 30
133+ # %% ../nbs/api/10_processors.ipynb 32
127134def filter_stream_ (cell , * words ):
128135 "Remove output lines containing any of `words` in `cell` stream output"
129136 if not words : return
130137 for outp in cell .get ('outputs' , []):
131138 if outp .output_type == 'stream' :
132139 outp ['text' ] = [l for l in outp .text if not re .search ('|' .join (words ), l )]
133140
134- # %% ../nbs/api/10_processors.ipynb 32
141+ # %% ../nbs/api/10_processors.ipynb 34
135142_magics_pattern = re .compile (r'^\s*(%%|%).*' , re .MULTILINE )
136143
137144def clean_magics (cell ):
138145 "A preprocessor to remove cell magic commands"
139146 if cell .cell_type == 'code' : cell .source = _magics_pattern .sub ('' , cell .source ).strip ()
140147
141- # %% ../nbs/api/10_processors.ipynb 34
148+ # %% ../nbs/api/10_processors.ipynb 36
142149_re_hdr_dash = re .compile (r'^#+\s+.*\s+-\s*$' , re .MULTILINE )
143150
144151def rm_header_dash (cell ):
@@ -147,14 +154,14 @@ def rm_header_dash(cell):
147154 src = cell .source .strip ()
148155 if cell .cell_type == 'markdown' and src .startswith ('#' ) and src .endswith (' -' ): del (cell ['source' ])
149156
150- # %% ../nbs/api/10_processors.ipynb 36
157+ # %% ../nbs/api/10_processors.ipynb 38
151158_hide_dirs = {'export' ,'exporti' , 'hide' ,'default_exp' }
152159
153160def rm_export (cell ):
154161 "Remove cells that are exported or hidden"
155162 if cell .directives_ and (cell .directives_ .keys () & _hide_dirs ): del (cell ['source' ])
156163
157- # %% ../nbs/api/10_processors.ipynb 38
164+ # %% ../nbs/api/10_processors.ipynb 40
158165_re_showdoc = re .compile (r'^show_doc' , re .MULTILINE )
159166def _is_showdoc (cell ): return cell ['cell_type' ] == 'code' and _re_showdoc .search (cell .source )
160167def _add_directives (cell , d ):
@@ -166,7 +173,7 @@ def clean_show_doc(cell):
166173 if not _is_showdoc (cell ): return
167174 _add_directives (cell , {'output' :'asis' ,'echo' :'false' })
168175
169- # %% ../nbs/api/10_processors.ipynb 39
176+ # %% ../nbs/api/10_processors.ipynb 41
170177def _ast_contains (trees , types ):
171178 for tree in trees :
172179 for node in ast .walk (tree ):
@@ -187,7 +194,7 @@ def _do_eval(cell):
187194 return True
188195 if _show_docs (trees ): return True
189196
190- # %% ../nbs/api/10_processors.ipynb 40
197+ # %% ../nbs/api/10_processors.ipynb 42
191198class exec_show_docs (Processor ):
192199 "Execute cells needed for `show_docs` output, including exported cells and imports"
193200 def begin (self ):
@@ -214,13 +221,13 @@ def end(self):
214221 widgets = {** old , ** new , 'state' : {** old .get ('state' , {}), ** new ['state' ]}}
215222 self .nb .metadata ['widgets' ] = {mimetype : widgets }
216223
217- # %% ../nbs/api/10_processors.ipynb 42
224+ # %% ../nbs/api/10_processors.ipynb 44
218225def _import_obj (s ):
219226 mod_nm , obj_nm = s .split (':' )
220227 mod = importlib .import_module (mod_nm )
221228 return getattr (mod , obj_nm )
222229
223- # %% ../nbs/api/10_processors.ipynb 43
230+ # %% ../nbs/api/10_processors.ipynb 45
224231class FilterDefaults :
225232 "Override `FilterDefaults` to change which notebook processors are used"
226233 def xtra_procs (self ):
@@ -230,7 +237,7 @@ def xtra_procs(self):
230237 def base_procs (self ):
231238 return [FrontmatterProc , populate_language , add_show_docs , insert_warning ,
232239 strip_ansi , hide_line , filter_stream_ , rm_header_dash ,
233- clean_show_doc , exec_show_docs , rm_export , clean_magics , hide_ , add_links , strip_hidden_metadata ]
240+ clean_show_doc , exec_show_docs , rm_export , clean_magics , hide_ , add_links , add_fold , strip_hidden_metadata ]
234241
235242 def procs (self ):
236243 "Processors for export"
0 commit comments