get-css-colors.py
· 1.9 KiB · Python
Raw
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import re
import argparse
def extract_colors(css_file, output_file):
color_pattern = re.compile(r'(#(?:[0-9a-fA-F]{3}){1,2})|'
r'(rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d*\.?\d+\s*)?\))')
colors = set()
with open(css_file, 'r') as file:
for line in file:
matches = color_pattern.finditer(line)
for match in matches:
color = match.group(0)
colors.add(color)
with open(output_file, 'w') as file:
for color in colors:
file.write(color + '\n')
def replace_colors(css_file, translation_file, output_file):
with open(translation_file, 'r') as file:
translations = dict(line.strip().split(' -> ') for line in file)
with open(css_file, 'r') as file:
css_content = file.read()
for old_color, new_color in translations.items():
css_content = css_content.replace(old_color, new_color)
with open(output_file, 'w') as file:
file.write(css_content)
def main():
parser = argparse.ArgumentParser(description="CSS Color Extractor and Replacer")
parser.add_argument("command", choices=['extract', 'replace'], help="Command to execute (extract or replace)")
parser.add_argument("css_file", help="Path to the CSS file")
parser.add_argument("output_file", help="Path to the output file")
parser.add_argument("--translation_file", help="Path to the translation file for color replacement", default="")
args = parser.parse_args()
if args.command == 'extract':
extract_colors(args.css_file, args.output_file)
elif args.command == 'replace':
if not args.translation_file:
print("Error: Translation file required for replace command")
return
replace_colors(args.css_file, args.translation_file, args.output_file)
if __name__ == "__main__":
main()
| 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | import sys |
| 4 | import re |
| 5 | import argparse |
| 6 | |
| 7 | def extract_colors(css_file, output_file): |
| 8 | color_pattern = re.compile(r'(#(?:[0-9a-fA-F]{3}){1,2})|' |
| 9 | r'(rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d*\.?\d+\s*)?\))') |
| 10 | colors = set() |
| 11 | |
| 12 | with open(css_file, 'r') as file: |
| 13 | for line in file: |
| 14 | matches = color_pattern.finditer(line) |
| 15 | for match in matches: |
| 16 | color = match.group(0) |
| 17 | colors.add(color) |
| 18 | |
| 19 | with open(output_file, 'w') as file: |
| 20 | for color in colors: |
| 21 | file.write(color + '\n') |
| 22 | |
| 23 | def replace_colors(css_file, translation_file, output_file): |
| 24 | with open(translation_file, 'r') as file: |
| 25 | translations = dict(line.strip().split(' -> ') for line in file) |
| 26 | |
| 27 | with open(css_file, 'r') as file: |
| 28 | css_content = file.read() |
| 29 | |
| 30 | for old_color, new_color in translations.items(): |
| 31 | css_content = css_content.replace(old_color, new_color) |
| 32 | |
| 33 | with open(output_file, 'w') as file: |
| 34 | file.write(css_content) |
| 35 | |
| 36 | def main(): |
| 37 | parser = argparse.ArgumentParser(description="CSS Color Extractor and Replacer") |
| 38 | parser.add_argument("command", choices=['extract', 'replace'], help="Command to execute (extract or replace)") |
| 39 | parser.add_argument("css_file", help="Path to the CSS file") |
| 40 | parser.add_argument("output_file", help="Path to the output file") |
| 41 | parser.add_argument("--translation_file", help="Path to the translation file for color replacement", default="") |
| 42 | |
| 43 | args = parser.parse_args() |
| 44 | |
| 45 | if args.command == 'extract': |
| 46 | extract_colors(args.css_file, args.output_file) |
| 47 | elif args.command == 'replace': |
| 48 | if not args.translation_file: |
| 49 | print("Error: Translation file required for replace command") |
| 50 | return |
| 51 | replace_colors(args.css_file, args.translation_file, args.output_file) |
| 52 | |
| 53 | if __name__ == "__main__": |
| 54 | main() |
| 55 |