-
Notifications
You must be signed in to change notification settings - Fork 65
Fix image handling for RTD builds #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Implement automatic image collection from multiple source locations - Add path fixing for included markdown files (source and doctree level) - Use builder-inited event for reliable image copying timing - Add build output copying to ensure images are available in HTML - Fix HTML paths to use relative paths for local file viewing - Remove debug code and update README with image handling instructions This fix ensures images work correctly on both branch and main RTD builds, as well as local builds. The solution is generic and works for any contributor adding images anywhere in the documentation. Tested and verified on both test branch and main branch RTD builds.
aboada
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have reviewed conf.py. Some of the comments apply to other parts of the code.
Please let me know if you have any questions or need any help. Thanks!
| # Source locations to copy from | ||
| image_sources = [ | ||
| # Main image directory (for OSE_ORGANIZATION.md and similar) | ||
| repo_root / "doc" / "assets" / "img", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check the validity of repo_root before use or handle any exceptions?
| try: | ||
| shutil.copy2(img, dest_file) | ||
| copied_count += 1 | ||
| except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should handle specific errors here. Some examples: PermissionError, FileNotFoundError. It applies to other exceptions in this file.
| except Exception as e: | ||
| print(f"Warning: Could not copy {img.name}: {e}") | ||
| # Don't warn on overwrites (same file) | ||
| if "already exists" not in str(e).lower(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same as:
exception shutil.SameFileError
This exception is raised if source and destination in copyfile() are the same file.
from shutil-documentation.
| figures_dir = source_file.parent / "figures" | ||
| if figures_dir.exists(): | ||
| # Copy to _images (they'll be found there) | ||
| for pattern in ["*.png", "*.jpg", "*.jpeg", "*.gif", "*.svg"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we are processing images every time we find a markdown file. My suggestion for improving this is to keep track of the images (a set may do the job) and process them once.
| 2. Standalone files with relative paths also benefit from the fix | ||
| 3. The logic is safe - it only changes relative paths and skips absolute paths/URLs | ||
| """ | ||
| import re |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest adhering to PEP8 guidelines.
From PEP8:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:Standard library imports.
Related third party imports.
Local application/library specific imports.
You should put a blank line between each group of imports.
| pass # Ignore overwrites | ||
|
|
||
|
|
||
| def fix_included_image_paths_source(app, docname, source): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the functions in this file are too long and may be against the Single Responsibility Principle. I suggest keeping them short, concise, and focusing on a single concern (when possible). Just a comment for the future and something to keep in mind, it does not need to be addressed here.
|
|
||
| import shutil | ||
| from pathlib import Path | ||
| import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import.
| pass # Ignore overwrites | ||
|
|
||
|
|
||
| def fix_included_image_paths_source(app, docname, source): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think app and docname are not used within this scope. Please check.
| return fixed_path | ||
|
|
||
| # Match markdown image syntax:  | ||
| image_pattern = r'!\[([^\]]*)\]\(([^)]+)\)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this regex consider a line with 2 or more images? What if the image has a title like:

See markdown-guide.
What type of PR is this? (check all applicable)
Description
This PR fixes an issue where images worked correctly on branch RTD builds but failed when merged to main. The solution implements automatic image handling that works consistently across all build environments (local, branch RTD, and main RTD).
Key Changes:
doc/assets/img/,figures/directories, etc.)config-initedtobuilder-initedevent for more reliable timingTechnical Details:
The fix uses Sphinx event hooks to:
builder-initedevent)source-readevent)doctree-readevent)build-finishedevent)build-finishedevent)The solution is generic and works for any contributor adding images anywhere in the documentation, without requiring special configuration or knowledge of Sphinx internals.
Related Issues & Documents
QA Instructions, Screenshots, Recordings
RTD Hosted link -- https://mole-form.readthedocs.io/en/main/
Testing Performed:
make doc-htmldoc/assets/img/(OSE_ORGANIZATION.md images)source/math_functions/figures/(StaggeredGrids.md, CSRCReportOnMOLE.md)source/api/examples/md/figures/(example figures)figures/directoriesHow to Test:
cd doc/sphinx make doc-clean make doc-htmlbuild/html/outputdoc/assets/img/or afigures/directoryScreenshots:
Added/updated tests?
have not been included
Read Contributing Guide and Code of Conduct
[optional] Are there any post deployment tasks we need to perform?
No post-deployment tasks required. The fix is automatic and works for all existing and future images.
[optional] What gif best describes this PR or how it makes you feel?