use std::collections::HashMap;
use once_cell::sync::Lazy;
use regex::Regex;
use super::blade;
use super::ViewError;
#[derive(Debug, Clone)]
pub struct Slot {
pub name: String,
pub content: String,
}
#[derive(Debug, Clone)]
pub struct ComponentBlock {
pub template: String,
pub attributes: HashMap<String, String>,
pub body: String,
}
pub trait Component: Send + Sync {
fn view(&self) -> &str;
fn data(&self) -> HashMap<String, serde_json::Value> {
HashMap::new()
}
}
static MODERN_SLOT: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?s)<x-slot:([a-zA-Z0-9_-]+)\s*>(.*?)</x-slot\s*>").unwrap());
static CLASSIC_SLOT: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(?s)@slot\s*\(\s*'([^']+)'\s*\)(.*?)@endslot"#).unwrap());
pub fn extract_slots(input: &str) -> (String, Vec<Slot>) {
let mut slots = Vec::new();
let body = MODERN_SLOT
.replace_all(input, |caps: ®ex::Captures| {
slots.push(Slot {
name: caps[1].to_string(),
content: caps[2].to_string(),
});
String::new()
})
.to_string();
let body = CLASSIC_SLOT
.replace_all(&body, |caps: ®ex::Captures| {
slots.push(Slot {
name: caps[1].to_string(),
content: caps[2].to_string(),
});
String::new()
})
.to_string();
(body, slots)
}
static COMPONENT_BLOCK: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?s)@component\s*\(\s*'([^']+)'\s*(?:,\s*\{([^}]*)\}\s*)?\)(.*?)@endcomponent")
.unwrap()
});
pub fn split_components(input: &str) -> (String, Vec<ComponentBlock>) {
let mut blocks = Vec::new();
let mut index = 0usize;
let page = COMPONENT_BLOCK
.replace_all(input, |caps: ®ex::Captures| {
let template = caps[1].to_string();
let attributes = parse_attributes(caps.get(2).map(|m| m.as_str()).unwrap_or(""));
let body = caps[3].to_string();
let token = format!("__LARASTVEL_COMPONENT_{}__", index);
index += 1;
blocks.push(ComponentBlock {
template,
attributes,
body,
});
format!("{{{{ {} }}}}", token)
})
.to_string();
(page, blocks)
}
fn parse_attributes(input: &str) -> HashMap<String, String> {
let mut attributes = HashMap::new();
static ATTR: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"([a-zA-Z_][a-zA-Z0-9_-]*)\s*:\s*('([^']*)'|"([^"]*)"|([^,}\s]+))"#).unwrap()
});
for caps in ATTR.captures_iter(input) {
let key = caps[1].to_string();
let value = caps
.get(3)
.or_else(|| caps.get(4))
.or_else(|| caps.get(5))
.map(|m| m.as_str())
.unwrap_or("")
.to_string();
attributes.insert(key, value);
}
attributes
}
pub(crate) fn render_component_block(
tera: &mut tera::Tera,
views_root: &std::path::Path,
context: &tera::Context,
block: &ComponentBlock,
) -> Result<String, ViewError> {
let (slot_body, slots) = extract_slots(&block.body);
let mut component_context = context.clone();
for (key, value) in &block.attributes {
component_context.insert(key, value);
}
for slot in slots {
let html = render_fragment(tera, context, &slot.content)?;
component_context.insert(&slot.name, &html);
}
let default_slot = render_fragment(tera, context, &slot_body)?;
component_context.insert("slot", &default_slot);
let template_path = views_root.join(&block.template);
let raw = std::fs::read_to_string(&template_path).map_err(|e| ViewError::Render {
template: block.template.clone(),
source: Box::new(e),
})?;
tera.render_str(&blade::compile(&raw), &component_context)
.map_err(|e| ViewError::Render {
template: block.template.clone(),
source: Box::new(e),
})
}
fn render_fragment(
tera: &mut tera::Tera,
context: &tera::Context,
input: &str,
) -> Result<String, ViewError> {
let compiled = blade::compile(input);
tera.render_str(&compiled, context)
.map_err(|e| ViewError::Render {
template: "<fragment>".to_string(),
source: Box::new(e),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_modern_slots() {
let (body, slots) =
extract_slots("<x-slot:header>Welcome</x-slot><x-slot:footer>Bye</x-slot>Body");
assert_eq!(body, "Body");
assert_eq!(slots.len(), 2);
assert_eq!(slots[0].name, "header");
assert_eq!(slots[0].content, "Welcome");
assert_eq!(slots[1].name, "footer");
assert_eq!(slots[1].content, "Bye");
}
#[test]
fn extracts_classic_slots() {
let (body, slots) = extract_slots("@slot('title') <b>Hi</b> @endslot Body");
assert_eq!(body.trim(), "Body");
assert_eq!(slots.len(), 1);
assert_eq!(slots[0].name, "title");
assert_eq!(slots[0].content.trim(), "<b>Hi</b>");
}
#[test]
fn extracts_slots_with_whitespace_tags() {
let (body, slots) = extract_slots("<x-slot:header >Hello</x-slot >Body");
assert_eq!(body, "Body");
assert_eq!(slots[0].name, "header");
assert_eq!(slots[0].content, "Hello");
}
#[test]
fn no_slots_passthrough() {
let input = "<div>Plain body</div>";
let (body, slots) = extract_slots(input);
assert_eq!(body, input);
assert!(slots.is_empty());
}
#[test]
fn splits_component_blocks_with_placeholders() {
let (page, blocks) = split_components(
"Before @component('components/card.html', { title: \"Hi\" })Body@endcomponent After",
);
assert!(page.contains("Before"));
assert!(page.contains("After"));
assert!(page.contains("{{ __LARASTVEL_COMPONENT_0__ }}"));
assert!(!page.contains("@component"));
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].template, "components/card.html");
assert_eq!(blocks[0].attributes.get("title").unwrap(), "Hi");
assert_eq!(blocks[0].body, "Body");
}
#[test]
fn splits_component_blocks_without_attributes() {
let (page, blocks) =
split_components("@component('components/alert.html')Oops@endcomponent");
assert!(page.contains("{{ __LARASTVEL_COMPONENT_0__ }}"));
assert_eq!(blocks.len(), 1);
assert!(blocks[0].attributes.is_empty());
assert_eq!(blocks[0].body, "Oops");
}
#[test]
fn parses_attributes() {
let attrs = parse_attributes("type: \"danger\", count: 42, active: true");
assert_eq!(attrs.get("type").unwrap(), "danger");
assert_eq!(attrs.get("count").unwrap(), "42");
assert_eq!(attrs.get("active").unwrap(), "true");
assert_eq!(attrs.len(), 3);
}
#[test]
fn renders_component_block_pipeline() {
let dir = std::env::temp_dir().join(format!("larastvel_view_test_{}", uuid_v4()));
let views = dir.join("resources/views");
std::fs::create_dir_all(views.join("components")).unwrap();
std::fs::write(
views.join("components/card.html"),
"<section><h1>{{ title }}</h1><aside>{{ header }}</aside><p>{{ slot }}</p></section>",
)
.unwrap();
let mut tera = tera::Tera::parse(views.join("**/*.html").to_str().unwrap()).unwrap();
let mut context = tera::Context::new();
context.insert("user", "Ada");
let page = "@component('components/card.html', { title: \"Stats\" })\
<x-slot:header>Hi {{ user }}</x-slot>Body text@endcomponent";
let (page, blocks) = split_components(page);
let mut rendered = page.clone();
for (i, block) in blocks.iter().enumerate() {
let html = render_component_block(&mut tera, &views, &context, block).unwrap();
let marker = format!("{{{{ __LARASTVEL_COMPONENT_{}__ }}}}", i);
rendered = rendered.replace(&marker, &html);
}
rendered = tera.render_str(&rendered, &context).unwrap();
assert!(rendered.contains("<h1>Stats</h1>"), "{rendered}");
assert!(rendered.contains("<aside>Hi Ada</aside>"), "{rendered}");
assert!(rendered.contains("<p>Body text</p>"), "{rendered}");
let _ = std::fs::remove_dir_all(&dir);
}
fn uuid_v4() -> String {
uuid::Uuid::new_v4().to_string()
}
}