Skip to content

Commit

Permalink
Add source link (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok authored Jul 28, 2024
1 parent 6c58266 commit c9b5d3e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"sphinx.ext.autosummary",
"sphinx.ext.autosectionlabel",
"sphinx.ext.intersphinx",
"sphinx.ext.linkcode",
"sphinx.ext.napoleon",
"sphinxcontrib.mermaid",
# "breathe",
Expand Down Expand Up @@ -96,6 +97,40 @@ def _get_source():
"navigation_with_keys": True,
}


def linkcode_resolve(domain, info):
import importlib
import inspect

if domain != "py":
return None
if not info["module"]:
return None

base = "https://github.com/facebookresearch/spdl/tree/main"

mod = importlib.import_module(info["module"])

parts = info["fullname"].split(".")
obj = getattr(mod, parts[0])
filename = obj.__module__.replace(".", "/")
for part in parts[1:]:
obj = getattr(obj, part)

try:
src, ln = inspect.getsourcelines(obj)
return f"{base}/src/{filename}.py?#L{ln}-L{ln + len(src)}"
except Exception:
pass

# Fallback for property
try:
src, ln = inspect.getsourcelines(obj.fget)
return f"{base}/src/{filename}.py?#L{ln}-L{ln + len(src)}"
except Exception:
return None


# -- Options for HTML output -------------------------------------------------
# Custom directives

Expand Down

0 comments on commit c9b5d3e

Please sign in to comment.