summaryrefslogtreecommitdiff
path: root/crates/typst-library/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-12-02 13:34:02 +0100
committerLaurenz <laurmaedje@gmail.com>2024-12-04 10:12:07 +0100
commitd00a5d6c9b35e4a340be8df18a11943e3591b86a (patch)
tree699ef153f8444989c9e27d037bdbdff4931065c3 /crates/typst-library/src
parent2b8dc9b14da365cb36345d41fd1ca93894f50c68 (diff)
Add contextual `target` function
Diffstat (limited to 'crates/typst-library/src')
-rw-r--r--crates/typst-library/src/foundations/mod.rs9
-rw-r--r--crates/typst-library/src/foundations/target.rs38
-rw-r--r--crates/typst-library/src/lib.rs6
3 files changed, 49 insertions, 4 deletions
diff --git a/crates/typst-library/src/foundations/mod.rs b/crates/typst-library/src/foundations/mod.rs
index 9259a7d1..28f98318 100644
--- a/crates/typst-library/src/foundations/mod.rs
+++ b/crates/typst-library/src/foundations/mod.rs
@@ -31,6 +31,8 @@ mod selector;
mod str;
mod styles;
mod symbol;
+#[path = "target.rs"]
+mod target_;
mod ty;
mod value;
mod version;
@@ -61,6 +63,7 @@ pub use self::selector::*;
pub use self::str::*;
pub use self::styles::*;
pub use self::symbol::*;
+pub use self::target_::*;
pub use self::ty::*;
pub use self::value::*;
pub use self::version::*;
@@ -79,6 +82,7 @@ use typst_syntax::Spanned;
use crate::diag::{bail, SourceResult, StrResult};
use crate::engine::Engine;
use crate::routines::EvalMode;
+use crate::{Feature, Features};
/// Foundational types and functions.
///
@@ -88,7 +92,7 @@ use crate::routines::EvalMode;
pub static FOUNDATIONS: Category;
/// Hook up all `foundations` definitions.
-pub(super) fn define(global: &mut Scope, inputs: Dict) {
+pub(super) fn define(global: &mut Scope, inputs: Dict, features: &Features) {
global.category(FOUNDATIONS);
global.define_type::<bool>();
global.define_type::<i64>();
@@ -116,6 +120,9 @@ pub(super) fn define(global: &mut Scope, inputs: Dict) {
global.define_func::<assert>();
global.define_func::<eval>();
global.define_func::<style>();
+ if features.is_enabled(Feature::Html) {
+ global.define_func::<target>();
+ }
global.define_module(calc::module());
global.define_module(sys::module(inputs));
}
diff --git a/crates/typst-library/src/foundations/target.rs b/crates/typst-library/src/foundations/target.rs
new file mode 100644
index 00000000..b743ea1a
--- /dev/null
+++ b/crates/typst-library/src/foundations/target.rs
@@ -0,0 +1,38 @@
+use comemo::Tracked;
+
+use crate::diag::HintedStrResult;
+use crate::foundations::{elem, func, Cast, Context};
+
+/// The compilation target.
+#[derive(Debug, Default, Copy, Clone, PartialEq, Hash, Cast)]
+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,
+}
+
+impl Target {
+ /// Whether this is the HTML target.
+ pub fn is_html(self) -> bool {
+ self == Self::Html
+ }
+}
+
+/// This element exists solely to host the `target` style chain field.
+/// It is never constructed and not visible to users.
+#[elem]
+pub struct TargetElem {
+ /// The compilation target.
+ pub target: Target,
+}
+
+/// Returns the current compilation target.
+#[func(contextual)]
+pub fn target(
+ /// The callsite context.
+ context: Tracked<Context>,
+) -> HintedStrResult<Target> {
+ Ok(TargetElem::target_in(context.styles()?))
+}
diff --git a/crates/typst-library/src/lib.rs b/crates/typst-library/src/lib.rs
index 5aec0916..a2bb61f3 100644
--- a/crates/typst-library/src/lib.rs
+++ b/crates/typst-library/src/lib.rs
@@ -193,7 +193,7 @@ impl LibraryBuilder {
pub fn build(self) -> Library {
let math = math::module();
let inputs = self.inputs.unwrap_or_default();
- let global = global(math.clone(), inputs);
+ let global = global(math.clone(), inputs, &self.features);
let std = Value::Module(global.clone());
Library {
global,
@@ -236,9 +236,9 @@ pub enum Feature {
}
/// Construct the module with global definitions.
-fn global(math: Module, inputs: Dict) -> Module {
+fn global(math: Module, inputs: Dict, features: &Features) -> Module {
let mut global = Scope::deduplicating();
- self::foundations::define(&mut global, inputs);
+ self::foundations::define(&mut global, inputs, features);
self::model::define(&mut global);
self::text::define(&mut global);
global.reset_category();