|
27 | 27 | from typing import Any, Dict |
28 | 28 |
|
29 | 29 | from docutils.nodes import document |
| 30 | +import requests |
30 | 31 | from sphinx import addnodes |
31 | 32 | from sphinx.application import Sphinx |
32 | 33 |
|
@@ -285,8 +286,94 @@ def update_footer_theme( |
285 | 286 | context["ansys_sphinx_theme_version"] = __version__ |
286 | 287 |
|
287 | 288 |
|
| 289 | +def add_cheat_sheet( |
| 290 | + app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: document |
| 291 | +) -> None: |
| 292 | + """Add a cheat sheet to the left navigation sidebar. |
| 293 | +
|
| 294 | + Parameters |
| 295 | + ---------- |
| 296 | + app : ~sphinx.application.Sphinx |
| 297 | + Application instance for rendering the documentation. |
| 298 | + pagename : str |
| 299 | + Name of the current page. |
| 300 | + templatename : str |
| 301 | + Name of the template being used. |
| 302 | + context : dict |
| 303 | + Context dictionary for the page. |
| 304 | + doctree : ~docutils.nodes.document |
| 305 | + The doctree. |
| 306 | + """ |
| 307 | + cheatsheet_options = app.config.html_theme_options.get("cheatsheet", {}) |
| 308 | + pages = cheatsheet_options.get("pages", ["index"]) |
| 309 | + pages = [pages] if isinstance(pages, str) else pages |
| 310 | + if cheatsheet_options and any(pagename == page for page in pages): |
| 311 | + if cheatsheet_options.get("needs_download"): |
| 312 | + static_folder = app.config.html_static_path or ["static"] |
| 313 | + download_cheatsheet_to_static(app, cheatsheet_options, static_folder, context) |
| 314 | + sidebar = context.get("sidebars", []) |
| 315 | + sidebar.append("cheatsheet_sidebar.html") |
| 316 | + context["sidebars"] = sidebar |
| 317 | + |
| 318 | + |
| 319 | +def download_cheatsheet_to_static( |
| 320 | + app: Sphinx, |
| 321 | + cheatsheet_options: Dict[str, Any], |
| 322 | + static_folder: pathlib.Path, |
| 323 | + context: Dict[str, Any], |
| 324 | +) -> None: |
| 325 | + """Download the cheatsheet to the static directory. |
| 326 | +
|
| 327 | + Parameters |
| 328 | + ---------- |
| 329 | + app : ~sphinx.application.Sphinx |
| 330 | + Application instance for rendering the documentation. |
| 331 | + cheatsheet_options : dict |
| 332 | + Dictionary containing the cheat sheet options. |
| 333 | + static_folder : pathlib.Path |
| 334 | + Path containing the static folder. |
| 335 | + context : dict |
| 336 | + Dictionary containing the context for the page. |
| 337 | + """ |
| 338 | + cheatsheet_url = cheatsheet_options.get("url", "") |
| 339 | + cheatsheet_thumbnail = cheatsheet_options.get("thumbnail", "") |
| 340 | + static_path = pathlib.Path(app.outdir) / static_folder[0] |
| 341 | + context["cheatsheet_static_path"] = str(static_folder[0]) |
| 342 | + |
| 343 | + # Download cheat sheet file if URL is provided |
| 344 | + if cheatsheet_url: |
| 345 | + download_file(cheatsheet_url, static_path) |
| 346 | + |
| 347 | + # Download cheat sheet image if thumbnail URL is provided |
| 348 | + if cheatsheet_thumbnail: |
| 349 | + download_file(cheatsheet_thumbnail, static_path) |
| 350 | + |
| 351 | + |
| 352 | +def download_file(url: str, directory: pathlib.Path) -> None: |
| 353 | + """ |
| 354 | + Download a file from the given URL and save it to a given directory. |
| 355 | +
|
| 356 | + Parameters |
| 357 | + ---------- |
| 358 | + url : str |
| 359 | + URL of the file to download. |
| 360 | + directory : pathlib.Path |
| 361 | + Directory to save the file to. |
| 362 | + """ |
| 363 | + filename = url.split("/")[-1] |
| 364 | + if not directory.exists(): |
| 365 | + directory.mkdir(parents=True, exist_ok=True) |
| 366 | + if not (directory / filename).exists(): |
| 367 | + file_path = directory / filename |
| 368 | + with open(file_path, "wb") as file: |
| 369 | + response = requests.get(url) |
| 370 | + if response.status_code != 200: |
| 371 | + raise FileNotFoundError(f"Failed to download file from {url}.") |
| 372 | + file.write(response.content) |
| 373 | + |
| 374 | + |
288 | 375 | def setup(app: Sphinx) -> Dict: |
289 | | - """Connect to the sphinx theme app. |
| 376 | + """Connect to the Sphinx theme app. |
290 | 377 |
|
291 | 378 | Parameters |
292 | 379 | ---------- |
@@ -316,6 +403,7 @@ def setup(app: Sphinx) -> Dict: |
316 | 403 | app.add_css_file("https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css") |
317 | 404 | app.connect("html-page-context", update_footer_theme) |
318 | 405 | app.connect("html-page-context", fix_edit_html_page_context) |
| 406 | + app.connect("html-page-context", add_cheat_sheet) |
319 | 407 | app.config.templates_path.append(str(TEMPLATES_PATH)) |
320 | 408 | return { |
321 | 409 | "version": __version__, |
|
0 commit comments