generated from hjess/PythonTemplateProject
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 37s
105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
import markdown
|
|
from jinja2 import Environment, DictLoader
|
|
|
|
# Define Jinja2 custom functions
|
|
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 box(title, content):
|
|
"""Render a box component."""
|
|
return f'''
|
|
<div class="box">
|
|
<strong>{title}</strong>
|
|
<p>{content}</p>
|
|
</div>
|
|
'''
|
|
|
|
def note(content):
|
|
"""Render a note component."""
|
|
return f'''
|
|
<div class="note">
|
|
<p>{content}</p>
|
|
</div>
|
|
'''
|
|
def link_to(title, url):
|
|
"""Render a box component."""
|
|
return f'''
|
|
<a href="{url}" target="_blank" rel="noopener noreferrer">{title}</a>
|
|
'''
|
|
|
|
def warning(content):
|
|
"""Render a warning component."""
|
|
return f'''
|
|
<div class="warning">
|
|
⚠️ <p>{content}</p>
|
|
</div>
|
|
'''
|
|
|
|
|
|
def slider(options, images):
|
|
"""Render a slider using the provided HTML structure."""
|
|
width = options.get("width", 500)
|
|
height = options.get("height", 375)
|
|
|
|
html_content = []
|
|
for i, val in enumerate(images):
|
|
if i % 2 == 0:
|
|
html_content.append(f"""<button onclick="openModal('modal{i}')" class="open"> <img src="{val}" width={width} height={height}> Open Modal {i} </button>""".strip())
|
|
else:
|
|
html_content.append(f"""<button onclick="openModal('modal{i}')" class="open_inv"> <img src="{val}" width={width} height={height}> Open Modal {i} </button>""".strip())
|
|
|
|
html_content.append(f"""<div class="modal" id="modal{i}"> <div class="modal-content"> <h2>Modal {i}</h2> <p>This is modal {i}.</p> <img src="{val}" width="80%" height="80%"> <button onclick="closeModal('modal{i}')">Close</button> </div> </div>""")
|
|
html = '\n'.join( html_content )
|
|
|
|
return html
|
|
|
|
|
|
def create_jinja_environment():
|
|
"""Create and configure the Jinja2 environment."""
|
|
env = Environment(loader=DictLoader({"base_template": "{{ content | safe }}"}))
|
|
env.globals.update({
|
|
"img_left_overlay": img_left_overlay,
|
|
"box": box,
|
|
"note": note,
|
|
"warning": warning,
|
|
"link_to": link_to,
|
|
"slider": slider,
|
|
})
|
|
return env
|
|
|
|
def render_markdown_with_jinja(markdown_content: str):
|
|
"""
|
|
Convert Markdown to HTML and apply Jinja2 rendering for custom tags.
|
|
|
|
Args:
|
|
markdown_content (str): Raw Markdown content.
|
|
|
|
Returns:
|
|
tuple: Rendered HTML content and metadata as a dictionary.
|
|
"""
|
|
# Step 1: Convert Markdown to HTML and extract metadata
|
|
md = markdown.Markdown(extensions=["extra", "nl2br", "meta"])
|
|
intermediate_html = md.convert(markdown_content)
|
|
metadata = {key: " ".join(value) for key, value in md.Meta.items()} if md.Meta else {}
|
|
|
|
# Step 2: Pass the resulting HTML with Jinja2 custom tags through Jinja2
|
|
env = create_jinja_environment()
|
|
|
|
template = env.get_template("base_template")
|
|
final_html = template.render(content=intermediate_html)
|
|
|
|
#Step 3: Re-render final_html in Jinja2 for embedded tags like {{ box(...) }}
|
|
|
|
try:
|
|
final_output = env.from_string(final_html).render()
|
|
except:
|
|
print(final_html)
|
|
|
|
return final_output, metadata
|