Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sphinx.ext.linkcode for more precise source code links #11851

Merged
Merged
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
099636d
switched from sphinx.ext.viewcode to sphinx.ext.linkcode
melechlapson Feb 20, 2024
00b05e7
removed extra line
melechlapson Feb 21, 2024
a20ad5c
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 21, 2024
2f94288
Add section header for source code links
melechlapson Feb 21, 2024
1d5c34d
removed docstring
melechlapson Feb 21, 2024
1849ce1
update return string
melechlapson Feb 21, 2024
cca9b04
added back blank line
melechlapson Feb 21, 2024
5bd18cd
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of https://githu…
melechlapson Feb 21, 2024
b985ba6
Added a method to determine the GitHub branch
melechlapson Feb 21, 2024
914d2cd
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 21, 2024
1deb939
add blank line
melechlapson Feb 21, 2024
b84b399
remove print statement
melechlapson Feb 21, 2024
01e4ca6
Try to fix error for contextlib file
Eric-Arellano Feb 21, 2024
25ff4df
Try to fix error for Jenkins run #20240221.52
melechlapson Feb 21, 2024
ace069b
Check that qiskit in module name sooner
Eric-Arellano Feb 21, 2024
5420982
moved valid code object verification earlier
melechlapson Feb 27, 2024
d60bbf4
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 27, 2024
17881d4
added try except statement to getattr call
melechlapson Feb 27, 2024
9330e45
added extra try/except block
melechlapson Feb 27, 2024
ab8dad0
Also support Azure Pipelines
Eric-Arellano Feb 27, 2024
6ef1f68
removed unused import
melechlapson Feb 27, 2024
b45113b
Revert Azure support to keep things simple
Eric-Arellano Feb 27, 2024
a3e5297
added extra "/" to final URL
melechlapson Feb 27, 2024
e3382e7
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of https://githu…
melechlapson Feb 27, 2024
825e02a
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 28, 2024
51f4bcb
Merge branch 'main' of github.com:Qiskit/qiskit-terra into mlapson/sw…
Eric-Arellano Mar 7, 2024
ab7f411
Move GitHub branch logic to GitHub Action
Eric-Arellano Mar 7, 2024
2a28edb
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of github.com:me…
Eric-Arellano Mar 7, 2024
cde30ef
switched to importlib and removed redundant block of code
melechlapson Mar 20, 2024
5545bc8
Apply suggestions from code review
melechlapson Mar 21, 2024
7c95384
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Mar 21, 2024
5b74a03
added back spaces
melechlapson Apr 4, 2024
8a805da
Merge branch 'main' of github.com:Qiskit/qiskit-terra into mlapson/sw…
Eric-Arellano Apr 4, 2024
abf4871
Clarify docs_deploy GitHub logic
Eric-Arellano Apr 4, 2024
3386c4c
Use pathlib for relativizing file name
Eric-Arellano Apr 4, 2024
614e8f8
Fix relative_to() path
Eric-Arellano Apr 5, 2024
19d052d
Remove tox prefix
Eric-Arellano Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
# pylint: disable=invalid-name,missing-function-docstring

"""Sphinx documentation builder."""

melechlapson marked this conversation as resolved.
Show resolved Hide resolved
import datetime
import doctest
import inspect
import sys
from pathlib import PurePath

project = "Qiskit"
project_copyright = f"2017-{datetime.date.today().year}, Qiskit Development Team"
Expand All @@ -39,7 +41,7 @@
"sphinx.ext.intersphinx",
"sphinx.ext.doctest",
# This is used by qiskit/documentation to generate links to github.com.
"sphinx.ext.viewcode",
"sphinx.ext.linkcode",
"matplotlib.sphinxext.plot_directive",
"reno.sphinxext",
"sphinxcontrib.katex",
Expand Down Expand Up @@ -155,3 +157,41 @@
# ----------------------------------------------------------------------------------

plot_html_show_formats = False


# Need this method for the sphinx.ext.linkcode extension
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
def linkcode_resolve(domain, info):
"""
Determine the URL corresponding to Python object
"""
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
if domain != "py":
return None

module = sys.modules.get(info["module"])
if module is None:
return None

obj = module
for part in info["fullname"].split("."):
obj = getattr(obj, part)
is_valid_code_object = (
inspect.isclass(obj) or inspect.ismethod(obj) or inspect.isfunction(obj)
)
if not is_valid_code_object:
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work for attributes? Does it not matter because sphinx doesn't generate source links for documented attributes of a class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, attributes are skipped.


full_file_name = inspect.getsourcefile(obj)
if full_file_name is None:
return None
repo_root = PurePath(__file__).parent.parent
file_name = PurePath(full_file_name).relative_to(repo_root)

try:
source, lineno = inspect.getsourcelines(obj)
except OSError:
linespec = ""
else:
ending_lineno = lineno + len(source) - 1
linespec = f"#L{lineno}-L{ending_lineno}"

return f"https://github.com/Qiskit/qiskit_sphinx_theme/tree/{release}/{file_name}/{linespec}"
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
Loading