module

Hwaro::Utils::CrinjaUtils

Constants

NIL_VALUE = Crinja::Value.new(nil)

Pre-allocated nil value to avoid repeated allocations in hot paths

Instance methods

from_extra(value : Hwaro::Models::ExtraValue, depth : Int32 = 0) : Crinja::Value

Convert an extra field value (from front matter) to Crinja::Value. Recursive so nested [extra.*] hashes and arrays-of-hashes traverse via {{ page.extra.a.b }} in templates.

Source
from_json(value : JSON::Any, depth : Int32 = 0) : Crinja::Value

Convert a JSON::Any value to Crinja::Value

Source
from_toml(value : Hash(String, TOML::Any), depth : Int32 = 0) : Crinja::Value

Convert a Hash(String, TOML::Any) to Crinja::Value

Source
from_toml(value : TOML::Any, depth : Int32 = 0) : Crinja::Value

Convert a TOML::Any value to Crinja::Value

Source
from_yaml(value : YAML::Any, depth : Int32 = 0) : Crinja::Value

Convert a YAML::Any value to Crinja::Value.

depth guards against a self-referencing YAML anchor, whose parsed graph is cyclic and would otherwise recurse until the stack dies (see Utils::Nesting). Both call sites — DataDisk.parse_file and the load_data template function — already rescue and degrade, so the raise surfaces as "data file skipped" rather than a crash.

Source
parse_data_string(content : String, format : String) : Crinja::Value | Nil

Parse a data payload into the Crinja::Value templates see, dispatching on a bare format name ("json", "toml", "yaml"/"yml", "csv"). This is the one place that mapping lives: data/ files (DataDisk.parse_file), the load_data() template function, and [[data.remote]] payloads (RemoteData#parse_body) all route through it, so a key can move between a disk file, a template call and a remote source and parse identically. CSV matches the long-standing load_data() shape: an array of rows, each an array of stripped string cells.

Returns nil for an unrecognized format — every caller owns its own error policy (warn and drop the key, debug-log and return none, or raise a classified fetch error), and no two of them agree, so this helper neither logs nor raises. Parse exceptions propagate to the caller for the same reason.

BOM stripping is the caller's call too: the data/ and remote paths strip it (JSON and TOML both reject a leading BOM outright), while load_data() deliberately never has — see its call site.

Source
to_count(value : Crinja::Value, default : Int32 = 0, min : Int32 = 0, max : Int32 = Int32::MAX) : Int32

Coerce a template/filter argument to an Int32 count, clamped to [min, max], with default for anything that isn't a number at all.

Numeric arguments reach templates as strings far more often than not: parse_shortcode_args_jinja produces a Hash(String, String), so every value a shortcode forwards is a String. Crinja::Value#as_number raises Crinja::TypeError on those, and the raise propagated out of the filter and aborted the whole page render — resize_image(width="800") killed the page instead of resizing. Out-of-range and NaN values clamp here too rather than raising OverflowError from Float64#to_i.

Source