|
3 | 3 | from datetime import datetime
|
4 | 4 | import os
|
5 | 5 | from pathlib import Path
|
| 6 | +import sys |
6 | 7 | from typing import List
|
7 | 8 |
|
8 | 9 | from github import Github
|
9 | 10 | import pyvista
|
10 | 11 | import requests
|
| 12 | +import sphinx |
11 | 13 | from sphinx.builders.latex import LaTeXBuilder
|
12 | 14 |
|
13 | 15 | from ansys_sphinx_theme import (
|
|
24 | 26 | THIS_PATH = Path(__file__).parent.resolve()
|
25 | 27 | EXAMPLE_PATH = (THIS_PATH / "examples" / "sphinx_examples").resolve()
|
26 | 28 |
|
| 29 | +# To allow using 'helper' python file as a module |
| 30 | +sys.path.append(Path(__file__).parent) |
| 31 | + |
27 | 32 | # Project information
|
28 | 33 | project = "ansys_sphinx_theme"
|
29 | 34 | 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]:
|
265 | 270 | },
|
266 | 271 | "pdf_guide": {"version": get_version_match(__version__)}, # noqa: E501
|
267 | 272 | }
|
| 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) |
0 commit comments