use crate::ast::Node;
use crate::next::schema::value::{Value, own_entries};
use crate::next::schema::{Config, Variables};
use crate::types::Scalar;
use indexmap::IndexMap;
#[derive(Clone, Default)]
pub struct Bindings {
values: IndexMap<String, Value>,
}
impl std::fmt::Debug for Bindings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map().entries(self.values.iter()).finish()
}
}
impl Bindings {
pub fn new() -> Bindings {
Bindings::default()
}
pub fn with_variable(self, name: impl Into<String>, value: &Scalar) -> Bindings {
self.with_value(name, Value::from_scalar(value))
}
pub fn with_value(mut self, name: impl Into<String>, value: Value) -> Bindings {
self.values.insert(name.into(), value);
self
}
pub fn with_frontmatter(mut self, frontmatter: &Scalar) -> Bindings {
let mut markdoc = IndexMap::new();
if let Some(Ok(mut scalar)) = self.values.get("markdoc").map(Value::to_scalar)
&& let Scalar::Object(members) = &mut scalar
{
markdoc = std::mem::take(members);
}
markdoc.insert("frontmatter".to_string(), frontmatter.clone());
self.values.insert(
"markdoc".to_string(),
Value::from_scalar(&Scalar::Object(markdoc)),
);
self
}
pub fn with_config(self, config: &Scalar) -> Bindings {
self.with_variable("config", config)
}
pub fn with_globals(self, globals: &Scalar) -> Bindings {
self.with_variable("global", globals)
}
pub fn with_all<'a>(
mut self,
map: impl IntoIterator<Item = (&'a String, &'a Scalar)>,
) -> Bindings {
for (name, value) in map {
self = self.with_variable(name.clone(), value);
}
self
}
pub fn to_value(&self) -> Value {
Value::object(self.values.clone())
}
pub fn apply(&self, config: &mut Config) {
let mut merged: IndexMap<String, Value> = match &config.variables {
Some(Variables::Value(value @ Value::Object(_))) => {
own_entries(value).into_iter().collect()
}
_ => IndexMap::new(),
};
for (name, value) in &self.values {
merged.insert(name.clone(), value.clone());
}
config.variables = Some(Variables::Value(Value::object(merged)));
}
}
pub fn frontmatter_variables(document: &Node) -> Bindings {
match document.attributes.get("frontmatter") {
Some(frontmatter) => Bindings::new().with_frontmatter(frontmatter),
None => Bindings::new(),
}
}
pub fn bind(config: &mut Config, name: impl Into<String>, value: Value) {
Bindings::new().with_value(name, value).apply(config);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ext::{Frontmatter, ParseOptions, parse};
use crate::next::index;
use crate::next::renderers::html;
const SOURCE: &str = "---\ntitle: Hello\nproducts: [Bot250]\n---\n\
# {% $markdoc.frontmatter.title %}\n\n\
{% $config.product %} / {% $global.site %} / {% $edition %}\n";
fn yaml() -> ParseOptions {
ParseOptions {
frontmatter: Frontmatter::Yaml,
..Default::default()
}
}
fn rendered(source: &str, config: &Config) -> String {
let doc = parse(source, &yaml()).unwrap();
html::render(&index::transform(&doc, config).unwrap()).unwrap()
}
fn object(pairs: &[(&str, &str)]) -> Scalar {
Scalar::Object(
pairs
.iter()
.map(|(k, v)| (k.to_string(), Scalar::String(v.to_string())))
.collect(),
)
}
#[test]
fn without_bindings_upstream_binds_nothing() {
assert_eq!(
rendered(SOURCE, &Config::default()),
"<article><h1></h1><p> / / </p></article>"
);
}
#[test]
fn bindings_render_as_the_old_context_did() {
let doc = parse(SOURCE, &yaml()).unwrap();
let mut config = Config::default();
frontmatter_variables(&doc)
.with_config(&object(&[("product", "Bot250")]))
.with_globals(&object(&[("site", "Support")]))
.with_variable("edition", &Scalar::String("2.0".into()))
.apply(&mut config);
let new = rendered(SOURCE, &config);
assert_eq!(
new,
"<article><h1>Hello</h1><p>Bot250 / Support / 2.0</p></article>"
);
}
#[test]
fn apply_merges_into_the_configs_object() {
let mut config = Config::default();
bind(&mut config, "a", Value::string("1"));
bind(&mut config, "b", Value::string("2"));
Bindings::new()
.with_variable("a", &Scalar::String("3".into()))
.apply(&mut config);
let Some(Variables::Value(v)) = &config.variables else {
panic!("an object")
};
assert_eq!(v.to_scalar().unwrap(), object(&[("a", "3"), ("b", "2")]));
let b = Bindings::new()
.with_variable("markdoc", &object(&[("other", "x")]))
.with_frontmatter(&Scalar::String("raw".into()));
assert_eq!(
b.to_value().to_scalar().unwrap(),
Scalar::Object(IndexMap::from([(
"markdoc".to_string(),
object(&[("other", "x"), ("frontmatter", "raw")])
)]))
);
}
#[test]
fn raw_frontmatter_binds_as_the_raw_text() {
let doc = parse(
"---\ntitle: x\n---\n{% $markdoc.frontmatter %}\n",
&ParseOptions::default(),
)
.unwrap();
let mut config = Config::default();
frontmatter_variables(&doc).apply(&mut config);
assert_eq!(
html::render(&index::transform(&doc, &config).unwrap()).unwrap(),
"<article><p>title: x</p></article>"
);
let none = parse("# No frontmatter\n", &ParseOptions::default()).unwrap();
assert!(
frontmatter_variables(&none).to_value().to_scalar().unwrap()
== Scalar::Object(IndexMap::new())
);
}
}