-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CI to build SVGs with images embedded
- Loading branch information
1 parent
04e83bc
commit 55a569c
Showing
3 changed files
with
106 additions
and
0 deletions.
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,40 @@ | ||
name: Update dist | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'images/experiments/**' | ||
- '.github/workflows/dist.yml' | ||
- 'scripts/embed_images.py' | ||
schedule: | ||
- cron: "30 2 * * *" | ||
workflow_dispatch: ~ | ||
|
||
jobs: | ||
update: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
|
||
- name: Update | ||
run: python -u ./scripts/embed_images.py | ||
|
||
- name: Get current time | ||
run: echo "curr_time=$(date -uIs)" >> $GITHUB_ENV | ||
|
||
- name: Push | ||
uses: JamesIves/github-pages-deploy-action@v4 | ||
with: | ||
branch: dist | ||
folder: dist | ||
git-config-name: github-actions[bot] | ||
git-config-email: 41898282+github-actions[bot]@users.noreply.github.com | ||
commit-message: ${{ env.curr_time }} |
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,2 @@ | ||
.idea/ | ||
dist/ |
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,64 @@ | ||
import re | ||
import base64 | ||
from urllib import request | ||
from http.client import HTTPResponse | ||
from pathlib import Path | ||
from functools import lru_cache | ||
from typing import Optional | ||
|
||
matcher = re.compile(r'(?<=xlink:href=")([^"]+)(?=")') | ||
|
||
|
||
@lru_cache(maxsize=64) | ||
def fetch(url: str) -> tuple[bytes, Optional[str]]: | ||
print(f'Fetching {url}') | ||
response: HTTPResponse | ||
for _ in range(3): | ||
try: | ||
with request.urlopen(url, timeout=10) as response: | ||
if response.status != 200: | ||
raise Exception(f'{response.status} {response.reason}') | ||
content_type = response.getheader('Content-Type') | ||
print(f'Fetched {url} with content type {content_type}') | ||
return response.read(), content_type | ||
except Exception as e: | ||
print(f'Failed to fetch {url}: {e}') | ||
if _ == 2: | ||
raise e | ||
|
||
|
||
def replacer(match: re.Match) -> str: | ||
url = match.group(1) | ||
data, content_type = fetch(url) | ||
data = base64.b64encode(data).decode() | ||
content_type = content_type or 'image/png' | ||
return f'data:{content_type};base64,{data}' | ||
|
||
|
||
def embed_images(svg: str) -> str: | ||
return matcher.sub(replacer, svg) | ||
|
||
|
||
def main(): | ||
base_path = Path(__file__).parent.parent | ||
dist_path = base_path / 'dist' | ||
source_path = base_path / 'images' / 'experiments' | ||
|
||
dist_path.mkdir(parents=True, exist_ok=True) | ||
|
||
with (dist_path / 'README.md').open('w') as readme: | ||
readme.write('# Dist\n\n') | ||
|
||
for file in sorted(source_path.glob('*.svg')): | ||
print(f'Processing {file}') | ||
with open(file, 'r') as f: | ||
svg = f.read() | ||
svg = embed_images(svg) | ||
with open(dist_path / file.name, 'w') as f: | ||
f.write(svg) | ||
readme.write(f'## [{file.name}]({file.name})\n\n' | ||
f'[![{file.name}]({file.name})]({file.name})\n\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |