Compare commits

..

2 Commits

Author SHA1 Message Date
Matthias@Dell
afdb00a4e7 add sass supoort 2023-11-19 17:05:54 +01:00
Matthias@Dell
d06c1ab792 rm prints 2023-11-19 17:05:40 +01:00
2 changed files with 63 additions and 31 deletions

View File

@ -27,9 +27,14 @@ OUT_DIR = build
# SOURCE FILES: # SOURCE FILES:
# all SRC_FLS and all files (recursively) in the SRC_DIRS will be built # all SRC_FLS and all files (recursively) in the SRC_DIRS will be built
# all files in PROJECT_DIR (not recursively) are source files # all files in PROJECT_DIR (not recursively) are source files
SRC_DIRS = de en script style SRC_DIRS = de en script
SRC_FLS = SRC_FLS =
# CSS FILES:
# directories which may contain sass and scss to compile sass to a correspondig css in OUT_DIR/CSS_DIR (also css, it will simply be copied)
CSS_DIRS = style
CSS_FILES =
# SOURCE FILES: # SOURCE FILES:
# all RESOURCE_FLS and all files in the RESOURCE_DIRS will be copied to OUT_DIR # all RESOURCE_FLS and all files in the RESOURCE_DIRS will be copied to OUT_DIR
RESOURCE_DIRS = resources RESOURCE_DIRS = resources
@ -49,11 +54,17 @@ LANGS = de en
# PREPROCESSOR # PREPROCESSOR
# path to of the files that should be included # path to of the files that should be included
INCLUDE_DIR = include INCLUDE_DIR = include
# additional search paths passed to sass compiler
SASS_INCLUDE_DIRS = include/style
# ADVANCED # ADVANCED
# the command to run the html preprocessor # the command to run the html preprocessor
HTML_PP_CMD = python3 html-preprocessor --exit-on light HTML_PP_CMD = python3 html-preprocessor --exit-on light
# command to compile sass and scss files with
# --indented is added for sass and --no-indented for scss
# --source-maps-urls=absolute is appended for generating dependency files
SASS_CMD = sass --color
DEP_DIR = .dependencies DEP_DIR = .dependencies
@ -68,23 +79,27 @@ DEP_DIR = .dependencies
# make everything relative to PROJECT_DIR # make everything relative to PROJECT_DIR
_SRC_DIRS = $(addprefix $(PROJECT_DIR)/, $(SRC_DIRS)) _SRC_DIRS = $(addprefix $(PROJECT_DIR)/, $(SRC_DIRS))
_SRC_FLS = $(addprefix $(PROJECT_DIR)/, $(SRC_FLS)) _SRC_FLS = $(addprefix $(PROJECT_DIR)/, $(SRC_FLS))
_CSS_FLS = $(addprefix $(PROJECT_DIR)/, $(CSS_FLS))
_CSS_DIRS = $(addprefix $(PROJECT_DIR)/, $(CSS_DIRS))
_SASS_INCLUDE_DIRS = $(addprefix $(PROJECT_DIR)/, $(SASS_INCLUDE_DIRS))
_RES_DIRS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_DIRS)) _RES_DIRS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_DIRS))
_RES_FLS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_FLS)) _RES_FLS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_FLS))
_COMMON_DIR = $(addprefix $(PROJECT_DIR)/, $(COMMON_DIR)) _COMMON_DIR = $(addprefix $(PROJECT_DIR)/, $(COMMON_DIR))
_INCLUDE_DIR = $(addprefix $(PROJECT_DIR)/, $(INCLUDE_DIR)) _INCLUDE_DIR = $(addprefix $(PROJECT_DIR)/, $(INCLUDE_DIR))
# NORMAL SRC # NORMAL SRC
# all SRC_DIRS + all subdirs of each srcdir # all SRC_DIRS + CSS_DIRS + all subdirs of each srcdir
_SRC_SUB_DIRS = $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null)) _SRC_SUB_DIRS = $(foreach srcdir, $(_SRC_DIRS) $(_CSS_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null))
# add files in project dir # add files in project dir
_SRC_FLS += $(shell find $(PROJECT_DIR)/ -maxdepth 1 -type f) _SRC_FLS += $(shell find $(PROJECT_DIR)/ -maxdepth 1 -type f)
# add files src dirs, recursively # add files src dirs, recursively
_SRC_FLS += $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type f 2>/dev/null)) _SRC_FLS += $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type f 2>/dev/null))
# OUT_DIRS = $(OUT_DIR) $(addprefix $(OUT_DIR)/, $(_SRC_SUB_DIRS)) _CSS_FLS += $(foreach srcdir, $(_CSS_DIRS), $(shell find $(srcdir)/ -type f 2>/dev/null))
OUT_DIRS = $(OUT_DIR)/ $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_SUB_DIRS)) OUT_DIRS = $(OUT_DIR)/ $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_SUB_DIRS))
# path of the source files after being processed # path of the (css/sass) source files after being processed
# OUT_FLS = $($(notdir _SRC_FLS):%=$(OUT_DIR)/%)
OUT_FLS = $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_FLS)) OUT_FLS = $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_FLS))
OUT_FLS += $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(foreach cssfile, $(_CSS_FLS), $(shell echo $(cssfile) | sed 's/\.s[ac]ss$$/.css/')))
# RESOURCES # RESOURCES
_RES_SUB_DIRS = $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null)) _RES_SUB_DIRS = $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null))
@ -108,11 +123,15 @@ _DEP_DIRS = $(sort $(patsubst $(OUT_DIR)/%, $(DEP_DIR)/%, $(OUT_DIRS) $(ML_OUT
# needed for reading # needed for reading
_DEP_FLS = $(shell find $(DEP_DIR) -type f -name '*.d' 2>/dev/null) _DEP_FLS = $(shell find $(DEP_DIR) -type f -name '*.d' 2>/dev/null)
# SASS, add load-paths
_SASS_CMD = $(SASS_CMD) $(foreach includedir, $(_SASS_INCLUDE_DIRS), --load-path=$(includedir)) --source-map-urls=absolute
# PRINTING # PRINTING
FMT_VAR_SRC ="Variable '\e[1;34m%s\e[0m': \e[0;33m%s\e[0m\n" FMT_VAR_SRC ="Variable '\e[1;34m%s\e[0m': \e[0;33m%s\e[0m\n"
FMT_VAR_OUT ="Variable '\e[1;34m%s\e[0m': \e[0;35m%s\e[0m\n" FMT_VAR_OUT ="Variable '\e[1;34m%s\e[0m': \e[0;35m%s\e[0m\n"
FMT_DIR ="\e[1;34mMaking directory\e[0m: \e[0;35m%s\e[0m\n" FMT_DIR ="\e[1;34mMaking directory\e[0m: \e[0;35m%s\e[0m\n"
FMT_OUT_HTML ="\e[1;34mBuilding html\e[0m \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n" FMT_OUT_HTML ="\e[1;34mBuilding html\e[0m \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n"
FMT_OUT_CSS ="\e[1;34mBuilding css\e[0m \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n"
FMT_OUT_OTHER ="\e[1;34mBuilding\e[0m: \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n" FMT_OUT_OTHER ="\e[1;34mBuilding\e[0m: \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n"
FMT_OUT_ML_HTML="\e[1;34mBuilding html\e[0m in lang \e[1;34m%s\e[0m: \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n" FMT_OUT_ML_HTML="\e[1;34mBuilding html\e[0m in lang \e[1;34m%s\e[0m: \e[1;33m%s\e[0m at \e[1;35m%s\e[0m\n"
@ -140,6 +159,7 @@ print:
@printf $(FMT_VAR_OUT) "OUT_FLS" "$(OUT_FLS)" @printf $(FMT_VAR_OUT) "OUT_FLS" "$(OUT_FLS)"
@printf $(FMT_VAR_SRC) "_RES_FLS" "$(_RES_FLS)" @printf $(FMT_VAR_SRC) "_RES_FLS" "$(_RES_FLS)"
@printf $(FMT_VAR_OUT) "RES_OUT_FLS" "$(RES_OUT_FLS)" @printf $(FMT_VAR_OUT) "RES_OUT_FLS" "$(RES_OUT_FLS)"
@printf $(FMT_VAR_OUT) "_CSS_FLS" "$(_CSS_FLS)"
ifdef COMMON_DIR ifdef COMMON_DIR
@printf $(FMT_VAR_SRC) "_ML_SRC_FLS" "$(_ML_SRC_FLS)" @printf $(FMT_VAR_SRC) "_ML_SRC_FLS" "$(_ML_SRC_FLS)"
@printf $(FMT_VAR_OUT) "ML_OUT_FLS" "$(ML_OUT_FLS)" @printf $(FMT_VAR_OUT) "ML_OUT_FLS" "$(ML_OUT_FLS)"
@ -181,6 +201,21 @@ $(OUT_DIR)/%.html: $(PROJECT_DIR)/%.html | $(OUT_DIRS) $(_DEP_DIRS)
@#awk -i inplace '{FS="" sub(/<!--.*-->/,"")}1' $@ @#awk -i inplace '{FS="" sub(/<!--.*-->/,"")}1' $@
@#awk -i inplace '{if (NF != 0) print}' $@ @#awk -i inplace '{if (NF != 0) print}' $@
$(OUT_DIR)/%.css: $(PROJECT_DIR)/%.sass | $(OUT_DIRS) $(_DEP_DIRS)
@printf $(FMT_OUT_CSS) "$<" "$@";
@$(_SASS_CMD) --indented $< $@
@# generate a dependecy file from the source map and delete the map
@depfile=$(patsubst $(OUT_DIR)/%,$(DEP_DIR)/%,$@).d; echo -n "$@: " > "$$depfile"; \
jq -r '.sources | @sh' $@.map | tr -d \' | sed 's|file://||g' >> "$$depfile"; \
rm $@.map
$(OUT_DIR)/%.css: $(PROJECT_DIR)/%.scss | $(OUT_DIRS) $(_DEP_DIRS)
@printf $(FMT_OUT_CSS) "$<" "$@";
@$(_SASS_CMD) --no-indented $< $@
@# generate a dependecy file from the source map and delete the map
@depfile=$(patsubst $(OUT_DIR)/%,$(DEP_DIR)/%,$@).d; echo -n "$@: " > "$$depfile"; \
jq -r '.sources | @sh' $@.map | tr -d \' | sed 's|file://||g' >> "$$depfile"; \
rm $@.map
$(OUT_DIR)/%: $(PROJECT_DIR)/% | $(OUT_DIRS) $(RES_OUT_DIRS) $(OUT_DIR)/%: $(PROJECT_DIR)/% | $(OUT_DIRS) $(RES_OUT_DIRS)
@printf $(FMT_OUT_OTHER) "$<" "$@" @printf $(FMT_OUT_OTHER) "$<" "$@"
@cp -r $< $@ @cp -r $< $@
@ -196,8 +231,9 @@ stop:
killall nginx killall nginx
clean: clean:
-rm $(OUT_FLS) $(ML_OUT_FLS) 2>/dev/null -@rm $(OUT_FLS) $(ML_OUT_FLS) 2>/dev/null
-rm -r $(DEP_DIR) 2>/dev/null -@rm -r $(DEP_DIR) 2>/dev/null
cleaner: cleaner:
-rm -r $(OUT_DIR) -@rm -r $(OUT_DIR)
-@rm -r $(DEP_DIR) 2>/dev/null

View File

@ -227,6 +227,8 @@ def cmd_include(args: str, variables:dict[str, str]={}) -> str:
while p.i < len(p): # at start of new line or end of comment while p.i < len(p): # at start of new line or end of comment
p.next_line() p.next_line()
ptrace(f"cmd_include: Processing at i={p.i} in line {pos2line(p.file, p.i)}") ptrace(f"cmd_include: Processing at i={p.i} in line {pos2line(p.file, p.i)}")
# print(filename, p.i, pos2line(p.file, p.i))
# TODO: hangs here
if not p.find_comment_begin(): continue if not p.find_comment_begin(): continue
if not p.find_comment_end(): continue if not p.find_comment_end(): continue
@ -390,7 +392,7 @@ class HTMLParser(Parser):
Parse a html file Parse a html file
Each function operates the positon indicated by i until the position "line_end" Each function operates the positon indicated by i until the position "line_end"
""" """
def __init__(self, file, variables:dict[str, str]): def __init__(self, file, variables:dict[str, str], remove_comments=False):
super().__init__(file) super().__init__(file)
self.i = 0 self.i = 0
self.variables = variables self.variables = variables
@ -402,10 +404,11 @@ class HTMLParser(Parser):
self.pos["conditional_block_beg"] = -1 # char pos of the first char of the last block, if waiting for elif, else or endif self.pos["conditional_block_beg"] = -1 # char pos of the first char of the last block, if waiting for elif, else or endif
self.state["cmd_in_cmt"] = False self.state["cmd_in_cmt"] = False
self.state["last_condition"] = False # if the last if condition was true self.state["last_condition"] = False # if the last if condition was true
self.remove_comments = remove_comments
def next_line(self): def next_line(self):
"""update i and line_end""" """update i and line_end"""
self.pos["line_end"] = self.file.find('\n', self.i) self.pos["line_end"] = self.file.find('\n', self.i+1)
if self.pos["line_end"] < 0: self.pos["line_end"] = len(self) if self.pos["line_end"] < 0: self.pos["line_end"] = len(self)
def use_variables(self): def use_variables(self):
@ -479,7 +482,7 @@ class HTMLParser(Parser):
def command_end(self): def command_end(self):
if self.pos["cmd_end"] == self.pos["cmt_end"]: # reached end of comment if self.pos["cmd_end"] == self.pos["cmt_end"]: # reached end of comment
if self.state["cmd_in_cmt"]: if self.state["cmd_in_cmt"] or self.remove_comments:
# remove comment tags if a command was found # remove comment tags if a command was found
remove_newline = 0 remove_newline = 0
if self[self.pos["cmt_beg"]-1] == '\n' and self[self.pos["cmt_end"]+len(COMMENT_END)] == '\n': # if the comment consumes the whole line, remove the entire line if self[self.pos["cmt_beg"]-1] == '\n' and self[self.pos["cmt_end"]+len(COMMENT_END)] == '\n': # if the comment consumes the whole line, remove the entire line
@ -502,8 +505,8 @@ class HTMLParser(Parser):
# i = possible_command_end commented, because if something containing new commands is inserted we need to parse that as well # i = possible_command_end commented, because if something containing new commands is inserted we need to parse that as well
def parse_file(_file:str, variables:dict[str,str]): def parse_file(_file:str, variables:dict[str,str], remove_comments):
p = HTMLParser(_file, variables) p = HTMLParser(_file, variables, remove_comments=remove_comments)
sidenav_include_pos = -1 sidenav_include_pos = -1
while p.i < len(p): # at start of new line or end of comment while p.i < len(p): # at start of new line or end of comment
@ -570,6 +573,7 @@ def parse_file(_file:str, variables:dict[str,str]):
else: else:
cmd_output = "" cmd_output = ""
p.replace_command_with_output(cmd_output) p.replace_command_with_output(cmd_output)
p.command_end() p.command_end()
if sidenav_include_pos >= 0: if sidenav_include_pos >= 0:
@ -600,25 +604,17 @@ def substitute_variables(html:str, variables:dict[str, str]):
""" """
************************************************************ COMMAND LINE ************************************************************ ************************************************************ COMMAND LINE ************************************************************
""" """
def missing_arg_val(arg):
print("Missing argument for", arg)
exit(1)
def missing_arg(arg):
print("Missing ", arg)
exit(1)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="bUwUma html preprocessor") parser = argparse.ArgumentParser(prog="bUwUma html preprocessor")
parser.add_argument("--input", action="store", help="path to the input file", required=True) parser.add_argument("--input", action="store", help="path to the input file", required=True)
parser.add_argument("--output", action="store", help="output to this file", default="") parser.add_argument("--output", action="store", help="output to this file", default="")
parser.add_argument("--inplace", action="store_true", help="overwrite input file") parser.add_argument("--inplace", action="store_true", help="overwrite input file")
parser.add_argument("--var", action="append", help="set a variable --var varname=value") parser.add_argument("--var", action="append", help="set a variable --var varname=value", default=[])
parser.add_argument("--output-deps", action="store", help="output a Makefile listing all dependencies", default="") parser.add_argument("--output-deps", action="store", help="output a Makefile listing all dependencies", default="")
parser.add_argument("--exit-on", action="store", help="exit when an error of the given severity occures", choices=["light", "serious", "critical"], default="serious") parser.add_argument("--exit-on", action="store", help="exit when an error of the given severity occures", choices=["light", "serious", "critical"], default="serious")
parser.add_argument("--debug", action="store_true", help="be more verbose") parser.add_argument("--debug", action="store_true", help="be more verbose", default=False)
parser.add_argument("--trace", action="store_true", help="be extremly verbose") parser.add_argument("--trace", action="store_true", help="be extremly verbose", default=False)
parser.add_argument("--preserve-comments", action="store_true", help="do not remove normal html comments", default=False)
variables:dict[str, str] = {} variables:dict[str, str] = {}
args = parser.parse_args() args = parser.parse_args()
@ -633,8 +629,9 @@ if __name__ == "__main__":
args.input = args.input.strip(" ") args.input = args.input.strip(" ")
args.output = args.output.strip(" ") args.output = args.output.strip(" ")
args.output_deps = args.output_deps.strip(" ") args.output_deps = args.output_deps.strip(" ")
DEBUG = args.debug
TRACE = args.trace TRACE = args.trace
if args.trace: args.debug = True
DEBUG = args.debug
# sanity checks # sanity checks
if not path.isfile(args.input): if not path.isfile(args.input):
@ -656,8 +653,7 @@ if __name__ == "__main__":
with open(args.input, "r") as file: with open(args.input, "r") as file:
target_html = file.read() target_html = file.read()
output_html = parse_file(target_html, variables, not args.preserve_comments)
output_html = parse_file(target_html, variables)
# pdebug(f"Output: {output_html}") # pdebug(f"Output: {output_html}")