Components in documents
A component can be a part of a document: a finding, a method, a signature block. Its render holds document elements, or returns the part written in Markdown or LaTeX.
<import package="KScript.Document" />
<type name="Panel" extends="Component">
<construct>
<field name="heading" type="string" />
</construct>
<fn name="render">
<Section title="{.heading}">
<slot />
</Section>
</fn>
</type>
<type name="Finding" extends="Component">
<construct>
<field name="title" type="string" />
<field name="count" type="int" />
</construct>
<fn name="render" value="```md
## {$title}
We saw **{$count}** of them in \( 10^{3} \) generations.
```" />
</type>
<type name="Method" extends="Component">
<construct>
<field name="name" type="string" />
<field name="share" type="string" />
</construct>
<fn name="render" value="```latex
\subsection{Method: {$name}}
We used \textbf{$name} on {$share} of the runs.
\begin{equation}
N(t) = N_0 e^{rt}
\end{equation}
```" />
</type>
<Document title="Gliders" into="report">
<Panel heading="Results">
<Finding title="Gliders" count="3" />
<Method name="counting" share="50%" />
</Panel>
</Document>
<echo value="$report->convert(to: markdown)" />import KScript.Document
type Panel extends Component {
construct({ heading: string })
fn render() {
Section(title: "{.heading}") {
slot
}
}
}
type Finding extends Component {
construct({ title: string, count: int })
fn render() => ```md
## {$title}
We saw **{$count}** of them in \( 10^{3} \) generations.
```
}
type Method extends Component {
construct({ name: string, share: string })
fn render() => ```latex
\subsection{Method: {$name}}
We used \textbf{$name} on {$share} of the runs.
\begin{equation}
N(t) = N_0 e^{rt}
\end{equation}
```
}
Document(title: "Gliders", into: report) {
Panel(heading: "Results") {
Finding(title: "Gliders", count: 3)
Method(name: "counting", share: "50%")
}
}
echo $report->convert(to: markdown)- Inside
Document, a component draws with its documentrender. What it adds goes where it is written, so a heading in a part that sits inside aSectionbecomes a subsection. slotworks in arenderthat holds elements. A block is all text, so it has nowhere to put the caller's content.- A document is drawn once, so a component in it keeps no state between draws.
- In a
mdblock,{ }holds a value, as it does in aParagraph. Maths is written\( x^{2} \)and\[ x^{2} \], and keeps its braces. - In a
latexblock the braces are LaTeX's. A group that holds only a value, such as{$name}or{@today()}, is filled in and keeps its braces, so\textbf{$name}is the name in bold. The value is escaped, so a%,&or_in it is shown as text. - A
latexblock reads the LaTeX a part is usually written in:
| LaTeX | What it reads |
|---|---|
| Structure | \section, \subsection, \subsubsection (with * for no number) and \label |
| Text | \textbf, \textit, \emph, \texttt, \underline, \href, \url, \footnote, \cite, \ref, \\, ~, `` and '', -- and ---, and % comments |
| Maths | $...$, \( \), \[ \], $$ $$, and the equation, equation*, displaymath, align and align* environments |
| Blocks | itemize, enumerate, quote, quotation, center, verbatim and figure (with \includegraphics, \caption and \label) |
| Layout | \newpage, \clearpage, \tableofcontents, \hrule, \smallskip, \medskip, \bigskip and \par |
A command it doesn't read is an error that names it, so nothing is left out without a word. kscript check and the editor report it where it is written. Write that part with document elements instead.