(backend) parse YAML metadata

YAML header is processed, to fill Django
templates with the right parametrized values.

Used an external dependency, frontmatter.
This commit is contained in:
Samuel Paccoud - DINUM
2024-01-10 15:19:14 +01:00
committed by Lebaud Antoine
parent 312a680b66
commit 44ff62c08f
2 changed files with 8 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ from django.template.base import Template as DjangoTemplate
from django.template.context import Context
from django.template.engine import Engine
import frontmatter
import markdown
from weasyprint import CSS, HTML
from weasyprint.text.fonts import FontConfiguration
@@ -236,8 +237,12 @@ class Template(BaseModel):
Generate and return a PDF document for this template around the
markdown body passed as argument.
"""
body_html = markdown.markdown(textwrap.dedent(body)) if body else ""
document_html = HTML(string=DjangoTemplate(self.code).render(Context({"body": body_html})))
document = frontmatter.loads(body)
metadata = document.metadata
markdown_body = document.content.strip()
body_html = markdown.markdown(textwrap.dedent(markdown_body)) if markdown_body else ""
document_html = HTML(string=DjangoTemplate(self.code).render(Context({"body": body_html, **metadata})))
css = CSS(
string=self.css,
font_config=FontConfiguration(),

View File

@@ -47,6 +47,7 @@ dependencies = [
"nested-multipart-parser==1.5.0",
"psycopg[binary]==3.1.14",
"PyJWT==2.8.0",
"python-frontmatter==1.0.1",
"requests==2.31.0",
"sentry-sdk==1.38.0",
"url-normalize==1.4.3",