https enforcement

This commit is contained in:
Henrik Jess
2024-12-11 19:10:25 +01:00
parent d78efed30b
commit 27ee0516d9
4 changed files with 175 additions and 1 deletions

101
markdown_render.py Normal file
View File

@@ -0,0 +1,101 @@
from markdown_it import MarkdownIt
import re
class MarkdownRenderer:
def render_img_left_overlay(self, params):
src = params.get("src", "")
return f'''
<div class="img-left-overlay">
<img src="{src}" alt="Overlay Image">
<div class="overlay-text">Overlay Text</div>
</div>
'''
def render_box(self, params):
title = params.get("title", "Box")
content = params.get("content", "")
return f'''
<div class="box">
<strong>{title}</strong>
<p>{content}</p>
</div>
'''
def render_note(self, params):
content = params.get("content", "")
return f'''
<div class="note">
<p>{content}</p>
</div>
'''
def render_warning(self, params):
content = params.get("content", "")
return f'''
<div class="warning">
⚠️ <p>{content}</p>
</div>
'''
def __init__(self):
self.md = MarkdownIt()
self.TAG_RENDERERS = {
"img-left-overlay": self.render_img_left_overlay,
"box": self.render_box,
"note": self.render_note,
"warning": self.render_warning,
}
self._add_custom_tags_plugin()
print("Custom Markdown renderer initialized!")
def _add_custom_tags_plugin(self):
def custom_tag_rule(state, silent):
# Match custom tags
pattern = r"^\{(\w+):\s*([^}]*)\}$"
line = state.src[state.pos:].strip()
print(f"Parsing line: '{line}'") # Debug current line
match = re.match(pattern, line)
if not match:
print("No match for custom tag.")
return False
tag_name = match.group(1)
params_raw = match.group(2)
print(f"Matched tag: {tag_name}, Params: {params_raw}")
# Parse parameters
params = {}
for pair in params_raw.split(","):
if "=" in pair:
key, value = pair.split("=", 1)
params[key.strip()] = value.strip()
# Check if the tag_name exists in TAG_RENDERERS
if tag_name in self.TAG_RENDERERS:
print(f"Tag '{tag_name}' found. Creating token...")
token = state.push("custom_tag", "", 0)
token.meta = {"tag_name": tag_name, "params": params}
state.pos += len(line)
return True
print(f"Tag '{tag_name}' not found in TAG_RENDERERS.")
return False
def render_custom_tag(tokens, idx, options, env, *args):
token = tokens[idx]
tag_name = token.meta["tag_name"]
params = token.meta["params"]
print(f"Rendering tag '{tag_name}' with params: {params}")
if tag_name in self.TAG_RENDERERS:
return self.TAG_RENDERERS[tag_name](params)
return ""
# Register the plugin
self.md.inline.ruler.before("emphasis", "custom_tags", custom_tag_rule)
self.md.add_render_rule("custom_tag", render_custom_tag)
print("Custom tag plugin registered!")
def render(self, markdown_content):
print("Rendering Markdown content...")
return self.md.render(markdown_content)