use once_cell::sync::Lazy;
use regex::Regex;
fn blade_expr_to_tera(expr: &str) -> String {
let s = expr.trim().replace("$", "").replace("->", ".");
if s.starts_with('!') && !s[1..].trim().starts_with('=') {
format!("not {}", s[1..].trim())
} else {
s
}
}
fn foreach_to_tera(args: &str) -> String {
let args = args.trim();
if let Some(pos) = args.find(" as ") {
let collection = blade_expr_to_tera(&args[..pos]);
let rest = args[pos + 4..].trim();
if let Some(arrow) = rest.find("=>") {
let key = blade_expr_to_tera(&rest[..arrow]);
let value = blade_expr_to_tera(&rest[arrow + 2..]);
format!("{{% for {key}, {value} in {collection} %}}")
} else {
let value = blade_expr_to_tera(rest);
format!("{{% for {value} in {collection} %}}")
}
} else {
format!("{{% for _item in {} %}}", blade_expr_to_tera(args))
}
}
pub fn compile(input: &str) -> String {
let mut output = input.to_string();
static ELSEIF: Lazy<Regex> = Lazy::new(|| Regex::new(r"@elseif\s*\(([^)]*)\)").unwrap());
output = ELSEIF
.replace_all(&output, |caps: ®ex::Captures| {
format!("{{% elif {} %}}", blade_expr_to_tera(&caps[1]))
})
.to_string();
static IF_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"@if\s*\(([^)]*)\)").unwrap());
output = IF_START
.replace_all(&output, |caps: ®ex::Captures| {
format!("{{% if {} %}}", blade_expr_to_tera(&caps[1]))
})
.to_string();
static ENDIF: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endif\s*$").unwrap());
output = ENDIF.replace_all(&output, "{% endif %}").to_string();
static UNLESS_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"@unless\s*\(([^)]*)\)").unwrap());
output = UNLESS_START
.replace_all(&output, |caps: ®ex::Captures| {
format!("{{% if not ({}) %}}", blade_expr_to_tera(&caps[1]))
})
.to_string();
static ENDUNLESS: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endunless\s*$").unwrap());
output = ENDUNLESS.replace_all(&output, "{% endif %}").to_string();
static ISS_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"@isset\s*\(([^)]*)\)").unwrap());
output = ISS_START
.replace_all(&output, |caps: ®ex::Captures| {
format!("{{% if {} is defined %}}", blade_expr_to_tera(&caps[1]))
})
.to_string();
static ENDISS: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endisset\s*$").unwrap());
output = ENDISS.replace_all(&output, "{% endif %}").to_string();
static EMPTY_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"@empty\s*\(([^)]*)\)").unwrap());
output = EMPTY_START
.replace_all(&output, |caps: ®ex::Captures| {
format!("{{% if {} is empty %}}", blade_expr_to_tera(&caps[1]))
})
.to_string();
static ENDEMPTY: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endempty\s*$").unwrap());
output = ENDEMPTY.replace_all(&output, "{% endif %}").to_string();
static FOREACH: Lazy<Regex> = Lazy::new(|| Regex::new(r"@foreach\s*\((.+?)\)").unwrap());
output = FOREACH
.replace_all(&output, |caps: ®ex::Captures| foreach_to_tera(&caps[1]))
.to_string();
static ENDFOREACH: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endforeach\s*$").unwrap());
output = ENDFOREACH.replace_all(&output, "{% endfor %}").to_string();
static INCLUDE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"@include\s*\(\s*['"]([^'"]+)['"]\s*\)"#).unwrap());
output = INCLUDE
.replace_all(&output, |caps: ®ex::Captures| {
format!(r#"{{% include "{path}" %}}"#, path = &caps[1])
})
.to_string();
static AUTH_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@auth\s*$").unwrap());
output = AUTH_START
.replace_all(&output, "{% if auth_check %}")
.to_string();
static ENDAUTH: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endauth\s*$").unwrap());
output = ENDAUTH.replace_all(&output, "{% endif %}").to_string();
static GUEST_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@guest\s*$").unwrap());
output = GUEST_START
.replace_all(&output, "{% if not auth_check %}")
.to_string();
static ENDGUEST: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endguest\s*$").unwrap());
output = ENDGUEST.replace_all(&output, "{% endif %}").to_string();
static ERROR_START: Lazy<Regex> =
Lazy::new(|| Regex::new(r"@error\s*\(\s*'([^']+)'\s*\)").unwrap());
output = ERROR_START
.replace_all(&output, |caps: ®ex::Captures| {
let field = &caps[1];
format!(r#"{{% if errors["{field}"] %}}"#)
})
.to_string();
static ENDERROR: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@enderror\s*$").unwrap());
output = ENDERROR.replace_all(&output, "{% endif %}").to_string();
static ELSE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@else\s*$").unwrap());
output = ELSE.replace_all(&output, "{% else %}").to_string();
static CSRF: Lazy<Regex> = Lazy::new(|| Regex::new(r"@csrf\b").unwrap());
output = CSRF
.replace_all(
&output,
r#"<input type="hidden" name="_token" value="{{ csrf_token }}">"#,
)
.to_string();
static METHOD: Lazy<Regex> =
Lazy::new(|| Regex::new(r"@method\s*\(\s*'([^']+)'\s*\)").unwrap());
output = METHOD
.replace_all(&output, |caps: ®ex::Captures| {
let method = &caps[1];
format!(r#"<input type="hidden" name="_method" value="{}">"#, method)
})
.to_string();
output
}
pub fn compile_with_csrf(input: &str, csrf_token: &str) -> String {
let compiled = compile(input);
compiled.replace("{{ csrf_token }}", csrf_token)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_directive() {
let result = compile("@auth\n<p>Logged in</p>\n@endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@auth"));
assert!(!result.contains("@endauth"));
}
#[test]
fn test_auth_with_else() {
let result = compile("@auth\n<p>A</p>\n@else\n<p>B</p>\n@endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% else %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_auth_indented() {
let result = compile(" @auth\n <p>Hi</p>\n @endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_guest_directive() {
let result = compile("@guest\n<p>Guest</p>\n@endguest");
assert!(result.contains("{% if not auth_check %}"));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@guest"));
}
#[test]
fn test_error_directive() {
let result = compile("@error('email')\n<p>Invalid</p>\n@enderror");
assert!(result.contains(r#"{% if errors["email"] %}"#));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@error"));
}
#[test]
fn test_error_with_spaces() {
let result = compile("@error( 'name' )\n<p>Error</p>\n@enderror");
assert!(result.contains(r#"errors["name"]"#));
}
#[test]
fn test_csrf_directive() {
let result = compile("<form>@csrf</form>");
assert!(result.contains("csrf_token"));
assert!(result.contains("_token"));
assert!(result.contains("type=\"hidden\""));
}
#[test]
fn test_csrf_multiple() {
let result = compile("@csrf\n@csrf");
assert_eq!(result.matches("csrf_token").count(), 2);
}
#[test]
fn test_csrf_word_boundary() {
let result = compile("@csrf_token should be preserved, but @csrf should be replaced");
assert!(result.contains("@csrf_token"));
assert!(result.contains("<input"));
assert!(result.contains("csrf_token"));
}
#[test]
fn test_method_put() {
let result = compile("@method('PUT')");
assert!(result.contains("_method"));
assert!(result.contains("PUT"));
assert!(result.contains("type=\"hidden\""));
}
#[test]
fn test_method_delete() {
let result = compile("@method('DELETE')");
assert!(result.contains("DELETE"));
}
#[test]
fn test_compile_with_csrf_replaces_token() {
let result = compile_with_csrf("@csrf", "abc123");
assert!(result.contains("abc123"));
assert!(!result.contains("{{ csrf_token }}"));
}
#[test]
fn test_mixed_directives() {
let input = "\
@auth
<form method=\"POST\">@csrf</form>
@error('email')
<p>{{ message }}</p>
@enderror
@else
@guest
<p>Please log in</p>
@endguest
@endauth
";
let result = compile(input);
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% else %}"));
assert!(result.contains("{% endif %}"));
assert!(result.contains("csrf_token"));
assert!(result.contains(r#"errors["email"]"#));
assert!(result.contains("{% if not auth_check %}"));
}
#[test]
fn test_if_basic() {
let result = compile("@if($user)\n<p>Hi</p>\n@endif");
assert!(result.contains("{% if user %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_if_with_property() {
let result = compile("@if($user.is_admin)\n<p>Admin</p>\n@endif");
assert!(result.contains("{% if user.is_admin %}"));
}
#[test]
fn test_if_with_arrow() {
let result = compile("@if($user->is_admin)\n<p>Admin</p>\n@endif");
assert!(result.contains("{% if user.is_admin %}"));
}
#[test]
fn test_if_with_negation() {
let result = compile("@if(!$user)\n<p>Guest</p>\n@endif");
assert!(result.contains("{% if not user %}"));
}
#[test]
fn test_if_elseif() {
let result = compile("@if($role == 'admin')\n<p>A</p>\n@elseif($role == 'user')\n<p>U</p>\n@else\n<p>?</p>\n@endif");
assert!(result.contains("{% if role == 'admin' %}"));
assert!(result.contains("{% elif role == 'user' %}"));
assert!(result.contains("{% else %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_unless() {
let result = compile("@unless($user.is_banned)\n<p>Welcome</p>\n@endunless");
assert!(result.contains("{% if not (user.is_banned) %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_isset() {
let result = compile("@isset($name)\n<p>{{ name }}</p>\n@endisset");
assert!(result.contains("{% if name is defined %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_empty() {
let result = compile("@empty($items)\n<p>No items</p>\n@endempty");
assert!(result.contains("{% if items is empty %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_foreach_basic() {
let result = compile("@foreach($users as $user)\n<p>{{ user }}</p>\n@endforeach");
assert!(result.contains("{% for user in users %}"));
assert!(result.contains("{% endfor %}"));
}
#[test]
fn test_foreach_key_value() {
let result = compile(
"@foreach($roles as $name => $role)\n<p>{{ name }}: {{ role }}</p>\n@endforeach",
);
assert!(result.contains("{% for name, role in roles %}"));
assert!(result.contains("{% endfor %}"));
}
#[test]
fn test_include_single_quotes() {
let result = compile("@include('partials.header')");
assert_eq!(result.trim(), r#"{% include "partials.header" %}"#);
}
#[test]
fn test_include_double_quotes() {
let result = compile(r#"@include("partials.footer")"#);
assert_eq!(result.trim(), r#"{% include "partials.footer" %}"#);
}
#[test]
fn test_no_directives_passthrough() {
let input = "<h1>Hello</h1>\n<p>{{ name }}</p>";
let result = compile(input);
assert_eq!(result, input);
}
}