Skip to content

Commit

Permalink
Add CI to build SVGs with images embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
Rongronggg9 committed Jun 19, 2022
1 parent 04e83bc commit 55a569c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/dist.yml
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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
dist/
64 changes: 64 additions & 0 deletions scripts/embed_images.py
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()

0 comments on commit 55a569c

Please sign in to comment.