diff options
Diffstat (limited to 'library/src')
| -rw-r--r-- | library/src/compute/data.rs | 90 | ||||
| -rw-r--r-- | library/src/lib.rs | 1 |
2 files changed, 91 insertions, 0 deletions
diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 0c17972d..c2dc632b 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -207,6 +207,96 @@ fn format_json_error(error: serde_json::Error) -> String { format!("failed to parse json file: syntax error in line {}", error.line()) } +/// Read structured data from a YAML file. +/// +/// The file must contain a valid YAML object or array. YAML mappings will be +/// converted into Typst dictionaries, and YAML sequences will be converted into +/// Typst arrays. Strings and booleans will be converted into the Typst +/// equivalents, null-values (`null`, `~` or empty ``) will be converted into +/// `{none}`, and numbers will be converted to floats or integers depending on +/// whether they are whole numbers. +/// +/// Note that mapping keys that are not a string cause the entry to be +/// discarded. +/// +/// Custom YAML tags are ignored, though the loaded value will still be +/// present. +/// +/// The function returns a dictionary or value or an array, depending on +/// the YAML file. +/// +/// 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 +/// ```example +/// #let bookshelf(contents) = { +/// for author, works in contents { +/// author +/// for work in works [ +/// - #work.title (#work.published) +/// ] +/// } +/// } +/// +/// #bookshelf(yaml("scifi-authors.yaml")) +/// ``` +/// +/// Display: YAML +/// Category: data-loading +/// Returns: array or value or dictionary +#[func] +pub fn yaml( + /// Path to a YAML file. + path: Spanned<EcoString>, +) -> Value { + let Spanned { v: path, span } = path; + let path = vm.locate(&path).at(span)?; + let data = vm.world().file(&path).at(span)?; + let value: serde_yaml::Value = + serde_yaml::from_slice(&data).map_err(format_yaml_error).at(span)?; + convert_yaml(value) +} + +/// Convert a YAML value to a Typst value. +fn convert_yaml(value: serde_yaml::Value) -> Value { + match value { + serde_yaml::Value::Null => Value::None, + serde_yaml::Value::Bool(v) => Value::Bool(v), + serde_yaml::Value::Number(v) => match v.as_i64() { + Some(int) => Value::Int(int), + None => Value::Float(v.as_f64().unwrap_or(f64::NAN)), + }, + serde_yaml::Value::String(v) => Value::Str(v.into()), + serde_yaml::Value::Sequence(v) => { + Value::Array(v.into_iter().map(convert_yaml).collect()) + } + serde_yaml::Value::Mapping(v) => Value::Dict( + v.into_iter() + .map(|(key, value)| (convert_yaml_key(key), convert_yaml(value))) + .filter_map(|(key, value)| key.map(|key|(key, value))) + .collect(), + ) + } +} + +/// Converts an arbitary YAML mapping key into a Typst Dict Key. +/// Currently it only does so for strings, everything else +/// returns None +fn convert_yaml_key(key: serde_yaml::Value) -> Option<Str> { + match key { + serde_yaml::Value::String(v) => Some(Str::from(v)), + _ => None, + } +} + +/// Format the user-facing YAML error message. +#[track_caller] +fn format_yaml_error(error: serde_yaml::Error) -> String { + format!("failed to parse yaml file: {}", error.to_string().trim()) +} + /// Read structured data from an XML file. /// /// The XML file is parsed into an array of dictionaries and strings. XML nodes diff --git a/library/src/lib.rs b/library/src/lib.rs index 178db8a2..cabafd8c 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -124,6 +124,7 @@ fn global(math: Module, calc: Module) -> Module { global.define("read", compute::read); global.define("csv", compute::csv); global.define("json", compute::json); + global.define("yaml", compute::yaml); global.define("xml", compute::xml); // Calc. |
