Skip to content
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

Automate theme README screenshots #43

Merged
merged 39 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
14b8e58
add update readme script
gabalafou Feb 23, 2024
431cad4
run script then pre-commit hook to update all theme READMEs
gabalafou Feb 23, 2024
6487cce
rename variable theme_docstring
gabalafou Feb 23, 2024
ef34459
deduplicate contrast table rows
gabalafou Feb 23, 2024
c4ec57b
comments and file name clarfication
gabalafou Feb 23, 2024
9875293
match variable names
gabalafou Feb 23, 2024
0277540
rename two screenshot files for consistency
gabalafou Feb 23, 2024
779ef70
Add Jinja2, add Hatch command, and update CONTRIBUTING.md
gabalafou Mar 6, 2024
a0657dd
Rename test to tests
gabalafou Mar 25, 2024
71aeae1
Add Playwright to take screenshots when updating theme READMEs
gabalafou Mar 25, 2024
6abdafe
Update screenshots
gabalafou Mar 25, 2024
20ea058
Update rendered html files of code samples for each theme
gabalafou Mar 25, 2024
76162d5
fix script
gabalafou Mar 25, 2024
d4af402
Run prettier on theme README when updating
gabalafou Mar 26, 2024
4e74003
Revert "Rename test to tests"
gabalafou Mar 26, 2024
a1c3a2d
harmonize with #41
gabalafou Mar 26, 2024
583027b
revert test/results
gabalafou Mar 26, 2024
714bd98
Update a11y_pygments/utils/wcag_contrast.py
gabalafou Mar 27, 2024
1989c7b
Update a11y_pygments/utils/wcag_contrast.py
gabalafou Mar 27, 2024
e82e202
Update scripts/templates/README.md
gabalafou Mar 27, 2024
0be052b
Update CONTRIBUTING.md
gabalafou Mar 27, 2024
7053590
Merge branch 'main' into update-readme-script
gabalafou Mar 27, 2024
f649b77
Run update_readmes script
gabalafou Mar 27, 2024
81ef01f
Update scripts/update_readme_for_theme.py
gabalafou Mar 27, 2024
97d5b86
Remove Jinja
gabalafou Mar 27, 2024
34ad110
some refactoring, renaming
gabalafou Mar 28, 2024
d3ed304
doc strings, comments, type hints
gabalafou Mar 28, 2024
3b05288
more comments
gabalafou Mar 28, 2024
6b31bc9
update docs
gabalafou Mar 28, 2024
ab5e3a6
Update scripts/update_theme_docs.py
gabalafou Mar 28, 2024
4365669
Merge branch 'update-readme-script' into auto-shot
gabalafou Mar 28, 2024
2d9c5b1
Update screenshots
gabalafou Mar 28, 2024
0d09542
path.as_uri
gabalafou Mar 28, 2024
7e1f786
undo irrelevant changes
gabalafou Mar 28, 2024
208ef9b
safer assertion
gabalafou Mar 28, 2024
a809be6
typo
gabalafou Mar 28, 2024
b6c61e7
update docs
gabalafou Mar 28, 2024
b085802
Merge branch 'main' into auto-shot
gabalafou Mar 28, 2024
d32939e
update help text
gabalafou Mar 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ Once you are happy with the colors and the rules in the style file, please updat
hatch run dev:update_theme_docs your_theme_module_name
```

This script will generate a table of contrast ratios of the colors you've chosen and their compliance with WCAG. It will also pull the docstring from the `Theme` class and put it in the README as the description for your theme. If you are porting a theme or color palette developed elsewhere please acknowledge your source(s) in the docstring of the `Theme` class.
This script will:

- generate a table of contrast ratios of the colors you've chosen and their compliance with WCAG,
- pull the docstring from the `Theme` class and put it in the README as the description for your theme (if you are porting a theme or color
palette developed elsewhere please acknowledge your source(s) in the docstring of the `Theme` class),
- take a screenshot of your theme applied to a code sample, save it at `a11y_pygments/<your_theme>/images/<your_theme>.png`, and embed it in the theme README.

Also, don't forget to add the name of your theme to our list of supported themes in the [main README](README.md).

Expand Down
Binary file modified a11y_pygments/a11y_dark/images/a11y-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/a11y_light/images/a11y-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/blinds_dark/images/blinds-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/blinds_light/images/blinds-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/github_dark/images/github-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/github_light/images/github-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/gotthard_dark/images/gotthard-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/gotthard_light/images/gotthard-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/greative/images/greative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified a11y_pygments/pitaya_smoothie/images/pitaya-smoothie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 16 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ authors = [
{ name = "Stephannie Jimenez Gacha", email = "steff456@hotmail.com" },
{ name = "Tania Allard", email = "trallard@bitsandchips.me" },
]
maintainers= [
maintainers = [
# TODO @gabalafou to add his email
{name = "Gabriel Fouasnon", email=""},
{ name = "Gabriel Fouasnon", email = "" },
]
keywords = ["a11y", "accessibility", "pygments", "WCAG", "Sphinx"]
classifiers = [
Expand Down Expand Up @@ -74,15 +74,25 @@ raw-options = { local_scheme = "no-local-version" }

[project.optional-dependencies]
tests = ["hypothesis", "pytest"]
dev = ["pre-commit",
"twine>=5.0",
"pkginfo>=1.10", # needed to support metadata 2.3
dev = [
"pre-commit",
"setuptools",
"playwright",
"twine>=5.0",
"pkginfo>=1.10", # needed to support metadata 2.3
]

[tool.hatch.build.targets]
sdist = { exclude = ["/.github", "/docs", "/test", "/scripts"] }
wheel = { packages = ["a11y_pygments"] }

[tool.hatch.envs.dev]
features = ["dev"]
gabalafou marked this conversation as resolved.
Show resolved Hide resolved
post-install-commands = ["playwright install"]

[tool.hatch.envs.test]
features = ["tests"]
gabalafou marked this conversation as resolved.
Show resolved Hide resolved

[tool.hatch.envs.dev.scripts]
render_html = "python test/render_html.py"
create_css = "python test/run_css.py --save-dir docs/_build"
Expand All @@ -99,6 +109,6 @@ skip_gitignore = true
fix = true
exclude = ["test/scripts/*.py"]
ignore = [
"E501"
"E501",
# line too long | Black takes care of this
]
50 changes: 40 additions & 10 deletions scripts/update_theme_docs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# This script generates READMEs for themes from their style.py definitions

import logging
import string
import sys
from argparse import ArgumentParser
from importlib import import_module
from inspect import getdoc
from pathlib import Path
from typing import Type

from playwright.sync_api import sync_playwright
from pygments.styles import get_style_by_name

from a11y_pygments.utils.utils import find_all_themes_packages
Expand All @@ -17,11 +21,14 @@
hexstr_without_hash,
)

# This script generates READMEs for themes from their style.py definitions

HERE = Path(__file__).parent
REPO = HERE.parent

# TODO fix this hack later when restructuring the repo
sys.path.append(str(REPO / "test"))
from render_html import outdir as html_outdir # noqa: E402
from render_html import render_html # noqa: E402


def markdown_table(rows: list[list[str]]) -> str:
"""Pretty format a table as Markdown
Expand Down Expand Up @@ -58,7 +65,16 @@ def markdown_table(rows: list[list[str]]) -> str:


def contrast_markdown_table(color_cls: Type, background_color: str) -> list[list[str]]:
"""Calculate contrast ratios and WCAG ratings for all foreground colors"""
"""Create Markdown table of contrast ratios and WCAG ratings for foreground colors against background color

Args:
color_cls (class object): A mapping of color names to six-value hex CSS color
strings. The mapping comes from a class named Colors defined in
<theme-name>/style.py.
background_color (str): Theme default background color for code blocks
in six-value #RRGGBB hex format. For example, white would be "#ffffff"
(case insensitive).
"""

# Start table
rows = [["Color", "Hex", "Ratio", "Normal text", "Large text"]]
Expand Down Expand Up @@ -104,11 +120,22 @@ def contrast_markdown_table(color_cls: Type, background_color: str) -> list[list
def update_readme(theme: str):
"""Given a theme module name, update that theme's README file on disk"""

theme_kebab_case = theme.replace("_", "-")
outdir = REPO / "a11y_pygments" / theme

# Take a screenshot of the theme applied to a sample bash script
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(viewport={"width": 620, "height": 720})
bash_sample_rendered_html = html_outdir / theme_kebab_case / "bash.html"
page.goto(bash_sample_rendered_html.absolute().as_uri())
page.screenshot(path=outdir / "images" / f"{theme_kebab_case}.png")
browser.close()

# Get the theme colors
theme_style_module = import_module(f"a11y_pygments.{theme}.style")
color_cls = theme_style_module.Colors # access to foreground colors
theme_cls = theme_style_module.Theme # access to docstring
theme_kebab_case = theme.replace("_", "-")
style = get_style_by_name(
theme_kebab_case
) # access to background and highlight colors
Expand Down Expand Up @@ -137,22 +164,25 @@ def update_readme(theme: str):
if __name__ == "__main__":
parser = ArgumentParser(
prog="Update theme readme",
description="Updates the README.md file for themes in the accessible-pygments repo",
description="Updates theme README.md files in the accessible-pygments repo",
)
parser.add_argument(
"themes", nargs="*", help="Name(s) of theme(s) to update (example: a11y_dark)"
"themes", nargs="*", help="Themes to update (example: a11y_dark)"
)
args = parser.parse_args()

# Check that each theme provided on the command line is a known theme. If
# not, error and exit immediately
all_themes = find_all_themes_packages()
for theme in args.themes:
assert (
theme in all_themes
), "Invalid theme (did you forget to use an underscore _ instead of a hyphen -?)"
for i, theme in enumerate(args.themes):
if "-" in theme:
logging.info("Converting to snake_case: %s", theme)
args.themes[i] = theme = theme.replace("-", "_")
assert theme in all_themes, f"Theme {theme} not found"

# Update themes provided or, if none provided, update all themes in the repo
themes = args.themes or all_themes
render_html(theme.replace("_", "-") for theme in themes)

for theme in themes:
update_readme(theme)
Loading