Skip to content

Commit

Permalink
Create an actual Reference object
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlatr committed Feb 1, 2025
1 parent a1a72a7 commit 972993d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pydoctor/epydoc2stan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,8 @@ def __init__(self, document:nodes.document,
def _transform(self, node:nodes.title_reference) -> None:
ctx = self.ctx
module = self.module
_, target = parse_reference(node)
ref = parse_reference(node)
target = ref.target
# we're setting two attributes here: 'refuri' and 'rawtarget'.
# 'refuri' might already be created by the colorizer or docstring parser,
# but 'rawtarget' is only created from within this transform, so we can
Expand Down Expand Up @@ -1301,7 +1302,7 @@ def _transform(self, node:nodes.title_reference) -> None:
# If we're dealing with an annotation, give precedence to the module's
# lookup (wrt PEP 563)
lookup_context = module
linker.warn_ambiguous_annotation(module, ctx, target)

# save pre-resolved refuri
attribs['refuri'] = '.'.join(chain(lookup_context.expandName(name).split('.'), rest))

Expand Down
16 changes: 13 additions & 3 deletions pydoctor/node2stan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from docutils import nodes, frontend, __version_info__ as docutils_version_info

from twisted.web.template import Tag
import attr

if TYPE_CHECKING:
from twisted.web.template import Flattenable
from pydoctor.epydoc.markup import DocstringLinker
Expand Down Expand Up @@ -58,7 +60,13 @@ def gettext(node: Union[nodes.Node, List[nodes.Node]]) -> List[str]:
filtered.extend(gettext(child))
return filtered

def parse_reference(node:nodes.title_reference) -> Tuple[Union[str, Sequence[nodes.Node]], str]:
@attr.s(auto_attribs=True)
class Reference:
label: str | Sequence[nodes.Node]
target: str
rawtarget: str

def parse_reference(node:nodes.title_reference) -> Reference:
"""
Split a reference into (label, target).
"""
Expand All @@ -76,7 +84,7 @@ def parse_reference(node:nodes.title_reference) -> Tuple[Union[str, Sequence[nod
# Support linking to functions and methods with () at the end
if target.endswith('()'):
target = target[:len(target)-2]
return label, target
return Reference(label, target, node.attributes.get('rawtarget', target))

_TARGET_RE = re.compile(r'^(.*?)\s*<(?:URI:|URL:)?([^<>]+)>$')
_VALID_IDENTIFIER_RE = re.compile('[^0-9a-zA-Z_]')
Expand Down Expand Up @@ -131,7 +139,9 @@ def visit_obj_reference(self, node: obj_reference) -> None:
self._handle_reference(node, link_func=self._linker.link_to)

def _handle_reference(self, node: nodes.title_reference, link_func: Callable[[str, "Flattenable"], "Flattenable"]) -> None:
node_label, target = parse_reference(node)
ref = parse_reference(node)
node_label = ref.label
target = ref.target
label: "Flattenable"
if not isinstance(node_label, str):
label = node2stan(node_label, self._linker)
Expand Down

0 comments on commit 972993d

Please sign in to comment.