Compare commits

...

9 Commits

Author SHA1 Message Date
Malo
dfe6e4ae47
Merge 5a1f539b2db3f72a45033975003201a0b278ef0f into 627f5b9d4f39f91affc529f276bb500d42887dae 2025-07-18 01:28:19 +08:00
Lachlan Kermode
627f5b9d4f
Add show rule for smallcaps in HTML (#6600) 2025-07-17 16:09:13 +00:00
Malo
5a1f539b2d Fix merge 2025-07-16 02:45:56 +01:00
Malo
fc098322f9
Merge branch 'main' into query-html-export 2025-07-16 01:45:41 +01:00
Malo
89650af4b2 Add documentation 2025-07-15 22:46:52 +01:00
Malo
44bb2da462 Improve target documentations 2025-06-26 20:06:16 +01:00
Malo
54809020bf Make --target optional 2025-06-26 20:04:46 +01:00
Malo
fee95dd0b0 Merge branch 'main' into query-html-export
# Conflicts:
#	crates/typst-cli/src/query.rs
#	crates/typst-library/src/diag.rs
2025-06-24 19:44:59 +01:00
Malo
8684daa17c Add --target argument for typst query 2025-06-07 19:33:12 +01:00
7 changed files with 73 additions and 11 deletions

View File

@ -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 {

View File

@ -6,10 +6,11 @@ use typst::engine::Sink;
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
use typst::layout::PagedDocument;
use typst::syntax::{Span, SyntaxMode};
use typst::World;
use typst::{Document, World};
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 +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<_>>())

View File

@ -14,8 +14,8 @@ use typst_library::model::{
RefElem, StrongElem, TableCell, TableElem, TermsElem,
};
use typst_library::text::{
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SpaceElem, StrikeElem,
SubElem, SuperElem, UnderlineElem,
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SmallcapsElem,
SpaceElem, StrikeElem, SubElem, SuperElem, UnderlineElem,
};
use typst_library::visualize::ImageElem;
@ -47,6 +47,7 @@ pub fn register(rules: &mut NativeRuleMap) {
rules.register(Html, OVERLINE_RULE);
rules.register(Html, STRIKE_RULE);
rules.register(Html, HIGHLIGHT_RULE);
rules.register(Html, SMALLCAPS_RULE);
rules.register(Html, RAW_RULE);
rules.register(Html, RAW_LINE_RULE);
@ -390,6 +391,20 @@ const STRIKE_RULE: ShowFn<StrikeElem> =
const HIGHLIGHT_RULE: ShowFn<HighlightElem> =
|elem, _, _| Ok(HtmlElem::new(tag::mark).with_body(Some(elem.body.clone())).pack());
const SMALLCAPS_RULE: ShowFn<SmallcapsElem> = |elem, _, styles| {
Ok(HtmlElem::new(tag::span)
.with_attr(
attr::style,
if elem.all.get(styles) {
"font-variant-caps: all-small-caps"
} else {
"font-variant-caps: small-caps"
},
)
.with_body(Some(elem.body.clone()))
.pack())
};
const RAW_RULE: ShowFn<RawElem> = |elem, _, styles| {
let lines = elem.lines.as_deref().unwrap_or_default();

View File

@ -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

View File

@ -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 "<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,

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p><span style="font-variant-caps: small-caps">Test 012</span><br><span style="font-variant-caps: all-small-caps">Test 012</span></p>
</body>
</html>

View File

@ -11,6 +11,6 @@
#show smallcaps: set text(fill: red)
#smallcaps[Smallcaps]
--- smallcaps-all ---
--- smallcaps-all render html ---
#smallcaps(all: false)[Test 012] \
#smallcaps(all: true)[Test 012]