match and replace #filename.ext

This commit is contained in:
ktyl 2021-09-13 21:38:18 +01:00
parent 278e520c78
commit 705990582e
1 changed files with 14 additions and 0 deletions

14
ppp.py
View File

@ -1,5 +1,6 @@
import sys import sys
import os.path import os.path
import re
err = False err = False
paths = [] paths = []
@ -14,6 +15,9 @@ if argc < 3:
sep="/" sep="/"
src_dir = sep.join(sys.argv[1].split(sep)[:-1]) src_dir = sep.join(sys.argv[1].split(sep)[:-1])
# regex patterns
p_var = re.compile(r'(#.+?\.css)')
def print_usage(): def print_usage():
print("\nusage: python ppp.py ROOT TEMPLATES [...]") print("\nusage: python ppp.py ROOT TEMPLATES [...]")
@ -27,6 +31,7 @@ def preprocess_file(path):
for l in content: for l in content:
# replace lines that start with #include with the contents of a file
if l.startswith("#include"): if l.startswith("#include"):
include_path = l.split(" ")[1] include_path = l.split(" ")[1]
@ -36,6 +41,15 @@ def preprocess_file(path):
preprocess_file(include_path) preprocess_file(include_path)
continue 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) print(l)