From 36efa5490412afe642efdebfb06f3e82389be478 Mon Sep 17 00:00:00 2001 From: "Matthias@Dell" Date: Sun, 19 Nov 2023 19:28:22 +0100 Subject: [PATCH] add cmd unset --- README.md | 13 +++++++++++++ html-preprocessor | 13 +++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5eca3f8..d6f3eea 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,19 @@ Same as `set`, but it returns the value of the variable that is being set. This ### default 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. +`` + +**Argument**: +Name of the variable + +**Return Value**: +Empty string + --- ### comment diff --git a/html-preprocessor b/html-preprocessor index 3fbaae2..faf8412 100755 --- a/html-preprocessor +++ b/html-preprocessor @@ -273,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}'") if not (space > 0 and space < len(args)-1): variables[args] = "" - pdebug(f"cmd_set: Setting to emptry string: {args}") + pdebug(f"cmd_set: Setting to empty string: {args}") else: varname = args[:space] variables[varname] = "" @@ -308,6 +308,14 @@ def cmd_set(args: str, variables:dict[str, str]={}) -> str: cmd_return(args, variables) 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: separator = args.find(' ') if args[:separator] not in variables: @@ -331,8 +339,9 @@ def cmd_warning(args: str, variables:dict[str, str]={}) -> str: command2function:dict[str, Callable[[str, dict[str,str]], str]] = { "include": cmd_include, "section": cmd_section, - "set": cmd_set, "return": cmd_return, + "set": cmd_set, + "unset": cmd_unset, "default": cmd_default, "comment": cmd_comment, "uncomment": cmd_uncomment,