Add `--target` argument for `typst query` (#6405)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
492bd27885
commit
a7ad9d426a
|
|
@ -155,6 +155,10 @@ pub struct QueryCommand {
|
|||
#[clap(long)]
|
||||
pub pretty: bool,
|
||||
|
||||
/// The target to compile for.
|
||||
#[clap(long, default_value_t)]
|
||||
pub target: Target,
|
||||
|
||||
/// World arguments.
|
||||
#[clap(flatten)]
|
||||
pub world: WorldArgs,
|
||||
|
|
@ -457,6 +461,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 {
|
||||
/// PDF and image formats.
|
||||
#[default]
|
||||
Paged,
|
||||
/// HTML.
|
||||
Html,
|
||||
}
|
||||
|
||||
display_possible_values!(Target);
|
||||
|
||||
/// Which format to use for diagnostics.
|
||||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
|
||||
pub enum DiagnosticFormat {
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ use typst::World;
|
|||
use typst::diag::{HintedStrResult, StrResult, Warned, bail};
|
||||
use typst::engine::Sink;
|
||||
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
|
||||
use typst::introspection::Introspector;
|
||||
use typst::layout::PagedDocument;
|
||||
use typst::syntax::{Span, SyntaxMode};
|
||||
use typst_eval::eval_string;
|
||||
use typst_html::HtmlDocument;
|
||||
|
||||
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 +24,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::<PagedDocument>(&world)
|
||||
.map(|output| output.map(|document| document.introspector)),
|
||||
Target::Html => typst::compile::<HtmlDocument>(&world)
|
||||
.map(|output| output.map(|document| document.introspector)),
|
||||
};
|
||||
|
||||
match output {
|
||||
// Retrieve and print query results.
|
||||
Ok(document) => {
|
||||
let data = retrieve(&world, command, &document)?;
|
||||
Ok(introspector) => {
|
||||
let data = retrieve(&world, command, &introspector)?;
|
||||
let serialized = format(data, command)?;
|
||||
println!("{serialized}");
|
||||
print_diagnostics(&world, &[], &warnings, command.process.diagnostic_format)
|
||||
|
|
@ -54,7 +61,7 @@ pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
|
|||
fn retrieve(
|
||||
world: &dyn World,
|
||||
command: &QueryCommand,
|
||||
document: &PagedDocument,
|
||||
introspector: &Introspector,
|
||||
) -> HintedStrResult<Vec<Content>> {
|
||||
let selector = eval_string(
|
||||
&typst::ROUTINES,
|
||||
|
|
@ -76,11 +83,7 @@ fn retrieve(
|
|||
})?
|
||||
.cast::<LocatableSelector>()?;
|
||||
|
||||
Ok(document
|
||||
.introspector
|
||||
.query(&selector.0)
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>())
|
||||
Ok(introspector.query(&selector.0).into_iter().collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
/// Format the query result in the output format.
|
||||
|
|
|
|||
|
|
@ -151,6 +151,13 @@ pub struct Warned<T> {
|
|||
pub warnings: EcoVec<SourceDiagnostic>,
|
||||
}
|
||||
|
||||
impl<T> Warned<T> {
|
||||
/// Maps the output, keeping the same warnings.
|
||||
pub fn map<R, F: FnOnce(T) -> R>(self, f: F) -> Warned<R> {
|
||||
Warned { output: f(self.output), warnings: self.warnings }
|
||||
}
|
||||
}
|
||||
|
||||
/// An error or warning in a source or text file.
|
||||
///
|
||||
/// The contained spans will only be detached if any of the input source files
|
||||
|
|
|
|||
|
|
@ -117,6 +117,8 @@ use crate::foundations::{Array, Context, LocatableSelector, Value, func};
|
|||
/// ]
|
||||
/// ```
|
||||
///
|
||||
/// ## 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::{Array, Context, LocatableSelector, Value, func};
|
|||
/// $ typst query example.typ "<note>" --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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue