summaryrefslogtreecommitdiff
path: root/crates/typst-cli
diff options
context:
space:
mode:
authorLaurenz <ritters_werth@outlook.com>2023-12-18 12:18:41 +0100
committerGitHub <noreply@github.com>2023-12-18 12:18:41 +0100
commit22ba6825db3b82e0b0f83ef6052f17289893e385 (patch)
treecf69e7ac0eeb49f13ffb0b81e36461e07cb766ba /crates/typst-cli
parent356bdeba18153efd4209657182971e22bdaf4db2 (diff)
Key/Value data from CLI (#2894)
Diffstat (limited to 'crates/typst-cli')
-rw-r--r--crates/typst-cli/src/args.rs26
-rw-r--r--crates/typst-cli/src/world.rs17
2 files changed, 40 insertions, 3 deletions
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs
index 075412cd..8f0261f1 100644
--- a/crates/typst-cli/src/args.rs
+++ b/crates/typst-cli/src/args.rs
@@ -1,6 +1,7 @@
use std::fmt::{self, Display, Formatter};
use std::path::PathBuf;
+use clap::builder::ValueParser;
use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use semver::Version;
@@ -116,6 +117,15 @@ pub struct SharedArgs {
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
pub root: Option<PathBuf>,
+ /// Add a string key-value pair visible through `sys.inputs`
+ #[clap(
+ long = "input",
+ value_name = "key=value",
+ action = ArgAction::Append,
+ value_parser = ValueParser::new(parse_input_pair),
+ )]
+ pub inputs: Vec<(String, String)>,
+
/// Adds additional directories to search for fonts
#[clap(
long = "font-path",
@@ -134,6 +144,22 @@ pub struct SharedArgs {
pub diagnostic_format: DiagnosticFormat,
}
+/// Parses key/value pairs split by the first equal sign.
+///
+/// This function will return an error if the argument contains no equals sign
+/// or contains the key (before the equals sign) is empty.
+fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
+ let (key, val) = raw
+ .split_once('=')
+ .ok_or("input must be a key and a value separated by an equal sign")?;
+ let key = key.trim().to_owned();
+ if key.is_empty() {
+ return Err("the key was missing or empty".to_owned());
+ }
+ let val = val.trim().to_owned();
+ Ok((key, val))
+}
+
/// Lists all discovered fonts in system and custom font paths
#[derive(Debug, Clone, Parser)]
pub struct FontsCommand {
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 8f8a7e5c..cd4244fc 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -7,7 +7,7 @@ use chrono::{DateTime, Datelike, Local};
use comemo::Prehashed;
use ecow::eco_format;
use typst::diag::{FileError, FileResult, StrResult};
-use typst::foundations::{Bytes, Datetime};
+use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook};
use typst::{Library, World};
@@ -68,14 +68,25 @@ impl SystemWorld {
// Resolve the virtual path of the main file within the project root.
let main_path = VirtualPath::within_root(&input, &root)
- .ok_or("input file must be contained in project root")?;
+ .ok_or("source file must be contained in project root")?;
+
+ let library = {
+ // Convert the input pairs to a dictionary.
+ let inputs: Dict = command
+ .inputs
+ .iter()
+ .map(|(k, v)| (k.as_str().into(), v.as_str().into_value()))
+ .collect();
+
+ Library::builder().with_inputs(inputs).build()
+ };
Ok(Self {
workdir: std::env::current_dir().ok(),
input,
root,
main: FileId::new(None, main_path),
- library: Prehashed::new(Library::build()),
+ library: Prehashed::new(library),
book: Prehashed::new(searcher.book),
fonts: searcher.fonts,
slots: RwLock::new(HashMap::new()),