\documentclass{klartex-base}

\VAR{page_template_source}

%# --- Apply page template overrides ---
\BLOCK{if not page_template.page_numbers}
\fancyfoot[C]{}
\BLOCK{endif}
\BLOCK{if not page_template.first_page_header}
\fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrulewidth}{0pt}}
\thispagestyle{plain}
\BLOCK{endif}

\BLOCK{if lang == 'en'}
\setdoclang{en}
\BLOCK{else}
\setdoclang{sv}
\BLOCK{endif}

\BLOCK{if doc_title}
\setdoctitle{\VAR{doc_title}}
\BLOCK{endif}

\begin{document}

%# --- Macro for rendering a party fieldset ---
\BLOCK{macro render_party(party, label, group="")}
\fieldset[\VAR{group}]{\VAR{label}}{%
\textbf{\VAR{party.name}}\\[0.3em]
\BLOCK{if party.get("org_number")}
Org.nr: \VAR{party.org_number}\\
\BLOCK{endif}
\BLOCK{if party.get("id_number")}
Personnr: \VAR{party.id_number}\\
\BLOCK{endif}
\BLOCK{if party.get("address")}
\VAR{party.address}
\BLOCK{else}
\BLOCK{if party.get("address_line1")}
\VAR{party.address_line1}\\
\BLOCK{endif}
\BLOCK{if party.get("address_line2")}
\VAR{party.address_line2}
\BLOCK{endif}
\BLOCK{endif}
}
\BLOCK{endmacro}

%# --- Recursive macro for clause items ---
\BLOCK{macro render_items(item_list, list_type="numbered")}
\BLOCK{if list_type == "bullet"}
\begin{itemize}[leftmargin=1.2em, itemsep=0.5em, topsep=0.3em]
\BLOCK{else}
\begin{subclauses}
\BLOCK{endif}
\BLOCK{for item in item_list}
\BLOCK{if item is string}
\item {\interlinepenalty=10000 \VAR{item}\par}
\BLOCK{else}
\BLOCK{if item.get("title")}
\item \textbf{\VAR{item.title}}
\BLOCK{if item.get("text")}
\\ \VAR{item.text}
\BLOCK{endif}
\BLOCK{else}
\item \VAR{item.text}
\BLOCK{endif}
\VAR{render_items(item["items"], item.get("list", "numbered"))}
\BLOCK{endif}
\BLOCK{endfor}
\BLOCK{if list_type == "bullet"}
\end{itemize}
\BLOCK{else}
\end{subclauses}
\BLOCK{endif}
\BLOCK{endmacro}

%# --- Body blocks ---
\BLOCK{set clause_started = [false] }
\BLOCK{for block in body}

\BLOCK{if block.type == 'title_page'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\makedoctitle{\VAR{block.get('party1', '')}}{\VAR{block.get('party2', '')}}{\VAR{block.get('title', '')}}

\BLOCK{elif block.type == 'heading'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\BLOCK{if block.get('level', 1) == 1}
{\fontsize{24pt}{29pt}\selectfont\bfseries \VAR{block.text}\par}\vspace{3em}
\BLOCK{elif block.get('level', 1) == 2}
{\Large\bfseries \VAR{block.text}\par}\vspace{1.5em}
\BLOCK{else}
{\large\bfseries \VAR{block.text}\par}\vspace{1em}
\BLOCK{endif}

\BLOCK{elif block.type == 'parties'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\noindent
\begin{minipage}[t]{0.48\textwidth}
\VAR{render_party(block.party1, block.get("label1", "Part 1"), "parties")}
\end{minipage}%
\hspace{0.04\textwidth}%
\begin{minipage}[t]{0.48\textwidth}
\VAR{render_party(block.party2, block.get("label2", "Part 2"), "parties")}
\end{minipage}

\BLOCK{elif block.type == 'preamble' or block.type == 'text'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}

\vspace{1em}
\noindent \VAR{block.text}

\BLOCK{elif block.type == 'clause'}
\BLOCK{if not clause_started[0]}
\begin{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, true) }
\BLOCK{endif}
\clause{\VAR{block.title}}
\BLOCK{if block["items"] | length == 1 and block["items"][0] is string}
\\ \VAR{block["items"][0]}
\BLOCK{else}
\VAR{render_items(block["items"])}
\BLOCK{endif}

\BLOCK{elif block.type == 'page_break'}
\clearpage

\BLOCK{elif block.type == 'signatures'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\BLOCK{if block.get("new_page", true)}
\signatureblock{\VAR{block.parties[0].name}}{\VAR{block.parties[1].name}}{\VAR{block.parties[0].get('signatory', block.parties[0].name)}}{\VAR{block.parties[1].get('signatory', block.parties[1].name)}}{\VAR{block.parties[0].get('title', '')}}{\VAR{block.parties[1].get('title', '')}}
\BLOCK{else}
\signatureblock*{\VAR{block.parties[0].name}}{\VAR{block.parties[1].name}}{\VAR{block.parties[0].get('signatory', block.parties[0].name)}}{\VAR{block.parties[1].get('signatory', block.parties[1].name)}}{\VAR{block.parties[0].get('title', '')}}{\VAR{block.parties[1].get('title', '')}}
\BLOCK{endif}

\BLOCK{elif block.type == 'metadata_table'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\noindent
\begin{tabular}{@{}ll}
\BLOCK{for entry in block.entries}
\textbf{\VAR{entry.label}} & \VAR{entry.value} \\
\BLOCK{endfor}
\end{tabular}

\BLOCK{elif block.type == 'attendees'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\vspace{1em}
\noindent\textbf{Narvarande:} \VAR{block.attendees | join(', ')}
\BLOCK{if block.get('adjusters')}
\noindent\textbf{Justerare:} \VAR{block.adjusters | join(', ')}
\BLOCK{endif}
\vspace{1em}

\BLOCK{elif block.type == 'agenda'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\begin{dagordning}
\BLOCK{for item in block["items"]}
\punkt{\VAR{item.title}}
\BLOCK{if item.get('discussion')}

\noindent \VAR{item.discussion}
\BLOCK{endif}
\BLOCK{if item.get('decision')}

\noindent \textbf{Beslut:} \VAR{item.decision}
\BLOCK{endif}
\BLOCK{endfor}
\end{dagordning}

\BLOCK{elif block.type == 'name_roster'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\namnrollista{\VAR{block.title}}{%
\BLOCK{for person in block.people}
\person{\VAR{person.name}}{\VAR{person.role}}{\VAR{person.get('note', '')}}
\BLOCK{endfor}
}

\BLOCK{elif block.type == 'adjuster_signatures'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\BLOCK{if block.get('adjusters')}
\vspace{2cm}
\noindent
\BLOCK{set adj_label = block.get('label', 'Justerare') }
\BLOCK{for adjuster in block.adjusters}
\begin{minipage}[t]{0.45\textwidth}
\VAR{adj_label}\\[1cm]
\dottedline\\[0.3cm]
\VAR{adjuster}
\end{minipage}\BLOCK{if not loop.last}\hfill\BLOCK{endif}
\BLOCK{endfor}
\BLOCK{endif}

\BLOCK{elif block.type == 'latex'}
\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{set _ = clause_started.__setitem__(0, false) }
\BLOCK{endif}
\VAR{block.source}

\BLOCK{endif}
\BLOCK{endfor}

\BLOCK{if clause_started[0]}
\end{clauses}
\BLOCK{endif}

\end{document}
