Skip to main content

mdvault_core/templates/
engine.rs

1use regex::Regex;
2use std::collections::HashMap;
3use std::path::{Path, PathBuf};
4
5use chrono::Local;
6use thiserror::Error;
7use tracing::debug;
8
9use crate::config::types::ResolvedConfig;
10use crate::vars::datemath::{evaluate_date_expr, is_date_expr, parse_date_expr};
11
12use super::discovery::TemplateInfo;
13use super::repository::LoadedTemplate;
14
15#[derive(Debug, Error)]
16pub enum TemplateRenderError {
17    #[error("invalid regex for template placeholder: {0}")]
18    Regex(String),
19}
20
21pub type RenderContext = HashMap<String, String>;
22
23/// Build a minimal render context with date/time and config variables.
24///
25/// This is useful for resolving template output paths from frontmatter
26/// before the actual output path is known.
27pub fn build_minimal_context(
28    cfg: &ResolvedConfig,
29    template: &TemplateInfo,
30) -> RenderContext {
31    let mut ctx = RenderContext::new();
32
33    // Date/time (basic versions - date math expressions are handled separately)
34    let now = Local::now();
35    ctx.insert("date".into(), now.format("%Y-%m-%d").to_string());
36    ctx.insert("time".into(), now.format("%H:%M").to_string());
37    ctx.insert("datetime".into(), now.to_rfc3339());
38    // Add today/now as aliases
39    ctx.insert("today".into(), now.format("%Y-%m-%d").to_string());
40    ctx.insert("now".into(), now.to_rfc3339());
41
42    // From config
43    ctx.insert("vault_root".into(), cfg.vault_root.to_string_lossy().to_string());
44    ctx.insert("templates_dir".into(), cfg.templates_dir.to_string_lossy().to_string());
45    ctx.insert("captures_dir".into(), cfg.captures_dir.to_string_lossy().to_string());
46    ctx.insert("macros_dir".into(), cfg.macros_dir.to_string_lossy().to_string());
47
48    // Template info
49    ctx.insert("template_name".into(), template.logical_name.clone());
50    ctx.insert("template_path".into(), template.path.to_string_lossy().to_string());
51
52    ctx
53}
54
55pub fn build_render_context(
56    cfg: &ResolvedConfig,
57    template: &TemplateInfo,
58    output_path: &Path,
59) -> RenderContext {
60    let mut ctx = build_minimal_context(cfg, template);
61
62    // Output info
63    let output_abs = absolutize(output_path);
64    ctx.insert("output_path".into(), output_abs.to_string_lossy().to_string());
65    if let Some(name) = output_abs.file_name().and_then(|s| s.to_str()) {
66        ctx.insert("output_filename".into(), name.to_string());
67    }
68    if let Some(parent) = output_abs.parent() {
69        ctx.insert("output_dir".into(), parent.to_string_lossy().to_string());
70    }
71
72    ctx
73}
74
75fn absolutize(path: &Path) -> PathBuf {
76    if path.is_absolute() {
77        path.to_path_buf()
78    } else {
79        std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")).join(path)
80    }
81}
82
83/// Clean up YAML content by removing problematic lines and quoting special values.
84///
85/// This handles two cases:
86/// 1. Unreplaced template variables (e.g., `field: {{var}}`) - removes the line
87/// 2. YAML-problematic values (e.g., `field: -`) - quotes the value
88///
89/// Examples:
90/// - `status: {{status}}` where status wasn't provided -> line removed
91/// - `status: todo` where status was provided -> line kept
92/// - `phone: -` -> becomes `phone: "-"` (quoted to avoid YAML list marker interpretation)
93fn remove_unreplaced_vars(content: &str) -> String {
94    content
95        .lines()
96        .filter_map(|line| {
97            // Check if line has a key-value pair
98            if let Some((key, value)) = line.split_once(':') {
99                let value = value.trim();
100
101                // Case 1: Unreplaced template variable - remove line
102                if value.starts_with("{{")
103                    && value.ends_with("}}")
104                    && !value.contains(' ')
105                {
106                    return None;
107                }
108
109                // Case 2: YAML-problematic values - quote them
110                // Check if value needs quoting (single dash, or starts with special YAML chars)
111                if needs_yaml_quoting(value) {
112                    return Some(format!("{}: \"{}\"", key, value));
113                }
114            }
115            Some(line.to_string())
116        })
117        .collect::<Vec<_>>()
118        .join("\n")
119        + "\n" // Add trailing newline
120}
121
122/// Check if a YAML value needs quoting to avoid parsing errors.
123///
124/// Returns true for values that would be misinterpreted or cause parsing errors:
125/// - List markers: `-` (single dash starts a list item)
126/// - Empty string: becomes null without quotes
127///
128/// Note: YAML booleans (true/false/yes/no/on/off) and null values (null/~)
129/// are NOT quoted because they are valid YAML values. Templates with these
130/// values will have them parsed correctly as their respective types.
131fn needs_yaml_quoting(value: &str) -> bool {
132    // Already quoted - no need to quote again
133    if (value.starts_with('"') && value.ends_with('"'))
134        || (value.starts_with('\'') && value.ends_with('\''))
135    {
136        return false;
137    }
138
139    // Single dash is a list marker in YAML - must be quoted
140    if value == "-" {
141        return true;
142    }
143
144    // Empty string becomes null in YAML - quote to preserve as empty string
145    if value.is_empty() {
146        return true;
147    }
148
149    false
150}
151
152pub fn render(
153    template: &LoadedTemplate,
154    ctx: &RenderContext,
155) -> Result<String, TemplateRenderError> {
156    debug!("Rendering template '{}' with vars: {:?}", template.logical_name, ctx.keys());
157    let rendered_body = render_string(&template.body, ctx)?;
158
159    // Check if template has frontmatter to include in output.
160    // We render from the RAW frontmatter text to avoid YAML parsing issues
161    // with template variables like {{title}} being interpreted as YAML mappings.
162    if let Some(ref raw_fm) = template.raw_frontmatter {
163        // Filter out template-specific fields (output, lua, vars)
164        let filtered_fm = filter_template_fields(raw_fm);
165        if !filtered_fm.trim().is_empty() {
166            // Render variables in the filtered frontmatter text
167            let rendered_fm = render_string(&filtered_fm, ctx)?;
168            // Remove lines with unreplaced template variables (optional fields)
169            let cleaned_fm = remove_unreplaced_vars(&rendered_fm);
170            return Ok(format!("---\n{}---\n\n{}", cleaned_fm, rendered_body));
171        }
172    }
173
174    Ok(rendered_body)
175}
176
177/// Filter out template-specific fields (output, lua, vars) from raw frontmatter.
178/// These fields are used by the template system and should not appear in output.
179fn filter_template_fields(raw_fm: &str) -> String {
180    let template_fields = ["output:", "lua:", "vars:"];
181    let mut result = Vec::new();
182    let mut skip_until_next_field = false;
183
184    for line in raw_fm.lines() {
185        // Check if this line starts a template-specific field
186        let trimmed = line.trim_start();
187        let starts_template_field =
188            template_fields.iter().any(|f| trimmed.starts_with(f));
189
190        if starts_template_field {
191            // Start skipping this field and any continuation lines
192            skip_until_next_field = true;
193            continue;
194        }
195
196        // If we're skipping continuation lines from a template field
197        if skip_until_next_field {
198            // Check if this is a new top-level field (not indented)
199            // A new field starts at column 0 and contains a colon
200            let is_new_field = !line.starts_with(' ')
201                && !line.starts_with('\t')
202                && !line.trim().is_empty()
203                && line.contains(':');
204
205            if is_new_field {
206                // This is a new field, stop skipping and include this line
207                skip_until_next_field = false;
208                result.push(line);
209            }
210            // Continue to next line (either included the new field or skipped continuation)
211            continue;
212        }
213
214        // Not skipping, include this line
215        result.push(line);
216    }
217
218    let mut filtered = result.join("\n");
219    // Ensure trailing newline if original had one
220    if raw_fm.ends_with('\n') && !filtered.ends_with('\n') {
221        filtered.push('\n');
222    }
223    filtered
224}
225
226/// Render a string template with variable substitution.
227///
228/// Supports:
229/// - Simple variables: `{{var_name}}`
230/// - Date math expressions: `{{today + 1d}}`, `{{now - 2h}}`, `{{today | %Y-%m-%d}}`
231/// - Filters: `{{var_name | filter}}` (currently supports: slugify)
232pub fn render_string(
233    template: &str,
234    ctx: &RenderContext,
235) -> Result<String, TemplateRenderError> {
236    // Match both simple vars and date math expressions
237    // Captures everything between {{ and }} that looks like a valid expression
238    let re = Regex::new(r"\{\{([^{}]+)\}\}")
239        .map_err(|e| TemplateRenderError::Regex(e.to_string()))?;
240
241    let result = re.replace_all(template, |caps: &regex::Captures<'_>| {
242        let expr = caps[1].trim();
243
244        // Check for filter syntax first: "var_name | filter"
245        if let Some((var_name, filter)) = parse_filter_expr(expr) {
246            if let Some(value) = ctx.get(var_name) {
247                return apply_filter(value, filter);
248            }
249            // Variable not found, but might be a date expression with format
250            // (e.g., "today | %Y-%m-%d")
251            if is_date_expr(expr)
252                && let Ok(parsed) = parse_date_expr(expr)
253            {
254                return evaluate_date_expr(&parsed);
255            }
256            debug!("Template variable not found for filter: {}", var_name);
257            return caps[0].to_string();
258        }
259
260        // Check context variable FIRST - if explicitly set, use it
261        // This allows variables like "week" or "date" to override date expressions
262        if let Some(val) = ctx.get(expr) {
263            return val.clone();
264        }
265
266        // If no context variable, check if it's a date math expression
267        if is_date_expr(expr)
268            && let Ok(parsed) = parse_date_expr(expr)
269        {
270            return evaluate_date_expr(&parsed);
271        }
272
273        // Not found anywhere
274        debug!("Template variable not found: {}", expr);
275        caps[0].to_string()
276    });
277
278    Ok(result.into_owned())
279}
280
281/// Parse a filter expression like "var_name | filter_name".
282/// Returns (var_name, filter_name) if valid, None otherwise.
283fn parse_filter_expr(expr: &str) -> Option<(&str, &str)> {
284    // Don't parse date expressions with format as filters (e.g., "today | %Y-%m-%d")
285    if is_date_expr(expr) {
286        return None;
287    }
288
289    let parts: Vec<&str> = expr.splitn(2, '|').collect();
290    if parts.len() == 2 {
291        let var_name = parts[0].trim();
292        let filter = parts[1].trim();
293        if !var_name.is_empty() && !filter.is_empty() {
294            return Some((var_name, filter));
295        }
296    }
297    None
298}
299
300/// Apply a filter to a value.
301fn apply_filter(value: &str, filter: &str) -> String {
302    match filter {
303        "slugify" => slugify(value),
304        "lowercase" | "lower" => value.to_lowercase(),
305        "uppercase" | "upper" => value.to_uppercase(),
306        "trim" => value.trim().to_string(),
307        _ => value.to_string(), // Unknown filter, return unchanged
308    }
309}
310
311/// Convert a string to a URL-friendly slug.
312///
313/// - Converts to lowercase
314/// - Replaces spaces and underscores with hyphens
315/// - Removes non-alphanumeric characters (except hyphens)
316/// - Collapses multiple hyphens into one
317/// - Trims leading/trailing hyphens
318fn slugify(s: &str) -> String {
319    let mut result = String::with_capacity(s.len());
320
321    for c in s.chars() {
322        if c.is_ascii_alphanumeric() {
323            result.push(c.to_ascii_lowercase());
324        } else if c == ' ' || c == '_' || c == '-' {
325            // Only add hyphen if last char wasn't already a hyphen
326            if !result.ends_with('-') {
327                result.push('-');
328            }
329        }
330        // Other characters are skipped
331    }
332
333    // Trim leading/trailing hyphens
334    result.trim_matches('-').to_string()
335}
336
337/// Resolve the output path for a template.
338///
339/// If the template has frontmatter with an `output` field, render it with the context.
340/// Otherwise, return None.
341pub fn resolve_template_output_path(
342    template: &LoadedTemplate,
343    cfg: &ResolvedConfig,
344    ctx: &RenderContext,
345) -> Result<Option<PathBuf>, TemplateRenderError> {
346    if let Some(ref fm) = template.frontmatter
347        && let Some(ref output) = fm.output
348    {
349        let rendered = render_string(output, ctx)?;
350        let path = cfg.vault_root.join(&rendered);
351        return Ok(Some(path));
352    }
353    Ok(None)
354}
355
356#[cfg(test)]
357mod tests {
358    use super::*;
359
360    #[test]
361    fn test_slugify_basic() {
362        assert_eq!(slugify("Hello World"), "hello-world");
363        assert_eq!(slugify("Test Task"), "test-task");
364    }
365
366    #[test]
367    fn test_slugify_special_chars() {
368        assert_eq!(slugify("Hello, World!"), "hello-world");
369        assert_eq!(slugify("What's up?"), "whats-up");
370        assert_eq!(slugify("foo@bar.com"), "foobarcom");
371    }
372
373    #[test]
374    fn test_slugify_underscores() {
375        assert_eq!(slugify("hello_world"), "hello-world");
376        assert_eq!(slugify("foo_bar_baz"), "foo-bar-baz");
377    }
378
379    #[test]
380    fn test_slugify_multiple_spaces() {
381        assert_eq!(slugify("hello   world"), "hello-world");
382        assert_eq!(slugify("  leading and trailing  "), "leading-and-trailing");
383    }
384
385    #[test]
386    fn test_slugify_mixed() {
387        assert_eq!(slugify("My Task: Do Something!"), "my-task-do-something");
388        assert_eq!(slugify("2024-01-15 Meeting Notes"), "2024-01-15-meeting-notes");
389    }
390
391    #[test]
392    fn test_render_string_with_slugify_filter() {
393        let mut ctx = RenderContext::new();
394        ctx.insert("title".into(), "Hello World".into());
395
396        let result = render_string("{{title | slugify}}", &ctx).unwrap();
397        assert_eq!(result, "hello-world");
398    }
399
400    #[test]
401    fn test_render_string_with_lowercase_filter() {
402        let mut ctx = RenderContext::new();
403        ctx.insert("name".into(), "HELLO".into());
404
405        let result = render_string("{{name | lowercase}}", &ctx).unwrap();
406        assert_eq!(result, "hello");
407
408        let result = render_string("{{name | lower}}", &ctx).unwrap();
409        assert_eq!(result, "hello");
410    }
411
412    #[test]
413    fn test_render_string_with_uppercase_filter() {
414        let mut ctx = RenderContext::new();
415        ctx.insert("name".into(), "hello".into());
416
417        let result = render_string("{{name | uppercase}}", &ctx).unwrap();
418        assert_eq!(result, "HELLO");
419    }
420
421    #[test]
422    fn test_render_string_filter_in_path() {
423        let mut ctx = RenderContext::new();
424        ctx.insert("vault_root".into(), "/vault".into());
425        ctx.insert("title".into(), "My New Task".into());
426
427        let result =
428            render_string("{{vault_root}}/tasks/{{title | slugify}}.md", &ctx).unwrap();
429        assert_eq!(result, "/vault/tasks/my-new-task.md");
430    }
431
432    #[test]
433    fn test_render_string_unknown_filter() {
434        let mut ctx = RenderContext::new();
435        ctx.insert("name".into(), "hello".into());
436
437        // Unknown filter returns value unchanged
438        let result = render_string("{{name | unknown}}", &ctx).unwrap();
439        assert_eq!(result, "hello");
440    }
441
442    #[test]
443    fn test_render_string_missing_var_with_filter() {
444        let ctx = RenderContext::new();
445
446        // Missing variable with filter returns original placeholder
447        let result = render_string("{{missing | slugify}}", &ctx).unwrap();
448        assert_eq!(result, "{{missing | slugify}}");
449    }
450
451    #[test]
452    fn test_date_format_not_parsed_as_filter() {
453        let ctx = RenderContext::new();
454
455        // Date expressions with format should still work
456        let result = render_string("{{today | %Y-%m-%d}}", &ctx).unwrap();
457        // Should be a date, not "today" with filter "%Y-%m-%d"
458        assert!(result.contains('-'));
459        assert!(!result.contains("today"));
460    }
461
462    #[test]
463    fn test_context_variable_overrides_date_expression() {
464        // Regression test: context variables should take precedence over date expressions
465        // This is important for templates like "{{week}}" where a computed week value
466        // should be used instead of evaluating "week" as a date expression
467        let mut ctx = RenderContext::new();
468
469        // Set a "week" variable that should override the "week" date expression
470        ctx.insert("week".into(), "2026-W06".into());
471        let result = render_string("Journal/Weekly/{{week}}.md", &ctx).unwrap();
472        assert_eq!(result, "Journal/Weekly/2026-W06.md");
473
474        // Same for "date" - context variable should override date expression
475        ctx.insert("date".into(), "2026-02-15".into());
476        let result = render_string("Journal/Daily/{{date}}.md", &ctx).unwrap();
477        assert_eq!(result, "Journal/Daily/2026-02-15.md");
478
479        // Without context variable, date expression should still work
480        let empty_ctx = RenderContext::new();
481        let result = render_string("{{today}}", &empty_ctx).unwrap();
482        // Should be today's date, not "{{today}}"
483        assert!(result.contains('-') && result.len() == 10);
484    }
485
486    #[test]
487    fn test_remove_unreplaced_vars() {
488        // Test removing unreplaced template variables
489        let content = "status: todo\nphone: {{phone}}\nemail: test@example.com\n";
490        let result = super::remove_unreplaced_vars(content);
491        assert!(result.contains("status: todo"));
492        assert!(!result.contains("phone:"), "unreplaced var line should be removed");
493        assert!(result.contains("email: test@example.com"));
494    }
495
496    #[test]
497    fn test_remove_unreplaced_vars_quotes_dash() {
498        // Test quoting of YAML-problematic dash value
499        let content = "name: John\nphone: -\nemail: test@example.com\n";
500        let result = super::remove_unreplaced_vars(content);
501        assert!(result.contains("name: John"));
502        assert!(
503            result.contains("phone: \"-\""),
504            "dash should be quoted, got: {}",
505            result
506        );
507        assert!(result.contains("email: test@example.com"));
508    }
509
510    #[test]
511    fn test_needs_yaml_quoting() {
512        use super::needs_yaml_quoting;
513
514        // Should need quoting - only values that cause parsing errors
515        assert!(needs_yaml_quoting("-")); // List marker
516        assert!(needs_yaml_quoting("")); // Empty string becomes null
517
518        // Should NOT need quoting - valid YAML values
519        assert!(!needs_yaml_quoting("hello"));
520        assert!(!needs_yaml_quoting("123"));
521        assert!(!needs_yaml_quoting("\"already quoted\""));
522        assert!(!needs_yaml_quoting("'already quoted'"));
523        assert!(!needs_yaml_quoting("test@example.com"));
524
525        // YAML booleans should NOT be quoted (they're valid YAML)
526        assert!(!needs_yaml_quoting("true"));
527        assert!(!needs_yaml_quoting("false"));
528        assert!(!needs_yaml_quoting("yes"));
529        assert!(!needs_yaml_quoting("no"));
530
531        // YAML null values should NOT be quoted (they're valid YAML)
532        assert!(!needs_yaml_quoting("null"));
533        assert!(!needs_yaml_quoting("~"));
534    }
535}