From 74d2ec12e97f8a7e95a70a7dc14dbdd7fcf2267c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 6 Sep 2022 17:56:56 +0200 Subject: [PATCH] Add mkdocs hook to hide internal section children --- .gitignore | 1 + docs/mkdocs/docs/api/internal/index.md | 9 ++++++++ docs/mkdocs/mkdocs.yml | 5 +++++ docs/mkdocs/scripts/internal_section.py | 30 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 docs/mkdocs/docs/api/internal/index.md create mode 100644 docs/mkdocs/scripts/internal_section.py diff --git a/.gitignore b/.gitignore index 30b62bfcf..50a36d968 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ /docs/mkdocs/docs/__pycache__/ /docs/mkdocs/docs/examples/ /docs/mkdocs/docs/images/json.gif +/docs/mkdocs/scripts/__pycache__/ /docs/mkdocs/site/ /docs/mkdocs/venv/ diff --git a/docs/mkdocs/docs/api/internal/index.md b/docs/mkdocs/docs/api/internal/index.md new file mode 100644 index 000000000..812d1ebe5 --- /dev/null +++ b/docs/mkdocs/docs/api/internal/index.md @@ -0,0 +1,9 @@ +--- +x-nlohmann-json-is-internal: true +--- + +# Developer Documentation + +!!! missing + + This section is under construction. diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 823a59bea..3deb09e52 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -277,6 +277,8 @@ nav: - 'NLOHMANN_JSON_VERSION_MAJOR': api/macros/nlohmann_json_version_major.md - 'NLOHMANN_JSON_VERSION_MINOR': api/macros/nlohmann_json_version_major.md - 'NLOHMANN_JSON_VERSION_PATCH': api/macros/nlohmann_json_version_major.md + - Internal: + - api/internal/index.md # Extras extra: @@ -341,6 +343,9 @@ plugins: 'api/basic_json/operator_literal_json.md': api/operator_literal_json.md 'api/basic_json/operator_literal_json_pointer.md': api/operator_literal_json_pointer.md 'api/json_pointer/operator_string.md': api/json_pointer/operator_string_t.md + - mkdocs-simple-hooks: + hooks: + on_page_context: scripts.internal_section:on_page_context extra_css: - css/custom.css diff --git a/docs/mkdocs/scripts/internal_section.py b/docs/mkdocs/scripts/internal_section.py new file mode 100644 index 000000000..742539c90 --- /dev/null +++ b/docs/mkdocs/scripts/internal_section.py @@ -0,0 +1,30 @@ +import sys + +from copy import deepcopy +from mkdocs.structure.nav import Navigation, Section, Page + +def _get_internal_sections(items, current_page): + res = [] + sections = [item for item in items if isinstance(item, Section)] + while sections: + for section in sections[:]: + for item in section.children: + if isinstance(item, Section): + sections.append(item) + elif isinstance(item, Page): + if item.meta.get("x-nlohmann-json-is-internal", False): + res.append(section) + sections.remove(section) + return res + +def on_page_context(context, page, config, nav): + sys.setrecursionlimit(1200) + nav = deepcopy(nav) + context["nav"] = nav + + sections = _get_internal_sections(nav.items, page) + for section in sections: + if not section.active: + section.children = [child for child in section.children if child.is_index] + + return context