|
23 | 23 | """Module for the Ansys Sphinx theme."""
|
24 | 24 |
|
25 | 25 | from itertools import islice, tee
|
26 |
| -import logging |
27 | 26 | import os
|
28 | 27 | import pathlib
|
29 | 28 | import re
|
|
34 | 33 | from docutils import nodes
|
35 | 34 | from sphinx import addnodes
|
36 | 35 | from sphinx.application import Sphinx
|
| 36 | +from sphinx.util import logging |
37 | 37 |
|
38 | 38 | from ansys_sphinx_theme.extension.linkcode import DOMAIN_KEYS, sphinx_linkcode_resolve
|
39 | 39 | from ansys_sphinx_theme.latex import generate_404
|
|
51 | 51 | import importlib_metadata
|
52 | 52 |
|
53 | 53 | __version__ = importlib_metadata.version(__name__.replace(".", "-"))
|
| 54 | +logger = logging.getLogger(__name__) |
54 | 55 |
|
55 | 56 |
|
56 | 57 | # Declare the fundamental paths of the theme
|
@@ -260,7 +261,7 @@ def fix_edit_link_page(link: str) -> str:
|
260 | 261 | edit=True,
|
261 | 262 | )
|
262 | 263 | except ValueError as e:
|
263 |
| - logging.debug(f"An error occurred: {e}") # Log the exception as debug info |
| 264 | + logger.error(f"An error occurred: {e}") # Log the exception as debug info |
264 | 265 | return link
|
265 | 266 |
|
266 | 267 | elif "api" in pagename:
|
@@ -413,10 +414,25 @@ def convert_pdf_to_png(pdf_path: pathlib.Path, output_dir: pathlib.Path, output_
|
413 | 414 | images[0].save(output_dir / output_png, "PNG")
|
414 | 415 | except Exception as e:
|
415 | 416 | raise RuntimeError(
|
416 |
| - f"Failed to convert PDF to PNG: {e}, ensure `poppler` is installed. See https://pypi.org/project/pdf2image/" # noqa: E501 |
| 417 | + f"Failed to convert PDF to PNG: {e}. Ensure the PDF file is valid and poppler is installed." # noqa: E501 |
417 | 418 | )
|
418 | 419 |
|
419 | 420 |
|
| 421 | +def run_quarto_command(command, cwd): |
| 422 | + """Run quarto command and logs its output.""" |
| 423 | + command = ["quarto"] + command |
| 424 | + try: |
| 425 | + result = subprocess.run(command, cwd=cwd, check=True, capture_output=True, text=True) |
| 426 | + if result.stdout: |
| 427 | + logger.info(result.stdout) |
| 428 | + |
| 429 | + if result.stderr: |
| 430 | + logger.info(result.stderr) |
| 431 | + |
| 432 | + except subprocess.CalledProcessError as e: |
| 433 | + raise RuntimeError(f"Failed to run the command: {e}") |
| 434 | + |
| 435 | + |
420 | 436 | def add_cheat_sheet(
|
421 | 437 | app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: nodes.document
|
422 | 438 | ) -> None:
|
@@ -459,78 +475,69 @@ def build_quarto_cheatsheet(app: Sphinx):
|
459 | 475 | return
|
460 | 476 |
|
461 | 477 | cheatsheet_file = cheatsheet_options.get("file", "")
|
462 |
| - output_dir = "_static" |
463 |
| - version = cheatsheet_options.get("version", "main") |
464 |
| - |
465 | 478 | if not cheatsheet_file:
|
466 | 479 | return
|
467 | 480 |
|
| 481 | + output_dir = "_static" |
| 482 | + version = cheatsheet_options.get("version", "main") |
| 483 | + |
468 | 484 | cheatsheet_file = pathlib.Path(app.srcdir) / cheatsheet_file
|
| 485 | + output_dir_path = pathlib.Path(app.outdir) / output_dir |
469 | 486 | file_name = str(cheatsheet_file.name)
|
470 | 487 | file_path = cheatsheet_file.parent
|
471 |
| - output_dir_path = pathlib.Path(app.outdir) / output_dir |
472 |
| - try: |
473 |
| - # Add the cheatsheet to the Quarto project |
474 |
| - subprocess.run( |
475 |
| - [ |
476 |
| - "quarto", |
477 |
| - "add", |
478 |
| - f"ansys/pyansys-quarto-cheatsheet@{CHEAT_SHEET_QUARTO_EXTENTION_VERSION}", |
479 |
| - "--no-prompt", |
480 |
| - ], |
481 |
| - cwd=file_path, |
482 |
| - capture_output=True, |
483 |
| - text=True, |
484 |
| - ) |
485 |
| - |
486 |
| - # Render the cheatsheet |
487 |
| - subprocess.run( |
488 |
| - [ |
489 |
| - "quarto", |
490 |
| - "render", |
491 |
| - file_name, |
492 |
| - "--to", |
493 |
| - "cheat_sheet-pdf", |
494 |
| - "--output-dir", |
495 |
| - output_dir_path, |
496 |
| - "-V", |
497 |
| - f"version={version}", |
498 |
| - ], |
499 |
| - cwd=file_path, |
500 |
| - capture_output=True, |
501 |
| - text=True, |
502 |
| - ) |
503 |
| - |
504 |
| - # Remove the cheatsheet from the Quarto project |
505 |
| - subprocess.run( |
506 |
| - ["quarto", "remove", "ansys/cheat_sheet", "--no-prompt"], |
507 |
| - cwd=file_path, |
508 |
| - capture_output=True, |
509 |
| - text=True, |
510 |
| - ) |
511 |
| - |
512 |
| - # Remove all supplementary files |
513 |
| - supplementary_files = [ |
514 |
| - "_static/slash.png", |
515 |
| - "_static/bground.png", |
516 |
| - "_static/ansys.png", |
517 |
| - ] |
518 |
| - for file in supplementary_files: |
519 |
| - file_path = cheatsheet_file.parent / file |
520 |
| - if file_path.exists(): |
521 |
| - file_path.unlink() |
522 | 488 |
|
523 |
| - # If static folder is clean, delete it |
524 |
| - if not list(cheatsheet_file.parent.glob("_static/*")): |
525 |
| - cheatsheet_file.parent.joinpath("_static").rmdir() |
| 489 | + logger.info(f"Building Quarto cheatsheet: {file_name}") |
| 490 | + |
| 491 | + # Adapt with new |
| 492 | + run_quarto_command(["--version"], file_path) |
| 493 | + run_quarto_command( |
| 494 | + [ |
| 495 | + "add", |
| 496 | + f"ansys/pyansys-quarto-cheatsheet@{CHEAT_SHEET_QUARTO_EXTENTION_VERSION}", |
| 497 | + "--no-prompt", |
| 498 | + ], |
| 499 | + file_path, |
| 500 | + ) |
| 501 | + run_quarto_command( |
| 502 | + [ |
| 503 | + "render", |
| 504 | + file_name, |
| 505 | + "--to", |
| 506 | + "cheat_sheet-pdf", |
| 507 | + "--output-dir", |
| 508 | + output_dir_path, |
| 509 | + "-V", |
| 510 | + f"version={version}", |
| 511 | + ], |
| 512 | + file_path, |
| 513 | + ) |
| 514 | + run_quarto_command( |
| 515 | + ["remove", "ansys/pyansys-quarto-cheatsheet", "--no-prompt"], |
| 516 | + file_path, |
| 517 | + ) |
| 518 | + supplementary_files = [ |
| 519 | + "_static/slash.png", |
| 520 | + "_static/bground.png", |
| 521 | + "_static/ansys.png", |
| 522 | + ] |
| 523 | + for file in supplementary_files: |
| 524 | + file_path = cheatsheet_file.parent / file |
| 525 | + if file_path.exists(): |
| 526 | + file_path.unlink() |
526 | 527 |
|
527 |
| - except subprocess.CalledProcessError as e: |
528 |
| - raise RuntimeError(f"Failed to build Quarto cheatsheet: {e}. Ensure Quarto is installed.") |
| 528 | + # If static folder is clean, delete it |
| 529 | + if not list(cheatsheet_file.parent.glob("_static/*")): |
| 530 | + cheatsheet_file.parent.joinpath("_static").rmdir() |
529 | 531 |
|
530 | 532 | output_file = output_dir_path / file_name.replace(".qmd", ".pdf")
|
531 | 533 | app.config.html_theme_options["cheatsheet"]["output_dir"] = f"{output_dir}/{output_file.name}"
|
532 | 534 | output_png = file_name.replace(".qmd", ".png")
|
| 535 | + # Check output file exists |
| 536 | + if not output_file.exists(): |
| 537 | + raise FileNotFoundError(f"Failed to build Quarto cheatsheet: {output_file} does not exist.") |
| 538 | + |
533 | 539 | convert_pdf_to_png(output_file, output_dir_path, output_png)
|
| 540 | + logger.info(f"Cheat sheet build finished successfully: {output_file}") |
534 | 541 | app.config.html_theme_options["cheatsheet"]["thumbnail"] = f"{output_dir}/{output_png}"
|
535 | 542 |
|
536 | 543 |
|
|
0 commit comments