Skip to content

Commit 9e559ad

Browse files
authored
Merge pull request #1057 from seeM/fix-1032
set `output-file` using notebook filename instead of `default_exp`
2 parents f57e54e + 5e79159 commit 9e559ad

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

nbdev/_modidx.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
'nbdev.doclinks._get_exps': ('api/doclinks.html#_get_exps', 'nbdev/doclinks.py'),
5959
'nbdev.doclinks._get_modidx': ('api/doclinks.html#_get_modidx', 'nbdev/doclinks.py'),
6060
'nbdev.doclinks._lineno': ('api/doclinks.html#_lineno', 'nbdev/doclinks.py'),
61+
'nbdev.doclinks._nbpath2html': ('api/doclinks.html#_nbpath2html', 'nbdev/doclinks.py'),
6162
'nbdev.doclinks._qual_mod': ('api/doclinks.html#_qual_mod', 'nbdev/doclinks.py'),
6263
'nbdev.doclinks._qual_sym': ('api/doclinks.html#_qual_sym', 'nbdev/doclinks.py'),
6364
'nbdev.doclinks._qual_syms': ('api/doclinks.html#_qual_syms', 'nbdev/doclinks.py'),
@@ -76,8 +77,6 @@
7677
'nbdev.export.nb_export': ('api/export.html#nb_export', 'nbdev/export.py')},
7778
'nbdev.extract_attachments': {},
7879
'nbdev.frontmatter': { 'nbdev.frontmatter.FrontmatterProc': ('api/frontmatter.html#frontmatterproc', 'nbdev/frontmatter.py'),
79-
'nbdev.frontmatter.FrontmatterProc._default_exp_': ( 'api/frontmatter.html#frontmatterproc._default_exp_',
80-
'nbdev/frontmatter.py'),
8180
'nbdev.frontmatter.FrontmatterProc._update': ( 'api/frontmatter.html#frontmatterproc._update',
8281
'nbdev/frontmatter.py'),
8382
'nbdev.frontmatter.FrontmatterProc.begin': ( 'api/frontmatter.html#frontmatterproc.begin',

nbdev/doclinks.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def patch_name(o):
4343
return _sym_nm(a,o)
4444

4545
# %% ../nbs/api/doclinks.ipynb 9
46+
def _nbpath2html(p): return p.with_name(re.sub(r'\d+[a-zA-Z0-9]*_', '', p.name.lower())).with_suffix('.html')
47+
48+
# %% ../nbs/api/doclinks.ipynb 11
4649
def _get_modidx(pyfile, code_root, nbs_path):
4750
"Get module symbol index for a Python source file"
4851
cfg = get_config()
@@ -56,9 +59,8 @@ def _get_modidx(pyfile, code_root, nbs_path):
5659
top,*rest = cell.splitlines() # First line is cell header
5760
nb = top.split()[0]
5861
if nb != 'auto':
59-
nbpath = ((pyfile.parent)/nb).resolve() # NB paths are stored relative to .py file
60-
nbpath = nbpath.with_name(re.sub(r'\d+[a-zA-Z0-9]*_', '', nbpath.name.lower()))
61-
loc = nbpath.relative_to(nbs_path).with_suffix('.html')
62+
nbpath = (pyfile.parent/nb).resolve() # NB paths are stored relative to .py file
63+
loc = _nbpath2html(nbpath.relative_to(nbs_path))
6264

6365
def _stor(nm):
6466
for n in L(nm): d[f'{mod_name}.{n}'] = f'{loc.as_posix()}#{n.lower()}',rel_name
@@ -69,7 +71,7 @@ def _stor(nm):
6971
if isinstance(t2, _def_types): _stor(f'{tree.name}.{t2.name}')
7072
return {mod_name: d}
7173

72-
# %% ../nbs/api/doclinks.ipynb 10
74+
# %% ../nbs/api/doclinks.ipynb 12
7375
def _build_modidx(dest=None, nbs_path=None, skip_exists=False):
7476
"Create _modidx.py"
7577
if dest is None: dest = get_config().lib_path
@@ -87,7 +89,7 @@ def _build_modidx(dest=None, nbs_path=None, skip_exists=False):
8789
res['syms'].update(_get_modidx((dest.parent/file).resolve(), code_root, nbs_path=nbs_path))
8890
idxfile.write_text("# Autogenerated by nbdev\n\nd = "+pformat(res, width=140, indent=2, compact=True))
8991

90-
# %% ../nbs/api/doclinks.ipynb 15
92+
# %% ../nbs/api/doclinks.ipynb 17
9193
@delegates(globtastic)
9294
def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', skip_file_re='^[_.]', key='nbs_path', as_path=False, **kwargs):
9395
"Find all files in a directory matching an extension given a config key."
@@ -97,7 +99,7 @@ def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', skip_file_r
9799
skip_file_re=skip_file_re, recursive=recursive, **kwargs)
98100
return res.map(Path) if as_path else res
99101

100-
# %% ../nbs/api/doclinks.ipynb 16
102+
# %% ../nbs/api/doclinks.ipynb 18
101103
def nbglob_cli(
102104
path:str=None, # Path to notebooks
103105
symlinks:bool=False, # Follow symlinks?
@@ -111,7 +113,7 @@ def nbglob_cli(
111113
return nbglob(path, symlinks=symlinks, file_glob=file_glob, file_re=file_re, folder_re=folder_re,
112114
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, skip_folder_re=skip_folder_re)
113115

114-
# %% ../nbs/api/doclinks.ipynb 17
116+
# %% ../nbs/api/doclinks.ipynb 19
115117
@call_parse
116118
@delegates(nbglob_cli)
117119
def nbdev_export(
@@ -124,11 +126,11 @@ def nbdev_export(
124126
add_init(get_config().lib_path)
125127
_build_modidx()
126128

127-
# %% ../nbs/api/doclinks.ipynb 19
129+
# %% ../nbs/api/doclinks.ipynb 21
128130
import importlib,ast
129131
from functools import lru_cache
130132

131-
# %% ../nbs/api/doclinks.ipynb 20
133+
# %% ../nbs/api/doclinks.ipynb 22
132134
def _find_mod(mod):
133135
mp,_,mr = mod.partition('/')
134136
spec = importlib.util.find_spec(mp)
@@ -151,7 +153,7 @@ def _get_exps(mod):
151153

152154
def _lineno(sym, fname): return _get_exps(fname).get(sym, None) if fname else None
153155

154-
# %% ../nbs/api/doclinks.ipynb 22
156+
# %% ../nbs/api/doclinks.ipynb 24
155157
def _qual_sym(s, settings):
156158
if not isinstance(s,tuple): return s
157159
nb,py = s
@@ -166,10 +168,10 @@ def _qual_syms(entries):
166168
if 'doc_host' not in settings: return entries
167169
return {'syms': {mod:_qual_mod(d, settings) for mod,d in entries['syms'].items()}, 'settings':settings}
168170

169-
# %% ../nbs/api/doclinks.ipynb 23
171+
# %% ../nbs/api/doclinks.ipynb 25
170172
_re_backticks = re.compile(r'`([^`\s]+)`')
171173

172-
# %% ../nbs/api/doclinks.ipynb 24
174+
# %% ../nbs/api/doclinks.ipynb 26
173175
@lru_cache(None)
174176
class NbdevLookup:
175177
"Mapping from symbol names to docs and source URLs"

nbdev/frontmatter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# %% ../nbs/api/frontmatter.ipynb 2
77
from .imports import *
88
from .process import *
9+
from .doclinks import _nbpath2html
910

1011
from execnb.nbio import *
1112
from fastcore.imports import *
@@ -46,8 +47,6 @@ def _insertfm(nb, fm): nb.cells.insert(0, mk_cell(_dict2fm(fm), 'raw'))
4647
class FrontmatterProc(Processor):
4748
"A YAML and formatted-markdown frontmatter processor"
4849
def begin(self): self.fm = getattr(self.nb, 'frontmatter_', {})
49-
50-
def _default_exp_(self, cell, exp): self.default_exp = exp
5150

5251
def _update(self, f, cell):
5352
s = cell.get('source')
@@ -64,6 +63,5 @@ def cell(self, cell):
6463
def end(self):
6564
self.nb.frontmatter_ = self.fm
6665
if not self.fm: return
67-
exp = getattr(self, 'default_exp', None)
68-
if exp: self.fm.update({'output-file': exp+'.html'})
66+
self.fm.update({'output-file': _nbpath2html(Path(self.nb.path_)).name})
6967
_insertfm(self.nb, self.fm)

nbs/api/doclinks.ipynb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@
124124
"test_eq(_test_patch(s), ['_T._f', '_U._f', '_V._f'])"
125125
]
126126
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"#|export\n",
134+
"def _nbpath2html(p): return p.with_name(re.sub(r'\\d+[a-zA-Z0-9]*_', '', p.name.lower())).with_suffix('.html')"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"#|hide\n",
144+
"test_eq(_nbpath2html(Path('foo/bar/012abc_my.notebook.ipynb')), Path('foo/bar/my.notebook.html'))"
145+
]
146+
},
127147
{
128148
"cell_type": "code",
129149
"execution_count": null,
@@ -144,9 +164,8 @@
144164
" top,*rest = cell.splitlines() # First line is cell header\n",
145165
" nb = top.split()[0]\n",
146166
" if nb != 'auto':\n",
147-
" nbpath = ((pyfile.parent)/nb).resolve() # NB paths are stored relative to .py file\n",
148-
" nbpath = nbpath.with_name(re.sub(r'\\d+[a-zA-Z0-9]*_', '', nbpath.name.lower()))\n",
149-
" loc = nbpath.relative_to(nbs_path).with_suffix('.html')\n",
167+
" nbpath = (pyfile.parent/nb).resolve() # NB paths are stored relative to .py file\n",
168+
" loc = _nbpath2html(nbpath.relative_to(nbs_path))\n",
150169
"\n",
151170
" def _stor(nm):\n",
152171
" for n in L(nm): d[f'{mod_name}.{n}'] = f'{loc.as_posix()}#{n.lower()}',rel_name\n",

nbs/api/frontmatter.ipynb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"#|export\n",
3232
"from nbdev.imports import *\n",
3333
"from nbdev.process import *\n",
34+
"from nbdev.doclinks import _nbpath2html\n",
3435
"\n",
3536
"from execnb.nbio import *\n",
3637
"from fastcore.imports import *\n",
@@ -109,8 +110,6 @@
109110
"class FrontmatterProc(Processor):\n",
110111
" \"A YAML and formatted-markdown frontmatter processor\"\n",
111112
" def begin(self): self.fm = getattr(self.nb, 'frontmatter_', {})\n",
112-
" \n",
113-
" def _default_exp_(self, cell, exp): self.default_exp = exp\n",
114113
"\n",
115114
" def _update(self, f, cell):\n",
116115
" s = cell.get('source')\n",
@@ -127,8 +126,7 @@
127126
" def end(self):\n",
128127
" self.nb.frontmatter_ = self.fm\n",
129128
" if not self.fm: return\n",
130-
" exp = getattr(self, 'default_exp', None)\n",
131-
" if exp: self.fm.update({'output-file': exp+'.html'})\n",
129+
" self.fm.update({'output-file': _nbpath2html(Path(self.nb.path_)).name})\n",
132130
" _insertfm(self.nb, self.fm)"
133131
]
134132
},
@@ -183,7 +181,7 @@
183181
" echo: false\n",
184182
"key1: value1\n",
185183
"key2: value2\n",
186-
"output-file: foobar.html\n",
184+
"output-file: docs_test.html\n",
187185
"title: a title\n",
188186
"\n",
189187
"---\n",
@@ -221,7 +219,7 @@
221219
" 'key1': 'value1',\n",
222220
" 'key2': 'value2',\n",
223221
" 'categories': ['c1', 'c2'],\n",
224-
" 'output-file': 'foobar.html'}"
222+
" 'output-file': 'docs_test.html'}"
225223
]
226224
},
227225
"execution_count": null,
@@ -244,7 +242,7 @@
244242
"#|hide\n",
245243
"test_eq(d['description'], 'A description')\n",
246244
"test_eq(d['categories'], ['c1','c2'])\n",
247-
"test_eq(d['output-file'], 'foobar.html')"
245+
"test_eq(d['output-file'], 'docs_test.html')"
248246
]
249247
},
250248
{

nbs/api/migrate.ipynb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
" and provides utilities that enhance productivity.\n",
306306
"draft: 'true'\n",
307307
"image: fastcore_imgs/td.png\n",
308+
"output-file: 2020-09-01-fastcore.html\n",
308309
"permalink: /fastcore/\n",
309310
"search: 'false'\n",
310311
"title: 'fastcore: An Underrated Python Library'\n",
@@ -343,6 +344,7 @@
343344
" and provides utilities that enhance productivity.\n",
344345
"draft: 'true'\n",
345346
"image: fastcore_imgs/td.png\n",
347+
"output-file: 2020-09-01-fastcore.html\n",
346348
"permalink: /fastcore/\n",
347349
"search: 'false'\n",
348350
"title: 'fastcore: An Underrated Python Library'\n",
@@ -361,6 +363,7 @@
361363
"- jupyter\n",
362364
"description: A tutorial of fastpages for Jupyter notebooks.\n",
363365
"image: images/chart-preview.png\n",
366+
"output-file: 2020-02-20-test.html\n",
364367
"title: Fastpages Notebook Blog Post\n",
365368
"toc: true\n",
366369
"\n",

0 commit comments

Comments
 (0)