Compare commits

...

5 Commits

Author SHA1 Message Date
matthias@arch
ea8c4cc8c4 PROJECT_DIR respected, multilang optional 2023-09-29 16:05:51 +02:00
matthias@arch
a10bbc005e rename --target to --input 2023-09-29 14:36:55 +02:00
matthias@arch
4cbbd2961e added error, warning 2023-09-15 14:03:16 +02:00
matthias@arch
9c5b9e0fa2 fix code block 2023-09-14 16:35:08 +02:00
matthias@arch
43993000c6 refactor, add conditionals 2023-09-14 16:34:05 +02:00
3 changed files with 248 additions and 103 deletions

View File

@ -18,16 +18,17 @@
# change these to fir your project
#
# root dir for the project, all other paths relative to PROJECT_DIR (except for OUT_DIR)
PROJECT_DIR = .
# root dir for the project, all other paths relative to PROJECT_DIR (except for OUT_DIR and DEP_DIR)
PROJECT_DIR = src
# path where final website will be in, this one is not relative to PROJECT_DIR
OUT_DIR = ../quintern-test
OUT_DIR = build
# SOURCE FILES:
# all SRC_FLS and all files 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
SRC_DIRS = de en script style
SRC_FLS = rss.xml
SRC_FLS =
# SOURCE FILES:
# all RESOURCE_FLS and all files in the RESOURCE_DIRS will be copied to OUT_DIR
@ -41,7 +42,8 @@ RESOURCE_FLS =
# foreach lang in LANGS:
# run HTML_PP_CMD with --var lang=lang on file and output to OUT_DIR without the COMMON_DIR prefix, so COMMON_DIR/subdir/file.html -> OUT_DIR/lang/subdir/file.html
# all non-html files will handled the same way, but without the preprocessor being run on them. They are simply copied
COMMON_DIR = common
# leave COMMON_DIR empty to disable multi-lang feature
COMMON_DIR =
LANGS = de en
# PREPROCESSOR
@ -51,7 +53,7 @@ INCLUDE_DIR = include
# ADVANCED
# the command to run the html preprocessor
HTML_PP_CMD = python3 html_preprocessor.py --exit-on light
HTML_PP_CMD = python3 html-preprocessor --exit-on light
DEP_DIR = .dependencies
@ -70,12 +72,14 @@ _RES_DIRS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_DIRS))
_RES_FLS = $(addprefix $(PROJECT_DIR)/, $(RESOURCE_FLS))
_COMMON_DIR = $(addprefix $(PROJECT_DIR)/, $(COMMON_DIR))
_INCLUDE_DIR = $(addprefix $(PROJECT_DIR)/, $(INCLUDE_DIR))
_DEP_DIR = $(addprefix $(PROJECT_DIR)/, $(DEP_DIR))
# NORMAL SRC
# all SRC_DIRS + all subdirs of each srcdir
_SRC_SUB_DIRS = $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type d))
_SRC_FLS += $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type f))
_SRC_SUB_DIRS = $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null))
# add files in project dir
_SRC_FLS += $(shell find $(PROJECT_DIR)/ -maxdepth 1 -type f)
# add files src dirs, recursively
_SRC_FLS += $(foreach srcdir, $(_SRC_DIRS), $(shell find $(srcdir)/ -type f 2>/dev/null))
# OUT_DIRS = $(OUT_DIR) $(addprefix $(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
@ -83,12 +87,13 @@ OUT_DIRS = $(OUT_DIR)/ $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_SUB_D
OUT_FLS = $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_SRC_FLS))
# RESOURCES
_RES_SUB_DIRS = $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type d))
_RES_FLS += $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type f))
_RES_SUB_DIRS = $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type d 2>/dev/null))
_RES_FLS += $(foreach srcdir, $(_RES_DIRS), $(shell find $(srcdir)/ -type f 2>/dev/null))
RES_OUT_DIRS = $(OUT_DIR)/ $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_RES_SUB_DIRS))
RES_OUT_FLS = $(patsubst $(PROJECT_DIR)/%, $(OUT_DIR)/%, $(_RES_FLS))
# MULTILANG
ifdef COMMON_DIR
_ML_SRC_FLS = $(shell find $(_COMMON_DIR)/ -type f)
_ML_SRC_SUB_DIRS= $(shell find $(_COMMON_DIR)/ -type d)
# will contain one subdir for each lang, each of which contains every file from ML_SRC_FLS
@ -96,11 +101,12 @@ ML_OUT_DIR = $(OUT_DIR)
ML_OUT_LANG_DIRS= $(foreach lang, $(LANGS), $(addprefix $(ML_OUT_DIR)/, $(lang)))
ML_OUT_DIRS = $(foreach lang, $(LANGS), $(patsubst $(_COMMON_DIR)/%, $(ML_OUT_DIR)/$(lang)/%, $(_ML_SRC_SUB_DIRS)))
ML_OUT_FLS = $(foreach lang, $(LANGS), $(patsubst $(_COMMON_DIR)/%, $(ML_OUT_DIR)/$(lang)/%, $(_ML_SRC_FLS)))
endif
# needed for creating them
_DEP_DIRS = $(sort $(patsubst $(OUT_DIR)/%, $(_DEP_DIR)/%, $(OUT_DIRS) $(ML_OUT_DIRS)))
_DEP_DIRS = $(sort $(patsubst $(OUT_DIR)/%, $(DEP_DIR)/%, $(OUT_DIRS) $(ML_OUT_DIRS)))
# needed for reading
_DEP_FLS = $(shell find $(_DEP_DIR) -type f -name '*.d')
_DEP_FLS = $(shell find $(DEP_DIR) -type f -name '*.d' 2>/dev/null)
# PRINTING
FMT_VAR_SRC ="Variable '\e[1;34m%s\e[0m': \e[0;33m%s\e[0m\n"
@ -134,43 +140,48 @@ print:
@printf $(FMT_VAR_OUT) "OUT_FLS" "$(OUT_FLS)"
@printf $(FMT_VAR_SRC) "_RES_FLS" "$(_RES_FLS)"
@printf $(FMT_VAR_OUT) "RES_OUT_FLS" "$(RES_OUT_FLS)"
ifdef COMMON_DIR
@printf $(FMT_VAR_SRC) "_ML_SRC_FLS" "$(_ML_SRC_FLS)"
@printf $(FMT_VAR_OUT) "ML_OUT_FLS" "$(ML_OUT_FLS)"
endif
@printf $(FMT_VAR_SRC) "_DEP_FLS" "$(_DEP_FLS)"
@# @printf $(FMT_VAR_SRC) "y" "$(y)"
# MULTILANG RULES
# DIRECTORIES
$(sort $(ML_OUT_DIRS) $(_DEP_DIRS) $(RES_OUT_DIRS) $(OUT_DIRS)):
@printf $(FMT_DIR) "$@"
@mkdir -p $@
# build/ml_tmp/lang/subdir/xyz.html
# MULTILANG RULES
ifdef COMMON_DIR
# $@ is the target to trigger the rule, but all languages have to be built now
$(foreach out_dir, $(ML_OUT_LANG_DIRS), $(out_dir)/%.html): $(_COMMON_DIR)/%.html | $(ML_OUT_DIRS) $(_DEP_DIRS)
@#echo "$$@=$@, $$<=$< $$^=$^"
@# \$@=build/ml_tmp/lang/subdir/xyz.html, \$<=common/subdir/xyz.html
@lang=`echo $(patsubst $(ML_OUT_DIR)/%, %, $@) | awk -F "/" '{print $$1}'`; \
printf $(FMT_OUT_ML_HTML) "$$lang" "$<" "$@"; \
$(HTML_PP_CMD) --target "$<" --output "$@" --var include_dir=$(_INCLUDE_DIR) --var lang=$$lang --output-deps "$(patsubst $(OUT_DIR)/%, $(_DEP_DIR)/%.d, $@)";
@RAW_TARGET=`echo $@ $(foreach lang, $(LANGS), | sed 's|$(ML_OUT_DIR)/$(lang)/||')`;\
for lang in $(LANGS); do \
target=$(ML_OUT_DIR)/$$lang/$$RAW_TARGET;\
printf $(FMT_OUT_ML_HTML) "$$lang" "$<" "$$target"; \
$(HTML_PP_CMD) --input "$<" --output "$$target" --var include_dir=$(_INCLUDE_DIR) --var lang=$$lang --output-deps "`echo $${target}.d | sed 's|$(OUT_DIR)/|$(DEP_DIR)/|'`"; \
done
# rule for all not html files
$(foreach out_dir, $(ML_OUT_LANG_DIRS), $(out_dir)/%): $(_COMMON_DIR)/% | $(ML_OUT_DIRS)
@lang=`echo $(patsubst $(ML_OUT_DIR)/%, %, $@) | awk -F "/" '{print $$1}'`; \
printf $(FMT_OUT_ML_OTHER) "$$lang" "$<" "$@" ; \
cp $< $@
endif
#
# (NORMAL/RE-)SOURCE RULES
#
$(OUT_DIR)/%.html: %.html | $(OUT_DIRS) $(_DEP_DIRS)
$(OUT_DIR)/%.html: $(PROJECT_DIR)/%.html | $(OUT_DIRS) $(_DEP_DIRS)
@printf $(FMT_OUT_HTML) "$<" "$@";
$(HTML_PP_CMD) --target "$<" --output "$@" --var include_dir=$(_INCLUDE_DIR) --output-deps "$(_DEP_DIR)/$<.d";
@$(HTML_PP_CMD) --input "$<" --output "$@" --var include_dir=$(_INCLUDE_DIR) --output-deps "$(subst $(DEP_DIR)/$(PROJECT_DIR), $(DEP_DIR), $(DEP_DIR)/$<.d)";
@# remove comments and empty lines. two separate lines bc the substitution might create new empty lines
@#awk -i inplace '{FS="" sub(/<!--.*-->/,"")}1' $@
@#awk -i inplace '{if (NF != 0) print}' $@
$(OUT_DIR)/%: % | $(OUT_DIRS) $(RES_OUT_DIRS)
$(OUT_DIR)/%: $(PROJECT_DIR)/% | $(OUT_DIRS) $(RES_OUT_DIRS)
@printf $(FMT_OUT_OTHER) "$<" "$@"
@cp -r $< $@
@ -185,8 +196,8 @@ stop:
killall nginx
clean:
-rm $(OUT_FLS) $(ML_OUT_FLS)
-rm -r $(_DEP_DIR)
-rm $(OUT_FLS) $(ML_OUT_FLS) 2>/dev/null
-rm -r $(DEP_DIR) 2>/dev/null
cleaner:
-rm -r $(OUT_DIR)

View File

@ -11,7 +11,7 @@ refer to the article [on my website](https://quintern.xyz/en/software/buwuma.htm
# HTML Preprocessor Documentation
## Syntax
### Commands
- All commands must be located within a html comment what starts with `<!--` and ends with `-->`.
- All commands must be located within a html comment that starts with `<!--` and ends with `-->`.
- Commands start with a `#` character, the command must follow the `#` immediately.
- Everything after the command until the end of the comment or a newline character are considered the argument of the command.
@ -27,7 +27,7 @@ refer to the article [on my website](https://quintern.xyz/en/software/buwuma.htm
- All commands return a string, which can be empty.
- If a comment contains a command, the entire comment will replaced with the return value of the command.
- If there are multiple commands in a command, it will be replaced by all the return values added together.
- If there are multiple commands in a comment, it will be replaced by all the return values added together.
### Variables
- Variable names must only consist of these characters: `a-zA-Z0-9_`
@ -96,7 +96,7 @@ Any string
**Return Value**:
The argument in comment tags
This can be useful in multiline comments that contain other commands: In that case, the comment tags will be removed and each command replaced with
This can be useful in multi-line comments that contain other commands: In that case, the comment tags will be removed and each command replaced with
its return value, so if you want to just have commented text in there you can use `#comment`
### uncomment
@ -115,6 +115,29 @@ This can be useful when you want to look at the unprocessed html without variabl
---
### conditionals
To turn on or off entire blocks, `if`, `elif` can `else` be used.
These commands can not be nested and must not appear in multi-line comments.
Logical and `&&` and logical or `||` can be used to chain conditions.
If a condition is true, the corresponding block is included while all other blocks are deleted.
**Synopsis**
```
<!-- #if #$(var) == value && #$(other_var) == other_value -->
...
<!-- #elif #$(var) == value || #$(other_var) != other_value -->
...
<!-- #else -->
...
<!-- #endif -->
```
**Argument** Condition for `if` and `elif`, ignored for `else` and `endif`
**Return Value** Empty String
---
### sidenav
Manage the generation of a content menu which contains links to all headings in your html that have an id. The menu is called sidenav here.
An entry is a html heading with a id: `<h1 id=myheading>This heading will be linked in the sidenav</h1>`
@ -166,7 +189,8 @@ Empty string
## Pitfalls
- The `#include` command must not be in the last line of the file
- The `#include` command can not be in multiline comment if the included file also contains comments
- The `#include` command can not be in multi-line comment if the included file also contains comments
- `#if`, `#elif`, `#else` and `#endif` must not be in multi-line comments
- The maps in `set` must have **at least 2** options
- If you want to use variables in markdown, you have to escape the `#` with a backslash, so `#$(var)` becomes `\#$(var)`
- You can not use the `return` command from within the arguments of other commands. Commands are executed in order, so `return` will end up as argument of the first command and thus never be executed

View File

@ -106,6 +106,20 @@ def generate_dependecy_file(filename:str, deps:list[str]):
s += f"{dep}:\n"
return line1 #+ "\n" + s
def evaluate_condition(input_string) -> bool:
words = re.split(r"(==|!=|&&|\|\|)", input_string.replace(" ", ""))
for i in range(len(words)):
if words[i] not in ["==", "!=", "&&", "||"]:
words[i] = '"' + words[i].replace('"', r'\"') + '"'
condition = "".join(words).replace("&&", " and ").replace("||", " or ")
ptrace(f"> Evaluating condition {condition}")
try:
return eval(condition)
except SyntaxError:
error(f"Pythonized condition is invalid: {condition}", level=error_levels["light"])
return False
"""
@ -269,6 +283,13 @@ def cmd_comment(args: str, variables:dict[str, str]={}) -> str:
def cmd_uncomment(args: str, variables:dict[str, str]={}) -> str:
return args
def cmd_error(args: str, variables:dict[str, str]={}) -> str:
error(f"Encounted 'error' command: {args}", level=error_levels["critical"])
return ""
def cmd_warning(args: str, variables:dict[str, str]={}) -> str:
error(f"Encounted 'warning' command: {args}", level=error_levels["light"])
return ""
command2function:dict[str, Callable[[str, dict[str,str]], str]] = {
"include": cmd_include,
@ -277,107 +298,196 @@ command2function:dict[str, Callable[[str, dict[str,str]], str]] = {
"default": cmd_default,
"comment": cmd_comment,
"uncomment": cmd_uncomment,
"sidenav": Sidenav.cmd_sidenav
"sidenav": Sidenav.cmd_sidenav,
"warning": cmd_warning,
"error": cmd_error,
}
"""
************************************************************ PARSING ************************************************************
"""
def parse_file(file:str, variables:dict[str,str]):
class Parser():
def __init__(self, file):
self.file = file
self.pos: dict[str, int] = {}
self.state: dict[str, bool] = {}
def remove(self, start, stop, ignore_bounds=[]):
"""remove range [start, stop) of text and update positions"""
delete_length = stop - start
nl, esl = "\n", "\\n"
ptrace(f"- Deleting range [{start}, {stop}) of length {delete_length}: '{self.file[start:stop].replace(nl, esl)}'")
assert(stop >= start)
assert(stop <= len(self.file))
self.file = self.file[:start] + self.file[stop:]
for k,pos in self.pos.items():
if pos >= stop: self.pos[k] -= delete_length
elif pos > start and not k in ignore_bounds: error(f"Position {k}={pos} within deleted range [{start},{stop})", level=1)
def replace(self, start, stop, replacement):
assert(stop >= start)
assert(stop <= len(self.file))
ptrace(f"- Replacing range [{start}, {stop}): '{self.file[start:stop]}' with '{replacement}'")
self.file = self.file[:start] + replacement + self.file[stop:]
length_difference = stop - start - len(replacement)
for k,pos in self.pos.items():
if pos >= stop: self.pos[k] -= length_difference
elif pos > start: error(f"Position {k}={pos} within replaced range [{start},{stop})", level=1)
def __getitem__(self, key):
return self.file[key]
def __len__(self):
return len(self.file)
def parse_file(_file:str, variables:dict[str,str]):
p = Parser(_file)
sidenav_include_pos = -1
comment_begin = -1
remove_comment = False
p.pos["cmt_beg"] = -1
p.pos["cmt_end"] = -1
p.pos["cmd_beg"] = -1
p.pos["cmdend"] = -1
p.pos["conditional_block_beg"] = -1 # char pos of the first char of the last block, if waiting for elif, else or endif
p.state["cmd_in_cmt"] = False
p.state["last_condition"] = False # if the last if condition was true
i = 0
# if file.count(COMMENT_BEGIN) != file.count(COMMENT_END):
while i < len(file): # at start of new line or end of comment
while i < len(p): # at start of new line or end of comment
ptrace(f"Processing at i={i} in line {pos2line(p.file, i)}")
# replace variable usages in the current line
line_end = file.find('\n', i)
if line_end < 0: line_end = len(file)
file = file[:i] + replace_variables(file[i:line_end], variables) + file[line_end:]
line_end = file.find('\n', i)
if line_end < 0: line_end = len(file)
ptrace("Line after replacing variables:", file[i:line_end])
p.pos["line_end"] = p.file.find('\n', i)
if p.pos["line_end"] < 0: p.pos["line_end"] = len(p)
p.replace(i, p.pos["line_end"], replace_variables(p[i:p.pos["line_end"]], variables))
ptrace("> Line after replacing variables:", p.file[i:p.pos["line_end"]])
# check if heading for sidenav in line
match = re.search(re_sidenav_heading, file[i:line_end])
match = re.search(re_sidenav_heading, p[i:p.pos["line_end"]])
if match:
Sidenav.addEntry(match.groups()[1], f"#{match.groups()[0]}")
ptrace("> Found heading with id:", match.groups())
if comment_begin < 0: # if not in comment, find next comment
comment_begin = file.find(COMMENT_BEGIN, i, line_end)
# look for comment
if p.pos["cmt_beg"] < 0: # if not in comment, find next comment
p.pos["cmt_beg"] = p.file.find(COMMENT_BEGIN, i, p.pos["line_end"])
# ptrace(f"i={i}, line_end={line_end}, comment_begin={comment_begin}")
if comment_begin < 0:
i = line_end + 1
if p.pos["cmt_beg"] < 0:
i = p.pos["line_end"] + 1
continue
else:
# jump to comment_begin
old_i = i
i = comment_begin + len(COMMENT_BEGIN) # after comment begin
i = p.pos["cmt_beg"] + len(COMMENT_BEGIN) # after comment begin
ptrace(f"> Found comment begin, jumping from pos {old_i} to {i}")
# if here, i at the character after COMMENT_BEGIN
# sanity check
tmp_next_begin = file.find(COMMENT_BEGIN, i)
if 0 < tmp_next_begin and tmp_next_begin < file.find(COMMENT_END, i):
error(f"Found next comment begin before the comment starting in line {pos2line(file, comment_begin)} is ended! Skipping comment. Comment without proper closing tags: '{file[i:line_end]}'", level=error_levels["light"])
comment_begin = -1
continue
# either at newline (if in multiline comment) or at comment end
possible_command_end = line_end
comment_end = file.find(COMMENT_END, i, line_end)
# ptrace(f"i={i}, line_end={line_end}, comment_begin={comment_begin}, comment_end={comment_end}, line={file[i:line_end]}")
if comment_end > 0: possible_command_end = comment_end
assert(possible_command_end >= i)
# in comment, i at the character after COMMENT_BEGIN
p.pos["cmt_end"] = p.file.find(COMMENT_END, i) #, p.pos["line_end"])
# sanity checks
if p.pos["cmt_end"] < 0:
error(f"Comment starting in line {pos2line(p.file, p.pos['cmt_beg'])} is never ended.", level=error_levels["serious"])
else:
tmp_next_begin = p.file.find(COMMENT_BEGIN, i)
if 0 < tmp_next_begin and tmp_next_begin < p.pos["cmt_end"]:
error(f"Found next comment begin before the comment starting in line {pos2line(p.file, p.pos['cmt_beg'])} is ended! Skipping comment. Comment without proper closing tags: '{p.file[i:p.pos['line_end']]}'", level=error_levels["light"])
p.pos["cmt_beg"] = -1
continue
# either at newline (if in multiline comment) or at comment end
p.pos["cmd_beg"] = i
p.pos["cmd_end"] = min(p.pos["line_end"], p.pos["cmt_end"])
assert p.pos["cmd_end"] >= i, f"cmd_end={p.pos['cmd_end']}, i={i}, line_end={p.pos['line_end']}, cmt_end={p.pos['cmt_end']}"
ptrace(f"> Possible command end: {p.pos['cmd_end']}, possible command: '{p[i:p.pos['cmd_end']]}'")
ptrace(f"> Possible command end: {possible_command_end}, possible command: {file[i:possible_command_end]}")
# find commands
# pdebug(">>> Line ", file[i:possible_command_end])
match = re.fullmatch(re_preprocessor_command, file[i:possible_command_end].strip(" "))
match = re.fullmatch(re_preprocessor_command, p[i:p.pos["cmd_end"]].strip(" "))
if match: # command comment
remove_comment = True
p.state["cmd_in_cmt"] = True
command = match.groups()[0]
args = match.groups()[1].replace('\t', ' ').strip(' ')
ptrace(f"> Found command '{command}' with args '{args}'")
if command == "sidenav" and args == "include": # if args contains anything else this wont work
sidenav_include_pos = comment_begin # remove the comment
insert_str = ""
elif command not in command2function:
error(f"Invalid command in line {pos2line(file, i)}: {command}", level=error_levels["light"])
insert_str = ""
pdebug(f"> Found command '{command}' with args '{args}'")
# delete from previous block if
if command in ["elif", "else", "endif"]:
if p.pos["conditional_block_beg"] < 0: error(f"Misplaced '{command}' in line {pos2line(p.file, i)}")
if p.state["last_condition"]:
# delete block from here at next endif
p.state["last_condition"] = False
else:
# delete block from last condition statement
ptrace(f"> Deleting block from last condition")
p.remove(p.pos["conditional_block_beg"], p.pos["cmt_beg"])
i = p.pos["cmd_beg"]
p.pos["conditional_block_beg"] = i
if command == "endif":
p.pos["conditional_block_beg"] = -1
p.state["last_condition"] = False
p.state["any_condition"] = False
# evaluate ifs
if command == "if":
p.pos["conditional_block_beg"] = i
p.state["last_condition"] = evaluate_condition(args)
p.state["any_condition"] = p.state["last_condition"]
pdebug(f"> Command {command} condition evaluated to {p.state['last_condition']}")
cmd_output = ""
elif command =="elif":
p.pos["conditional_block_beg"] = i
p.state["last_condition"] = evaluate_condition(args) if not p.state["any_condition"] else False
if p.state["last_condition"]:
p.state["any_condition"] = True
pdebug(f"> Command {command} condition evaluated to {p.state['last_condition']}")
cmd_output = ""
elif command == "else":
p.pos["conditional_block_beg"] = i
p.state["last_condition"] = True if not p.state["any_condition"] else False
cmd_output = ""
elif p.pos["conditional_block_beg"] < 0 or p.state["last_condition"]:
if command == "sidenav" and args == "include": # if args contains anything else this wont work
sidenav_include_pos = p.pos["cmt_beg"] # remove the comment
cmd_output = ""
elif command == "endif":
cmd_output = ""
elif command not in command2function:
error(f"Invalid command in line {pos2line(p.file, i)}: {command}", level=error_levels["light"])
cmd_output = ""
else:
cmd_output = command2function[command](args, variables)
else:
insert_str = command2function[command](args, variables)
file = file[:i] + insert_str + file[possible_command_end:]
# replaced string of length possible_command_end - i with one of length insert_str
index_correction = -(possible_command_end - i) + len(insert_str)
possible_command_end += index_correction
line_end += index_correction
comment_end += index_correction
ptrace(f"> After command, the line is now '{file[i:possible_command_end]}'")
# i += len(insert_str)
cmd_output = ""
p.replace(i, p.pos["cmd_end"], cmd_output)
ptrace(f"> After command, the line is now '{p.file[i:p.pos['line_end']]}'")
# remove comment if done
if possible_command_end == comment_end:
remove_newline = 0
if file[comment_begin-1] == '\n' and file[comment_end+len(COMMENT_END)] == '\n': # if the comment consumes the whole file, remove the entire line
remove_newline = 1
if remove_comment:
# remove the comment tags, basically uncomment the comment
# pdebug(f"Removing comment tags from pos {comment_begin} to {comment_end}")
file = file[:comment_begin] + file[comment_begin+len(COMMENT_BEGIN):comment_end] + file[comment_end+len(COMMENT_END)+remove_newline:]
possible_command_end -= len(COMMENT_BEGIN)
if p.pos["cmd_end"] == p.pos["cmt_end"]: # reached end of comment
if p.state["cmd_in_cmt"]:
# remove comment tags if a command was found
remove_newline = 0
if p[p.pos["cmt_beg"]-1] == '\n' and p[p.pos["cmt_end"]+len(COMMENT_END)] == '\n': # if the comment consumes the whole line, remove the entire line
remove_newline = 1
# remove comment if done
ptrace(f"Deleting opening comment tags")
p.remove(p.pos["cmt_beg"], p.pos["cmt_beg"] + len(COMMENT_BEGIN))
p.remove(p.pos["cmt_end"], p.pos["cmt_end"] + len(COMMENT_END) + remove_newline, ignore_bounds=["cmt_end", "cmd_end", "line_end"])
# process the line again, because a command might have inserted new comments
i -= len(COMMENT_BEGIN)
remove_comment = False
comment_begin = -1
p.state["cmd_in_cmt"] = False
p.pos["cmt_beg"] = -1
p.pos["cmt_end"] = -1
p.pos["cmd_end"] = -1
else: # multiline comment
i = line_end + 1
ptrace(f"Multiline comment, jumping to next line. char[i]='{file[i]}'")
p.pos["cmt_end"] = -1
p.pos["cmd_end"] = -1
i = p.pos["line_end"] + 1
ptrace(f"> Multiline comment, jumping to next line.")
# i = possible_command_end commented, because if something containing new commands is inserted we need to parse that as well
if sidenav_include_pos >= 0:
file = file[:sidenav_include_pos] + Sidenav.generate() + file[sidenav_include_pos:]
return file
return p.file[:sidenav_include_pos] + Sidenav.generate() + p.file[sidenav_include_pos:]
else:
return p.file
def replace_variables(html:str, variables:dict[str, str]):
@ -389,7 +499,7 @@ def replace_variables(html:str, variables:dict[str, str]):
matches.append(match)
html_list = list(html)
for match in reversed(matches):
pdebug(f"Found variable usage {match.groups()[0]}, match from {match.start()} to {match.end()}")
pdebug(f"> Found variable usage {match.groups()[0]}, match from {match.start()} to {match.end()}")
value = ""
if match.groups()[0] in variables: value = variables[match.groups()[0]]
for _ in range(match.start(), match.end()):
@ -411,9 +521,9 @@ def missing_arg(arg):
def help():
helpstring = """Synopsis:
Inject <inject-file> into <target-file>:
python3 html-inect.py --target <target-file> --output <output-file> [OPTIONS]
python3 html-inect.py --input <input-file> --output <output-file> [OPTIONS]
\nCommand line options:
--target <file> path to the target file
--input <file> path to the input file
--output <file> output to this file instead of overwriting target
--inplace edit target file in place
--var <varname>=<value> set the value of a variable. Can be used multiple times
@ -433,7 +543,7 @@ if __name__ == "__main__":
inplace = False
i = 1
while i in range(1, len(argv)):
if argv[i] == "--target":
if argv[i] == "--input":
if len(argv) > i + 1: target_path = argv[i+1].strip(" ")
else: missing_arg_val(argv[i])
i += 1
@ -468,7 +578,7 @@ if __name__ == "__main__":
error(f"Invalid argument: {argv[i]}")
i += 1
# sanity checks
if not target_path: missing_arg("--target")
if not target_path: missing_arg("--input")
if not os.path.isfile(target_path): error(f"Invalid target: {target_path} (does not exist)")
if inplace: output_path = target_path
if not output_path: