weaver_lib/filters/
raw_html.rs

1use liquid::Error;
2use liquid_core::{Display_filter, Filter, FilterReflection, ParseFilter};
3use liquid_core::{Result, Runtime};
4use liquid_core::{Value, ValueView};
5
6#[derive(Clone, ParseFilter, FilterReflection)]
7#[filter(
8    name = "raw",
9    description = "Output the raw input unescaped.",
10    parsed(RawHtmlFilter)
11)]
12pub struct RawHtml;
13
14#[derive(Debug, Default, Display_filter)]
15#[name = "raw"]
16struct RawHtmlFilter;
17
18impl Filter for RawHtmlFilter {
19    fn evaluate(&self, input: &dyn ValueView, _runtime: &dyn Runtime) -> Result<Value> {
20        // Check if the input is a scalar (string, number, etc.)
21        let scalar_input = input.as_scalar().ok_or_else(|| {
22            Error::with_msg("RawHtml filter expects a scalar (string, number, etc.) input.")
23        })?;
24
25        // Get the owned string from the scalar.
26        // Using into_owned() gets a String whether the scalar was borrowed or owned.
27        let raw_string = scalar_input.into_owned();
28
29        // Create a liquid::model::Value::Scalar from the raw string.
30        // The crucial part: We rely on Value::scalar() NOT marking the string for HTML escaping,
31        // unlike strings produced by liquid::model::to_value(). This is the standard pattern
32        // for raw output in other templating engines, and the most likely mechanism here.
33        Ok(Value::scalar(raw_string)) // Return the string as a raw scalar value
34    }
35}