aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQrius <[email protected]>2025-02-21 13:57:23 +0100
committerQrius <[email protected]>2025-02-21 13:57:26 +0100
commit5fb097851d88b42188ee0502270d8c336102783a (patch)
tree8e17109a55e4c88925ed92ee7cbcd6e7cab787d1
parent5394cfcf6e0ab0d110429b22dffa7e8bd1cf39dc (diff)
downloadskaldpress-5fb097851d88b42188ee0502270d8c336102783a.tar.gz
skaldpress-5fb097851d88b42188ee0502270d8c336102783a.zip
Add some more macros
-rw-r--r--src/smp/builtins.py28
-rw-r--r--src/smp/macro_processor.py10
-rw-r--r--tests/input_files/special_macros_13
-rw-r--r--tests/input_files/wodl_13
-rw-r--r--tests/input_files/wodl_23
5 files changed, 39 insertions, 8 deletions
diff --git a/src/smp/builtins.py b/src/smp/builtins.py
index 4cbb0a3..f463a24 100644
--- a/src/smp/builtins.py
+++ b/src/smp/builtins.py
@@ -1,9 +1,12 @@
import smp.exceptions
import subprocess
+import urllib.request
+import urllib.error
import datetime
import markdown
from gfm import AutolinkExtension, TaskListExtension
+
def smp_builtin_define(macro_processor, macro_name, macro_value=None):
macro_name = macro_processor.process_input(macro_name)
if macro_value is not None:
@@ -116,6 +119,7 @@ def smp_builtin_array_each(macro_processor, array_name, template):
out += macro_processor.process_input(macro_processor.expand_macro(template, el))
return out
+
def smp_builtin_explode(macro_processor, array_name, delimiter, input):
if array_name not in macro_processor.macros:
raise Exception(f"{array_name} is not a macro")
@@ -126,11 +130,13 @@ def smp_builtin_explode(macro_processor, array_name, delimiter, input):
macro_processor.macros[array_name].append(el)
return ""
+
def smp_builtin_format_time(macro_processor, format, time):
timestamp = macro_processor.process_input(time)
dobj = datetime.datetime.fromisoformat(timestamp)
return dobj.strftime(format)
+
def smp_builtin_html_from_markdown(macro_processor, text, extensions=list()):
# Get rid of quoting, I don't remember why, but the rust implementation does it like this.
for _ in range(2):
@@ -139,6 +145,28 @@ def smp_builtin_html_from_markdown(macro_processor, text, extensions=list()):
extensions.append(TaskListExtension(max_depth=2))
return markdown.markdown(text, extensions=extensions)
+
+global LINK_CACHE
+LINK_CACHE = dict()
+
+
+def smp_builtin_wodl(macro_processor, link, timeout_seconds=5):
+ if link in LINK_CACHE:
+ return LINK_CACHE[link]
+
+ try:
+ r = urllib.request.urlopen(link, timeout=timeout_seconds)
+ working_link = (r.status == 200) and (r.reason == "OK")
+ LINK_CACHE[link] = (working_link, r.status, r.reason)
+ if not working_link:
+ macro_processor.warnings.append(
+ f"Dead link {link} ({r.status} {r.reason})!"
+ )
+ except urllib.error.URLError as e:
+ macro_processor.warnings.append(f"Dead link {link} ({e})!")
+ return ""
+
+
def smp_builtin_dumpenv(macro_processor):
out = ""
out += "━ Macros ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
diff --git a/src/smp/macro_processor.py b/src/smp/macro_processor.py
index ad996d7..e85fbe9 100644
--- a/src/smp/macro_processor.py
+++ b/src/smp/macro_processor.py
@@ -42,11 +42,10 @@ class MacroProcessor:
""" Global environment for python execution """
py_global_env: dict
- special_macros: dict[str, tuple[str, str]]
+ special_macros: dict[str, tuple[Any, Any]]
def __init__(self, prefix=""):
self.macros = dict()
- self.special_macros = dict()
self.macro_invocations = list()
self.warnings = list()
self.py_global_env = dict()
@@ -72,8 +71,10 @@ class MacroProcessor:
self.macros[f"{prefix}array_size"] = smp.builtins.smp_builtin_array_size
self.macros[f"{prefix}explode"] = smp.builtins.smp_builtin_explode
self.macros[f"{prefix}format_time"] = smp.builtins.smp_builtin_format_time
- self.macros[f"{prefix}html_from_markdown"] = smp.builtins.smp_builtin_html_from_markdown
- self.special_macros[f"test"] = ("", "")
+ self.macros[f"{prefix}html_from_markdown"] = (
+ smp.builtins.smp_builtin_html_from_markdown
+ )
+ self.macros[f"{prefix}wodl"] = smp.builtins.smp_builtin_wodl
def expand_macro(self, macro_name: str, args: list[str] = list()) -> str:
# Ignore trailing underscore in macro name, the parser will pop a space in front if
@@ -152,7 +153,6 @@ class MacroProcessor:
while i < len(input):
c = input[i]
peek = None if i + 1 >= len(input) else input[i + 1]
- #prev = None if i - 1 < 0 else input[i - 1]
# import sys
# print(f"[{i:4}] {repr(c):4} -> {repr(peek):4} [{state}] = {repr(output)}", file=sys.stderr)
diff --git a/tests/input_files/special_macros_1 b/tests/input_files/special_macros_1
deleted file mode 100644
index eba505b..0000000
--- a/tests/input_files/special_macros_1
+++ /dev/null
@@ -1,3 +0,0 @@
-@test
----
-@test
diff --git a/tests/input_files/wodl_1 b/tests/input_files/wodl_1
new file mode 100644
index 0000000..6466022
--- /dev/null
+++ b/tests/input_files/wodl_1
@@ -0,0 +1,3 @@
+wodl(https://google.com)
+---
+
diff --git a/tests/input_files/wodl_2 b/tests/input_files/wodl_2
new file mode 100644
index 0000000..51be3e1
--- /dev/null
+++ b/tests/input_files/wodl_2
@@ -0,0 +1,3 @@
+wodl(http://domene_som_ikke_finnes.no)
+---
+