Skip to content

Commit

Permalink
Prefix partially qualified identifiers with "."
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Jul 9, 2023
1 parent ac61716 commit dc1b8e2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
11 changes: 7 additions & 4 deletions pdoc/render_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,13 @@ def possible_sources(
yield identifier, ""
return

if len(set(s.partition(".")[0] for s in all_modules)) == 1:
# handle relative identifiers
if identifier.startswith("."):
pkgname = next(iter(all_modules)).partition(".")[0]
if not identifier.startswith(pkgname):
identifier = f"{pkgname}.{identifier}"
# check that all modules reside within the same top-level module
if all(s.startswith(pkgname + ".") or s == pkgname for s in all_modules):
if not identifier.startswith(pkgname):
identifier = f"{pkgname}{identifier}"

modulename = identifier
qualname = None
Expand Down Expand Up @@ -342,7 +345,7 @@ def linkify_repl(m: re.Match):
r"""
# Part 1: foo.bar or foo.bar() (without backticks)
(?<![/=?#&]) # heuristic: not part of a URL
\b
(?:\.|\b)
# First part of the identifier (e.g. "foo")
(?!\d)[a-zA-Z0-9_]+
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/demopackage.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ <h1 class="modulename">
</span></pre></div>


<div class="docstring"><p>This class is defined in .child_c and inherits from .<a href="#B">demopackage.B</a></p>
<div class="docstring"><p>This class is defined in .child_c and inherits from <a href="#B">demopackage.B</a></p>
</div>


Expand Down
6 changes: 3 additions & 3 deletions test/testdata/single_module_links.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h1 class="modulename">
</span><span id="L-6"><a href="#L-6"><span class="linenos"> 6</span></a><span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
</span><span id="L-7"><a href="#L-7"><span class="linenos"> 7</span></a><span class="sd"> Test that submodules are linked to when documenting a single module.</span>
</span><span id="L-8"><a href="#L-8"><span class="linenos"> 8</span></a>
</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Do `helpers.something` with a `types.BigThing`</span>
</span><span id="L-9"><a href="#L-9"><span class="linenos"> 9</span></a><span class="sd"> Do `.helpers.something` with a `.types.BigThing`</span>
</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a><span class="sd"> &#39;&#39;&#39;</span>
</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a> <span class="n">something</span><span class="p">()</span>
</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a> <span class="n">BigThing</span><span class="p">()</span>
Expand All @@ -85,7 +85,7 @@ <h1 class="modulename">
</span><span id="abc-7"><a href="#abc-7"><span class="linenos"> 7</span></a><span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
</span><span id="abc-8"><a href="#abc-8"><span class="linenos"> 8</span></a><span class="sd"> Test that submodules are linked to when documenting a single module.</span>
</span><span id="abc-9"><a href="#abc-9"><span class="linenos"> 9</span></a>
</span><span id="abc-10"><a href="#abc-10"><span class="linenos">10</span></a><span class="sd"> Do `helpers.something` with a `types.BigThing`</span>
</span><span id="abc-10"><a href="#abc-10"><span class="linenos">10</span></a><span class="sd"> Do `.helpers.something` with a `.types.BigThing`</span>
</span><span id="abc-11"><a href="#abc-11"><span class="linenos">11</span></a><span class="sd"> &#39;&#39;&#39;</span>
</span><span id="abc-12"><a href="#abc-12"><span class="linenos">12</span></a> <span class="n">something</span><span class="p">()</span>
</span><span id="abc-13"><a href="#abc-13"><span class="linenos">13</span></a> <span class="n">BigThing</span><span class="p">()</span>
Expand All @@ -94,7 +94,7 @@ <h1 class="modulename">

<div class="docstring"><p>Test that submodules are linked to when documenting a single module.</p>

<p>Do <code><a href="single_module_links/helpers.html#something">helpers.something</a></code> with a <code><a href="single_module_links/types.html#BigThing">types.BigThing</a></code></p>
<p>Do <code><a href="single_module_links/helpers.html#something">.helpers.something</a></code> with a <code><a href="single_module_links/types.html#BigThing">.types.BigThing</a></code></p>
</div>


Expand Down
2 changes: 1 addition & 1 deletion test/testdata/single_module_links/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def abc():
'''
Test that submodules are linked to when documenting a single module.
Do `helpers.something` with a `types.BigThing`
Do `.helpers.something` with a `.types.BigThing`
'''
something()
BigThing()

0 comments on commit dc1b8e2

Please sign in to comment.