From 7429e2a0633defa415f644e23e83a85c07dc3ba1 Mon Sep 17 00:00:00 2001 From: johnzielke Date: Fri, 17 May 2024 07:44:00 -0400 Subject: [PATCH] Fix doc source links for read the docs (#7779) Fixes the current docs build. Currently no [source] links are shown anymore. It seems like read the docs uses a different working directory, therefore breaking the source links. It also will not have the correct git tag referenced. This should fix both of these issues. If there is a way to test if this fix works without merging to dev, let me know. ### Description A few sentences describing the changes proposed in this pull request. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: John Zielke Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- docs/source/conf.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 543372ebc7..a91f38081f 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -140,7 +140,7 @@ def generate_apidocs(*args): "github_repo": "MONAI", "github_version": "dev", "doc_path": "docs/source", - "conf_py_path": "/docs/", + "conf_py_path": "/docs/source", "VERSION": version, } html_scaled_image_link = False @@ -167,11 +167,24 @@ def setup(app): # -- Linkcode configuration -------------------------------------------------- +DEFAULT_REF = "dev" +read_the_docs_ref = os.environ.get("READTHEDOCS_GIT_IDENTIFIER", None) +if read_the_docs_ref: + # When building on ReadTheDocs, link to the specific commit + # https://docs.readthedocs.io/en/stable/reference/environment-variables.html#envvar-READTHEDOCS_GIT_IDENTIFIER + git_ref = read_the_docs_ref +elif os.environ.get("GITHUB_REF_TYPE", "branch") == "tag": + # When building a tag, link to the tag itself + git_ref = os.environ.get("GITHUB_REF", DEFAULT_REF) +else: + git_ref = os.environ.get("GITHUB_SHA", DEFAULT_REF) + DEFAULT_REPOSITORY = "Project-MONAI/MONAI" repository = os.environ.get("GITHUB_REPOSITORY", DEFAULT_REPOSITORY) -base_code_url = f"https://github.com/{repository}/blob/{version}" +base_code_url = f"https://github.com/{repository}/blob/{git_ref}" MODULE_ROOT_FOLDER = "monai" +repo_root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) # Adjusted from https://github.com/python-websockets/websockets/blob/main/docs/conf.py @@ -201,7 +214,7 @@ def linkcode_resolve(domain, info): except TypeError: # e.g. object is a typing.Union return None - file = os.path.relpath(file, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) + file = os.path.relpath(file, repo_root_path) if not file.startswith(MODULE_ROOT_FOLDER): # e.g. object is a typing.NewType return None