KScript 文档
ZH

markdown 字段与方法

字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。

字段

字段类型说明
.extensionstring源文件扩展名(不含点)。
.textstring带围栏值的源文本。

来自 KScript.Document

这些需要 <import package="KScript.Document" />import KScript.Document。参见 KScript.Document。

#markdown->find(text: string) → number KScript.Document

首个字面匹配的从零开始的源偏移量,若无匹配则为 -1。
参数接受
text 必需string
示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<echo>{ $notes->find('ideas') }</echo>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
echo $notes->find('ideas')

#markdown->insert(content, at?: int) → markdown KScript.Document

返回一个副本,在从零开始的偏移位置插入相同格式的文本或源码。未指定偏移时则追加到末尾。
参数接受
content 必填任意值
at 可选int

参数可以使用命名方式:->insert(…, at: …)。

示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<echo>{ $notes->insert(' More soon.').text }</echo>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
echo $notes->insert(' More soon.').text

#markdown->pages() → number KScript.Document

PDF 文件的页数。
示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<echo>{ $notes->pages() } pages</echo>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
echo "{ $notes->pages() } pages"

#markdown->replace(search: string, replacement: string) → markdown KScript.Document

返回一个副本,其中每个字面匹配项都被替换。
参数接受
search 必填string
replacement 必填string

参数可以使用命名方式:->replace(…, replacement: …)。

示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<echo>{ $notes->replace('ideas', 'plans').text }</echo>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
echo $notes->replace('ideas', 'plans').text

#markdown->save(path: string, as?: pdf|docx|'latex'|'markdown'|html) KScript.Document

将文档写入文件。文件扩展名(或 as:)决定格式:.md 保留源码,.pdf、.docx、.tex 或 .html 则进行渲染。若无扩展名,则默认为 .md。
参数接受
path 必需string
as 可选以下词之一:pdf|docx|'latex'|'markdown'|html

参数可以使用命名方式:->save(…, as: …)。

示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<do>$notes->save('notes.pdf')</do>
<do>$notes->save('notes.docx')</do>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
$notes->save('notes.pdf')
$notes->save('notes.docx')

#markdown->words() → number KScript.Document

文本的字数(不计数学公式和代码)。
示例
<let name="notes" value="```markdown
  # Notes

  Some *ideas* for the launch.
  ```" />
<echo>{ $notes->words() } words</echo>
$notes = ```markdown
  # Notes

  Some *ideas* for the launch.
  ```
echo "{ $notes->words() } words"

来自 any 的方法