From 3d299a2780d24ad01e7d8a751a740f8d06ec7d39 Mon Sep 17 00:00:00 2001
From: Romain Deville <code@romaindeville.fr>
Date: Fri, 16 Apr 2021 19:12:17 +0200
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adding=20support=20of=20extra=20`na?=
 =?UTF-8?q?v`=20&=20=F0=9F=90=9B=20Fix=20typo?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add support of extra `nav` key which can be defined in
`docs/_data/extra.yml` allowing to overload the `nav` key of
`mkdocs.yml` file, allowing to forked repo to have their own `nav`
without modifying the `nav` key of the `mkdocs.yml` file.

Fix minor typo in file `docs/_data/plugins.py` not loading `extra.yml`
file correctly.
---
 docs/_data/plugins.py           | 21 ++++++++++++++++++++-
 templates/docs/_data/plugins.py | 21 ++++++++++++++++++++-
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/docs/_data/plugins.py b/docs/_data/plugins.py
index ece11ce..2d676be 100644
--- a/docs/_data/plugins.py
+++ b/docs/_data/plugins.py
@@ -461,6 +461,24 @@ def set_repo_url(env: dict, repo_slug: str) -> None:
             )
 
 
+def set_nav(env: dict, repo_slug: str) -> None:
+    """Update content of the `nav` key in `env.conf`.
+
+    Update the value of `nav` key for mkdocs documentation based on (in
+    precedence order):
+
+    - Value of `nav` in `vars.yml` or `extra.yml` in `docs/_data/`, allowing
+      overloading of nav for forked repo.
+    - Value of `nav` in `mkdocs.yml`
+
+    Arguments:
+        env: Mkdocs macro plugin environment dictionary.
+        repo_slug: Repo slug or name of the repo folder.
+    """
+    if "nav" in env.variables and env.variables["nav"]:
+        env.conf["nav"] = env.variables["nav"]
+
+
 def update_theme(env: dict, repo_slug: str) -> None:
     """Update content of the `theme` key in `env.conf`.
 
@@ -526,6 +544,7 @@ def set_config(env: dict) -> None:
     set_copyright(env, git_repo)
     set_repo_name(env, repo_slug)
     set_repo_url(env, repo_slug)
+    set_nav(env, repo_slug)
     update_theme(env, repo_slug)
 
     if "subrepo" in env.variables:
@@ -568,7 +587,7 @@ def load_yaml_file(path: str, filename: str) -> None:
         schema.validate(raise_exception=True)
         data_content = schema.source
     else:
-        with open(filename) as file:
+        with open(source_file) as file:
             data_content = yaml.safe_load(file)
 
     return data_content, data_type
diff --git a/templates/docs/_data/plugins.py b/templates/docs/_data/plugins.py
index ece11ce..2d676be 100644
--- a/templates/docs/_data/plugins.py
+++ b/templates/docs/_data/plugins.py
@@ -461,6 +461,24 @@ def set_repo_url(env: dict, repo_slug: str) -> None:
             )
 
 
+def set_nav(env: dict, repo_slug: str) -> None:
+    """Update content of the `nav` key in `env.conf`.
+
+    Update the value of `nav` key for mkdocs documentation based on (in
+    precedence order):
+
+    - Value of `nav` in `vars.yml` or `extra.yml` in `docs/_data/`, allowing
+      overloading of nav for forked repo.
+    - Value of `nav` in `mkdocs.yml`
+
+    Arguments:
+        env: Mkdocs macro plugin environment dictionary.
+        repo_slug: Repo slug or name of the repo folder.
+    """
+    if "nav" in env.variables and env.variables["nav"]:
+        env.conf["nav"] = env.variables["nav"]
+
+
 def update_theme(env: dict, repo_slug: str) -> None:
     """Update content of the `theme` key in `env.conf`.
 
@@ -526,6 +544,7 @@ def set_config(env: dict) -> None:
     set_copyright(env, git_repo)
     set_repo_name(env, repo_slug)
     set_repo_url(env, repo_slug)
+    set_nav(env, repo_slug)
     update_theme(env, repo_slug)
 
     if "subrepo" in env.variables:
@@ -568,7 +587,7 @@ def load_yaml_file(path: str, filename: str) -> None:
         schema.validate(raise_exception=True)
         data_content = schema.source
     else:
-        with open(filename) as file:
+        with open(source_file) as file:
             data_content = yaml.safe_load(file)
 
     return data_content, data_type
-- 
GitLab