From 705990582ed7eb99637adc8e2266e91f6d09ecf5 Mon Sep 17 00:00:00 2001 From: ktyl Date: Mon, 13 Sep 2021 21:38:18 +0100 Subject: [PATCH] match and replace #filename.ext --- ppp.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ppp.py b/ppp.py index 0ac3f27..067dcc6 100644 --- a/ppp.py +++ b/ppp.py @@ -1,5 +1,6 @@ import sys import os.path +import re err = False paths = [] @@ -14,6 +15,9 @@ if argc < 3: sep="/" src_dir = sep.join(sys.argv[1].split(sep)[:-1]) +# regex patterns +p_var = re.compile(r'(#.+?\.css)') + def print_usage(): print("\nusage: python ppp.py ROOT TEMPLATES [...]") @@ -27,6 +31,7 @@ def preprocess_file(path): for l in content: + # replace lines that start with #include with the contents of a file if l.startswith("#include"): include_path = l.split(" ")[1] @@ -36,6 +41,15 @@ def preprocess_file(path): preprocess_file(include_path) continue + # inline replace any occurrence of #filename with the contents of that file + match = re.search(p_var, l) + if match: + path = "/".join([src_dir, match.group(0)[1:]]) + + with open(path) as var_file: + var_content = var_file.read().strip() + l = re.sub(p_var, var_content, l) + print(l)