This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
svg_css_replacement.py
56 lines (47 loc) · 1.7 KB
/
svg_css_replacement.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright © 2021 Adobe, Inc.
# Author: Frank Grießhammer
'''
Convert inline CSS in SVG files to SVG attributes
'''
import pathlib
import re
def convert_css_to_svg(attr_string):
'''
expects a string of inline CSS, and converts them to SVG attributes
'''
attributes = attr_string.split(';')
svg_attrs = []
for attribute in attributes:
if attribute:
# some lines end with ; and others don’t – this means there may be
# an empty string
attr_name, attr_value = attribute.split(':')
svg_attrs.append(f'{attr_name}="{attr_value}"')
return(' '.join(svg_attrs))
# find all SVGs in all folders that may contain them
svgs = []
folders = ['svg', 'svg_bw', 'flags', 'flags_bw']
for folder in folders:
svgs.extend(pathlib.Path(folder).glob('*.svg'))
for svg in svgs:
with open(svg, 'r') as svg_in:
svg_data = svg_in.read()
# check if a SVG even contains an inline style
if re.findall(r'style="', svg_data):
print('fixing', svg)
fixed_svg = []
for line in svg_data.splitlines():
# if it does, fix the styles on a line-by-line basis
style_match = re.match(r'.+(style="(.+?)").*', line)
if style_match:
css_inline_style = style_match.group(1)
css_attrs = style_match.group(2)
svg_attrs = convert_css_to_svg(css_attrs)
fixed_line = line.replace(
style_match.group(1), svg_attrs)
fixed_svg.append(fixed_line)
else:
fixed_svg.append(line)
# save the SVG
with open(svg, 'w') as svg_out:
svg_out.write('\n'.join(fixed_svg))