-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinline-readme-svg.py
More file actions
executable file
·34 lines (27 loc) · 1006 Bytes
/
inline-readme-svg.py
File metadata and controls
executable file
·34 lines (27 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# Inline SVGs into README.md for crates.io release
import base64
import re
import urllib.parse
in_file = 'README.md'
out_file = '.cargo.README.md'
img_re = re.compile(r'<img(?:\s*)src="([^"]+)"(?:\s*)/>')
def svg_to_data_url(contents: bytes) -> str:
# b64 = base64.b64encode(contents).decode('ascii')
mediatype = 'image/svg+xml'
# return f'data:{mediatype};base64,{b64}'
contents = urllib.parse.quote_from_bytes(contents)
return f'data:{mediatype},{contents}'
def main():
with open(in_file) as f, open(out_file, 'w') as fw:
for line in f:
if (m := img_re.match(line)) is not None:
svg_filename = m.group(1)
print(svg_filename)
with open(svg_filename, 'rb') as svg:
svg_contents = svg.read()
data_url = svg_to_data_url(svg_contents)
line = f"<img src='{data_url}'/>\n"
fw.write(line)
if __name__ == '__main__':
main()