Skip to content

Commit

Permalink
Enhance doclinks to handle function calls and improve symbol detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoop57 committed Nov 19, 2024
1 parent 2a66459 commit 9ca103f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion nbdev/doclinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,13 @@ def code(self, sym):
_,py,gh = res
line = _lineno(sym, py)
return f'{gh}#L{line}'

def _link_sym(self, m):
l = m.group(1)
s = self.doc(l)
if s is None: return m.group(0)
l = l.replace('\\', r'\\')
if m.group(0).endswith('()`'): l += '()'
return rf"[`{l}`]({s})"

def link_line(self, l): return _re_backticks.sub(self._link_sym, l)
Expand Down
35 changes: 24 additions & 11 deletions nbs/api/05_doclinks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,12 @@
"test_eq(_re_backticks.findall('no match'), [])\n",
"test_eq(_re_backticks.findall('`weird``thing`'), ['weird', 'thing'])\n",
"test_eq(_re_backticks.findall('`spaces not allowed `'), [])\n",
"test_eq(_re_backticks.findall('`sym.with.dots`'), ['sym.with.dots'])"
"test_eq(_re_backticks.findall('`sym.with.dots`'), ['sym.with.dots'])\n",
"test_eq(_re_backticks.findall('unicode `αβγ`'), ['αβγ'])\n",
"test_eq(_re_backticks.findall('long `extremely.long.module.name.with.many.dots.function`'), \n",
" ['extremely.long.module.name.with.many.dots.function'])\n",
"test_eq(_re_backticks.findall('special chars `foo[bar]` `foo$bar`'), ['foo[bar]', 'foo$bar'])\n",
"test_eq(_re_backticks.findall('consecutive `foo``bar``baz`'), ['foo', 'bar', 'baz'])"
]
},
{
Expand Down Expand Up @@ -533,12 +538,13 @@
" _,py,gh = res\n",
" line = _lineno(sym, py)\n",
" return f'{gh}#L{line}'\n",
"\n",
" \n",
" def _link_sym(self, m):\n",
" l = m.group(1)\n",
" s = self.doc(l)\n",
" if s is None: return m.group(0)\n",
" l = l.replace('\\\\', r'\\\\')\n",
" if m.group(0).endswith('()`'): l += '()'\n",
" return rf\"[`{l}`]({s})\"\n",
"\n",
" def link_line(self, l): return _re_backticks.sub(self._link_sym, l)\n",
Expand All @@ -565,14 +571,6 @@
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/nathan/miniconda3/envs/main/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
},
{
"data": {
"text/plain": [
Expand Down Expand Up @@ -664,6 +662,7 @@
"outputs": [],
"source": [
"assert c.doc('numpy.array').startswith('http')\n",
"assert not c.doc('numpy.Array')\n",
"assert c.doc('NbdevLookup').endswith('#nbdevlookup')\n",
"assert not c.doc('array')"
]
Expand Down Expand Up @@ -804,7 +803,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"This is a link to [`numpy.array`](https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array) and to [`get_config`](https://nbdev.fast.ai/api/config.html#get_config) but not a link to `foobar`.\n",
"This is a link to [`numpy.array`](https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array) and to [`get_config()`](https://nbdev.fast.ai/api/config.html#get_config) but not a link to `foobar`.\n",
"And not a link to <code>dict2nb</code>.\n",
"\n",
" This is not a link to `get_config`\n",
Expand All @@ -820,6 +819,20 @@
"print(NbdevLookup('nbdev').linkify(md))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test code blocks\n",
"md = \"\"\"```python\n",
"def foo():\n",
" return `bar`\n",
"```\"\"\"\n",
"assert NbdevLookup().linkify(md) == md"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 9ca103f

Please sign in to comment.