From d69e65ee9677b88288e79a099c7f4b5f6cea54a7 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:40:50 +0200 Subject: [PATCH 01/10] feat: adding set custom var directive. Also functions to remove 'edit this page' and 'show source' buttons. --- doc/source/conf.py | 128 ++++++++++++++++++++++++++++++++++++++++++ doc/source/helpers.py | 109 +++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 doc/source/helpers.py diff --git a/doc/source/conf.py b/doc/source/conf.py index aded60f55..c339e49ce 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -3,11 +3,13 @@ from datetime import datetime import os from pathlib import Path +import sys from typing import List from github import Github import pyvista import requests +import sphinx from sphinx.builders.latex import LaTeXBuilder from ansys_sphinx_theme import ( @@ -24,6 +26,9 @@ THIS_PATH = Path(__file__).parent.resolve() EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve() +# To allow using 'helper' python file as a module +sys.path.append(Path(__file__).parent) + # Project information project = "ansys_sphinx_theme" copyright = f"(c) {datetime.now().year} ANSYS, Inc. All rights reserved" @@ -265,3 +270,126 @@ def download_and_process_files(example_links: List[str]) -> List[str]: }, "pdf_guide": {"version": get_version_match(__version__)}, # noqa: E501 } + + +def remove_edit_this_page_if_directive( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, + page_vars: dict, +): + """Remove 'edit this page' button. + + Remove the 'edit this page' link in this page if the page variable + 'hide_edit_page_button' is true. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + page_vars : dict + Page variables + """ + # Remove the edit button for the index page + if "hide_edit_page_button" in page_vars: + if page_vars["hide_edit_page_button"].lower() == "true": + # breakpoint() + context.pop("theme_use_edit_page_button", False) + + +def remove_show_source_if_directive( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, + page_vars: dict, +): + """Remove the 'show_source' link. + + Remove the 'show_source' link in this page if the page variable + 'hide_show_source' is true. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + page_vars : dict + Page variables + """ + # Remove the edit button for the index page + if "hide_show_source" in page_vars: + if page_vars["hide_show_source"].lower() == "true": + context["show_source"] = False + + +def pre_build_page_html( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, +): + """Apply hooks before building HTML. + + Apply the hooks as functions before building the HTML files. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + """ + from helpers import get_page_vars + + page_vars = get_page_vars(app, pagename) + + ## Hooks + remove_edit_this_page_if_directive(app, pagename, templatename, context, doctree, page_vars) + + remove_show_source_if_directive(app, pagename, templatename, context, doctree, page_vars) + + +def setup(app: sphinx): + """Add custom configuration to sphinx app. + + Parameters + ---------- + app : sphinx.application.sphinx + The Sphinx application. + """ + from helpers import SetPageVariableDirective, add_custom_variables_to_context + + # Register the directive + app.add_directive("setpagevar", SetPageVariableDirective) + + # Hook into the html-page-context event + app.connect("html-page-context", add_custom_variables_to_context) + + # setting pre-build functions + app.connect("html-page-context", pre_build_page_html) diff --git a/doc/source/helpers.py b/doc/source/helpers.py new file mode 100644 index 000000000..7eb188304 --- /dev/null +++ b/doc/source/helpers.py @@ -0,0 +1,109 @@ +"""Helper classes and functions for documentation build.""" + +from docutils.parsers.rst import Directive +import sphinx + + +def get_page_vars(app: sphinx.app, pagename: str) -> dict: + """Get page variables. + + Get each page variables. + + Parameters + ---------- + app : app + Sphinx app + pagename : str + Page name + + Returns + ------- + dict + Dictionary with variables as keys, and their values. + """ + env = app.builder.env + + if ( + not hasattr(env, "pages_vars") + or not env.pages_vars + or not env.pages_vars.get(pagename, None) + ): + return + + return env.pages_vars[pagename] + + +class SetPageVariableDirective(Directive): + """Set page variables. + + Set page variables. + + Parameters + ---------- + - variable: str + - value: str + + Example + + .. setpagevar:: my_key my_value + + The key cannot have spaces. + + .. setpagevar:: my_key my value + + """ + + has_content = False + required_arguments = 2 # Variable name and value + optional_arguments = 0 + + def run(self): + """Run directive.""" + var_name = self.arguments[0] + var_value = self.arguments[1] + env = self.state.document.settings.env + + # Store the variable in the environment specific to each page + if not hasattr(env, "pages_vars"): + env.pages_vars = {} + + # Store the variable for the current page (env.docname is the document name) + if env.docname not in env.pages_vars: + env.pages_vars[env.docname] = {} + + env.pages_vars[env.docname][var_name] = var_value + + return [] + + +def add_custom_variables_to_context( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, +) -> None: + """Add customs variables to build context. + + This is needed to be able to access the vars at the build stage. + + Parameters + ---------- + app : Sphinx.app + Sphinx app. + pagename : str + Page name + templatename : str + Template page + context : Sphinx.context + Page context + doctree : Sphinx.doctree + Page doctree + """ + env = app.builder.env + + # Check if there are page-specific variables stored by the directive + if hasattr(env, "pages_vars"): + if pagename in env.pages_vars: + # Add the stored variables to the context for this page + context.update(env.pages_vars[pagename]) From 20138c24f551feb855050dae85684c1b8fc873f4 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:41:39 +0200 Subject: [PATCH 02/10] docs: hidding buttons using new directive --- doc/source/index.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/source/index.rst b/doc/source/index.rst index 559542643..4a97d478a 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -8,6 +8,11 @@ This theme is specifically tailored for documentation related to Ansys projects helping to ensure consistency in its look and feel. Various useful extensions are included in the theme to make documentation more appealing and user-friendly. + +.. setpagevar:: hide_edit_page_button True + +.. setpagevar:: hide_show_source True + .. grid:: 2 :gutter: 2 2 3 4 From 6abb62ce01354edec843b3083bf7c4d1a63f12ac Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:49:09 +0000 Subject: [PATCH 03/10] chore: adding changelog file 523.added.md --- doc/changelog.d/523.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/523.added.md diff --git a/doc/changelog.d/523.added.md b/doc/changelog.d/523.added.md new file mode 100644 index 000000000..a78a039bc --- /dev/null +++ b/doc/changelog.d/523.added.md @@ -0,0 +1 @@ +feat: adding custon vars directives \ No newline at end of file From eb749b2d9df77a7fa2e113668a1ac8979824190c Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:13:47 +0000 Subject: [PATCH 04/10] chore: adding changelog file 523.added.md --- doc/changelog.d/523.added.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/523.added.md b/doc/changelog.d/523.added.md index a78a039bc..b22b8a34a 100644 --- a/doc/changelog.d/523.added.md +++ b/doc/changelog.d/523.added.md @@ -1 +1 @@ -feat: adding custon vars directives \ No newline at end of file +feat: adding custom vars directives \ No newline at end of file From b1dc9d446533dfba5032cf78020d902a3f1de1ac Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:27:17 +0200 Subject: [PATCH 05/10] fix: typing hints --- doc/source/conf.py | 45 ++++++++++++++++++++++--------------------- doc/source/helpers.py | 19 +++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index c339e49ce..506e4534a 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -9,7 +9,8 @@ from github import Github import pyvista import requests -import sphinx +from sphinx.addnodes import document as doctree +from sphinx.application import Sphinx as App from sphinx.builders.latex import LaTeXBuilder from ansys_sphinx_theme import ( @@ -27,7 +28,7 @@ EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve() # To allow using 'helper' python file as a module -sys.path.append(Path(__file__).parent) +sys.path.append(str(Path(__file__).parent)) # Project information project = "ansys_sphinx_theme" @@ -273,11 +274,11 @@ def download_and_process_files(example_links: List[str]) -> List[str]: def remove_edit_this_page_if_directive( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, page_vars: dict, ): """Remove 'edit this page' button. @@ -287,15 +288,15 @@ def remove_edit_this_page_if_directive( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree page_vars : dict Page variables @@ -308,11 +309,11 @@ def remove_edit_this_page_if_directive( def remove_show_source_if_directive( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, page_vars: dict, ): """Remove the 'show_source' link. @@ -322,15 +323,15 @@ def remove_show_source_if_directive( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree page_vars : dict Page variables @@ -342,11 +343,11 @@ def remove_show_source_if_directive( def pre_build_page_html( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, ): """Apply hooks before building HTML. @@ -354,15 +355,15 @@ def pre_build_page_html( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree """ from helpers import get_page_vars @@ -375,12 +376,12 @@ def pre_build_page_html( remove_show_source_if_directive(app, pagename, templatename, context, doctree, page_vars) -def setup(app: sphinx): +def setup(app: App): """Add custom configuration to sphinx app. Parameters ---------- - app : sphinx.application.sphinx + app : sphinx.application.Sphinxlication.sphinx The Sphinx application. """ from helpers import SetPageVariableDirective, add_custom_variables_to_context diff --git a/doc/source/helpers.py b/doc/source/helpers.py index 7eb188304..fd04b0872 100644 --- a/doc/source/helpers.py +++ b/doc/source/helpers.py @@ -1,10 +1,11 @@ """Helper classes and functions for documentation build.""" from docutils.parsers.rst import Directive -import sphinx +from sphinx.addnodes import document as doctree +from sphinx.application import Sphinx as App -def get_page_vars(app: sphinx.app, pagename: str) -> dict: +def get_page_vars(app: App, pagename: str) -> dict: """Get page variables. Get each page variables. @@ -28,7 +29,7 @@ def get_page_vars(app: sphinx.app, pagename: str) -> dict: or not env.pages_vars or not env.pages_vars.get(pagename, None) ): - return + return {} return env.pages_vars[pagename] @@ -77,11 +78,11 @@ def run(self): def add_custom_variables_to_context( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, ) -> None: """Add customs variables to build context. @@ -89,15 +90,15 @@ def add_custom_variables_to_context( Parameters ---------- - app : Sphinx.app + app : sphinx.application.Sphinx Sphinx app. pagename : str Page name templatename : str Template page - context : Sphinx.context + context : dict Page context - doctree : Sphinx.doctree + doctree : sphinx.addnodes.document Page doctree """ env = app.builder.env From 54e5742ae455ca7e8e98cafc6440571d9f8a8a8d Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:40:50 +0200 Subject: [PATCH 06/10] feat: adding set custom var directive. Also functions to remove 'edit this page' and 'show source' buttons. --- doc/source/conf.py | 128 ++++++++++++++++++++++++++++++++++++++++++ doc/source/helpers.py | 109 +++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 doc/source/helpers.py diff --git a/doc/source/conf.py b/doc/source/conf.py index aded60f55..c339e49ce 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -3,11 +3,13 @@ from datetime import datetime import os from pathlib import Path +import sys from typing import List from github import Github import pyvista import requests +import sphinx from sphinx.builders.latex import LaTeXBuilder from ansys_sphinx_theme import ( @@ -24,6 +26,9 @@ THIS_PATH = Path(__file__).parent.resolve() EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve() +# To allow using 'helper' python file as a module +sys.path.append(Path(__file__).parent) + # Project information project = "ansys_sphinx_theme" copyright = f"(c) {datetime.now().year} ANSYS, Inc. All rights reserved" @@ -265,3 +270,126 @@ def download_and_process_files(example_links: List[str]) -> List[str]: }, "pdf_guide": {"version": get_version_match(__version__)}, # noqa: E501 } + + +def remove_edit_this_page_if_directive( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, + page_vars: dict, +): + """Remove 'edit this page' button. + + Remove the 'edit this page' link in this page if the page variable + 'hide_edit_page_button' is true. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + page_vars : dict + Page variables + """ + # Remove the edit button for the index page + if "hide_edit_page_button" in page_vars: + if page_vars["hide_edit_page_button"].lower() == "true": + # breakpoint() + context.pop("theme_use_edit_page_button", False) + + +def remove_show_source_if_directive( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, + page_vars: dict, +): + """Remove the 'show_source' link. + + Remove the 'show_source' link in this page if the page variable + 'hide_show_source' is true. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + page_vars : dict + Page variables + """ + # Remove the edit button for the index page + if "hide_show_source" in page_vars: + if page_vars["hide_show_source"].lower() == "true": + context["show_source"] = False + + +def pre_build_page_html( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, +): + """Apply hooks before building HTML. + + Apply the hooks as functions before building the HTML files. + + Parameters + ---------- + app : sphinx.app + Sphinx app + pagename : str + Page name + templatename : str + Template name + context : sphinx.context + Page context + doctree : sphinx.doctree + Page doctree + """ + from helpers import get_page_vars + + page_vars = get_page_vars(app, pagename) + + ## Hooks + remove_edit_this_page_if_directive(app, pagename, templatename, context, doctree, page_vars) + + remove_show_source_if_directive(app, pagename, templatename, context, doctree, page_vars) + + +def setup(app: sphinx): + """Add custom configuration to sphinx app. + + Parameters + ---------- + app : sphinx.application.sphinx + The Sphinx application. + """ + from helpers import SetPageVariableDirective, add_custom_variables_to_context + + # Register the directive + app.add_directive("setpagevar", SetPageVariableDirective) + + # Hook into the html-page-context event + app.connect("html-page-context", add_custom_variables_to_context) + + # setting pre-build functions + app.connect("html-page-context", pre_build_page_html) diff --git a/doc/source/helpers.py b/doc/source/helpers.py new file mode 100644 index 000000000..7eb188304 --- /dev/null +++ b/doc/source/helpers.py @@ -0,0 +1,109 @@ +"""Helper classes and functions for documentation build.""" + +from docutils.parsers.rst import Directive +import sphinx + + +def get_page_vars(app: sphinx.app, pagename: str) -> dict: + """Get page variables. + + Get each page variables. + + Parameters + ---------- + app : app + Sphinx app + pagename : str + Page name + + Returns + ------- + dict + Dictionary with variables as keys, and their values. + """ + env = app.builder.env + + if ( + not hasattr(env, "pages_vars") + or not env.pages_vars + or not env.pages_vars.get(pagename, None) + ): + return + + return env.pages_vars[pagename] + + +class SetPageVariableDirective(Directive): + """Set page variables. + + Set page variables. + + Parameters + ---------- + - variable: str + - value: str + + Example + + .. setpagevar:: my_key my_value + + The key cannot have spaces. + + .. setpagevar:: my_key my value + + """ + + has_content = False + required_arguments = 2 # Variable name and value + optional_arguments = 0 + + def run(self): + """Run directive.""" + var_name = self.arguments[0] + var_value = self.arguments[1] + env = self.state.document.settings.env + + # Store the variable in the environment specific to each page + if not hasattr(env, "pages_vars"): + env.pages_vars = {} + + # Store the variable for the current page (env.docname is the document name) + if env.docname not in env.pages_vars: + env.pages_vars[env.docname] = {} + + env.pages_vars[env.docname][var_name] = var_value + + return [] + + +def add_custom_variables_to_context( + app: sphinx.app, + pagename: str, + templatename: str, + context: sphinx.context, + doctree: sphinx.doctree, +) -> None: + """Add customs variables to build context. + + This is needed to be able to access the vars at the build stage. + + Parameters + ---------- + app : Sphinx.app + Sphinx app. + pagename : str + Page name + templatename : str + Template page + context : Sphinx.context + Page context + doctree : Sphinx.doctree + Page doctree + """ + env = app.builder.env + + # Check if there are page-specific variables stored by the directive + if hasattr(env, "pages_vars"): + if pagename in env.pages_vars: + # Add the stored variables to the context for this page + context.update(env.pages_vars[pagename]) From cec5b0d1538a9af73c43fe41a9c99983211f5a26 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:41:39 +0200 Subject: [PATCH 07/10] docs: hidding buttons using new directive --- doc/source/index.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/source/index.rst b/doc/source/index.rst index 559542643..4a97d478a 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -8,6 +8,11 @@ This theme is specifically tailored for documentation related to Ansys projects helping to ensure consistency in its look and feel. Various useful extensions are included in the theme to make documentation more appealing and user-friendly. + +.. setpagevar:: hide_edit_page_button True + +.. setpagevar:: hide_show_source True + .. grid:: 2 :gutter: 2 2 3 4 From bf29a0d6b059c409cfe009f5a7878f01793f64aa Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:49:09 +0000 Subject: [PATCH 08/10] chore: adding changelog file 523.added.md --- doc/changelog.d/523.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/523.added.md diff --git a/doc/changelog.d/523.added.md b/doc/changelog.d/523.added.md new file mode 100644 index 000000000..a78a039bc --- /dev/null +++ b/doc/changelog.d/523.added.md @@ -0,0 +1 @@ +feat: adding custon vars directives \ No newline at end of file From 0afd5b7261f0687280739fac13e649a8e899c4fd Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:13:47 +0000 Subject: [PATCH 09/10] chore: adding changelog file 523.added.md --- doc/changelog.d/523.added.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/523.added.md b/doc/changelog.d/523.added.md index a78a039bc..b22b8a34a 100644 --- a/doc/changelog.d/523.added.md +++ b/doc/changelog.d/523.added.md @@ -1 +1 @@ -feat: adding custon vars directives \ No newline at end of file +feat: adding custom vars directives \ No newline at end of file From 3652b927e78ba5f1d6fd253830a4d777c9668309 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:27:17 +0200 Subject: [PATCH 10/10] fix: typing hints --- doc/source/conf.py | 45 ++++++++++++++++++++++--------------------- doc/source/helpers.py | 19 +++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index c339e49ce..506e4534a 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -9,7 +9,8 @@ from github import Github import pyvista import requests -import sphinx +from sphinx.addnodes import document as doctree +from sphinx.application import Sphinx as App from sphinx.builders.latex import LaTeXBuilder from ansys_sphinx_theme import ( @@ -27,7 +28,7 @@ EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve() # To allow using 'helper' python file as a module -sys.path.append(Path(__file__).parent) +sys.path.append(str(Path(__file__).parent)) # Project information project = "ansys_sphinx_theme" @@ -273,11 +274,11 @@ def download_and_process_files(example_links: List[str]) -> List[str]: def remove_edit_this_page_if_directive( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, page_vars: dict, ): """Remove 'edit this page' button. @@ -287,15 +288,15 @@ def remove_edit_this_page_if_directive( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree page_vars : dict Page variables @@ -308,11 +309,11 @@ def remove_edit_this_page_if_directive( def remove_show_source_if_directive( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, page_vars: dict, ): """Remove the 'show_source' link. @@ -322,15 +323,15 @@ def remove_show_source_if_directive( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree page_vars : dict Page variables @@ -342,11 +343,11 @@ def remove_show_source_if_directive( def pre_build_page_html( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, ): """Apply hooks before building HTML. @@ -354,15 +355,15 @@ def pre_build_page_html( Parameters ---------- - app : sphinx.app + app : sphinx.application.Sphinx Sphinx app pagename : str Page name templatename : str Template name - context : sphinx.context + context : dict Page context - doctree : sphinx.doctree + doctree : sphinx.addnodes.document Page doctree """ from helpers import get_page_vars @@ -375,12 +376,12 @@ def pre_build_page_html( remove_show_source_if_directive(app, pagename, templatename, context, doctree, page_vars) -def setup(app: sphinx): +def setup(app: App): """Add custom configuration to sphinx app. Parameters ---------- - app : sphinx.application.sphinx + app : sphinx.application.Sphinxlication.sphinx The Sphinx application. """ from helpers import SetPageVariableDirective, add_custom_variables_to_context diff --git a/doc/source/helpers.py b/doc/source/helpers.py index 7eb188304..fd04b0872 100644 --- a/doc/source/helpers.py +++ b/doc/source/helpers.py @@ -1,10 +1,11 @@ """Helper classes and functions for documentation build.""" from docutils.parsers.rst import Directive -import sphinx +from sphinx.addnodes import document as doctree +from sphinx.application import Sphinx as App -def get_page_vars(app: sphinx.app, pagename: str) -> dict: +def get_page_vars(app: App, pagename: str) -> dict: """Get page variables. Get each page variables. @@ -28,7 +29,7 @@ def get_page_vars(app: sphinx.app, pagename: str) -> dict: or not env.pages_vars or not env.pages_vars.get(pagename, None) ): - return + return {} return env.pages_vars[pagename] @@ -77,11 +78,11 @@ def run(self): def add_custom_variables_to_context( - app: sphinx.app, + app: App, pagename: str, templatename: str, - context: sphinx.context, - doctree: sphinx.doctree, + context: dict, + doctree: doctree, ) -> None: """Add customs variables to build context. @@ -89,15 +90,15 @@ def add_custom_variables_to_context( Parameters ---------- - app : Sphinx.app + app : sphinx.application.Sphinx Sphinx app. pagename : str Page name templatename : str Template page - context : Sphinx.context + context : dict Page context - doctree : Sphinx.doctree + doctree : sphinx.addnodes.document Page doctree """ env = app.builder.env