From 49333b641e31a85f09530f7c582319fef458dcd4 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sat, 8 Jun 2024 16:15:02 +0100 Subject: [PATCH] chore: remove ppp submodule --- makefile | 6 ++-- ppp | 1 - ppp.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) delete mode 160000 ppp create mode 100644 ppp.py diff --git a/makefile b/makefile index 5fd9527..2c91c59 100644 --- a/makefile +++ b/makefile @@ -45,10 +45,10 @@ deploy: ssh $(HOST) "sudo $(REMOTE_SCRIPT)" $(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX) | $(OUT_DIR) - python ppp/ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX) > $@ + python ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX) > $@ $(OUT_DIR)/%.css: $(ROOT_DIR)/%.css $(CSS_INCLUDES) | $(OUT_DIR) - python ppp/ppp.py $< $(CSS_INCLUDES) > $@ + python ppp.py $< $(CSS_INCLUDES) > $@ $(OUT_DIR): mkdir -p $@ @@ -63,7 +63,7 @@ blog: $(HTML_INCLUDES) $(CSS_TARGETS) make --directory $(BLOG_BASE_DIR) html cp -r $(BLOG_BUILD_DIR)/* $(BLOG_OUT_DIR) for page in `find "$(BLOG_OUT_DIR)" -wholename "*.html"`; do \ - pipenv run python ppp/ppp.py $$page $(HTML_INCLUDES) > temp ; \ + python ppp.py $$page $(HTML_INCLUDES) > temp ; \ mv temp $$page ; \ cp $(CSS_TARGETS) $(IMG_DIR)/*.ico `dirname $$page` ; \ done diff --git a/ppp b/ppp deleted file mode 160000 index 2253d24..0000000 --- a/ppp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2253d24ad239afe2b4fe2cf53471bcc3414879cd diff --git a/ppp.py b/ppp.py new file mode 100644 index 0000000..1d7d3b4 --- /dev/null +++ b/ppp.py @@ -0,0 +1,83 @@ +import sys +import os.path +import re + +err = False +paths = [] + +def print_usage(): + print("\nusage: python ppp.py ROOT TEMPLATES [...]") + +# check arguments +argc = len(sys.argv) +if argc < 3: + print_usage() + sys.exit(1) + +includes = sys.argv[2:] + +# figure out src dir from root argument +sep="/" +src_dir = sep.join(sys.argv[2].split(sep)[:-1]) + +# regex patterns +p_var = re.compile(r'(#.+?\.css)') + +def preprocess_file(path): + lines=0 + + with open(path) as f: + content = f.readlines() + content = [x.strip('\n') for x in content] + lines=len(content) + + 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] + + # prepend directory + #include_path = "/".join([src_dir, include_path]) + for inc in includes: + if os.path.basename(inc) == include_path: + include_path = inc + break + + 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) + + +for i in range(1,argc): + path = sys.argv[i] + + if not os.path.isfile(path): + + print(path + " is not a file") + err = True + continue + + if path in paths: + # ignore duplicates + continue + + paths.append(path) + +if err: + print_usage() + sys.exit(1) + +preprocess_file(sys.argv[1]) + +sys.exit(0)