generated from hjess/PythonTemplateProject
markdown template
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
This commit is contained in:
@@ -1,101 +1,59 @@
|
||||
from markdown_it import MarkdownIt
|
||||
import re
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import markdown
|
||||
|
||||
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 img_left_overlay(src):
|
||||
"""Render an image with overlay."""
|
||||
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 box(title, content):
|
||||
"""Render a box component."""
|
||||
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 note(content):
|
||||
"""Render a note component."""
|
||||
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 warning(content):
|
||||
"""Render a warning component."""
|
||||
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 create_jinja_environment(template_dir="templates"):
|
||||
"""Set up Jinja2 environment and register custom components."""
|
||||
env = Environment(loader=FileSystemLoader(template_dir))
|
||||
env.globals.update({
|
||||
"img_left_overlay": img_left_overlay,
|
||||
"box": box,
|
||||
"note": note,
|
||||
"warning": warning,
|
||||
})
|
||||
return env
|
||||
|
||||
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
|
||||
def render_markdown_with_jinja(markdown_content, template_data=None):
|
||||
"""Process Markdown first, then inject Jinja2 components."""
|
||||
# Step 1: Convert Markdown to HTML
|
||||
md_html = markdown.markdown(markdown_content, extensions=['extra', 'nl2br'])
|
||||
|
||||
match = re.match(pattern, line)
|
||||
if not match:
|
||||
print("No match for custom tag.")
|
||||
return False
|
||||
# Step 2: Use Jinja2 to inject components into the already-rendered HTML
|
||||
env = create_jinja_environment()
|
||||
template = env.from_string(md_html)
|
||||
rendered_html = template.render(template_data or {})
|
||||
|
||||
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)
|
||||
return rendered_html
|
||||
|
||||
Reference in New Issue
Block a user