aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorQrius <[email protected]>2025-04-25 13:02:03 +0200
committerQrius <[email protected]>2025-04-25 13:02:05 +0200
commitdc12485f5c346e6f77aa7c9ae72f80cd3cc1e771 (patch)
tree3176e93e1a70fce8badc4d70885295e5fc74b32e /src
parenta2d2abac24a683c68e950bbcc3362af265c3e077 (diff)
downloadskaldpress-dc12485f5c346e6f77aa7c9ae72f80cd3cc1e771.tar.gz
skaldpress-dc12485f5c346e6f77aa7c9ae72f80cd3cc1e771.zip
Add v1 of special macros
Diffstat (limited to 'src')
-rw-r--r--src/smp/macro_processor.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/smp/macro_processor.py b/src/smp/macro_processor.py
index d2a2f94..19e692e 100644
--- a/src/smp/macro_processor.py
+++ b/src/smp/macro_processor.py
@@ -91,6 +91,11 @@ class MacroProcessor:
self._define_builtins(self.macros)
self._define_builtins(self.py_local_env_alt)
+ self.special_macros = {
+ "html_from_markdown": (smp.builtins.smp_builtin_html_from_markdown, None),
+ "template": (smp.builtins.smp_builtin_template, None),
+ }
+
def _import_symbols(self, module, env, function_prefix=""):
for name, fun in inspect.getmembers(module, inspect.isfunction):
if name.startswith("_"):
@@ -255,9 +260,7 @@ class MacroProcessor:
def process_input(self, input: str, file: str | None = None):
"""
- I also want to add special syntax for "special blocks",
- I am thinking of two main options, either some macro_names are intercepted, _or_ a special kind of macro can exist like
- These will be on a line-basis, so they simply end on newline
+ Extend special macro syntax to support nesting, blocks, etc
@if <python-expression>
@else <python-expression>
@@ -299,7 +302,7 @@ class MacroProcessor:
c = input[i]
peek = None if i + 1 >= len(input) else input[i + 1]
- def hi_range():
+ def hi_range(end=None):
if not self._debug_on(5):
return
_linestart = linestart
@@ -307,8 +310,10 @@ class MacroProcessor:
if state_start < linestart:
_linestart = state_start
_linenr = f"~{linenr-1}"
+ if end is None:
+ end = i
range = debug_hi_range(
- input, _linestart, state_start, i, color="\u001b[42m"
+ input, _linestart, state_start, end, color="\u001b[42m"
)
print(
f"[{file}:{_linenr}] {range} \t\u001b[33m({state})\u001b[0m",
@@ -341,6 +346,32 @@ class MacroProcessor:
state_start = i
continue
+ if c == "@" and i == linestart:
+ nline = input.find("\n", i)
+ nspace = input.find(" ", i)
+ if nline > -1 or nspace > 0.1:
+ args = []
+
+ end_special = nline
+ if -1 < nspace < nline:
+ end_special = nspace
+ args = [
+ x.strip()
+ for x in input[nspace:nline].split(" ")
+ if x is not ""
+ ]
+
+ sname = input[i + 1 : end_special]
+ if sname in self.special_macros:
+ hi_range(end=end_special)
+ args.append(input[nline + 1 :])
+ # @TODO: Make try/catch wrapper here as well
+ output += self._expand_callable_macro(
+ self.special_macros[sname][0], args
+ )
+ i = len(input) - 1
+ continue
+
if c == "%" and peek == '"':
state = ParserState.IN_QUOTES
quote_level += 1