From c4ebfee97d85a69d3fdc2afd0c03b5bb59070536 Mon Sep 17 00:00:00 2001 From: Pedro Brochado Date: Fri, 7 Jun 2024 16:47:08 -0300 Subject: [PATCH] Add Redoc viewer to plugin User Manual - Inject Redoc html tags into final html on "on_post_page" mkdocs hook - Change navigation: * Add "UserManual/{plugin}/restapi/" * Move CHANGES.md nav to "UserManual/{plugin}/changes/" - Fix "Help#quick-links" table links --- pyproject.toml | 1 + src/pulp_docs/mkdocs_hooks.py | 41 +++++++++++++++ src/pulp_docs/mkdocs_macros.py | 79 ++++++++++++++++++++++++----- src/pulp_docs/navigation.py | 7 --- src/pulp_docs/repository.py | 7 +-- src/pulp_docs/utils/aggregation.py | 16 ++++-- staging_docs/sections/help/index.md | 14 ++--- 7 files changed, 130 insertions(+), 35 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c269d8d..1f00f36 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ dependencies = [ "mkdocs-macros-plugin", "mkdocs-site-urls", "mkdocs-literate-nav", + "bs4", "importlib_resources", "httpx", "rich", diff --git a/src/pulp_docs/mkdocs_hooks.py b/src/pulp_docs/mkdocs_hooks.py index 82c7467..9191676 100644 --- a/src/pulp_docs/mkdocs_hooks.py +++ b/src/pulp_docs/mkdocs_hooks.py @@ -3,6 +3,7 @@ See: https://www.mkdocs.org/user-guide/configuration/#hooks """ +from bs4 import BeautifulSoup as bs def on_serve(server, config, builder): @@ -16,3 +17,43 @@ def on_serve(server, config, builder): server.unwatch(tmpdir) server.unwatch(mkdocs_yml) return server + + +REDOC_HEADER = """ + + +""" + +REDOC_TAG_TEMPLATE = """ + +""" + +REDOC_SCRIPT = """ + +""" + + +def on_post_page(output, page, config): + if basepath := page.meta.get("restapi_json_file"): + redoc_tag = REDOC_TAG_TEMPLATE % basepath + bs_page = bs(output, "html.parser") + + # Append scripts + bs_page.html.head.append(bs(REDOC_HEADER, "html.parser")) + + # Replace main content-container with tag + main_container = bs_page.find_all("div", class_="md-main__inner")[0] + main_container.replace_with(bs(redoc_tag, "html.parser")) + + # Append