From e0f7b166397e4d48e0a8e1f625b542360bf5925d Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Fri, 16 Dec 2022 07:29:46 +0100 Subject: [PATCH] added return command --- README.md | 7 +++++++ html-preprocessor | 16 +++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 31dd87b..fe30c40 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,15 @@ The first word is the name of the variable, the rest is the value or a dictionar **Return Value**: Empty string + You can make the value of `varname` dependant on the value of another variable `othervar` by using a dictionary-like syntax described above. In this case, `varname` will take the first value from the dictionary that matches tha value of `othervar`. `*` always everything and can be used as fallback. General wildcards like `a*` to match everything that starts with a are not supported. Instead of commas `,` you can also use semicolons `;` as separators, but this must be consistend within the map. +### return +Same as `set`, but it returns the value of the variable that is being set. This is meant to use with maps, when you need a variable from a map you can 'inline' it with `return` + ### default Same as `set`, but it sets the variable's value only if it has no value yet. @@ -91,6 +95,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 its return value, so if you want to just have commented text in there you can use `#comment` @@ -105,6 +110,7 @@ Any string **Return Value**: The argument + This can be useful when you want to look at the unprocessed html without variables or when your syntax highlighting gets confused by a variable. --- @@ -162,3 +168,4 @@ Empty string - The `#include` command must not be in the last line of the file - 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 diff --git a/html-preprocessor b/html-preprocessor index 8ea1a05..75ca0f1 100755 --- a/html-preprocessor +++ b/html-preprocessor @@ -215,7 +215,7 @@ def cmd_include(args: str, variables:dict[str, str]={}) -> str: glob_dependcies.append(args) return content -def cmd_set(args: str, variables:dict[str, str]={}) -> str: +def cmd_return(args: str, variables:dict[str, str]={}) -> str: # re_set_map = r"([a-zA-Z0-9_]+)\?\{(([a-zA-Z0-9_]+:.+,)*([a-zA-Z0-9_]+:.+))\}" # space = args.find(' ') @@ -247,15 +247,20 @@ def cmd_set(args: str, variables:dict[str, str]={}) -> str: variables[varname] = option[colon+1:].strip(" ") else: # simple asignment - value = args[space+1:] - variables[varname] = value.strip(" ") - pdebug(f"cmd_set: Assignment {varname} -> {value.strip(' ')}") + value = args[space+1:].strip(" ") + variables[varname] = value + pdebug(f"cmd_set: Assignment {varname} -> {value}") + return variables[varname] + return "" + +def cmd_set(args: str, variables:dict[str, str]={}) -> str: + cmd_return(args, variables) return "" def cmd_default(args: str, variables:dict[str, str]={}) -> str: separator = args.find(' ') if args[:separator] not in variables: - return cmd_set(args, variables) + cmd_return(args, variables) return "" @@ -268,6 +273,7 @@ def cmd_uncomment(args: str, variables:dict[str, str]={}) -> str: command2function:dict[str, Callable[[str, dict[str,str]], str]] = { "include": cmd_include, "set": cmd_set, + "return": cmd_return, "default": cmd_default, "comment": cmd_comment, "uncomment": cmd_uncomment,