use crate::ast::{Node, PathSegment, Variable};
use crate::ext::bundle::Bundle;
use crate::next::index::merge_config;
use crate::next::schema::value::{self, Value, strict_equals};
use crate::next::schema::{Config, Schema, SchemaAttribute, TransformFn, Variables};
use crate::next::tags::conditional::truthy;
use crate::next::transformer::{ResolvedNode, flat_push, resolve, resolve_node, resolve_variable};
use crate::types::{JsObject, MarkdocError, NodeType, Result, Scalar};
use indexmap::IndexMap;
use std::sync::Arc;
pub const MAX_LOOP_NESTING: usize = 200;
fn error(message: impl Into<String>) -> MarkdocError {
MarkdocError::TransformError(message.into())
}
fn is_tag(node: &Node, name: &str) -> bool {
node.node_type == NodeType::Tag && node.tag.as_deref() == Some(name)
}
fn attribute(name: &str, attribute: SchemaAttribute) -> (String, SchemaAttribute) {
(
name.to_string(),
SchemaAttribute {
render: Some(Value::Boolean(false)),
..attribute
},
)
}
pub fn switch() -> Schema {
let transform: TransformFn = Arc::new(transform_switch);
Schema {
attributes: Some(IndexMap::from([attribute(
"primary",
SchemaAttribute {
required: true,
..SchemaAttribute::default()
},
)])),
transform: Some(transform),
description: Some("Select one of several bodies by matching a value".into()),
..Schema::default()
}
}
pub fn case() -> Schema {
Schema {
attributes: Some(IndexMap::from([attribute(
"primary",
SchemaAttribute::default(),
)])),
description: Some("One branch of a `{% switch %}`; no value makes it the default".into()),
..Schema::default()
}
}
pub fn for_loop() -> Schema {
let transform: TransformFn = Arc::new(transform_for);
let string = || SchemaAttribute {
attr_type: Some(crate::next::schema::TypeSpec::Builtin(
crate::next::schema::BuiltinType::String,
)),
..SchemaAttribute::default()
};
Schema {
attributes: Some(IndexMap::from([
attribute(
"primary",
SchemaAttribute {
required: true,
..SchemaAttribute::default()
},
),
attribute("as", string()),
attribute("index", string()),
])),
transform: Some(transform),
description: Some("Repeat the body once per element of an array".into()),
..Schema::default()
}
}
pub fn schemas() -> IndexMap<String, Option<Schema>> {
IndexMap::from([
("switch".to_string(), Some(switch())),
("case".to_string(), Some(case())),
("for".to_string(), Some(for_loop())),
])
}
pub const LAYER: &str = "control_flow";
pub fn apply_bundle(bundle: &Bundle, config: &mut Config) {
for (name, schema) in bundle.layer_schemas(LAYER) {
if !matches!(config.tags.get(&name), Some(None)) {
config.tags.insert(name, Some(schema));
}
}
apply(config);
}
pub fn apply(config: &mut Config) {
for (name, schema) in schemas() {
let schema = schema.expect("schemas() has only schemas");
match config.tags.get_mut(&name) {
Some(Some(existing)) => {
existing.transform = schema.transform;
let attributes = existing.attributes.get_or_insert_with(IndexMap::new);
for (key, attribute) in schema.attributes.into_iter().flatten() {
attributes.entry(key).or_insert(attribute);
}
}
Some(None) => {}
None => {
config.tags.insert(name, Some(schema));
}
}
}
}
fn cases_of<'n, 'a>(switch: &'n ResolvedNode<'a>) -> Vec<&'n ResolvedNode<'a>> {
let mut found = Vec::new();
let mut stack: Vec<&ResolvedNode<'_>> = switch.children.iter().rev().collect();
while let Some(node) = stack.pop() {
if is_tag(node.node, "case") {
found.push(node);
} else if node.node.node_type != NodeType::Tag {
stack.extend(node.children.iter().rev());
}
}
found
}
fn transform_switch(node: &ResolvedNode<'_>, config: &Config) -> Result<Value> {
let subject = node.attribute("primary")?;
let mut chosen = None;
for case in cases_of(node) {
let matched = match &case.attributes {
Value::Object(o) if !o.contains_key("primary") && o.proto().is_object_prototype() => {
true
}
_ => {
let value = case.attribute("primary")?;
value.is_undefined() || strict_equals(&subject, &value)
}
};
if matched {
chosen = Some(case);
break;
}
}
let mut out = Vec::new();
for child in chosen.map(|case| &case.children[..]).unwrap_or_default() {
flat_push(&mut out, child.transform(config)?);
}
Ok(Value::array(out))
}
fn kind_of(value: &Value) -> &'static str {
match value {
Value::Undefined => "undefined",
Value::Null => "null",
Value::Boolean(_) => "a boolean",
Value::Number(_) => "a number",
Value::String(_) => "a string",
Value::Array(_) => "an array",
Value::Object(_) => "an object",
Value::Tag(_) => "a tag",
Value::Builtin(_) => "a built-in",
}
}
fn binding_name(node: &Node, key: &str, fallback: &str) -> Result<String> {
match node.attributes.get(key) {
None => Ok(fallback.to_string()),
Some(Scalar::String(s)) if !s.is_empty() => Ok(s.clone()),
Some(Scalar::String(_)) => Err(error(format!(
"a `for` tag's `{key}` is empty, and a variable needs a name."
))),
Some(Scalar::Variable(_) | Scalar::Function(_)) => Err(error(format!(
"a `for` tag's `{key}` is the *name* of the variable the loop binds, so it must \
be a literal string, not an expression, which would make the name depend on the \
data it is naming."
))),
Some(other) => Err(error(format!(
"a `for` tag's `{key}` must be a string naming a variable, not {}.",
kind_of(&Value::from_scalar(other))
))),
}
}
fn elements(subject: &Value) -> Result<Vec<Value>> {
match subject {
Value::Undefined | Value::Null => Ok(Vec::new()),
Value::Array(items) => Ok((0..items.len())
.map(|i| items.get(i).cloned().unwrap_or(Value::Undefined))
.collect()),
other => Err(error(format!(
"a `for` tag needs an array to iterate and was given {}. An unset variable is \
undefined and renders nothing; this is a value of the wrong kind.",
kind_of(other)
))),
}
}
fn scoped(config: &Config, names: &[String; 2], item: Value, index: usize) -> Config {
let bound = [item, Value::Number(index as f64)];
let variables = match &config.variables {
Some(Variables::Fn(outer)) => {
let (outer, names, bound) = (outer.clone(), names.clone(), bound.clone());
Variables::Fn(Arc::new(move |path: &[PathSegment]| {
let own = match path.first() {
Some(PathSegment::String(head)) => names.iter().rposition(|n| n == head),
_ => None,
};
let Some(at) = own else {
return outer(path);
};
let mut value = bound[at].clone();
for step in &path[1..] {
if value.is_undefined() {
value = Value::empty_object();
}
value = value::get_property(&value, &value::property_key(step))
.unwrap_or(Value::Undefined);
}
value
}))
}
other => {
let mut object = JsObject::new();
if let Some(Variables::Value(outer)) = other {
object.extend(value::own_entries(outer));
}
for (name, value) in names.iter().zip(bound) {
object.insert(name.clone(), value);
}
Variables::Value(Value::object(object))
}
};
Config {
variables: Some(variables),
..config.clone()
}
}
fn joins_lists(body: &[Node]) -> bool {
match (body.first(), body.last()) {
(Some(first), Some(last)) => {
first.node_type == NodeType::List
&& last.node_type == NodeType::List
&& first.attributes.get("ordered") == last.attributes.get("ordered")
}
_ => false,
}
}
fn transform_for(node: &ResolvedNode<'_>, config: &Config) -> Result<Value> {
let names = [
binding_name(node.node, "as", "item")?,
binding_name(node.node, "index", "index")?,
];
let items = elements(&node.attribute("primary")?)?;
let body = &node.node.children;
let joins = joins_lists(body);
let mut out: Vec<Value> = Vec::new();
for (index, item) in items.into_iter().enumerate() {
let config = scoped(config, &names, item, index);
let mut iteration = Vec::new();
for child in body {
let resolved = resolve_node(child, &config)?;
flat_push(&mut iteration, resolved.transform(&config)?);
}
let mut iteration = iteration.into_iter();
let Some(first) = iteration.next() else {
continue;
};
match (out.last_mut(), first) {
(Some(Value::Tag(previous)), Value::Tag(next))
if joins && strict_equals(&previous.name, &next.name) =>
{
Arc::make_mut(previous)
.children
.extend(next.children.iter().cloned());
}
(_, first) => out.push(first),
}
out.extend(iteration);
}
Ok(Value::array(out))
}
pub fn evaluate_conditionals(node: &Node, config: &Config) -> Result<Node> {
let config = merge_config(config);
let mut top = splice(vec![node.clone()], &config, 0)?;
let mut root = if top.len() == 1 {
top.pop().expect("one node")
} else {
Node::new(NodeType::Document, std::iter::empty(), top, None)
};
let mut stack: Vec<&mut Node> = vec![&mut root];
while let Some(node) = stack.pop() {
let children = std::mem::take(&mut node.children);
node.children = splice(children, &config, 0)?;
let Node {
children, slots, ..
} = node;
stack.extend(slots.values_mut());
stack.extend(children.iter_mut());
}
Ok(root)
}
fn splice(nodes: Vec<Node>, config: &Config, loops: usize) -> Result<Vec<Node>> {
let mut out = Vec::with_capacity(nodes.len());
let mut queue = nodes;
queue.reverse();
while let Some(node) = queue.pop() {
if is_tag(&node, "if") {
queue.extend(chosen_branch(&node, config)?.into_iter().rev());
} else if is_tag(&node, "switch") {
queue.extend(chosen_case(&node, config)?.into_iter().rev());
} else if is_tag(&node, "for") {
if loops >= MAX_LOOP_NESTING {
return Err(error(format!(
"`for` loops nested more than {MAX_LOOP_NESTING} deep"
)));
}
let joins = joins_lists(&node.children);
let mut iterations = Vec::new();
for iteration in iterations_of(&node, config)? {
append_iteration(
&mut iterations,
splice(iteration, config, loops + 1)?,
joins,
);
}
out.extend(iterations);
} else {
out.push(node);
}
}
Ok(out)
}
fn primary(node: &Node, config: &Config) -> Result<Value> {
match node.attributes.get("primary") {
Some(value) => resolve(value, config),
None => Ok(Value::Undefined),
}
}
fn chosen_branch(node: &Node, config: &Config) -> Result<Vec<Node>> {
let mut taken = truthy(&primary(node, config)?);
let mut out = Vec::new();
for child in &node.children {
if is_tag(child, "else") {
if taken {
break;
}
taken = match child.attributes.get("primary") {
Some(value) => truthy(&resolve(value, config)?),
None => true,
};
} else if taken {
out.push(child.clone());
}
}
Ok(out)
}
fn chosen_case(node: &Node, config: &Config) -> Result<Vec<Node>> {
let subject = primary(node, config)?;
let mut stack: Vec<&Node> = node.children.iter().rev().collect();
while let Some(child) = stack.pop() {
if is_tag(child, "case") {
let matched = match child.attributes.get("primary") {
None => true,
Some(value) => strict_equals(&subject, &resolve(value, config)?),
};
if matched {
return Ok(child.children.clone());
}
} else if child.node_type != NodeType::Tag {
stack.extend(child.children.iter().rev());
}
}
Ok(Vec::new())
}
fn iterations_of(node: &Node, config: &Config) -> Result<Vec<Vec<Node>>> {
let names = [
binding_name(node, "as", "item")?,
binding_name(node, "index", "index")?,
];
let items = elements(&primary(node, config)?)?;
let mut out = Vec::with_capacity(items.len());
for (index, item) in items.into_iter().enumerate() {
let bindings = scoped(&Config::default(), &names, item, index);
let mut body = node.children.clone();
bind(&mut body, &names, &bindings)?;
out.push(body);
}
Ok(out)
}
fn bind(body: &mut [Node], names: &[String; 2], bindings: &Config) -> Result<()> {
let mut stack: Vec<(&mut Node, [bool; 2])> =
body.iter_mut().map(|node| (node, [true, true])).collect();
while let Some((node, active)) = stack.pop() {
for value in node.attributes.values_mut() {
substitute(value, names, active, bindings)?;
}
let mut inner = active;
if is_tag(node, "for") {
let own = [
binding_name(node, "as", "item")?,
binding_name(node, "index", "index")?,
];
for (k, name) in names.iter().enumerate() {
if own.contains(name) {
inner[k] = false;
}
}
}
if inner == [false, false] {
continue;
}
let Node {
children, slots, ..
} = node;
stack.extend(slots.values_mut().map(|slot| (slot, inner)));
stack.extend(children.iter_mut().map(|child| (child, inner)));
}
Ok(())
}
fn substitute(
value: &mut Scalar,
names: &[String; 2],
active: [bool; 2],
bindings: &Config,
) -> Result<()> {
match value {
Scalar::Variable(variable) if reads(variable, names, active) => {
*value = match resolve_variable(variable, bindings)? {
Value::Undefined => Scalar::Undefined,
resolved => resolved.to_scalar().map_err(error)?,
};
}
Scalar::Function(function) => {
for parameter in function.parameters.values_mut() {
substitute(parameter, names, active, bindings)?;
}
}
Scalar::Array(items) => {
for item in items {
substitute(item, names, active, bindings)?;
}
}
Scalar::Object(object) => {
for item in object.values_mut() {
substitute(item, names, active, bindings)?;
}
}
_ => {}
}
Ok(())
}
fn reads(variable: &Variable, names: &[String; 2], active: [bool; 2]) -> bool {
match variable.path.first() {
Some(PathSegment::String(head)) => names
.iter()
.zip(active)
.any(|(name, on)| on && name == head),
_ => false,
}
}
fn append_iteration(out: &mut Vec<Node>, iteration: Vec<Node>, joins: bool) {
let mut iteration = iteration.into_iter();
let Some(mut first) = iteration.next() else {
return;
};
match out.last_mut() {
Some(previous) if joins && continues_a_list(previous, &first) => {
previous.children.append(&mut first.children);
}
_ => out.push(first),
}
out.extend(iteration);
}
fn continues_a_list(previous: &Node, next: &Node) -> bool {
previous.node_type == NodeType::List
&& next.node_type == NodeType::List
&& previous.attributes.get("ordered") == next.attributes.get("ordered")
}
#[cfg(test)]
mod tests;