KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

コンポーネント

ドキュメント内のコンポーネント

コンポーネントは文書の一部にもできます。たとえば所見、メソッド、署名欄などです。その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とエディターは、それが書かれた場所で報告します。その部分は代わりに文書要素で書いてください。

Edit this page on GitHub