Skip to content

Commit

Permalink
Merge pull request #1469 from ncoop57/regex
Browse files Browse the repository at this point in the history
Improve backtick regex matching for function names
  • Loading branch information
jph00 authored Nov 19, 2024
2 parents b263f6b + 9ca103f commit ccdc0b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
5 changes: 3 additions & 2 deletions nbdev/doclinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _qual_syms(entries):
return {'syms': {mod:_qual_mod(d, settings) for mod,d in entries['syms'].items()}, 'settings':settings}

# %% ../nbs/api/05_doclinks.ipynb
_re_backticks = re.compile(r'`([^`\s]+)`')
_re_backticks = re.compile(r'`([^`\s]+?)(?:\(\))?`')

# %% ../nbs/api/05_doclinks.ipynb
@lru_cache(None)
Expand Down 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
57 changes: 47 additions & 10 deletions nbs/api/05_doclinks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,28 @@
"outputs": [],
"source": [
"#|export\n",
"_re_backticks = re.compile(r'`([^`\\s]+)`')"
"_re_backticks = re.compile(r'`([^`\\s]+?)(?:\\(\\))?`')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#|hide\n",
"test_eq(_re_backticks.findall('test `foo` here'), ['foo'])\n",
"test_eq(_re_backticks.findall('test `foo()` here'), ['foo'])\n",
"test_eq(_re_backticks.findall('multiple `foo` and `bar()` matches'), ['foo', 'bar'])\n",
"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'])\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 @@ -517,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 Down Expand Up @@ -577,7 +599,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L226){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L227){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.doc\n",
"\n",
Expand All @@ -588,7 +610,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L226){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L227){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.doc\n",
"\n",
Expand Down Expand Up @@ -640,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 @@ -671,7 +694,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L231){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L232){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.code\n",
"\n",
Expand All @@ -682,7 +705,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L231){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L232){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.code\n",
"\n",
Expand Down Expand Up @@ -730,7 +753,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L248){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L249){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.linkify\n",
"\n",
Expand All @@ -739,7 +762,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L248){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#L249){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.linkify\n",
"\n",
Expand All @@ -761,7 +784,7 @@
"metadata": {},
"outputs": [],
"source": [
"md = \"\"\"This is a link to `numpy.array` and to `get_config` but not a link to `foobar`.\n",
"md = \"\"\"This is a link to `numpy.array` and to `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 @@ -780,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 @@ -796,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 ccdc0b6

Please sign in to comment.