1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! End-user guide to the somni-template language.
//!
//! Templates mix literal text with interpolations and directives. They are compiled into a
//! Somni program and rendered against an [`Env`](crate::Env).
//!
//! # Chapters
//!
//! - [`_01_syntax`] — delimiters, frontmatter, text line prefix
//! - [`_02_interpolation`] — `{{ expr }}`
//! - [`_03_conditionals`] — `if` / `else if` / `else`
//! - [`_04_loops`] — `for` / `endfor`
//! - [`_05_replace`] — search-and-replace blocks
//! - [`_06_include`] — nested templates
//! - [`_07_expressions`] — what may appear inside directives and interpolations
//!
//! Examples below use [`Syntax::lines`](crate::Syntax::lines) (`#` directives, `{{ }}`
//! interpolations). Other delimiter styles are equivalent; see [`_01_syntax`].
/// Delimiters, frontmatter, and the text line prefix.
///
/// # Delimiters
///
/// Two independent choices:
///
/// | Axis | Default ([`Syntax::brackets`](crate::Syntax::brackets)) | Line style ([`Syntax::lines`](crate::Syntax::lines)) |
/// |------|---------------------------------------------------------|------------------------------------------------------|
/// | Interpolation | `{{` … `}}` | same |
/// | Directives | `{%` … `%}` | whole line starting with `#` |
///
/// Both are configurable via [`Syntax`](crate::Syntax) (and frontmatter). Line directives
/// consume the entire physical line (leading whitespace through the trailing newline) and
/// emit nothing.
///
/// Keywords are case-insensitive (`if` / `IF`, `endfor` / `ENDFOR`, …).
///
/// # Frontmatter
///
/// A template may begin with a `---`-fenced block that overlays the [`Syntax`](crate::Syntax)
/// passed to [`Template::compile`](crate::Template::compile):
///
/// ```text
/// ---
/// expr: {{ }}
/// block: line //
/// text_prefix: //>
/// ---
/// ```
///
/// Keys (each optional, at most once):
///
/// - `expr: <open> <close>`
/// - `block: paired <open> <close>`
/// - `block: line <prefix>`
/// - `text_prefix: <prefix>`
///
/// Lines that are blank or whose first non-whitespace character is `#` are ignored.
/// Frontmatter wins for keys it sets; omitted keys keep the Rust-provided value.
///
/// # Text line prefix
///
/// [`Syntax::text_prefix`](crate::Syntax::text_prefix) (frontmatter `text_prefix`) names a
/// line marker that is stripped: leading whitespace and the prefix are dropped; the rest of
/// the line is ordinary template text (interpolations allowed).
///
/// Use this when the template is also a valid program in another language. Conditional
/// output can sit behind a comment marker so it is inert if the file is compiled or run
/// directly:
///
/// ```text
/// ---
/// block: line //
/// text_prefix: //>
/// ---
/// // if feature
/// //>enable_feature();
/// // endif
/// ```
///
/// Rendered (when `feature` is true): `enable_feature();\n`. As C/C++ source, the `//>` line
/// is only a comment.
///
/// When both `text_prefix` and a line block prefix could match, the longer prefix wins. They
/// must not be identical.
/// Emitting expression results into the output.
///
/// ```text
/// Hello, {{ name }}!
/// ```
///
/// The expression must evaluate to a `string`. Convert other types explicitly — a default
/// `str` conversion is registered on every [`Env`](crate::Env):
///
/// ```text
/// count = {{ str(count) }}
/// ```
///
/// Host functions and conversions registered on the env are callable the same way.
/// Conditional blocks.
///
/// ```text
/// #if online
/// (online)
/// #else if away
/// (away)
/// #else
/// (offline)
/// #endif
/// ```
///
/// Conditions are Somni expressions (see [`_07_expressions`]). Arms may nest arbitrarily with
/// other directives.
/// Iteration.
///
/// ```text
/// #for n in nums
/// - {{ str(n) }}
/// #endfor
/// ```
///
/// Optional type annotation on the loop variable:
///
/// ```text
/// #for n: int in nums
/// ```
///
/// When omitted, the type is inferred from uses of the variable.
///
/// The iterable must be provided by the host. Values registered with
/// [`Iter`](crate::Iter) are **single-pass**. To nest loops over the same source, register a
/// function that returns a fresh iterator and call it in the header:
///
/// ```text
/// #for i in items()
/// #for j in items()
/// …
/// #endfor
/// #endfor
/// ```
/// Literal search-and-replace over a body.
///
/// ```text
/// #replace "NAME" with name
/// Hello, NAME!
/// #endreplace
/// ```
///
/// Every occurrence of the quoted literal in the body text is replaced by the value of the
/// `with` expression (which must be a string). The body is otherwise a normal nested
/// template — directives and interpolations still run.
///
/// The literal may contain escapes (`\"`, `\\`, …). It must not be empty. A missing literal
/// is a no-op (body unchanged).
/// Nested templates.
///
/// ```text
/// #include "row.tmpl"
/// #include "cell.tmpl" with label: string = name, n: int = count
/// ```
///
/// Paths are resolved by the loader passed to
/// [`Template::compile_with`](crate::Template::compile_with). [`Template::compile`](crate::Template::compile)
/// rejects every include.
///
/// - **Bare `include`** (no `with`): the body is expanded in place and shares the caller’s
/// locals and env.
/// - **`include … with …`**: compiles to a separate function. Each binding is
/// `name: type = expr`; types are required. Only the listed bindings are in scope inside
/// the include.
///
/// An included file may have its own frontmatter (its own delimiters). Cycles are a compile
/// error.
/// Expressions inside interpolations and directives.
///
/// Expression syntax is [Somni](somni_expr)’s. Common forms:
///
/// - Literals, identifiers, field access: `point.x`
/// - Arithmetic and comparisons: `n > 1`, `a + b`
/// - Calls: `str(n)`, `upper(name)`, `items()`
/// - Struct equality in conditions when the host registered structs
///
/// Names come from the [`Env`](crate::Env) (values and functions) plus locals introduced by
/// `for` and `include … with`. There is no assignment and no statement forms — only
/// expressions.