diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 499276c6..12fda5e2 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -69,4 +69,4 @@ jobs: run: poetry run poe docs-run & - name: Accessibility testing (Pa11y) - run: poetry run poe docs-check-ci + run: poetry run poe docs-check diff --git a/pyproject.toml b/pyproject.toml index d1026d4e..ca0041d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,26 +62,6 @@ xmltodict = "*" jrnl = 'jrnl.cli:cli' [tool.poe.tasks] -script-clean-docs.interpreter = "python" -script-clean-docs.shell = """ -import pathlib -files = ["sitemap.xml", "list.json"] -for f in files: - pathlib.Path(f).unlink(missing_ok=True) -""" -script-generate-sitemap.interpreter = "python" -script-generate-sitemap.shell = ''' -import requests -sitemap = requests.get("http://127.0.0.1:8000/sitemap.xml") -with open('sitemap.xml', 'wb+') as f: - f.write(sitemap.content) -''' -script-generate-page-list-from-sitemap.shell = ''' - select='{urls: ["http://127.0.0.1:8000/", "http://127.0.0.1:8000/search.html?q=jrnl", .urlset.url[].loc]}' - poetry run xq "$select" sitemap.xml > list.json -''' -script-run-pa11y.cmd = "pa11y-ci -c list.json" - format-run = [ {cmd = "black ."}, ] @@ -100,31 +80,22 @@ sort-check = [ {cmd = "isort --version"}, {cmd = "isort --check ."}, ] + +docs-clean = {script = "tasks:delete_files(['sitemap.xml', 'config.json'])"} + docs-check = [ - "script-clean-docs", - "script-generate-sitemap", - "script-generate-page-list-from-sitemap", - "script-run-pa11y", - "script-clean-docs", -] -docs-check-ci = [ - "script-generate-sitemap", - {shell = ''' - echo "::group::sitemap.xml" - cat sitemap.xml - echo '::endgroup::' - '''}, - "script-generate-page-list-from-sitemap", - {shell = ''' - echo "::group::list.json" - cat list.json - echo '::endgroup::' - '''}, - "script-run-pa11y", + "docs-clean", + {script = "tasks:generate_sitemap"}, + {script = "tasks:output_file('sitemap.xml')"}, + {script = "tasks:generate_pa11y_config_from_sitemap"}, + {script = "tasks:output_file('config.json')"}, + {cmd = "pa11y-ci -c config.json"}, + "docs-clean", ] docs-run = [ {cmd = "mkdocs serve"}, ] + test-run =[ {cmd = "tox -q -e py --"}, ] diff --git a/tasks.py b/tasks.py new file mode 100644 index 00000000..30e9635f --- /dev/null +++ b/tasks.py @@ -0,0 +1,52 @@ +# Copyright © 2012-2022 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +DOCS_URL = "http://127.0.0.1:8000" +SITEMAP_FILENAME = "sitemap.xml" +CONFIG_FILENAME = "config.json" + +def delete_files(files): + import pathlib + + for file in files: + pathlib.Path(file).unlink(missing_ok=True) + + +def generate_sitemap(): + import requests + + sitemap = requests.get(f"{DOCS_URL}/{SITEMAP_FILENAME}") + with open(SITEMAP_FILENAME, 'wb') as f: + f.write(sitemap.content) + + +def generate_pa11y_config_from_sitemap(): + import xmltodict + import json + + with open(SITEMAP_FILENAME) as f: + xml_sitemap = xmltodict.parse(f.read()) + + urls = [ + f"{DOCS_URL}/", + f"{DOCS_URL}/search.html?q=jrnl", + ] + urls += [url["loc"] for url in xml_sitemap["urlset"]["url"]] + + with open(CONFIG_FILENAME, 'w') as f: + f.write(json.dumps({"urls": urls})) + + +def output_file(file): + import os + + if not os.getenv("CI", False): + return + + print(f"::group::{file}") + + with open(file) as f: + print(f.read()) + + print('::endgroup::') +