Created
June 4, 2020 01:10
-
-
Save s0nerik/e965c7689fdbfa51cac655c452d7b965 to your computer and use it in GitHub Desktop.
Preprocess SVG files for svg_flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding=utf8 | |
import re | |
import io | |
import os | |
import sys | |
regex = r"(<[^>]+href=\"#([^>]+)\"[^<]*>)[^\3]*(<[^>]+id=\"\2\"[^<]*>)" | |
pattern = re.compile(regex) | |
def process_file(path): | |
f = open(path, "r", encoding="utf-8") | |
content = f.read() | |
f.close() | |
while True: | |
match = pattern.search(content, re.MULTILINE) | |
if not match: | |
break | |
group_1 = match.group(1) | |
group_3 = match.group(3) | |
content = content.replace(group_1, '❎') | |
content = content.replace(group_3, '✅') | |
content = content.replace('❎', group_3) | |
content = content.replace('✅', group_1) | |
f = open(path, "w+", encoding="utf-8") | |
f.write(content) | |
f.close() | |
def process_dir(path): | |
for child_path in os.listdir(path): | |
process('%s/%s' % (path, child_path)) | |
def process(path): | |
if os.path.isfile(path): | |
process_file(path) | |
elif os.path.isdir(path): | |
process_dir(path) | |
path = sys.argv[1] | |
process(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment