Compare commits

...

5 Commits

Author SHA1 Message Date
Matthias@Dell
ca60a1b02c add remove blanks in output 2023-11-19 20:12:15 +01:00
Matthias@Dell
36efa54904 add cmd unset 2023-11-19 19:28:22 +01:00
Matthias@Dell
8ff1093877 fix comment deletion 2023-11-19 17:52:07 +01:00
Matthias@Dell
e25eca0a63 add restrictions for section 2023-11-19 17:51:54 +01:00
Matthias@Dell
bacc5c39dd adjust top comment 2023-11-19 17:10:16 +01:00
3 changed files with 39 additions and 19 deletions

View File

@ -1,6 +1,7 @@
# ABOUT # ABOUT
# - In this Makefile, 'building a file' means: # - In this Makefile, 'building a file' means:
# - if the file has a '.html' extension: run the html preprocessor on the file and place the output in the output directory # - if the file has a '.html' extension: run the html preprocessor on the file and place the output in the output directory
# - elif the file has a '.sass' or '.scss' extension: run the sass compiler on the file and place the output in the output directory
# - else: copy the file to the output directory # - else: copy the file to the output directory
# - Folder structure from source directories will be preserved in the output directory # - Folder structure from source directories will be preserved in the output directory
# - Abbreviations: # - Abbreviations:
@ -11,8 +12,6 @@
# - PP: preprocessor # - PP: preprocessor
# - DEP: dependency # - DEP: dependency
# TODO: fix that you have invoke make twice to build both languages
# #
# NORMAL SETTINGS # NORMAL SETTINGS
# change these to fir your project # change these to fir your project

View File

@ -97,6 +97,19 @@ Same as `set`, but it returns the value of the variable that is being set. This
### default ### default
Same as `set`, but it sets the variable's value only if it has no value yet. Same as `set`, but it sets the variable's value only if it has no value yet.
### unset
Unset a variable
**Synopsis**:
Unset `varname`, it will no longer be defined and can therefor be set with `default` again.
`<!-- #unset varname -->`
**Argument**:
Name of the variable
**Return Value**:
Empty string
--- ---
### comment ### comment
@ -171,7 +184,7 @@ Ignored
The generated sidenav The generated sidenav
#### `section` #### `section`
Group all following entries in named section. Group all following entries in named section. `section` may not appear in conditional blocks and multiline comments.
**Argument**: **Argument**:
The name of the section The name of the section

View File

@ -227,9 +227,6 @@ 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
@ -240,20 +237,17 @@ def cmd_include(args: str, variables:dict[str, str]={}) -> str:
pdebug(f"cmd_include Found command '{command}' with args '{cmd_args}'") pdebug(f"cmd_include Found command '{command}' with args '{cmd_args}'")
if command == "section": if command == "section":
if cmd_args.startswith(target_section): if cmd_args.startswith(target_section):
print(p.pos)
p.pos["start"] = max(p.pos["cmt_end"] + len(COMMENT_END), p.pos["line_end"] + 1) p.pos["start"] = max(p.pos["cmt_end"] + len(COMMENT_END), p.pos["line_end"] + 1)
print(f">{content[p.pos['start']:p.pos['start']+1]}<")
elif p.pos["start"] >= 0: #end elif p.pos["start"] >= 0: #end
p.pos["end"] = max(p.pos["cmt_end"] + len(COMMENT_END), p.pos["line_end"] + 1) p.pos["end"] = max(p.pos["cmt_end"] + len(COMMENT_END), p.pos["line_end"] + 1)
# p.pos["end"] = p.pos["cmt_beg"] # p.pos["end"] = p.pos["cmt_beg"]
p.replace_command_with_output("") p.replace_command_with_output("")
p.command_end() p.command_end()
if p.pos["start"] >= 0 and p.pos["end"] > 0: break if p.pos["start"] >= 0 and p.pos["end"] > 0: break
if p.pos["start"] >= 0: if p.pos["start"] >= 0:
if p.pos["end"] < 0: if p.pos["end"] < 0:
p.pos["end"] = len(p) p.pos["end"] = len(p)
content = p[p.pos["start"]:p.pos["end"]] content = p[p.pos["start"]:p.pos["end"]]
print(content)
else: else:
error(f"cmd_include: Could not find section {target_section} in file {filename}") error(f"cmd_include: Could not find section {target_section} in file {filename}")
except FileNotFoundError: except FileNotFoundError:
@ -279,7 +273,7 @@ def cmd_return(args: str, variables:dict[str, str]={}) -> str:
# pdebug(f"cmd_set: varname='{args[:space]}, 'arg='{args[space+1:]}', variables='{variables}'") # pdebug(f"cmd_set: varname='{args[:space]}, 'arg='{args[space+1:]}', variables='{variables}'")
if not (space > 0 and space < len(args)-1): if not (space > 0 and space < len(args)-1):
variables[args] = "" variables[args] = ""
pdebug(f"cmd_set: Setting to emptry string: {args}") pdebug(f"cmd_set: Setting to empty string: {args}")
else: else:
varname = args[:space] varname = args[:space]
variables[varname] = "" variables[varname] = ""
@ -314,6 +308,14 @@ def cmd_set(args: str, variables:dict[str, str]={}) -> str:
cmd_return(args, variables) cmd_return(args, variables)
return "" return ""
def cmd_unset(args: str, variables:dict[str, str]={}) -> str:
variable = args.strip(' ')
if variable not in variables:
pdebug(f"unset: variable '{variable}' is not set", level=error_levels["light"])
else:
variables.pop(variable)
return ""
def cmd_default(args: str, variables:dict[str, str]={}) -> str: def cmd_default(args: str, variables:dict[str, str]={}) -> str:
separator = args.find(' ') separator = args.find(' ')
if args[:separator] not in variables: if args[:separator] not in variables:
@ -337,8 +339,9 @@ def cmd_warning(args: str, variables:dict[str, str]={}) -> str:
command2function:dict[str, Callable[[str, dict[str,str]], str]] = { command2function:dict[str, Callable[[str, dict[str,str]], str]] = {
"include": cmd_include, "include": cmd_include,
"section": cmd_section, "section": cmd_section,
"set": cmd_set,
"return": cmd_return, "return": cmd_return,
"set": cmd_set,
"unset": cmd_unset,
"default": cmd_default, "default": cmd_default,
"comment": cmd_comment, "comment": cmd_comment,
"uncomment": cmd_uncomment, "uncomment": cmd_uncomment,
@ -483,18 +486,21 @@ 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"] or self.remove_comments: if self.state["cmd_in_cmt"] or self.remove_comments:
# 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
remove_newline = 1 remove_newline = 1
# remove comment if done if self.state["cmd_in_cmt"]: # remove comment tags if a command was found
ptrace(f"Deleting opening comment tags") ptrace(f"Deleting opening comment tags")
self.remove(self.pos["cmt_beg"], self.pos["cmt_beg"] + len(COMMENT_BEGIN)) self.remove(self.pos["cmt_beg"], self.pos["cmt_beg"] + len(COMMENT_BEGIN))
self.remove(self.pos["cmt_end"], self.pos["cmt_end"] + len(COMMENT_END) + remove_newline, ignore_bounds=["cmt_end", "cmd_end", "line_end"]) self.remove(self.pos["cmt_end"], self.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 # process the line again, because a command might have inserted new comments
self.i -= len(COMMENT_BEGIN) self.i -= len(COMMENT_BEGIN)
elif self.remove_comments: # remove entire comment
self.remove(self.pos["cmt_beg"], self.pos["cmt_end"] + len(COMMENT_END) + remove_newline, ignore_bounds=["cmt_end", "cmd_beg", "cmd_end", "line_end"])
self.i = self.pos["cmt_beg"]
self.state["cmd_in_cmt"] = False self.state["cmd_in_cmt"] = False
self.pos["cmt_beg"] = -1 self.pos["cmt_beg"] = -1
self.pos["cmd_beg"] = -1
self.pos["cmt_end"] = -1 self.pos["cmt_end"] = -1
self.pos["cmd_end"] = -1 self.pos["cmd_end"] = -1
else: # multiline comment else: # multiline comment
@ -654,6 +660,8 @@ if __name__ == "__main__":
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, not args.preserve_comments)
# remove empty lines
output_html = re.sub(r"[\t\r ]*\n(?:[\t\r ]*\n[\t\r ]*)+", r"\n", output_html)
# pdebug(f"Output: {output_html}") # pdebug(f"Output: {output_html}")