feat: initial commit
This commit is contained in:
commit
b802ff49ab
454 files changed
+52429
No files matched your search
@@ -0,0 +1,86 @@
|
||||
# CBOR
|
||||
|
||||
# cbor
|
||||
|
||||
Reads structured data from a CBOR file.
|
||||
|
||||
The file must contain a valid CBOR serialization. The CBOR values will be converted into corresponding Typst values as listed in the [table below](#conversion).
|
||||
|
||||
The function returns a dictionary, an array or, depending on the CBOR file, another CBOR data type.
|
||||
|
||||
## Conversion details
|
||||
|
||||
| CBOR value | Converted into Typst |
|
||||
| --- | --- |
|
||||
| integer | [`int`](/docs/reference/foundations/int/) (or [`float`](/docs/reference/foundations/float/)) |
|
||||
| bytes | [`bytes`](/docs/reference/foundations/bytes/) |
|
||||
| float | [`float`](/docs/reference/foundations/float/) |
|
||||
| text | [`str`](/docs/reference/foundations/str/) |
|
||||
| bool | [`bool`](/docs/reference/foundations/bool/) |
|
||||
| null | `none` |
|
||||
| array | [`array`](/docs/reference/foundations/array/) |
|
||||
| map | [`dictionary`](/docs/reference/foundations/dictionary/) |
|
||||
|
||||
| Typst value | Converted into CBOR |
|
||||
| --- | --- |
|
||||
| types that can be converted from CBOR | corresponding CBOR value |
|
||||
| [`symbol`](/docs/reference/foundations/symbol/) | text |
|
||||
| [`content`](/docs/reference/foundations/content/) | a map describing the content |
|
||||
| other types ([`length`](/docs/reference/layout/length/), etc.) | text via [`repr`](/docs/reference/foundations/repr/) |
|
||||
|
||||
### Notes
|
||||
|
||||
- Be aware that CBOR integers larger than 263-1 or smaller than -263 will be converted to floating point numbers, which may result in an approximative value.
|
||||
- CBOR tags are not supported, and an error will be thrown.
|
||||
- The `repr` function is [for debugging purposes only](/docs/reference/foundations/repr/#debugging-only), and its output is not guaranteed to be stable across Typst versions.
|
||||
|
||||
```typst
|
||||
#cbor(
|
||||
source
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a CBOR file or raw CBOR bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### cbor.decode
|
||||
|
||||
Reads structured data from CBOR bytes.
|
||||
|
||||
```typst
|
||||
#cbor.decode(
|
||||
data
|
||||
) -> any
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: CBOR data.
|
||||
- type: bytes
|
||||
- default: None
|
||||
|
||||
### cbor.encode
|
||||
|
||||
Encode structured data into CBOR bytes.
|
||||
|
||||
```typst
|
||||
#cbor.encode(
|
||||
value
|
||||
) -> bytes
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- value:
|
||||
- description: Value to be encoded.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# CSV
|
||||
|
||||
# csv
|
||||
|
||||
Reads structured data from a CSV file.
|
||||
|
||||
The CSV file will be read and parsed into a 2-dimensional array of strings: Each row in the CSV file will be represented as an array of strings, and all rows will be collected into a single array. Header rows will not be stripped.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let results = csv("example.csv")
|
||||
|
||||
#table(
|
||||
columns: 2,
|
||||
[*Condition*], [*Result*],
|
||||
..results.flatten(),
|
||||
)
|
||||
```
|
||||
|
||||
```typst
|
||||
#csv(
|
||||
source,
|
||||
delimiter: str,
|
||||
row-type: type
|
||||
) -> array
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a CSV file or raw CSV bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
- delimiter:
|
||||
- description: The delimiter that separates columns in the CSV file. Must be a single ASCII character.
|
||||
- type: str
|
||||
- default: ","
|
||||
- row-type:
|
||||
- description: How to represent the file\'s rows. - If set to `array`, each row is represented as a plain array of strings. - If set to `dictionary`, each row is represented as a dictionary mapping from header keys to strings. This option only makes sense when a header row is present in the CSV file.
|
||||
- type: type
|
||||
- default: array
|
||||
|
||||
|
||||
## Definitions
|
||||
### csv.decode
|
||||
|
||||
Reads structured data from a CSV string/bytes.
|
||||
|
||||
```typst
|
||||
#csv.decode(
|
||||
data,
|
||||
delimiter: str,
|
||||
row-type: type
|
||||
) -> array
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: CSV data.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
- delimiter:
|
||||
- description: The delimiter that separates columns in the CSV file. Must be a single ASCII character.
|
||||
- type: str
|
||||
- default: ","
|
||||
- row-type:
|
||||
- description: How to represent the file\'s rows. - If set to `array`, each row is represented as a plain array of strings. - If set to `dictionary`, each row is represented as a dictionary mapping from header keys to strings. This option only makes sense when a header row is present in the CSV file.
|
||||
- type: type
|
||||
- default: array
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Data Loading
|
||||
|
||||
Data loading from external files.
|
||||
|
||||
These functions help you with loading and embedding data, for example from the results of an experiment.
|
||||
|
||||
## Encoding
|
||||
|
||||
Some of the functions are also capable of encoding, e.g. [`cbor.encode`](/docs/reference/data-loading/cbor/#definitions-encode). They facilitate passing structured data to [plugins](/docs/reference/foundations/plugin/).
|
||||
|
||||
However, each data format has its own native types. Therefore, for an arbitrary Typst value, the encode-to-decode roundtrip might be lossy. In general, numbers, strings, and [arrays](/docs/reference/foundations/array/) or [dictionaries](/docs/reference/foundations/dictionary/) composed of them can be reliably converted, while other types may fall back to strings via [`repr`](/docs/reference/foundations/repr/), which is [for debugging purposes only](/docs/reference/foundations/repr/#debugging-only). Please refer to the page of each data format for details.
|
||||
|
||||
## Definitions
|
||||
|
||||
| Name | Description |
|
||||
| --- | --- |
|
||||
| [`cbor`](/reference/data-loading/cbor/) | Reads structured data from a CBOR file. |
|
||||
| [`csv`](/reference/data-loading/csv/) | Reads structured data from a CSV file. |
|
||||
| [`json`](/reference/data-loading/json/) | Reads structured data from a JSON file. |
|
||||
| [`read`](/reference/data-loading/read/) | Reads plain text or data from a file. |
|
||||
| [`toml`](/reference/data-loading/toml/) | Reads structured data from a TOML file. |
|
||||
| [`xml`](/reference/data-loading/xml/) | Reads structured data from an XML file. |
|
||||
| [`yaml`](/reference/data-loading/yaml/) | Reads structured data from a YAML file. |
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
# JSON
|
||||
|
||||
# json
|
||||
|
||||
Reads structured data from a JSON file.
|
||||
|
||||
The file must contain a valid JSON value, such as object or array. The JSON values will be converted into corresponding Typst values as listed in the [table below](#conversion).
|
||||
|
||||
The function returns a dictionary, an array or, depending on the JSON file, another JSON data type.
|
||||
|
||||
The JSON files in the example contain objects with the keys `temperature`, `unit`, and `weather`.
|
||||
|
||||
## Example
|
||||
|
||||
```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"))
|
||||
```
|
||||
|
||||
## Conversion details
|
||||
|
||||
| JSON value | Converted into Typst |
|
||||
| --- | --- |
|
||||
| `null` | `none` |
|
||||
| bool | [`bool`](/docs/reference/foundations/bool/) |
|
||||
| number | [`float`](/docs/reference/foundations/float/) or [`int`](/docs/reference/foundations/int/) |
|
||||
| string | [`str`](/docs/reference/foundations/str/) |
|
||||
| array | [`array`](/docs/reference/foundations/array/) |
|
||||
| object | [`dictionary`](/docs/reference/foundations/dictionary/) |
|
||||
|
||||
| Typst value | Converted into JSON |
|
||||
| --- | --- |
|
||||
| types that can be converted from JSON | corresponding JSON value |
|
||||
| [`bytes`](/docs/reference/foundations/bytes/) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
| [`symbol`](/docs/reference/foundations/symbol/) | string |
|
||||
| [`content`](/docs/reference/foundations/content/) | an object describing the content |
|
||||
| other types ([`length`](/docs/reference/layout/length/), etc.) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
|
||||
### Notes
|
||||
|
||||
- In most cases, JSON numbers will be converted to floats or integers depending on whether they are whole numbers. However, be aware that integers larger than 263-1 or smaller than -263 will be converted to floating-point numbers, which may result in an approximative value.
|
||||
- Bytes are not encoded as JSON arrays for performance and readability reasons. Consider using [`cbor.encode`](/docs/reference/data-loading/cbor/#definitions-encode) for binary data.
|
||||
- The `repr` function is [for debugging purposes only](/docs/reference/foundations/repr/#debugging-only), and its output is not guaranteed to be stable across Typst versions.
|
||||
|
||||
```typst
|
||||
#json(
|
||||
source
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a JSON file or raw JSON bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### json.decode
|
||||
|
||||
Reads structured data from a JSON string/bytes.
|
||||
|
||||
```typst
|
||||
#json.decode(
|
||||
data
|
||||
) -> any
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: JSON data.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
### json.encode
|
||||
|
||||
Encodes structured data into a JSON string.
|
||||
|
||||
```typst
|
||||
#json.encode(
|
||||
value,
|
||||
pretty: bool
|
||||
) -> str
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- value:
|
||||
- description: Value to be encoded.
|
||||
- type: any
|
||||
- default: None
|
||||
- pretty:
|
||||
- description: Whether to pretty print the JSON with newlines and indentation.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# Read
|
||||
|
||||
# read
|
||||
|
||||
Reads plain text or data from a file.
|
||||
|
||||
By default, the file will be read as UTF-8 and returned as a [string](/docs/reference/foundations/str/).
|
||||
|
||||
If you specify `encoding: none`, this returns raw [bytes](/docs/reference/foundations/bytes/) instead.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
An example for a HTML file: \
|
||||
#let text = read("example.html")
|
||||
#raw(text, block: true, lang: "html")
|
||||
|
||||
Raw bytes:
|
||||
#read("tiger.jpg", encoding: none)
|
||||
```
|
||||
|
||||
```typst
|
||||
#read(
|
||||
path,
|
||||
encoding: none | str
|
||||
) -> str bytes
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- path:
|
||||
- description: Path to a file. For more details, see the [Paths section](/docs/reference/syntax/#paths).
|
||||
- type: str
|
||||
- default: None
|
||||
- encoding:
|
||||
- description: The encoding to read the file with. If set to `none`, this function returns raw bytes.
|
||||
- type: none | str
|
||||
- default: "utf8"
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
# TOML
|
||||
|
||||
# toml
|
||||
|
||||
Reads structured data from a TOML file.
|
||||
|
||||
The file must contain a valid TOML table. The TOML values will be converted into corresponding Typst values as listed in the [table below](#conversion).
|
||||
|
||||
The function returns a dictionary representing the TOML table.
|
||||
|
||||
The TOML file in the example consists of a table with the keys `title`, `version`, and `authors`.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let details = toml("details.toml")
|
||||
|
||||
Title: #details.title \
|
||||
Version: #details.version \
|
||||
Authors: #(details.authors
|
||||
.join(", ", last: " and "))
|
||||
```
|
||||
|
||||
## Conversion details
|
||||
|
||||
First of all, TOML documents are tables. Other values must be put in a table to be encoded or decoded.
|
||||
|
||||
| TOML value | Converted into Typst |
|
||||
| --- | --- |
|
||||
| string | [`str`](/docs/reference/foundations/str/) |
|
||||
| integer | [`int`](/docs/reference/foundations/int/) |
|
||||
| float | [`float`](/docs/reference/foundations/float/) |
|
||||
| boolean | [`bool`](/docs/reference/foundations/bool/) |
|
||||
| datetime | [`datetime`](/docs/reference/foundations/datetime/) |
|
||||
| array | [`array`](/docs/reference/foundations/array/) |
|
||||
| table | [`dictionary`](/docs/reference/foundations/dictionary/) |
|
||||
|
||||
| Typst value | Converted into TOML |
|
||||
| --- | --- |
|
||||
| types that can be converted from TOML | corresponding TOML value |
|
||||
| `none` | ignored |
|
||||
| [`bytes`](/docs/reference/foundations/bytes/) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
| [`symbol`](/docs/reference/foundations/symbol/) | string |
|
||||
| [`content`](/docs/reference/foundations/content/) | a table describing the content |
|
||||
| other types ([`length`](/docs/reference/layout/length/), etc.) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
|
||||
### Notes
|
||||
|
||||
- Be aware that TOML integers larger than 263-1 or smaller than -263 cannot be represented losslessly in Typst, and an error will be thrown according to the [specification](https://toml.io/en/v1.0.0#integer).
|
||||
- Bytes are not encoded as TOML arrays for performance and readability reasons. Consider using [`cbor.encode`](/docs/reference/data-loading/cbor/#definitions-encode) for binary data.
|
||||
- The `repr` function is [for debugging purposes only](/docs/reference/foundations/repr/#debugging-only), and its output is not guaranteed to be stable across Typst versions.
|
||||
|
||||
```typst
|
||||
#toml(
|
||||
source
|
||||
) -> dictionary
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a TOML file or raw TOML bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### toml.decode
|
||||
|
||||
Reads structured data from a TOML string/bytes.
|
||||
|
||||
```typst
|
||||
#toml.decode(
|
||||
data
|
||||
) -> dictionary
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: TOML data.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
### toml.encode
|
||||
|
||||
Encodes structured data into a TOML string.
|
||||
|
||||
```typst
|
||||
#toml.encode(
|
||||
value,
|
||||
pretty: bool
|
||||
) -> str
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- value:
|
||||
- description: Value to be encoded. TOML documents are tables. Therefore, only dictionaries are suitable.
|
||||
- type: dictionary
|
||||
- default: None
|
||||
- pretty:
|
||||
- description: Whether to pretty-print the resulting TOML.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
# XML
|
||||
|
||||
# xml
|
||||
|
||||
Reads structured data from an XML file.
|
||||
|
||||
The XML file is parsed into an array of dictionaries and strings. XML nodes can be elements or strings. Elements are represented as dictionaries with the following keys:
|
||||
|
||||
- `tag`: The name of the element as a string.
|
||||
- `attrs`: A dictionary of the element's attributes as strings.
|
||||
- `children`: An array of the element's child nodes.
|
||||
|
||||
The XML file in the example contains a root `news` tag with multiple `article` tags. Each article has a `title`, `author`, and `content` tag. The `content` tag contains one or more paragraphs, which are represented as `p` tags.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#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)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```typst
|
||||
#xml(
|
||||
source
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to an XML file or raw XML bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### xml.decode
|
||||
|
||||
Reads structured data from an XML string/bytes.
|
||||
|
||||
```typst
|
||||
#xml.decode(
|
||||
data
|
||||
) -> any
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: XML data.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
# YAML
|
||||
|
||||
# yaml
|
||||
|
||||
Reads structured data from a YAML file.
|
||||
|
||||
The file must contain a valid YAML object or array. The YAML values will be converted into corresponding Typst values as listed in the [table below](#conversion).
|
||||
|
||||
The function returns a dictionary, an array or, depending on the YAML file, another YAML data type.
|
||||
|
||||
The YAML files in the example contain objects with authors as keys, each with a sequence of their own submapping with the keys "title" and "published".
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let bookshelf(contents) = {
|
||||
for (author, works) in contents {
|
||||
author
|
||||
for work in works [
|
||||
- #work.title (#work.published)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#bookshelf(
|
||||
yaml("scifi-authors.yaml")
|
||||
)
|
||||
```
|
||||
|
||||
## Conversion details
|
||||
|
||||
| YAML value | Converted into Typst |
|
||||
| --- | --- |
|
||||
| null-values (`null`, `~` or empty ``) | `none` |
|
||||
| boolean | [`bool`](/docs/reference/foundations/bool/) |
|
||||
| number | [`float`](/docs/reference/foundations/float/) or [`int`](/docs/reference/foundations/int/) |
|
||||
| string | [`str`](/docs/reference/foundations/str/) |
|
||||
| sequence | [`array`](/docs/reference/foundations/array/) |
|
||||
| mapping | [`dictionary`](/docs/reference/foundations/dictionary/) |
|
||||
|
||||
| Typst value | Converted into YAML |
|
||||
| --- | --- |
|
||||
| types that can be converted from YAML | corresponding YAML value |
|
||||
| [`bytes`](/docs/reference/foundations/bytes/) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
| [`symbol`](/docs/reference/foundations/symbol/) | string |
|
||||
| [`content`](/docs/reference/foundations/content/) | a mapping describing the content |
|
||||
| other types ([`length`](/docs/reference/layout/length/), etc.) | string via [`repr`](/docs/reference/foundations/repr/) |
|
||||
|
||||
### Notes
|
||||
|
||||
- In most cases, YAML numbers will be converted to floats or integers depending on whether they are whole numbers. However, be aware that integers larger than 263-1 or smaller than -263 will be converted to floating-point numbers, which may result in an approximative value.
|
||||
- Custom YAML tags are ignored, though the loaded value will still be present.
|
||||
- Bytes are not encoded as YAML sequences for performance and readability reasons. Consider using [`cbor.encode`](/docs/reference/data-loading/cbor/#definitions-encode) for binary data.
|
||||
- The `repr` function is [for debugging purposes only](/docs/reference/foundations/repr/#debugging-only), and its output is not guaranteed to be stable across Typst versions.
|
||||
|
||||
```typst
|
||||
#yaml(
|
||||
source
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a YAML file or raw YAML bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### yaml.decode
|
||||
|
||||
Reads structured data from a YAML string/bytes.
|
||||
|
||||
```typst
|
||||
#yaml.decode(
|
||||
data
|
||||
) -> any
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- data:
|
||||
- description: YAML data.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
### yaml.encode
|
||||
|
||||
Encode structured data into a YAML string.
|
||||
|
||||
```typst
|
||||
#yaml.encode(
|
||||
value
|
||||
) -> str
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- value:
|
||||
- description: Value to be encoded.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
Reference in new issue
Block a user