Skip to content

Commit

Permalink
[Doc] Add github links for source code references
Browse files Browse the repository at this point in the history
Previously, the use of `sphinx.ext.viewcode` generated new pages that
included the raw source code. This change adds the
`sphinx.ext.linkcode` extension, which allows defining a custom method
for generating source code URLs given a code reference. This now
includes links to the appropriate file and line number on github for
code references.

For now I left the `viewcode` extension in place. The result is that
code references now have 2 `[source]` links - one for the copy of the
source in the docs, and one to github. If the github links seem to
work well enough, we can drop `viewcode` later.

Signed-off-by: Russell Bryant <[email protected]>
  • Loading branch information
russellb committed Nov 26, 2024
1 parent 1f6584e commit 30d33fa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.

import inspect
import logging
import os
import sys
Expand All @@ -34,6 +35,7 @@
extensions = [
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx.ext.linkcode",
"sphinx.ext.intersphinx",
"sphinx_copybutton",
"sphinx.ext.autodoc",
Expand Down Expand Up @@ -94,6 +96,25 @@ def setup(app):
generate_examples()


def linkcode_resolve(domain, info):
if domain != 'py':
return None
if not info['module']:
return None
filename = info['module'].replace('.', '/')
module = info['module']
try:
obj = sys.modules[module]
for part in info['fullname'].split('.'):
obj = getattr(obj, part)
lineno = inspect.getsourcelines(obj)[1]
filename = (inspect.getsourcefile(obj)
or f"{filename}.py").split("vllm/", 1)[1]
return f"https://github.com/vllm-project/vllm/blob/main/{filename}#L{lineno}"
except Exception:
return f"https://github.com/vllm-project/vllm/blob/main/{filename}.py"


# Mock out external dependencies here, otherwise the autodoc pages may be blank.
autodoc_mock_imports = [
"compressed_tensors",
Expand Down

0 comments on commit 30d33fa

Please sign in to comment.