mirror of
https://github.com/typst/typst
synced 2025-07-28 14:57:54 +08:00
Add --target
argument for typst query
This commit is contained in:
parent
494e6a6422
commit
8684daa17c
@ -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 {
|
||||
|
@ -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::<PagedDocument>(&world)
|
||||
.map(|output| output.map(|document| retrieve(&world, command, &document))),
|
||||
Target::Html => typst::compile::<HtmlDocument>(&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<D: Document>(
|
||||
world: &dyn World,
|
||||
command: &QueryCommand,
|
||||
document: &PagedDocument,
|
||||
document: &D,
|
||||
) -> HintedStrResult<Vec<Content>> {
|
||||
let selector = eval_string(
|
||||
&typst::ROUTINES,
|
||||
@ -77,7 +83,7 @@ fn retrieve(
|
||||
.cast::<LocatableSelector>()?;
|
||||
|
||||
Ok(document
|
||||
.introspector
|
||||
.introspector()
|
||||
.query(&selector.0)
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>())
|
||||
|
@ -148,6 +148,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 file.
|
||||
///
|
||||
/// The contained spans will only be detached if any of the input source files
|
||||
|
Loading…
x
Reference in New Issue
Block a user