@@ -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_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+
415450def get_obj_module (qualname ):
416451 """
417452 Get a module/class/attribute and its original module by qualname.
@@ -456,6 +491,9 @@ def get_linenos(obj):
456491 return start , start + len (lines ) - 1
457492
458493
494+ URL_BASE = get_base_url () # Source code URL for Edit on GitHub links
495+
496+
459497def make_github_url (file_name ):
460498 """
461499 Generate the appropriate GH link for a given docs page. This function
@@ -465,9 +503,6 @@ def make_github_url(file_name):
465503 sphinx theme has a built-in `file_name` variable that looks like
466504 "/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 ]
0 commit comments