文档中的组件
组件可以是文档的一部分:一条发现、一个方法、一个签名区块。它的 render 包含文档元素,或返回以 Markdown 或 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)- 在
Document内,组件使用其文档render进行绘制。它添加的内容会出现在书写位置,因此位于Section内的某部分中的标题会成为小节。 slot适用于包含元素的render。块级内容全部为文本,因此无法容纳调用方提供的内容。- 文档只绘制一次,因此其中的组件在两次绘制之间不保留任何状态。
- 在
md块中,{ }保存一个值,与Paragraph中相同。数学公式写作\( x^{2} \)和\[ x^{2} \],并保留其花括号。 - 在
latex块中,花括号属于 LaTeX。仅包含一个值的分组,例如{$name}或{@today()},会被填充并保留其花括号,因此\textbf{$name}为加粗的名称。该值会被转义,因此其中的%、&或_会显示为文本。 latex块可读取部分内容通常所用的 LaTeX:
| LaTeX | 读取内容 |
|---|---|
| 结构 | \section、\subsection、\subsubsection(不编号时使用 *)以及 \label |
| 文本 | \textbf、\textit、\emph、\texttt、\underline、\href、\url、\footnote、\cite、\ref、\\、~、``、''、--、--- 以及 % 注释 |
| 数学 | $...$、\( \)、\[ \]、$$ $$,以及 equation、equation*、displaymath、align 和 align* 环境 |
| 区块 | itemize、enumerate、quote、quotation、center、verbatim 和 figure(以及 \includegraphics、\caption 和 \label) |
| 布局 | \newpage、\clearpage、\tableofcontents、\hrule、\smallskip、\medskip、\bigskip 和 \par |
无法读取的命令会报错并指明该命令,因此不会在没有任何提示的情况下遗漏内容。kscript check 和编辑器会在书写位置报告该错误。请改用文档元素编写该部分。