Download chp10.typ from m-rui001/typst_reference: direct link, hf CLI and curl.
- Browser
- Download file 17.1 kB
-
https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp10.typ
- Command line
-
hf download hf://datasets/m-rui001/typst_reference/chp10.typ
-
curl -L -o chp10.typ https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp10.typ
17.1 kB
| #let ctext(content)={ | |
| text(fill: red,content) | |
| } | |
| = 数据加载(Data Loading)<Data-Loading> | |
| 从外部文件加载数据。 | |
| 这些函数可帮助你加载并嵌入数据,例如来自实验结果的数据。 | |
| *编码* | |
| 部分函数同样支持编码功能,例如 cbor.encode。这类函数便于向#link(<Plugin>)[插件]传递结构化数据。 | |
| 但每种数据格式都有其原生类型。因此,对于任意 Typst | |
| 值,编码再解码的往返转换过程可能会丢失信息。一般来说,数字、字符串,以及由它们构成的#link(<array>)[数组]或#link(<dictionary>)[字典]可以可靠转换;而其他类型会通过 | |
| #link(<repr>)[repr] 转为字符串,该方式#link(<repr_For-debugging-purposes-only>)[仅用于调试]。详情请查阅各数据格式对应的文档页面。 | |
| == 导入CBOR文件(cbor)<cbor> | |
| 从CBOR文件读取结构化数据。 | |
| 简明二进制对象表示(CBOR,Concise Binary Object Representation)是基于RFC | |
| 7049标准定义的二进制数据交换格式,具有压缩性强、扩展性高且无需版本协商的特性,这些优势使其区别于ASN.1和MessagePack等传统二进制序列化方案,适用于结构化数据的编码与传输。该格式被System.Formats.Cbor命名空间实现,包含CborReader、CborWriter等核心类型,用于数据读写操作。 | |
| 该文件必须包含有效的CBOR序列化内容。CBOR值将转换为#link(<cbor_Conversion-details>)[下表]所列的对应Typst值。 | |
| 该函数返回字典、数组,或根据CBOR文件内容返回其他CBOR数据类型。 | |
| === 转换详情(Conversion details)<cbor_Conversion-details> | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*CBOR value*],[*Converted into Typst*]), | |
| table.hline(), | |
| [integer],[#link(<Integer>)[int] (or #link(<float>)[float])], | |
| [bytes],[#link(<bytes>)[bytes]], | |
| [float],[#link(<float>)[float]], | |
| [text],[#link(<String>)[str]], | |
| [bool],[#link(<bool>)[bool]], | |
| [null],[none], | |
| [array],[#link(<array>)[array]], | |
| [map],[#link(<dictionary>)[dictionary]], | |
| ) | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*Typst*],[*Converted into CBOR*]), | |
| table.hline(), | |
| [可从CBOR转换的类型],[对应的CBOR值], | |
| [#link(<symbol>)[symbol]],[text], | |
| [#link(<content>)[content]],[a map describing the content], | |
| [other types (#link(<length>)[length], etc.)],[text via #link(<repr>)[repr]], | |
| ) | |
| *注释* | |
| -- 请注意:大于$2^(63)-1$或小于$-2^(63)$的CBOR整数会被转换为浮点数,可能产生近似值。 | |
| -- 不支持CBOR标签,遇到时将抛出错误。 | |
| -- repr函数#link(<repr_For-debugging-purposes-only>)[仅用于调试],无法保证其输出在不同Typst版本间保持稳定。 | |
| === 参数(Parameters) | |
| ```typc | |
| cbor(source: str path bytes) → any | |
| ``` | |
| -- *source*: str path bytes | |
| CBOR文件的路径或原始CBOR字节。 | |
| === 定义(Definitions) | |
| - *encode* | |
| 将结构化数据编码为CBOR字节。 | |
| ```typc | |
| cbor.encode(value: any) → bytes | |
| ``` | |
| -- *value*: any | |
| 待编码的值。 | |
| == 导入csv格式文文件<csv> | |
| 从CSV文件读取结构化数据。 | |
| 读取并解析CSV文件,生成字符串二维数组:CSV文件中的每一行都会表示为一个字符串数组,所有行汇总为一个数组。不会移除表头行。 | |
| === 示例(Example) | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #let results = csv("example.csv") | |
| #table( | |
| columns: 2, | |
| [*Condition*], [*Result*], | |
| ..results.flatten(), | |
| ) | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #let results = csv("data/example.csv") | |
| #table( | |
| columns: 2, | |
| [*Condition*], [*Result*], | |
| ..results.flatten(), | |
| ) | |
| ] | |
| ) | |
| 附csv文件格式:csv文件是一个普通的文本文件,各行的数据一样多,通常每列的数据由逗号","隔开,当然也可以换成其它符号,不过默认的是逗号,example.csv中的数据如下面所示 | |
| ```txt | |
| 1..2, small | |
| 3..5, medium | |
| 6.., big | |
| ``` | |
| === 参数(Parameters) | |
| ```typc | |
| csv( | |
| source: str path bytes, | |
| delimiter: str, | |
| row-type: type, | |
| ) → array | |
| ``` | |
| - *source*: str path bytes | |
| CSV文件的路径或者原始CSV字节数据。 | |
| - *delimiter*: str | |
| CSV文件中用于分隔列的分隔符。必须是单个ASCII字符。 | |
| - *row-type*: type | |
| 如何表示文件中的行。 | |
| -- 若设置为array,每一行将以普通字符串数组表示。 | |
| -- 若设置为dictionary,每一行将以字典表示,字典将表头键映射为字符串。该选项仅在CSV文件包含表头行时有效。 | |
| == 导入json格式文文件(json)<json> | |
| 从JSON文件读取结构化数据。 | |
| 该文件必须包含有效的JSON值,例如对象或数组。JSON值将转换为下表所列的对应Typst值。 | |
| 此函数返回字典、数组,或是根据JSON文件内容返回其他JSON数据类型。 | |
| 示例中的JSON文件包含带有temperature、unit和weather这些键的对象。 | |
| === 示例(Example) | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #let forecast(day) = block[ | |
| #box(square( | |
| width: 2cm, | |
| inset: 8pt, | |
| fill: if day.weather == "sunny" { | |
| yellow | |
| } else { | |
| aqua | |
| }, | |
| align( | |
| bottom + right, | |
| strong(day.weather), | |
| ), | |
| )) | |
| #h(6pt) | |
| #set text(22pt, baseline: -8pt) | |
| #day.temperature °#day.unit | |
| ] | |
| #forecast(json("monday.json")) | |
| #forecast(json("tuesday.json")) | |
| ``` | |
| ],[ | |
| #let forecast(day) = block[ | |
| #box(square( | |
| width: 2cm, | |
| inset: 8pt, | |
| fill: if day.weather == "sunny" { | |
| yellow | |
| } else { | |
| aqua | |
| }, | |
| align( | |
| bottom + right, | |
| strong(day.weather), | |
| ), | |
| )) | |
| #h(6pt) | |
| #set text(22pt, baseline: -8pt) | |
| #day.temperature °#day.unit | |
| ] | |
| #forecast(json("data/monday.json")) | |
| #forecast(json("data/tuesday.json")) | |
| ] | |
| ) | |
| 附:monday.json文件内容: | |
| ```json | |
| { | |
| "weather": "sunny", | |
| "temperature": "18.5", | |
| "unit": "C" | |
| } | |
| ``` | |
| 和tuesday.json文件内容: | |
| ```json | |
| { | |
| "weather": "windy", | |
| "temperature": "14.5", | |
| "unit": "C" | |
| } | |
| ``` | |
| === 转换详情(Conversion details)<json_Conversion-details> | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*JSON value*],[*Converted into Typst*]), | |
| table.hline(), | |
| [null],[none], | |
| [bool],[#link(<bool>)[bool]], | |
| [number],[#link(<float>)[float] or #link(<Integer>)[int]], | |
| [string],[#link(<String>)[str]], | |
| [array],[#link(<array>)[array]], | |
| [Object],[#link(<dictionary>)[dictionary]], | |
| ) | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*Typst*],[*Converted into JSON*]), | |
| table.hline(), | |
| [可从JSON转换而来的类型],[对应的JSON值], | |
| [#link(<bytes>)[bytes]],[string via repr], | |
| [#link(<symbol>)[symbol]],[string], | |
| [#link(<content>)[content]],[an object describing the content], | |
| [other types (#link(<length>)[length], etc.)],[string via #link(<repr>)[repr]], | |
| ) | |
| *注释* | |
| -- 在大多数情况下,JSON 数字会根据是否为整数,转换为浮点数或整数。但请注意:大于 $2^(63)-1$ 或小于 $-2^(63)$ 的整数会被转换为浮点数,这可能产生近似值。 | |
| -- 出于性能和可读性考虑,字节不会编码为 JSON 数组。处理二进制数据可考虑使用 cbor.encode。 | |
| -- repr 函数仅用于调试,不保证其输出在不同 Typst 版本间保持稳定。 | |
| === 参数(Parameters) | |
| ```typc | |
| json(source: str path bytes) → any | |
| ``` | |
| - *source*: str path bytes | |
| JSON文件的路径或原始JSON字节数据。 | |
| === 定义(Definitions) | |
| - *encode*<json_encode> | |
| 将结构化数据编码为JSON字符串。 | |
| ```typc | |
| json.encode( | |
| value: any, | |
| pretty: bool, | |
| ) → str | |
| ``` | |
| #set par(hanging-indent: 1.3cm) | |
| -- *value*: any | |
| 待编码的值。 | |
| -- *pretty*: bool | |
| 是否使用换行和缩进对JSON进行美化输出。默认是:true. | |
| #set par(hanging-indent: 0cm) | |
| == 取数据(read)<read> | |
| 从文件读取纯文本或数据。 | |
| 默认情况下,文件将以UTF-8编码读取,并以字符串形式返回。 | |
| 如果你指定encoding: none,则会返回原始字节。 | |
| === 示例(Example) | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typc | |
| An example for a HTML file: \ | |
| #let text = read("example.html") | |
| #raw(text, block: true, lang: "html") | |
| Raw bytes: | |
| #read("tiger.jpg", encoding: none) | |
| ``` | |
| ],[ | |
| #set align(horizon+center) | |
| #image("images/x01.png",width: 95%) | |
| ] | |
| ) | |
| === 参数(Parameters) | |
| ```typc | |
| read( | |
| path: str path, | |
| encoding: none str, | |
| ) → strbytes | |
| ``` | |
| - *path*: str path | |
| 文件的路径。 | |
| - *encoding*: none str | |
| 读取文件所用的编码。 | |
| 若设为none,该函数将返回原始字节。 | |
| #table( | |
| stroke: none, | |
| columns: (1fr,1fr), | |
| table.header([*Variant*],[*Details*]), | |
| table.hline(), | |
| [\"utf8\"],[Unicode UTF-8编码。] | |
| ) | |
| == 导入toml格式文件(toml)<toml> | |
| 从TOML文件读取结构化数据。 | |
| 该文件必须包含合法的TOML表。TOML的值会转换为下表所列的对应Typst值。 | |
| 此函数返回一个代表该TOML表的字典。 | |
| 示例中的TOML文件包含一张表,拥有标题、版本和作者这些键。 | |
| === 示例(Example) | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| align: left, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typc | |
| #let details = toml("data/details.toml") | |
| Title: #details.title \ | |
| Version: #details.version \ | |
| Authors: #(details.authors | |
| .join(", ", last: " and ")) | |
| ``` | |
| ],[ | |
| #let details = toml("data/details.toml") | |
| Title: #details.title | |
| Version: #details.version | |
| Authors: #(details.authors | |
| .join(", ", last: " and ")) | |
| ] | |
| ) | |
| 其中details.toml文件的内容为: | |
| ```toml | |
| title="Secret project" | |
| version=2 | |
| authors=["Mr Robert","Miss Enola"] | |
| ``` | |
| === 转换详情(Conversion details)<toml_Conversion-details> | |
| 首先,TOML文档本质是表。其他值必须放入表中,才能进行编码或解码。 | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*TOML value*],[*Converted into Typst*]), | |
| table.hline(), | |
| [string],[#link(<String>)[str]], | |
| [integer],[#link(<Integer>)[int]], | |
| [float],[#link(<float>)[float]], | |
| [boolean],[#link(<bool>)[bool]], | |
| [datetime],[#link(<datetime>)[datetime]], | |
| [array],[#link(<array>)[array]], | |
| [table],[#link(<dictionary>)[dictionary]], | |
| ) | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*Typst value*],[*Converted into TOML*]), | |
| table.hline(), | |
| [可从TOML转换的类型],[对应的TOML值], | |
| [#ctext[none]],[ignored], | |
| [#link(<bytes>)[bytes]],[string via #link(<repr>)[repr]], | |
| [#link(<symbol>)[symbol]],[string], | |
| [#link(<content>)[content]],[a table describing the content], | |
| [other types (#link(<length>)[length], etc.)],[string via #link(<repr>)[repr]], | |
| ) | |
| *注释* | |
| -- 请注意:大于$2^(63)−1$或小于$−2^(63)$的TOML整数无法在Typst中无损表示,依据#link("https://toml.io/en/v1.0.0#integer")[规范],此时会抛出错误。 | |
| -- 出于性能与可读性考虑,字节不会编码为TOML数组。处理二进制数据可考虑使用#link(<cbor>)[cbor.encode]。 | |
| -- repr函数#link(<repr_For-debugging-purposes-only>)[仅用于调试],无法保证其输出在不同Typst版本间保持稳定。 | |
| === 参数(Parameters) | |
| ```typc | |
| toml(source: str path bytes) → any | |
| ``` | |
| - *source*: str path bytes | |
| TOML文件的路径或原始TOML字节数据。 | |
| === 定义(Definitions) | |
| - *encode*<toml_encode> | |
| 将结构化数据编码为TOML字符串。 | |
| ```typc | |
| toml.encode( | |
| value: dictionary, | |
| pretty: bool, | |
| ) → str | |
| ``` | |
| - *value*: dictionary, | |
| 待编码的值。 | |
| TOML文档本质是表结构,因此仅字典类型适用。 | |
| - *pretty*: bool, | |
| 是否对生成的TOML进行美化输出。 | |
| == 导入XML格式文件(xml)<xml> | |
| 从XML文件读取结构化数据。 | |
| XML文件会被解析为字典与字符串组成的数组。XML节点可以是元素或字符串。元素以字典形式表示,包含以下键: | |
| -- tag:字符串类型的元素名称。 | |
| -- attrs:由字符串构成的元素属性字典。 | |
| -- children:该元素子节点组成的数组。 | |
| 示例中的XML文件包含一个根news标签,内部有多个article标签。每篇文章拥有title、author以及content标签。content标签包含一个或多个段落,段落用p标签表示。 | |
| === 示例(Example) | |
| #table( | |
| columns: 1fr, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (x==0 and y==1) {luma(220)}, | |
| [ | |
| ```typc | |
| #let find-child(elem, tag) = { | |
| elem.children | |
| .find(e => "tag" in e and e.tag == tag) | |
| } | |
| #let article(elem) = { | |
| let title = find-child(elem, "title") | |
| let author = find-child(elem, "author") | |
| let pars = find-child(elem, "content") | |
| [= #title.children.first()] | |
| text(10pt, weight: "medium")[ | |
| Published by | |
| #author.children.first() | |
| ] | |
| for p in pars.children { | |
| if type(p) == dictionary { | |
| parbreak() | |
| p.children.first() | |
| } | |
| } | |
| } | |
| #let data = xml("example.xml") | |
| #for elem in data.first().children { | |
| if type(elem) == dictionary { | |
| article(elem) | |
| } | |
| } | |
| ``` | |
| ], | |
| [ | |
| #set align(center) | |
| #image("images/x02.png",width: 80%) | |
| ] | |
| ) | |
| 其中example.xml文件的内容为: | |
| ```xml | |
| <menu> | |
| <work> | |
| <title>2022 Budget approved</title> | |
| <author>John Doe</author> | |
| <content> | |
| <c1>The 2022 budget has been approved by Senate.</c1> | |
| </content> | |
| </work> | |
| <work> | |
| <title>Tiger win the World Series</title> | |
| <author>John Doe</author> | |
| <content> | |
| <c1>The Tigers have won the World Series.</c1> | |
| <c2>The beat the Giants 4 to 3.</c2> | |
| </content> | |
| </work> | |
| </menu> | |
| ``` | |
| === 参数(Parameters) | |
| ```typc | |
| xml(source: str path bytes) → any | |
| ``` | |
| - *source*: str path bytes | |
| XML文件的路径或原始XML字节。 | |
| == 导入YAML格式文件(yaml)<yaml> | |
| 从YAML文件读取结构化数据。 | |
| 该文件必须包含有效的YAML对象或数组。YAML值将转换为[下表]所列的对应Typst值。 | |
| 该函数返回字典、数组,或根据YAML文件内容返回其他YAML数据类型。 | |
| 示例中的YAML文件包含以作者为键的对象,每个作者对象带有一组子映射,子映射的键为“title”和“published”。 | |
| === 示例(Example) | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (luma(255),luma(240)), | |
| [ | |
| ```typc | |
| #let bookshelf(contents) = { | |
| for (author, works) in contents { | |
| author | |
| for work in works [ | |
| - #work.title (#work.published) | |
| ] | |
| } | |
| } | |
| #bookshelf( | |
| yaml("data/scifi-authors.yaml") | |
| ) | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon+center) | |
| #image("images/x03.png",width: 100%) | |
| ] | |
| ) | |
| 其中scifi-authors.yaml文件的内容如下: | |
| ```yaml | |
| Arthur C. Clarke: | |
| - title: Against the Fall of Night(1978) | |
| published: 1978 | |
| - title: The songs of distant earth | |
| published: 1986 | |
| Isaac Asimov: | |
| - title: Quasar, Quasar, Burning Bright | |
| published: 1977 | |
| - title: Far as Human Eye Could See | |
| published: 1987 | |
| ``` | |
| === 转换详情(Conversion details)<toml_Conversion-details> | |
| #table( | |
| align: left, | |
| columns: (1fr,2fr), | |
| stroke: none, | |
| table.header([*YAML value*],[*Converted into Typst*]), | |
| table.hline(), | |
| [null-values (null, ~ or empty )],[none], | |
| [boolean],[#link(<bool>)[bool]], | |
| [number],[#link(<float>)[float] or #link(<Integer>)[int]], | |
| [string],[str], | |
| [sequence],[array], | |
| [mapping],[dictionary], | |
| ) | |
| #table( | |
| align: left, | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| table.header([*Typst*],[*Converted into YAML*]), | |
| table.hline(), | |
| [可从YAML转换而来的类型],[对应的YAML值], | |
| [#link(<bytes>)[bytes]],[string via repr], | |
| [#link(<symbol>)[symbol]],[string], | |
| [#link(<content>)[content]],[a mapping describing the content], | |
| [other types (#link(<length>)[length], etc.)],[string via #link(<repr>)[repr]], | |
| ) | |
| *注释* | |
| -- 在大多数情况下,YAML数字会根据是否为整数,转换为浮点数或整数。但请注意:大于2⁶³−1或小于−2⁶³的整数会被转换为浮点数,这可能产生近似值。 | |
| -- 自定义YAML标签会被忽略,但加载后的值仍然保留。 | |
| -- 出于性能和可读性考虑,字节不会编码为YAML序列。二进制数据可考虑使用#link(<cbor>)[cbor.encode]。 | |
| -- repr函数#link(<repr_For-debugging-purposes-only>)[仅用于调试],其输出无法保证在不同Typst版本间保持稳定。 | |
| === 参数(Parameters) | |
| ```typc | |
| yaml(source: str path bytes) → any | |
| ``` | |
| - *source*: str path bytes | |
| YAML文件的路径或原始JSON字节数据。 | |
| === 定义(Definitions) | |
| 将结构化数据编码为YAML字符串。 | |
| ```typc | |
| yaml.encode(value: any) → str | |
| ``` | |
| - *value*: any | |
| 待编码的值。 | |