注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

yaml

从 YAML 文件读取结构和化数据

文件必须包含有效的 YAML 对象或数组。 YAML 映射将被转换为 Typst 字典,YAML 序列将被转换为 Typst 数组。 字符串和布尔值将被转换为相应的 Typst 类型值。 null 值(null~ 或空字符串 '')将转换为 none。 数字将根据是否为整数转化为浮点值或整数值。 自定义 YAML 标签将被忽略,但加载的值仍将存在。

示例中的 YAML 文件为一个以 author 为键,子键为 “title” 和 “published” 的对象。

举例

#let bookshelf(contents) = {
  for (author, works) in contents {
    author
    for work in works [
      - #work.title (#work.published)
    ]
  }
}

#bookshelf(
  yaml("scifi-authors.yaml")
)
Preview

参数
参数是函数的输入,它们在函数名称后面的括号中传入。

yaml() -> any

path
str
必需参数位置参数
位置参数按顺序传入,不带名称。

YAML 文件路径。

定义
函数和类型可以有与其关联的定义 (成员或方法)。可以使用 "." 操作符来访问调用它们。

decode

从 YAML 字符串/字节流读取结构化数据。

yaml.decode() -> any

data
str bytes
必需参数位置参数
位置参数按顺序传入,不带名称。

YAML 数据。

encode

将结构化数据编码为一个 YAML 字符串

yaml.encode() -> str

value
any
必需参数位置参数
位置参数按顺序传入,不带名称。

被编码的值。