Skip to content

Commit 904faee

Browse files
authored
[DOC] Fix Edit on Github sidebar link always points to latest commit (#2460)
* Update conf.py * was this a typo? * rename function * whatsnew entry * copilot review kevin dont get me wrong, i'm not an ai fanboy
1 parent cb40b81 commit 904faee

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

docs/sphinx/source/conf.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,41 @@ def setup(app):
412412
# https://gist.github.com/flying-sheep/b65875c0ce965fbdd1d9e5d0b9851ef1
413413

414414

415+
# select correct base URL depending on the build system context
416+
def get_source_files_base_url():
417+
"""
418+
Get the base URL for the source code to generate links to GitHub source.
419+
If the build is on ReadTheDocs and it's a stable version, use the
420+
versioned link. If it's a latest version, use the main link.
421+
422+
For other builds (e.g. pull requests), use the main link.
423+
Local builds will also use the main link.
424+
425+
Resulting base URL should end with a trailing slash.
426+
427+
See https://docs.readthedocs.com/platform/stable/reference/environment-variables.html
428+
""" # noqa: E501
429+
repo_url = os.environ.get(
430+
"READTHEDOCS_GIT_CLONE_URL",
431+
default="https://github.com/pvlib/pvlib-python",
432+
)
433+
READTHEDOCS_ENV = os.environ.get("READTHEDOCS", None) == "True"
434+
READTHEDOCS_VERSION = os.environ.get("READTHEDOCS_VERSION", None)
435+
READTHEDOCS_GIT_IDENTIFIER = os.environ.get(
436+
"READTHEDOCS_GIT_IDENTIFIER", None
437+
)
438+
if READTHEDOCS_ENV: # Building docs on ReadTheDocs
439+
if READTHEDOCS_VERSION == "latest": # latest version, commited to main
440+
repo_url += "/blob/main/"
441+
elif READTHEDOCS_VERSION == "stable": # stable version, has a tag
442+
repo_url += f"/blob/{READTHEDOCS_GIT_IDENTIFIER}/"
443+
else: # pull request, user and branch are unknown so use main
444+
repo_url += "/blob/main/"
445+
else: # Local build
446+
repo_url += "/blob/main/" # can't tell where to point to
447+
return repo_url
448+
449+
415450
def get_obj_module(qualname):
416451
"""
417452
Get a module/class/attribute and its original module by qualname.
@@ -456,18 +491,18 @@ def get_linenos(obj):
456491
return start, start + len(lines) - 1
457492

458493

494+
URL_BASE = get_source_files_base_url() # Edit on GitHub source code links
495+
496+
459497
def make_github_url(file_name):
460498
"""
461499
Generate the appropriate GH link for a given docs page. This function
462500
is intended for use in sphinx template files.
463501
464502
The target URL is built differently based on the type of page. The pydata
465503
sphinx theme has a built-in `file_name` variable that looks like
466-
"/docs/sphinx/source/api.rst" or "generated/pvlib.atmosphere.alt2pres.rst"
504+
"docs/sphinx/source/api.rst" or "generated/pvlib.atmosphere.alt2pres.rst"
467505
"""
468-
469-
URL_BASE = "https://github.com/pvlib/pvlib-python/blob/main/"
470-
471506
# is it a gallery page?
472507
if any(d in file_name for d in sphinx_gallery_conf['gallery_dirs']):
473508
example_folder = file_name.split("/")[-2]

docs/sphinx/source/whatsnew/v0.12.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Documentation
5959
(:issue:`2333`, :pull:`2437`)
6060
* Update references in :py:func:`~pvlib.iotools.get_cams` and :py:func:`~pvlib.iotools.read_cams`
6161
(:issue:`2427`, :pull:`2457`)
62+
* Fix ``Edit on GitHub`` links in stable documentation so they point to the tagged repository version matching the build environment (e.g., v0.12.0). (:issue:`2456`, :pull:`2460`)
63+
6264

6365
Requirements
6466
~~~~~~~~~~~~
@@ -82,6 +84,7 @@ Contributors
8284
* Kevin Anderson (:ghuser:`kandersolar`)
8385
* Will Holmgren (:ghuser:`wholmgren`)
8486
* Muhammad Rebaal (:ghuser:`Muhammad-Rebaal`)
87+
* Echedey Luis (:ghuser:`echedey-ls`)
8588
* omahs (:ghuser:`omahs`)
8689
* Adam R. Jensen (:ghuser:`AdamRJensen`)
8790
* Marion Schroedter-Homscheidt (:ghuser:`mschroedter`)

0 commit comments

Comments
 (0)