Skip to main content

laterite_web/theme/
markup.rs

1//! Markup a module adds to every theme engine: functions, filters, tests and
2//! globals installed into the environment before the templates compile.
3//!
4//! A module implements [`Markup`] and registers a [`MarkupReg`]; the site and
5//! the static build install every registration when they build an engine, in
6//! registration order.
7//!
8//! ```no_run
9//! use laterite_web::theme::markup::{Markup, MarkupReg};
10//! use minijinja::Environment;
11//! use std::sync::Arc;
12//!
13//! struct Money;
14//!
15//! impl Markup for Money {
16//!     fn install(&self, env: &mut Environment<'static>) {
17//!         env.add_filter("money", |minor: i64| format!("{}.{:02}", minor / 100, minor % 100));
18//!     }
19//! }
20//!
21//! # fn register(registry: &mut laterite_core::Registry) {
22//! registry.add(MarkupReg::new("acme.money", Arc::new(Money)));
23//! # }
24//! ```
25
26use minijinja::Environment;
27use std::sync::Arc;
28
29/// What a module installs into a theme engine.
30pub trait Markup: Send + Sync + 'static {
31    /// Adds the module's functions, filters, tests and globals to `env`.
32    fn install(&self, env: &mut Environment<'static>);
33}
34
35/// A registered markup extension: a key naming it, and the extension.
36#[non_exhaustive]
37pub struct MarkupReg {
38    /// A `vendor.name` key, for the boot log and a clash message.
39    pub key: String,
40    pub markup: Arc<dyn Markup>,
41}
42
43impl MarkupReg {
44    pub fn new(key: impl Into<String>, markup: Arc<dyn Markup>) -> Self {
45        Self {
46            key: key.into(),
47            markup,
48        }
49    }
50}
51
52/// Installs every registration into `env`, in order.
53pub fn install_all(regs: &[Arc<dyn Markup>], env: &mut Environment<'static>) {
54    for markup in regs {
55        markup.install(env);
56    }
57}