From 8684daa17c5a11c68530a0f2f0e4da33fb67fb77 Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Sat, 7 Jun 2025 19:33:12 +0100 Subject: [PATCH 1/5] Add `--target` argument for `typst query` --- crates/typst-cli/src/args.rs | 16 ++++++++++++++++ crates/typst-cli/src/query.rs | 22 ++++++++++++++-------- crates/typst-library/src/diag.rs | 7 +++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index fd0eb5f05..2a861a504 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -151,6 +151,10 @@ pub struct QueryCommand { #[clap(long)] pub pretty: bool, + /// The target to compile for. + #[clap(long)] + pub target: Target, + /// World arguments. #[clap(flatten)] pub world: WorldArgs, @@ -445,6 +449,18 @@ pub enum OutputFormat { display_possible_values!(OutputFormat); +/// The target to compile for. +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)] +pub enum Target { + /// The target that is used for paged, fully laid-out content. + #[default] + Paged, + /// The target that is used for HTML export. + Html, +} + +display_possible_values!(Target); + /// Which format to use for diagnostics. #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)] pub enum DiagnosticFormat { diff --git a/crates/typst-cli/src/query.rs b/crates/typst-cli/src/query.rs index 7806e456f..adf57bd36 100644 --- a/crates/typst-cli/src/query.rs +++ b/crates/typst-cli/src/query.rs @@ -4,12 +4,13 @@ use serde::Serialize; use typst::diag::{bail, HintedStrResult, StrResult, Warned}; use typst::engine::Sink; use typst::foundations::{Content, IntoValue, LocatableSelector, Scope}; +use typst::html::HtmlDocument; use typst::layout::PagedDocument; use typst::syntax::Span; -use typst::World; +use typst::{Document, World}; use typst_eval::{eval_string, EvalMode}; -use crate::args::{QueryCommand, SerializationFormat}; +use crate::args::{QueryCommand, SerializationFormat, Target}; use crate::compile::print_diagnostics; use crate::set_failed; use crate::world::SystemWorld; @@ -22,12 +23,17 @@ pub fn query(command: &QueryCommand) -> HintedStrResult<()> { world.reset(); world.source(world.main()).map_err(|err| err.to_string())?; - let Warned { output, warnings } = typst::compile(&world); + let Warned { output, warnings } = match command.target { + Target::Paged => typst::compile::(&world) + .map(|output| output.map(|document| retrieve(&world, command, &document))), + Target::Html => typst::compile::(&world) + .map(|output| output.map(|document| retrieve(&world, command, &document))), + }; match output { // Retrieve and print query results. - Ok(document) => { - let data = retrieve(&world, command, &document)?; + Ok(data) => { + let data = data?; let serialized = format(data, command)?; println!("{serialized}"); print_diagnostics(&world, &[], &warnings, command.process.diagnostic_format) @@ -51,10 +57,10 @@ pub fn query(command: &QueryCommand) -> HintedStrResult<()> { } /// Retrieve the matches for the selector. -fn retrieve( +fn retrieve( world: &dyn World, command: &QueryCommand, - document: &PagedDocument, + document: &D, ) -> HintedStrResult> { let selector = eval_string( &typst::ROUTINES, @@ -77,7 +83,7 @@ fn retrieve( .cast::()?; Ok(document - .introspector + .introspector() .query(&selector.0) .into_iter() .collect::>()) diff --git a/crates/typst-library/src/diag.rs b/crates/typst-library/src/diag.rs index 49cbd02c6..aa247e18b 100644 --- a/crates/typst-library/src/diag.rs +++ b/crates/typst-library/src/diag.rs @@ -148,6 +148,13 @@ pub struct Warned { pub warnings: EcoVec, } +impl Warned { + /// Maps the output, keeping the same warnings. + pub fn map R>(self, f: F) -> Warned { + Warned { output: f(self.output), warnings: self.warnings } + } +} + /// An error or warning in a source file. /// /// The contained spans will only be detached if any of the input source files From 54809020bfba90004272588beaafdc75d239f334 Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Thu, 26 Jun 2025 20:04:46 +0100 Subject: [PATCH 2/5] Make `--target` optional --- crates/typst-cli/src/args.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index 2a861a504..97cfe67c7 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -152,7 +152,7 @@ pub struct QueryCommand { pub pretty: bool, /// The target to compile for. - #[clap(long)] + #[clap(long, default_value_t)] pub target: Target, /// World arguments. From 44bb2da4624b2d0114c049bb2bd2394e2ca0399e Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Thu, 26 Jun 2025 20:06:16 +0100 Subject: [PATCH 3/5] Improve target documentations --- crates/typst-cli/src/args.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index 97cfe67c7..85ef3adc3 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -452,10 +452,10 @@ display_possible_values!(OutputFormat); /// The target to compile for. #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)] pub enum Target { - /// The target that is used for paged, fully laid-out content. + /// PDF and image formats. #[default] Paged, - /// The target that is used for HTML export. + /// HTML. Html, } From 89650af4b2ed0e179d99af9a0f6ba711086e9797 Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Tue, 15 Jul 2025 22:46:52 +0100 Subject: [PATCH 4/5] Add documentation --- crates/typst-library/src/introspection/query.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/typst-library/src/introspection/query.rs b/crates/typst-library/src/introspection/query.rs index b742ac010..083c30f96 100644 --- a/crates/typst-library/src/introspection/query.rs +++ b/crates/typst-library/src/introspection/query.rs @@ -117,6 +117,8 @@ use crate::foundations::{func, Array, Context, LocatableSelector, Value}; /// ] /// ``` /// +/// ## Retrieving a specific field +/// /// Frequently, you're interested in only one specific field of the resulting /// elements. In the case of the `metadata` element, the `value` field is the /// interesting one. You can extract just this field with the `--field` @@ -134,6 +136,12 @@ use crate::foundations::{func, Array, Context, LocatableSelector, Value}; /// $ typst query example.typ "" --field value --one /// "This is a note" /// ``` +/// +/// ## Querying for a specific export target +/// +/// In case you need to query a document when exporting for a specific target, +/// you can use the `--target` argument. Valid values are `paged`, and `html` +/// (if the [`html`]($html) feature is enabled). #[func(contextual)] pub fn query( engine: &mut Engine, From 5a1f539b2db3f72a45033975003201a0b278ef0f Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Wed, 16 Jul 2025 02:45:56 +0100 Subject: [PATCH 5/5] Fix merge --- crates/typst-cli/src/query.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/typst-cli/src/query.rs b/crates/typst-cli/src/query.rs index c1b11add3..57d796719 100644 --- a/crates/typst-cli/src/query.rs +++ b/crates/typst-cli/src/query.rs @@ -4,11 +4,11 @@ use serde::Serialize; use typst::diag::{bail, HintedStrResult, StrResult, Warned}; use typst::engine::Sink; use typst::foundations::{Content, IntoValue, LocatableSelector, Scope}; -use typst::html::HtmlDocument; use typst::layout::PagedDocument; use typst::syntax::{Span, SyntaxMode}; use typst::{Document, World}; use typst_eval::eval_string; +use typst_html::HtmlDocument; use crate::args::{QueryCommand, SerializationFormat, Target}; use crate::compile::print_diagnostics;