Add a summary report set from Markdown
from-markdown takes a hand-written summary such as SUMMARY.md and sets it in the full report's dress: the same cover, header, footers, table of contents, headings and grids. The shared macros move to macros.html.j2 so both templates draw from them, and layout.render_pdf takes the template and document title to use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ace2e1803
commit
864e2ff824
10 files changed
+573
-92
No files matched your search
@@ -39,11 +39,12 @@ CONTEXT = {
|
||||
}
|
||||
|
||||
|
||||
def render_html(context: dict[str, Any]) -> str:
|
||||
def render_html(context: dict[str, Any], template: str = TEMPLATE_NAME) -> str:
|
||||
"""Render the report as an HTML document.
|
||||
|
||||
Args:
|
||||
context: The page-ready context; see :data:`CONTEXT`.
|
||||
template: Which template in :data:`TEMPLATE_DIR` to draw it with.
|
||||
|
||||
Returns:
|
||||
The rendered HTML.
|
||||
@@ -55,14 +56,16 @@ def render_html(context: dict[str, Any]) -> str:
|
||||
lstrip_blocks=True,
|
||||
)
|
||||
|
||||
return environment.get_template(TEMPLATE_NAME).render(**context)
|
||||
return environment.get_template(template).render(**context)
|
||||
|
||||
|
||||
def render_pdf(context: dict[str, Any]) -> bytes:
|
||||
def render_pdf(context: dict[str, Any], template: str = TEMPLATE_NAME, title: str = TITLE) -> bytes:
|
||||
"""Render the report as a PDF.
|
||||
|
||||
Args:
|
||||
context: The page-ready context; see :data:`CONTEXT`.
|
||||
template: Which template in :data:`TEMPLATE_DIR` to draw it with.
|
||||
title: What the document calls itself in its metadata.
|
||||
|
||||
Returns:
|
||||
The PDF.
|
||||
@@ -78,12 +81,12 @@ def render_pdf(context: dict[str, Any]) -> bytes:
|
||||
|
||||
# Relative URLs in the template and the stylesheet -- the mark, the font files -- resolve
|
||||
# against the template directory.
|
||||
document = HTML(string=render_html(context), base_url=str(TEMPLATE_DIR)).render(
|
||||
document = HTML(string=render_html(context, template), base_url=str(TEMPLATE_DIR)).render(
|
||||
stylesheets=[CSS(filename=str(STYLESHEET), font_config=fonts)],
|
||||
font_config=fonts,
|
||||
)
|
||||
|
||||
document.metadata.title = f"{TITLE} - {context.get('cover', {}).get('subject', '')}".strip(" -")
|
||||
document.metadata.title = f"{title} - {context.get('cover', {}).get('subject', '')}".strip(" -")
|
||||
document.metadata.authors = [AUTHOR]
|
||||
|
||||
return bytes(document.write_pdf())
|
||||
Reference in new issue
Block a user