anvilforge-templates-codegen 0.3.5

Forge → Askama preprocessor: parser, lowering, build.rs integration.
Documentation
//! Compile Forge templates to Askama (compile-time, build.rs path) or to a
//! MiniJinja-compatible runtime form used by Spark.

use std::fs;
use std::path::{Path, PathBuf};

use walkdir::WalkDir;

use crate::lower::{lower, lower_with_target, LowerTarget};
use crate::parser::tokenize;

pub fn compile_source(source: &str) -> String {
    let tokens = tokenize(source);
    lower(&tokens)
}

/// Lower a Forge source string into MiniJinja-compatible syntax. Spark
/// directives (`@spark`, `@sparkScripts`) emit function calls bound to
/// MiniJinja's global function table; other directives behave the same as the
/// Askama path for now.
pub fn compile_source_runtime(source: &str) -> String {
    let tokens = tokenize(source);
    lower_with_target(&tokens, LowerTarget::MiniJinja)
}

pub fn compile_file(input: &Path, output: &Path) -> std::io::Result<()> {
    let raw = fs::read_to_string(input)?;
    let lowered = compile_source(&raw);
    if let Some(parent) = output.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(output, lowered)?;
    Ok(())
}

/// Walk `input_dir` for `*.forge.html` and emit a Rust source file that
/// registers each template's raw source via `inventory::submit!` as a
/// `spark::template::EmbeddedTemplate`. The user includes this from their
/// crate root with:
///
/// ```ignore
/// include!(concat!(env!("OUT_DIR"), "/spark_embedded_templates.rs"));
/// ```
///
/// The included `inventory::submit!` blocks register the embedded sources;
/// `spark::template::render` then resolves view paths from memory instead of
/// hitting disk, enabling single-binary distribution.
///
/// Calling this is optional — apps that don't include the generated file
/// continue to load templates from `resources/views/` at runtime.
pub fn emit_embedded_registry(input_dir: &Path, output_rs: &Path) -> std::io::Result<()> {
    if let Some(parent) = output_rs.parent() {
        fs::create_dir_all(parent)?;
    }

    if !input_dir.exists() {
        fs::write(
            output_rs,
            "// forge-codegen: input dir not found at build time, no templates embedded.\n",
        )?;
        return Ok(());
    }

    let abs_input = fs::canonicalize(input_dir)?;
    let mut out =
        String::from("// Generated by forge_codegen::emit_embedded_registry — do not edit.\n");

    for entry in WalkDir::new(&abs_input).into_iter().filter_map(|e| e.ok()) {
        if !entry.file_type().is_file() {
            continue;
        }
        let path = entry.path();
        let Some(file_name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        if !file_name.ends_with(".forge.html") && !file_name.ends_with(".forge") {
            continue;
        }
        let rel = path.strip_prefix(&abs_input).unwrap_or(path);
        let rel_str = rel.to_string_lossy().replace('\\', "/");
        let view_path = rel_str
            .trim_end_matches(".forge.html")
            .trim_end_matches(".forge")
            .to_string();
        let abs_str = path
            .canonicalize()
            .unwrap_or_else(|_| path.to_path_buf())
            .to_string_lossy()
            .to_string();
        out.push_str(&format!(
            "::anvilforge::inventory::submit! {{ ::anvilforge::spark::template::EmbeddedTemplate {{ view_path: {view:?}, source: include_str!({src:?}) }} }}\n",
            view = view_path,
            src = abs_str,
        ));
    }

    fs::write(output_rs, out)?;
    Ok(())
}

/// Walk `input_dir` for `*.forge.html` and write Askama-compatible `*.html`
/// files into `output_dir`, preserving the relative layout.
pub fn compile_dir(input_dir: &Path, output_dir: &Path) -> std::io::Result<Vec<PathBuf>> {
    let mut written = Vec::new();
    if !input_dir.exists() {
        return Ok(written);
    }
    for entry in WalkDir::new(input_dir).into_iter().filter_map(|e| e.ok()) {
        if !entry.file_type().is_file() {
            continue;
        }
        let path = entry.path();
        let Some(file_name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        if !file_name.ends_with(".forge.html") && !file_name.ends_with(".forge") {
            continue;
        }
        let rel = path.strip_prefix(input_dir).unwrap_or(path);
        let out_name = file_name
            .replace(".forge.html", ".html")
            .replace(".forge", ".html");
        let mut out_path = output_dir.to_path_buf();
        if let Some(parent) = rel.parent() {
            out_path.push(parent);
        }
        out_path.push(out_name);
        compile_file(path, &out_path)?;
        written.push(out_path);
    }
    Ok(written)
}