From 8a5df32f79bae31b3502110f9526e115973e982d Mon Sep 17 00:00:00 2001
From: Don Naro <dnaro@redhat.com>
Date: Wed, 28 Feb 2024 14:15:03 +0000
Subject: [PATCH] set base url for readthedocs (#224)

* set base url via build.py

Co-authored-by: Sviatoslav Sydorenko <578543+webknjaz@users.noreply.github.com>

* use python 3.11 consistently

* set rtd canonical url

---------

Co-authored-by: Sviatoslav Sydorenko <578543+webknjaz@users.noreply.github.com>
---
 .readthedocs.yaml |  4 ++--
 build.py          | 22 +++++++++++++++++-----
 noxfile.py        |  2 +-
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/.readthedocs.yaml b/.readthedocs.yaml
index 6da7af23..1e0c600f 100644
--- a/.readthedocs.yaml
+++ b/.readthedocs.yaml
@@ -6,7 +6,7 @@ version: 2
 build:
   os: ubuntu-22.04
   tools:
-    python: >-
+    python: >- # Python versions should match the noxfile as well as the constraint files.
       3.11
   commands:
   - python -m venv "${READTHEDOCS_VIRTUALENV_PATH}"
@@ -15,4 +15,4 @@ build:
     pip install nox
   - >-
     "${READTHEDOCS_VIRTUALENV_PATH}"/bin/python -Im
-    nox -s build -- "${READTHEDOCS_OUTPUT}"/html
+    nox -s build -- "${READTHEDOCS_OUTPUT}"/html "${READTHEDOCS_CANONICAL_URL}"
diff --git a/build.py b/build.py
index 7c65e76c..ebb8411e 100644
--- a/build.py
+++ b/build.py
@@ -1,5 +1,6 @@
 import shutil
 import sys
+from functools import partial
 from pathlib import Path
 
 from staticjinja import Site
@@ -8,11 +9,15 @@
 import sass
 
 
-def load_page_data():
-    yaml = Path("./data").glob("*.yaml")
-    return {
-        file_path.stem: load(file_path.read_text(), Loader=Loader) for file_path in yaml
+def load_page_data(template, /, *, base_url=None):
+    context_objects = Path("./data").glob("*.yaml")
+    context_mapping = {
+        file_path.stem: load(file_path.read_text(), Loader=Loader)
+        for file_path in context_objects
     }
+    if base_url is not None:
+        context_mapping["base"]["base_url"] = base_url
+    return context_mapping
 
 
 def clean_buildpath(output_dir):
@@ -25,12 +30,19 @@ def main():
     except IndexError:
         build_dir_str = "output"
 
+    try:
+        custom_domain_cli_arg = sys.argv[2]
+    except IndexError:
+        custom_domain_cli_arg = None
+
     buildpath = Path(build_dir_str)
     clean_buildpath(buildpath)
 
     site = Site.make_site()
     site.outpath = buildpath
-    site.contexts = [(".*.html", load_page_data)]
+
+    load_page_data_callable = partial(load_page_data, base_url=custom_domain_cli_arg)
+    site.contexts = [(".*.html", load_page_data_callable)]
     # disable automatic reloading
     site.render(use_reloader=False)
 
diff --git a/noxfile.py b/noxfile.py
index e2d38c3b..08f690bd 100644
--- a/noxfile.py
+++ b/noxfile.py
@@ -1,6 +1,6 @@
 import nox
 
-@nox.session
+@nox.session(python=["3.11"]) # The python version should match the readthedocs configuration.
 def build(session: nox.Session):
     session.install(
       "-r", "requirements.in",