11# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/09_processors.ipynb.
22
33# %% auto 0
4- __all__ = ['DEFAULT_FM_KEYS' , 'cell_lang' , 'add_links' , 'strip_ansi' , 'strip_hidden_metadata' , 'hide_' , 'hide_line' ,
4+ __all__ = ['DEFAULT_FM_KEYS' , 'nbflags_' , ' cell_lang' , 'add_links' , 'strip_ansi' , 'strip_hidden_metadata' , 'hide_' , 'hide_line' ,
55 'filter_stream_' , 'clean_magics' , 'lang_identify' , 'rm_header_dash' , 'rm_export' , 'clean_show_doc' ,
66 'exec_show_docs' , 'populate_language' , 'insert_warning' , 'add_show_docs' , 'is_frontmatter' , 'nb_fmdict' ,
77 'construct_fm' , 'insert_frontmatter' , 'infer_frontmatter' ]
2222import sys
2323
2424# %% ../nbs/09_processors.ipynb 9
25+ def nbflags_ (nbp , cell , * args ):
26+ "Hide cell from output"
27+ nbp .nb ._nbflags = args
28+
29+ # %% ../nbs/09_processors.ipynb 11
2530def cell_lang (cell ): return nested_attr (cell , 'metadata.language' , 'python' )
2631
2732def add_links (cell ):
@@ -32,25 +37,25 @@ def add_links(cell):
3237 if hasattr (o , 'data' ) and hasattr (o ['data' ], 'text/markdown' ):
3338 o .data ['text/markdown' ] = [nl .link_line (s ) for s in o .data ['text/markdown' ]]
3439
35- # %% ../nbs/09_processors.ipynb 12
40+ # %% ../nbs/09_processors.ipynb 14
3641_re_ansi_escape = re .compile (r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])' )
3742
3843def strip_ansi (cell ):
3944 "Strip Ansi Characters."
4045 for outp in cell .get ('outputs' , []):
4146 if outp .get ('name' )== 'stdout' : outp ['text' ] = [_re_ansi_escape .sub ('' , o ) for o in outp .text ]
4247
43- # %% ../nbs/09_processors.ipynb 14
48+ # %% ../nbs/09_processors.ipynb 16
4449def strip_hidden_metadata (cell ):
4550 '''Strips "hidden" metadata property from code cells so it doesn't interfere with docs rendering'''
4651 if cell .cell_type == 'code' and 'metadata' in cell : cell .metadata .pop ('hidden' ,None )
4752
48- # %% ../nbs/09_processors.ipynb 16
53+ # %% ../nbs/09_processors.ipynb 17
4954def hide_ (nbp , cell ):
5055 "Hide cell from output"
5156 del (cell ['source' ])
5257
53- # %% ../nbs/09_processors.ipynb 18
58+ # %% ../nbs/09_processors.ipynb 19
5459def _re_hideline (lang = None ): return re .compile (fr'{ langs [lang ]} \|\s*hide_line\s*$' , re .MULTILINE )
5560
5661def hide_line (cell ):
@@ -59,22 +64,22 @@ def hide_line(cell):
5964 if cell .cell_type == 'code' and _re_hideline (lang ).search (cell .source ):
6065 cell .source = '\n ' .join ([c for c in cell .source .splitlines () if not _re_hideline (lang ).search (c )])
6166
62- # %% ../nbs/09_processors.ipynb 20
67+ # %% ../nbs/09_processors.ipynb 21
6368def filter_stream_ (nbp , cell , * words ):
6469 "Remove output lines containing any of `words` in `cell` stream output"
6570 if not words : return
6671 for outp in cell .get ('outputs' , []):
6772 if outp .output_type == 'stream' :
6873 outp ['text' ] = [l for l in outp .text if not re .search ('|' .join (words ), l )]
6974
70- # %% ../nbs/09_processors.ipynb 22
75+ # %% ../nbs/09_processors.ipynb 23
7176_magics_pattern = re .compile (r'^\s*(%%|%).*' , re .MULTILINE )
7277
7378def clean_magics (cell ):
7479 "A preprocessor to remove cell magic commands"
7580 if cell .cell_type == 'code' : cell .source = _magics_pattern .sub ('' , cell .source ).strip ()
7681
77- # %% ../nbs/09_processors.ipynb 24
82+ # %% ../nbs/09_processors.ipynb 25
7883_langs = 'bash|html|javascript|js|latex|markdown|perl|ruby|sh|svg'
7984_lang_pattern = re .compile (rf'^\s*%%\s*({ _langs } )\s*$' , flags = re .MULTILINE )
8085
@@ -84,7 +89,7 @@ def lang_identify(cell):
8489 lang = _lang_pattern .findall (cell .source )
8590 if lang : cell .metadata .language = lang [0 ]
8691
87- # %% ../nbs/09_processors.ipynb 27
92+ # %% ../nbs/09_processors.ipynb 28
8893_re_hdr_dash = re .compile (r'^#+\s+.*\s+-\s*$' , re .MULTILINE )
8994
9095def rm_header_dash (cell ):
@@ -93,15 +98,15 @@ def rm_header_dash(cell):
9398 src = cell .source .strip ()
9499 if cell .cell_type == 'markdown' and src .startswith ('#' ) and src .endswith (' -' ): del (cell ['source' ])
95100
96- # %% ../nbs/09_processors.ipynb 29
101+ # %% ../nbs/09_processors.ipynb 30
97102_hide_dirs = {'export' ,'exporti' , 'hide' ,'default_exp' }
98103
99104def rm_export (cell ):
100105 "Remove cells that are exported or hidden"
101106 if cell .directives_ :
102107 if cell .directives_ .keys () & _hide_dirs : del (cell ['source' ])
103108
104- # %% ../nbs/09_processors.ipynb 31
109+ # %% ../nbs/09_processors.ipynb 32
105110_re_showdoc = re .compile (r'^show_doc' , re .MULTILINE )
106111def _is_showdoc (cell ): return cell ['cell_type' ] == 'code' and _re_showdoc .search (cell .source )
107112
@@ -110,7 +115,7 @@ def clean_show_doc(cell):
110115 if not _is_showdoc (cell ): return
111116 cell .source = '#| echo: false\n ' + cell .source
112117
113- # %% ../nbs/09_processors.ipynb 32
118+ # %% ../nbs/09_processors.ipynb 33
114119_imps = {ast .Import , ast .ImportFrom }
115120
116121def _show_docs (trees ):
@@ -126,47 +131,50 @@ def _do_eval(cell):
126131 if cell .directives_ .keys () & _show_dirs or filter_ex (trees , risinstance (_imps )): return True
127132 if _show_docs (trees ): return True
128133
129- # %% ../nbs/09_processors.ipynb 33
134+ # %% ../nbs/09_processors.ipynb 34
130135class exec_show_docs :
131136 "Execute cells needed for `show_docs` output, including exported cells and imports"
132137 def __init__ (self , nb ):
133138 self .k = CaptureShell ()
134139 if nb_lang (nb ) == 'python' : self .k .run_cell ('from nbdev.showdoc import show_doc' )
135140
136141 def __call__ (self , cell ):
142+ flags = getattr (cell .nb , '_nbflags' , [])
143+ if 'skip_showdoc' in flags : return
137144 if _do_eval (cell ): self .k .cell (cell )
138145 if self .k .exc :
139146 sys .stderr .write (f'Error in cell { cell .idx_ } :\n { cell .source } ' )
140147 raise self .k .exc [1 ] from None
141148
142- # %% ../nbs/09_processors.ipynb 36
149+ # %% ../nbs/09_processors.ipynb 37
143150def populate_language (nb ):
144151 "Insert cell language indicator based on notebook metadata. You should to use this before `lang_identify`"
145152 for cell in nb .cells :
146153 if cell .cell_type == 'code' : cell .metadata .language = nb_lang (nb )
147154
148- # %% ../nbs/09_processors.ipynb 39
155+ # %% ../nbs/09_processors.ipynb 40
149156def insert_warning (nb ):
150157 "Insert Autogenerated Warning Into Notebook after the first cell."
151158 content = "<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->"
152- nb .cells .insert (1 , mk_cell (content , False ))
159+ nb .cells .insert (1 , mk_cell (content , 'markdown' ))
153160
154- # %% ../nbs/09_processors.ipynb 43
161+ # %% ../nbs/09_processors.ipynb 44
155162_def_types = (ast .FunctionDef ,ast .AsyncFunctionDef ,ast .ClassDef )
156163def _def_names (cell , shown ):
157164 return [showdoc_nm (o ) for o in concat (cell .parsed_ ())
158165 if isinstance (o ,_def_types ) and o .name not in shown and o .name [0 ]!= '_' ]
159166
160- _re_exps = re .compile (r'^\s*#\|\s*(?:export|exports)' ).search
161-
162167def _get_nm (tree ):
163168 i = tree .value .args [0 ]
164169 return f'{ i .value .id } .{ i .attr } ' if isinstance (i , ast .Attribute ) else i .id
165170
166- # %% ../nbs/09_processors.ipynb 44
171+ # %% ../nbs/09_processors.ipynb 45
167172def add_show_docs (nb ):
168173 "Add show_doc cells after exported cells, unless they are already documented"
169- exports = L (cell for cell in nb .cells if cell .source and _re_exps (cell .source ))
174+ def _want (c ):
175+ return c .source and c .cell_type == 'code' and ('export' in c .directives_ or 'exports' in c .directives_ )
176+
177+ exports = L (cell for cell in nb .cells if _want (cell ))
170178 trees = nb .cells .map (NbCell .parsed_ ).concat ()
171179 shown_docs = {_get_nm (t ) for t in _show_docs (trees )}
172180 for cell in reversed (exports ):
@@ -175,7 +183,7 @@ def add_show_docs(nb):
175183 for nm in _def_names (cell , shown_docs ):
176184 nb .cells .insert (cell .idx_ + 1 , mk_cell (f'show_doc({ nm } )' ))
177185
178- # %% ../nbs/09_processors.ipynb 48
186+ # %% ../nbs/09_processors.ipynb 49
179187_re_title = re .compile (r'^#\s+(.*)[\n\r]+(?:^>\s+(.*))?' , flags = re .MULTILINE )
180188_re_fm = re .compile (r'^---.*\S+.*---' , flags = re .DOTALL )
181189_re_defaultexp = re .compile (r'^\s*#\|\s*default_exp\s+(\S+)' , flags = re .MULTILINE )
@@ -186,14 +194,14 @@ def _istitle(cell):
186194 txt = cell .get ('source' , '' )
187195 return bool (_re_title .search (txt )) if txt else False
188196
189- # %% ../nbs/09_processors.ipynb 49
197+ # %% ../nbs/09_processors.ipynb 50
190198def _default_exp (nb ):
191199 "get the default_exp from a notebook"
192200 code_src = nb .cells .filter (lambda x : x .cell_type == 'code' ).attrgot ('source' )
193201 default_exp = first (code_src .filter ().map (_re_defaultexp .search ).filter ())
194202 return default_exp .group (1 ) if default_exp else None
195203
196- # %% ../nbs/09_processors.ipynb 51
204+ # %% ../nbs/09_processors.ipynb 52
197205def nb_fmdict (nb , remove = True ):
198206 "Infer the front matter from a notebook's markdown formatting"
199207 md_cells = _celltyp (nb , 'markdown' ).filter (_istitle )
@@ -209,21 +217,21 @@ def nb_fmdict(nb, remove=True):
209217 return flags
210218 else : return {}
211219
212- # %% ../nbs/09_processors.ipynb 54
220+ # %% ../nbs/09_processors.ipynb 55
213221DEFAULT_FM_KEYS = ['title' , 'description' , 'author' , 'image' , 'categories' , 'output-file' , 'aliases' ]
214222
215223def construct_fm (fmdict :dict , keys = DEFAULT_FM_KEYS ):
216224 "construct front matter from a dictionary, but only for `keys`"
217225 if not fmdict : return None
218226 return '---\n ' + '\n ' .join ([f"{ k } : { fmdict [k ]} " for k in keys if k in fmdict ])+ '\n ---'
219227
220- # %% ../nbs/09_processors.ipynb 56
228+ # %% ../nbs/09_processors.ipynb 57
221229def insert_frontmatter (nb , fm_dict :dict , filter_keys :list = DEFAULT_FM_KEYS ):
222230 "Add frontmatter into notebook based on `filter_keys` that exist in `fmdict`."
223231 fm = construct_fm (fm_dict , keys = filter_keys )
224- if fm : nb .cells .insert (0 , NbCell (0 , dict (cell_type = 'raw' , metadata = {}, source = fm )))
232+ if fm : nb .cells .insert (0 , NbCell (0 , dict (cell_type = 'raw' , metadata = {}, source = fm , directives_ = {} )))
225233
226- # %% ../nbs/09_processors.ipynb 57
234+ # %% ../nbs/09_processors.ipynb 58
227235def infer_frontmatter (nb ):
228236 "Insert front matter if it doesn't exist automatically from nbdev styled markdown."
229237 if is_frontmatter (nb ): return
0 commit comments