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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! Data loading module — the functorial bridge from the filesystem to the
//! template context.
//!
//! # Formal specification
//!
//! The project's data model follows the algebra of *inductive types* and *free
//! constructions* as described in:
//!
//! - **Pierce, Benjamin C.** (2002). *Types and Programming Languages*. MIT
//! Press. Chapter 11 (algebraic datatypes) defines the sum-of-products
//! structure used by every `Node`, `AstNode`, and context value.
//!
//! - **Martin-Löf, Per** (1984). *Intuitionistic Type Theory*. Bibliopolis.
//! The `Value` type with `Object | Array | String | Number | Bool | Null`
//! forms a W-type (well-founded tree) whose introduction rules correspond
//! to the data constructors `<?data?>`, `<?record?>`, and `<?list?>`.
//!
//! - **Mac Lane, Saunders** (1971). *Categories for the Working Mathematician*.
//! Springer. The file-system-to-context mapping is a functor
//! `F : Dir → Set` where `Dir` is the discrete category of filesystem paths
//! and `Set` is the category of `serde_json::Value` objects.
//!
//! - **Wadler, Philip** (1992). "The Essence of Functional Programming".
//! POPL. The `<?map?>` directive instantiates the **list functor** `map`:
//! `map : (A → B) → [A] → [B]`, where the function `A → B` is given by the
//! child template block.
//!
//! - **Moggi, Eugenio** (1991). "Notions of Computation and Monads". *Inf. &
//! Comp.* 93(1). The `Context` type forms a computational monad `T(X) =
//! Env → X`, where `set_value` is the bind operation threading scope across
//! template evaluation.
//!
//! - **Swierstra, Wouter** (2008). "Data types à la carte". *JFP* 18(4).
//! Each HRML directive is a signature functor; `<?map?>`, `<?filter?>`,
//! `<?sort?>` are *algebraic effects* composed via coproducts of functors
//! into a pipeline: `F ∘ G ∘ H`.
//!
//! - **Burstall, Rod** (1969). "Proving Properties of Programs by Structural
//! Induction". *Computer Journal* 12(1). Template rendering correctness is
//! proved by structural induction on `Node` — each directive handler is a
//! case in the induction.
//!
//! ## Type algebra
//!
//! ```text
//! Value ≜ Null | Bool | Number | String
//! | Object(Map String Value)
//! | Array(Vec Value) — inductive W-type
//!
//! Node ≜ Text(String)
//! | Load { file: String, blocks: Map String [Node] }
//! | VoidElement { name: String, attrs: Map String String }
//! | Element { name: String, attrs, children: [Node] }
//!
//! Context ≜ { data: Value, vars: Map String Value,
//! components: Map String [Node], ... }
//!
//! load_data_file : Path → Result Value Error — point-wise parsing
//! load_data_dir : Path → Result Value Error — co-inductive fold
//! ```
//!
//! ## Laws
//!
//! **Functoriality of map**
//! ```text
//! ∀ f g, arr. map (f ∘ g) arr ≡ map f (map g arr)
//! ```
//!
//! **Idempotence of sort (stable sort)**
//! ```text
//! ∀ arr, k. sort over arr by k ; sort over arr by k ≡ sort over arr by k
//! ```
//!
//! **Filter–Map fusion (free theorem)**
//! ```text
//! filter(where=cond) over arr ; map(f) over arr
//! ≡ map(f) over filter(where=cond) over arr
//! ```
//! (when `f` is pure — holds for all template rendering since templates are
//! deterministic functions Context → Html.)
use ;
use fs;
use Path;
/// Load a single data file into a [`Value`] by dispatching on extension.
///
/// ## Formal signature
/// ```text
/// parse_ext : { json, toml, md, mdx } → (String → Value)
/// load_data_file = read ∘ parse_ext(ext(path))
/// ```
///
/// | Extension | Parser | Output type |
/// |-----------|----------------------|-----------------|
/// | `.json` | `serde_json` | arbitrary Value |
/// | `.toml` | `toml` → `serde_json`| arbitrary Value |
/// | `.md` | frontmatter split | Object(body) |
/// | `.mdx` | frontmatter split | Object(body) |
/// Build an array of data objects from every supported file in a directory.
///
/// ## Formal signature
/// ```text
/// load_data_dir(dir) ≜
/// let stems = sort { stem(f) | f ∈ dir, ext(f) ∈ {md,mdx,json,toml}, ¬ hidden(f) }
/// in map (λ s. let f = first_existing(s, {mdx, md, json, toml}) in
/// { slug: s } ∪ load_data_file(f)) stems
/// ```
///
/// Files starting with `.` or `_` are excluded. Only one extension per stem
/// is loaded, preferring `mdx` > `md` > `json` > `toml`.
///
/// This is a *co-inductive fold* over the filesystem: for each slug we
/// construct a record whose fields are the parsed frontmatter/JSON/TOML
/// plus the `slug` field injected by the fold.
///
/// ## References
///
/// The fold pattern follows **Meijer, Fokkinga, Paterson** (1991).
/// "Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire".