-
Notifications
You must be signed in to change notification settings - Fork 91
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
Add the sphinx-alt-text-validator tool to VCS install it in each API repo #2445
Merged
arnaucasau
merged 8 commits into
Qiskit:main
from
arnaucasau:AC/sphinx-alt-text-validator
Dec 9, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0ce521d
Initial structure
arnaucasau e4fbdc9
new pyproject.toml
arnaucasau dddaba5
rename folder
arnaucasau 16c0135
fix path
arnaucasau 0b4afb9
Remove print
arnaucasau 943fc25
Fix script
arnaucasau e79dd82
remove src folder
arnaucasau ad331ba
Update scripts/image-tester/sphinx_alt_text_validator/__init__.py
arnaucasau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "sphinx-alt-text-validator" | ||
version = "0.0.1" | ||
authors = [ | ||
{ name="Qiskit docs team" }, | ||
] | ||
description = "This is a tool that helps improving the accessibility of a project that uses Sphinx to build their documentation by detecting images without alt text defined" | ||
requires-python = ">=3.8" | ||
license = "Apache-2.0" | ||
classifiers = [ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Operating System :: OS Independent", | ||
] | ||
|
||
[project.scripts] | ||
sphinx-alt-text-validator = "sphinx_alt_text_validator:main" |
59 changes: 59 additions & 0 deletions
59
scripts/image-tester/sphinx_alt_text_validator/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2024 | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
from .verify_images import validate_image | ||
import multiprocessing | ||
import glob | ||
import sys | ||
import argparse | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(prog="verify_images.py") | ||
parser.add_argument("-f", "--folder", required=True) | ||
parser.add_argument("-s", "--skip", nargs="+") | ||
args = parser.parse_args() | ||
|
||
skip_list = args.skip or [] | ||
files = glob.glob(f"{args.folder}/**/*.py", recursive=True) | ||
filtered_files = [file for file in files if file not in skip_list] | ||
|
||
with multiprocessing.Pool() as pool: | ||
results = pool.map(validate_image, filtered_files) | ||
|
||
failed_files = { | ||
file: image_errors for file, image_errors in results if image_errors | ||
} | ||
|
||
if not failed_files: | ||
print("✅ All images have alt text") | ||
sys.exit(0) | ||
|
||
print("💔 Some images are missing the alt text", file=sys.stderr) | ||
|
||
for file, image_errors in failed_files.items(): | ||
print(f"\nErrors found in {file}:", file=sys.stderr) | ||
|
||
for image_error in image_errors: | ||
print(image_error, file=sys.stderr) | ||
|
||
print( | ||
"\nAlt text is crucial for making documentation accessible to all users.", | ||
"It should serve the same purpose as the images on the page,", | ||
"conveying the same meaning rather than describing visual characteristics.", | ||
"When an image contains words that are important to understanding the content,", | ||
"the alt text should include those words as well.", | ||
file=sys.stderr, | ||
arnaucasau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
sys.exit(1) |
68 changes: 68 additions & 0 deletions
68
scripts/image-tester/sphinx_alt_text_validator/verify_images.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python3 | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2024 | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Utility script to verify that all images have alt text""" | ||
|
||
from pathlib import Path | ||
|
||
|
||
def is_image(line: str) -> bool: | ||
"""Determine if a line is an image""" | ||
return line.strip().startswith((".. image:", ".. plot:")) | ||
|
||
|
||
def is_option(line: str) -> bool: | ||
"""Determine if a line is an option""" | ||
return line.strip().startswith(":") | ||
|
||
|
||
def is_valid_image(options: list[str]) -> bool: | ||
"""Validate one single image""" | ||
alt_exists = any(option.strip().startswith(":alt:") for option in options) | ||
nofigs_exists = any(option.strip().startswith(":nofigs:") for option in options) | ||
|
||
# Only `.. plot::`` directives without the `:nofigs:` option are required to have alt text. | ||
# Meanwhile, all `.. image::` directives need alt text and they don't have a `:nofigs:` option. | ||
return alt_exists or nofigs_exists | ||
|
||
|
||
def validate_image(file_path: str) -> tuple[str, list[str]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same function but removing this conditional: if file_path in ALLOWLIST_MISSING_ALT_TEXT:
return [file_path, []] Now the function gets the files filtered from |
||
"""Validate all the images of a single file""" | ||
|
||
invalid_images: list[str] = [] | ||
|
||
lines = Path(file_path).read_text().splitlines() | ||
|
||
image_found = False | ||
options: list[str] = [] | ||
|
||
for line_index, line in enumerate(lines): | ||
if image_found: | ||
if is_option(line): | ||
options.append(line) | ||
continue | ||
|
||
# Else, the prior image_found has no more options so we should determine if it was valid. | ||
# | ||
# Note that, either way, we do not early exit out of the loop iteration because this `line` | ||
# might be the start of a new image. | ||
if not is_valid_image(options): | ||
image_line = line_index - len(options) | ||
invalid_images.append( | ||
f"- Error in line {image_line}: {lines[image_line-1].strip()}" | ||
) | ||
|
||
image_found = is_image(line) | ||
options = [] | ||
|
||
return (file_path, invalid_images) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: using a set will be faster when the skip list is larger