|
| 1 | +"""Refresh the README typing SVGs with 24px of padding on both sides. |
| 2 | +
|
| 3 | +Run directly to fetch the upstream animations, or pass a directory containing |
| 4 | +light.svg and dark.svg to reuse previously downloaded animations. |
| 5 | +""" |
| 6 | +from pathlib import Path |
| 7 | +import sys |
| 8 | +from urllib.parse import urlencode |
| 9 | +from urllib.request import urlopen |
| 10 | +import xml.etree.ElementTree as ET |
| 11 | + |
| 12 | +NS = "http://www.w3.org/2000/svg" |
| 13 | +ET.register_namespace("", NS) |
| 14 | +ET.register_namespace("xlink", "http://www.w3.org/1999/xlink") |
| 15 | +assets = Path(__file__).resolve().parents[1] / "assets" |
| 16 | +padding, width, height = 24, 900, 62 |
| 17 | + |
| 18 | +for theme, background, color in [("light", "F6F8FA", "1F2328"), ("dark", "000000", "FFE70B")]: |
| 19 | + options = { |
| 20 | + "font": "Source Sans 3", "weight": 600, "size": 25, |
| 21 | + "duration": 2400, "pause": 1400, "background": background, |
| 22 | + "vCenter": "true", "width": width - 2 * padding, "height": height, |
| 23 | + "lines": "https://micromatrix.org;https://blog.micromatrix.org", "color": color, |
| 24 | + } |
| 25 | + data = (Path(sys.argv[1]) / f"{theme}.svg").read_bytes() if len(sys.argv) > 1 else urlopen( |
| 26 | + "https://readme-typing-svg.demolab.com?" + urlencode(options), timeout=45, |
| 27 | + ).read() |
| 28 | + animation = ET.fromstring(data) |
| 29 | + if animation.tag != f"{{{NS}}}svg": |
| 30 | + raise ValueError("Typing service did not return an SVG") |
| 31 | + animation.set("x", str(padding)) |
| 32 | + animation.set("y", "0") |
| 33 | + frame = ET.Element(f"{{{NS}}}svg", { |
| 34 | + "width": str(width), "height": str(height), "viewBox": f"0 0 {width} {height}", |
| 35 | + }) |
| 36 | + ET.SubElement(frame, f"{{{NS}}}rect", {"width": str(width), "height": str(height), "fill": f"#{background}"}) |
| 37 | + frame.append(animation) |
| 38 | + target = assets / f"profile-typing-{theme}.svg" |
| 39 | + target.write_text(ET.tostring(frame, encoding="unicode") + "\n") |
| 40 | + print(f"Updated {target.name}") |
0 commit comments