Use regex substitution
Co-authored-by: kvid <kvid@users.noreply.github.com>
This commit is contained in:
parent
d00cc8362d
commit
d94a249176
@ -9,20 +9,24 @@ from typing import Union
|
|||||||
mime_subtype_replacements = {'jpg': 'jpeg', 'tif': 'tiff'}
|
mime_subtype_replacements = {'jpg': 'jpeg', 'tif': 'tiff'}
|
||||||
|
|
||||||
def embed_svg_images(svg_in: str, base_path: Union[str, Path] = Path.cwd()) -> str:
|
def embed_svg_images(svg_in: str, base_path: Union[str, Path] = Path.cwd()) -> str:
|
||||||
# first, find any image references in SVG data, and cache the respective base64-encoded image
|
|
||||||
images_b64 = {} # cache of base64-encoded images
|
images_b64 = {} # cache of base64-encoded images
|
||||||
re_xlink=re.compile(r'xlink:href="(?P<URL>.*?)"', re.IGNORECASE)
|
|
||||||
for xlink in re_xlink.finditer(svg_in):
|
def image_tag(pre: str, url: str, post: str) -> str:
|
||||||
imgurl = xlink.group('URL')
|
return f'<image{pre} xlink:href="{url}"{post}>'
|
||||||
|
|
||||||
|
def replace(match: re.Match) -> str:
|
||||||
|
imgurl = match['URL']
|
||||||
if not imgurl in images_b64: # only encode/cache every unique URL once
|
if not imgurl in images_b64: # only encode/cache every unique URL once
|
||||||
imgurl_abs = (Path(base_path) / imgurl).resolve()
|
imgurl_abs = (Path(base_path) / imgurl).resolve()
|
||||||
images_b64[imgurl] = base64.b64encode(imgurl_abs.read_bytes()).decode('utf-8')
|
images_b64[imgurl] = base64.b64encode(imgurl_abs.read_bytes()).decode('utf-8')
|
||||||
# second, replace links with the base64-encoded data
|
return image_tag(
|
||||||
svg_out = svg_in
|
match['PRE'] or '',
|
||||||
for url, b64 in images_b64.items():
|
f'data:image/{get_mime_subtype(imgurl)};base64, {images_b64[imgurl]}',
|
||||||
svg_out = svg_out.replace(url, f'data:image/{get_mime_subtype(url)};base64, {b64}')
|
match['POST'] or '')
|
||||||
|
|
||||||
return svg_out
|
pattern = re.compile(
|
||||||
|
image_tag(r'(?P<PRE> [^>]*?)?', r'(?P<URL>[^"]*?)', r'(?P<POST> [^>]*?)?'), re.IGNORECASE)
|
||||||
|
return pattern.sub(replace, svg_in)
|
||||||
|
|
||||||
|
|
||||||
def get_mime_subtype(filename: Union[str, Path]) -> str:
|
def get_mime_subtype(filename: Union[str, Path]) -> str:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user