File size: 1,325 Bytes
ca2b985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Report export: Markdown and PDF."""

import tempfile
import time
from pathlib import Path

from markdown_pdf import MarkdownPdf, Section

from code_tribunal.ui.helpers import evidence_html


def export_md(ctx: dict) -> str:
    """Export trial context as a Markdown file. Returns the file path."""
    md = [
        "# CodeTribunal — Trial Report\n",
        f"**Generated**: {time.strftime('%Y-%m-%d %H:%M:%S')}\n",
        "---\n",
    ]
    for key in ["evidence", "verdict", "report"]:
        value = ctx.get(key, "")
        if value:
            md.append(f"## {key.title()}\n\n{value}\n\n")
    fp = tempfile.mktemp(suffix="_CodeTribunal_Report.md")
    Path(fp).write_text("\n".join(md))
    return fp


def export_pdf(ctx: dict) -> str:
    """Export trial context as a PDF file. Returns the file path."""
    sections = []
    for key in ["evidence", "verdict", "report"]:
        value = ctx.get(key, "")
        if value:
            sections.append(f"## {key.title()}\n\n{value}")

    full_md = (
        f"# CodeTribunal - Trial Report\n\n"
        f"*Generated: {time.strftime('%Y-%m-%d %H:%M:%S')}*\n\n---\n\n"
        + "\n".join(sections)
    )

    pdf = MarkdownPdf()
    pdf.add_section(Section(full_md))
    fp = tempfile.mktemp(suffix="_CodeTribunal_Report.pdf")
    pdf.save(fp)
    return fp