Skip to content

Commit d69e65e

Browse files
committed
feat: adding set custom var directive. Also functions to remove 'edit this page' and 'show source' buttons.
1 parent 9f7b176 commit d69e65e

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

doc/source/conf.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from datetime import datetime
44
import os
55
from pathlib import Path
6+
import sys
67
from typing import List
78

89
from github import Github
910
import pyvista
1011
import requests
12+
import sphinx
1113
from sphinx.builders.latex import LaTeXBuilder
1214

1315
from ansys_sphinx_theme import (
@@ -24,6 +26,9 @@
2426
THIS_PATH = Path(__file__).parent.resolve()
2527
EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve()
2628

29+
# To allow using 'helper' python file as a module
30+
sys.path.append(Path(__file__).parent)
31+
2732
# Project information
2833
project = "ansys_sphinx_theme"
2934
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]:
265270
},
266271
"pdf_guide": {"version": get_version_match(__version__)}, # noqa: E501
267272
}
273+
274+
275+
def remove_edit_this_page_if_directive(
276+
app: sphinx.app,
277+
pagename: str,
278+
templatename: str,
279+
context: sphinx.context,
280+
doctree: sphinx.doctree,
281+
page_vars: dict,
282+
):
283+
"""Remove 'edit this page' button.
284+
285+
Remove the 'edit this page' link in this page if the page variable
286+
'hide_edit_page_button' is true.
287+
288+
Parameters
289+
----------
290+
app : sphinx.app
291+
Sphinx app
292+
pagename : str
293+
Page name
294+
templatename : str
295+
Template name
296+
context : sphinx.context
297+
Page context
298+
doctree : sphinx.doctree
299+
Page doctree
300+
page_vars : dict
301+
Page variables
302+
"""
303+
# Remove the edit button for the index page
304+
if "hide_edit_page_button" in page_vars:
305+
if page_vars["hide_edit_page_button"].lower() == "true":
306+
# breakpoint()
307+
context.pop("theme_use_edit_page_button", False)
308+
309+
310+
def remove_show_source_if_directive(
311+
app: sphinx.app,
312+
pagename: str,
313+
templatename: str,
314+
context: sphinx.context,
315+
doctree: sphinx.doctree,
316+
page_vars: dict,
317+
):
318+
"""Remove the 'show_source' link.
319+
320+
Remove the 'show_source' link in this page if the page variable
321+
'hide_show_source' is true.
322+
323+
Parameters
324+
----------
325+
app : sphinx.app
326+
Sphinx app
327+
pagename : str
328+
Page name
329+
templatename : str
330+
Template name
331+
context : sphinx.context
332+
Page context
333+
doctree : sphinx.doctree
334+
Page doctree
335+
page_vars : dict
336+
Page variables
337+
"""
338+
# Remove the edit button for the index page
339+
if "hide_show_source" in page_vars:
340+
if page_vars["hide_show_source"].lower() == "true":
341+
context["show_source"] = False
342+
343+
344+
def pre_build_page_html(
345+
app: sphinx.app,
346+
pagename: str,
347+
templatename: str,
348+
context: sphinx.context,
349+
doctree: sphinx.doctree,
350+
):
351+
"""Apply hooks before building HTML.
352+
353+
Apply the hooks as functions before building the HTML files.
354+
355+
Parameters
356+
----------
357+
app : sphinx.app
358+
Sphinx app
359+
pagename : str
360+
Page name
361+
templatename : str
362+
Template name
363+
context : sphinx.context
364+
Page context
365+
doctree : sphinx.doctree
366+
Page doctree
367+
"""
368+
from helpers import get_page_vars
369+
370+
page_vars = get_page_vars(app, pagename)
371+
372+
## Hooks
373+
remove_edit_this_page_if_directive(app, pagename, templatename, context, doctree, page_vars)
374+
375+
remove_show_source_if_directive(app, pagename, templatename, context, doctree, page_vars)
376+
377+
378+
def setup(app: sphinx):
379+
"""Add custom configuration to sphinx app.
380+
381+
Parameters
382+
----------
383+
app : sphinx.application.sphinx
384+
The Sphinx application.
385+
"""
386+
from helpers import SetPageVariableDirective, add_custom_variables_to_context
387+
388+
# Register the directive
389+
app.add_directive("setpagevar", SetPageVariableDirective)
390+
391+
# Hook into the html-page-context event
392+
app.connect("html-page-context", add_custom_variables_to_context)
393+
394+
# setting pre-build functions
395+
app.connect("html-page-context", pre_build_page_html)

doc/source/helpers.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Helper classes and functions for documentation build."""
2+
3+
from docutils.parsers.rst import Directive
4+
import sphinx
5+
6+
7+
def get_page_vars(app: sphinx.app, pagename: str) -> dict:
8+
"""Get page variables.
9+
10+
Get each page variables.
11+
12+
Parameters
13+
----------
14+
app : app
15+
Sphinx app
16+
pagename : str
17+
Page name
18+
19+
Returns
20+
-------
21+
dict
22+
Dictionary with variables as keys, and their values.
23+
"""
24+
env = app.builder.env
25+
26+
if (
27+
not hasattr(env, "pages_vars")
28+
or not env.pages_vars
29+
or not env.pages_vars.get(pagename, None)
30+
):
31+
return
32+
33+
return env.pages_vars[pagename]
34+
35+
36+
class SetPageVariableDirective(Directive):
37+
"""Set page variables.
38+
39+
Set page variables.
40+
41+
Parameters
42+
----------
43+
- variable: str
44+
- value: str
45+
46+
Example
47+
48+
.. setpagevar:: my_key my_value
49+
50+
The key cannot have spaces.
51+
52+
.. setpagevar:: my_key my value
53+
54+
"""
55+
56+
has_content = False
57+
required_arguments = 2 # Variable name and value
58+
optional_arguments = 0
59+
60+
def run(self):
61+
"""Run directive."""
62+
var_name = self.arguments[0]
63+
var_value = self.arguments[1]
64+
env = self.state.document.settings.env
65+
66+
# Store the variable in the environment specific to each page
67+
if not hasattr(env, "pages_vars"):
68+
env.pages_vars = {}
69+
70+
# Store the variable for the current page (env.docname is the document name)
71+
if env.docname not in env.pages_vars:
72+
env.pages_vars[env.docname] = {}
73+
74+
env.pages_vars[env.docname][var_name] = var_value
75+
76+
return []
77+
78+
79+
def add_custom_variables_to_context(
80+
app: sphinx.app,
81+
pagename: str,
82+
templatename: str,
83+
context: sphinx.context,
84+
doctree: sphinx.doctree,
85+
) -> None:
86+
"""Add customs variables to build context.
87+
88+
This is needed to be able to access the vars at the build stage.
89+
90+
Parameters
91+
----------
92+
app : Sphinx.app
93+
Sphinx app.
94+
pagename : str
95+
Page name
96+
templatename : str
97+
Template page
98+
context : Sphinx.context
99+
Page context
100+
doctree : Sphinx.doctree
101+
Page doctree
102+
"""
103+
env = app.builder.env
104+
105+
# Check if there are page-specific variables stored by the directive
106+
if hasattr(env, "pages_vars"):
107+
if pagename in env.pages_vars:
108+
# Add the stored variables to the context for this page
109+
context.update(env.pages_vars[pagename])

0 commit comments

Comments
 (0)