|
2 | 2 |
|
3 | 3 | from datetime import datetime |
4 | 4 | import os |
| 5 | +import subprocess |
5 | 6 |
|
6 | 7 | from ansys_sphinx_theme import ansys_favicon, convert_version_to_pymeilisearch, get_version_match |
| 8 | +import sphinx |
7 | 9 | from sphinx.builders.latex import LaTeXBuilder |
8 | 10 |
|
9 | 11 | from pyansys import __version__ as pyansys_version |
|
104 | 106 | user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.2420.81" # noqa: E501 |
105 | 107 |
|
106 | 108 |
|
| 109 | +########################################################################### |
| 110 | + |
| 111 | +# ------------------------------------------------------------------------- |
| 112 | +# Defining Sphinx build hooks |
| 113 | +# ------------------------------------------------------------------------- |
| 114 | + |
107 | 115 | ########################################################################### |
108 | 116 | # Generate the package_versions directory |
109 | 117 | ########################################################################### |
@@ -348,14 +356,105 @@ def get_release_branches_in_metapackage(): |
348 | 356 | return ["main"] + release_branches, ["dev"] + versions |
349 | 357 |
|
350 | 358 |
|
351 | | -# ------------------------------------------------------------------------- |
352 | | -# Execute the previous functions to generate the package_versions directory |
353 | | -# ------------------------------------------------------------------------- |
| 359 | +########################################################################### |
| 360 | +# Adapting thumbnails to the documentation |
| 361 | +########################################################################### |
| 362 | +# This section adapts the thumbnails to the documentation. The thumbnails |
| 363 | +# are resized to 640x480 pixels and centered on a white background. |
| 364 | +# |
| 365 | +# The script resizes all images in the _static/thumbnails directory to 640x480 |
| 366 | +# pixels and saves the resized images in the same directory. After the |
| 367 | +# documentation build, the script reverts the changes to the thumbnails. |
| 368 | + |
| 369 | + |
| 370 | +def resize_with_background(input_image_path, output_image_path, target_size): |
| 371 | + """Resize an image while maintaining aspect ratio and centering it on a white background. |
| 372 | +
|
| 373 | + Parameters |
| 374 | + ---------- |
| 375 | + input_image_path : Path |
| 376 | + The path to the input image. |
| 377 | + output_image_path : Path |
| 378 | + The path to save the output image. |
| 379 | + target_size : tuple[int, int] |
| 380 | + The target size of the output image as a tuple (width, height) in pixels. |
| 381 | + """ |
| 382 | + from PIL import Image |
| 383 | + |
| 384 | + # Open the input image |
| 385 | + img = Image.open(input_image_path).convert("RGBA") # Ensure the image has an alpha channel |
| 386 | + |
| 387 | + # Resize the image while maintaining aspect ratio |
| 388 | + img.thumbnail(target_size, Image.LANCZOS) |
| 389 | + |
| 390 | + # Create a white background of the target size |
| 391 | + background = Image.new( |
| 392 | + "RGBA", target_size, (255, 255, 255, 255) |
| 393 | + ) # White background with no transparency |
| 394 | + |
| 395 | + # Calculate the position to center the resized image on the background |
| 396 | + img_w, img_h = img.size |
| 397 | + bg_w, bg_h = background.size |
| 398 | + offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2) |
| 399 | + |
| 400 | + # Paste the resized image onto the white background |
| 401 | + background.paste(img, offset, mask=img) # Use the image's transparency as a mask |
| 402 | + |
| 403 | + # Convert the image to RGB to remove the alpha channel (no transparency) |
| 404 | + background = background.convert("RGB") |
| 405 | + |
| 406 | + # Save the result to the output path |
| 407 | + background.save(output_image_path) |
354 | 408 |
|
355 | | -branches, versions = get_release_branches_in_metapackage() |
356 | | -generate_rst_files( |
357 | | - versions, |
358 | | - {version: build_versions_table(branch) for version, branch in zip(versions, branches)}, |
359 | | -) |
360 | 409 |
|
361 | 410 | ########################################################################### |
| 411 | + |
| 412 | +# -------------------------------------------------------------------------- |
| 413 | +# Sphinx build hooks |
| 414 | +# -------------------------------------------------------------------------- |
| 415 | + |
| 416 | + |
| 417 | +def resize_thumbnails(app: sphinx.application.Sphinx): |
| 418 | + """Resize all images in the current directory to 640x480 pixels.""" |
| 419 | + # Process all images |
| 420 | + from pathlib import Path |
| 421 | + |
| 422 | + thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails" |
| 423 | + |
| 424 | + for image in thumbnail_dir.glob("*.png"): |
| 425 | + target_size = (640, 480) |
| 426 | + resize_with_background(image, image, target_size) |
| 427 | + |
| 428 | + |
| 429 | +def revert_thumbnails(app: sphinx.application.Sphinx, exception): |
| 430 | + """Resize all images in the current directory to 640x480 pixels.""" |
| 431 | + from pathlib import Path |
| 432 | + |
| 433 | + thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails" |
| 434 | + |
| 435 | + subprocess.run(["git", "checkout", "--", thumbnail_dir]) |
| 436 | + |
| 437 | + |
| 438 | +def package_versions_table(app: sphinx.application.Sphinx): |
| 439 | + """Generate the package_versions directory.""" |
| 440 | + branches, versions = get_release_branches_in_metapackage() |
| 441 | + generate_rst_files( |
| 442 | + versions, |
| 443 | + {version: build_versions_table(branch) for version, branch in zip(versions, branches)}, |
| 444 | + ) |
| 445 | + |
| 446 | + |
| 447 | +def setup(app: sphinx.application.Sphinx): |
| 448 | + """Run different hook functions during the documentation build. |
| 449 | +
|
| 450 | + Parameters |
| 451 | + ---------- |
| 452 | + app : sphinx.application.Sphinx |
| 453 | + Sphinx instance containing all the configuration for the documentation build. |
| 454 | + """ |
| 455 | + # At the beginning of the build process - update the version in cheatsheet |
| 456 | + app.connect("builder-inited", resize_thumbnails) |
| 457 | + app.connect("builder-inited", package_versions_table) |
| 458 | + |
| 459 | + # Reverting the thumbnails - no local changes |
| 460 | + app.connect("build-finished", revert_thumbnails) |
0 commit comments