statica 0.32.1

statica — Powered HTML pipeline (parse, funnel, bind, scope, emit)
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Local funnel sources via `<link rel="statica/data" href id>`.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use serde_json::Value;

use crate::aliases::{self, AliasOptions};
use crate::content;
use crate::context::CanonicalRoot;
use crate::error::{Error, Result};
use crate::i18n;
use crate::parse::escape_text;
use crate::parse::{Document, Element, Node};

use std::path::Component;

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct DataSource {
    pub id: String,
    pub kind: content::DataKind,
    pub path: PathBuf,
    pub data: Arc<content::DataSet>,
}

impl DataSource {
    #[must_use]
    pub fn value(&self) -> Value {
        self.data.to_value()
    }

    #[must_use]
    pub fn array(&self) -> Option<Vec<Value>> {
        self.data.as_array()
    }
}

pub fn document_has_locale_data(doc: &Document) -> bool {
    doc.find(is_data_link).into_iter().any(|el| {
        el.attr("href")
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .is_some_and(i18n::src_has_locale_token)
    })
}

/// Whether a specific funnel `<link rel="statica/data" id="…">` uses `${locale}` in `href`.
pub fn data_link_has_locale_token(doc: &Document, id: &str) -> bool {
    doc.find(is_data_link).into_iter().any(|el| {
        el.attr("id") == Some(id)
            && el
                .attr("href")
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .is_some_and(i18n::src_has_locale_token)
    })
}

pub fn load_data_from_document(
    doc: &Document,
    site_root: &Path,
    page_dir: &Path,
    cache: &mut HashMap<PathBuf, Arc<content::DataSet>>,
    aliases: &AliasOptions,
    site: Option<(&str, &str)>,
) -> Result<HashMap<String, DataSource>> {
    load_data_links(
        doc,
        site_root,
        page_dir,
        cache,
        aliases,
        site,
        DataLinkFilter::WithoutLocaleToken,
    )
}

/// Load funnel sources whose `href` contains `${locale}` for the active locale.
pub fn load_locale_data_from_document(
    doc: &Document,
    site_root: &Path,
    page_dir: &Path,
    cache: &mut HashMap<PathBuf, Arc<content::DataSet>>,
    aliases: &AliasOptions,
    locale: &str,
    site: Option<(&str, &str)>,
) -> Result<HashMap<String, DataSource>> {
    load_data_links(
        doc,
        site_root,
        page_dir,
        cache,
        aliases,
        site,
        DataLinkFilter::WithLocaleTokenOnly { locale },
    )
}

#[derive(Clone, Copy)]
enum DataLinkFilter<'a> {
    WithoutLocaleToken,
    WithLocaleTokenOnly { locale: &'a str },
}

fn load_data_links(
    doc: &Document,
    site_root: &Path,
    page_dir: &Path,
    cache: &mut HashMap<PathBuf, Arc<content::DataSet>>,
    aliases: &AliasOptions,
    site: Option<(&str, &str)>,
    filter: DataLinkFilter<'_>,
) -> Result<HashMap<String, DataSource>> {
    let mut out = HashMap::new();
    for el in doc.find(is_data_link) {
        let id = match el.attr("id").map(str::trim).filter(|s| !s.is_empty()) {
            Some(id) => id.to_string(),
            None => {
                return Err(site_err(
                    site,
                    &["rel=\"statica/data\"", "rel='statica/data'"],
                    "statica/data link missing id",
                ));
            }
        };
        if CanonicalRoot::from_str(&id).is_some() {
            let id_dq = format!("id=\"{id}\"");
            let id_sq = format!("id='{id}'");
            return Err(site_err(
                site,
                &[&id_dq, &id_sq],
                format!(
                    "statica/data id `{id}` conflicts with canonical page context — rename this data source"
                ),
            ));
        }
        let Some(href) = el.attr("href").map(str::trim).filter(|s| !s.is_empty()) else {
            let id_dq = format!("id=\"{id}\"");
            return Err(site_err(
                site,
                &["rel=\"statica/data\"", id_dq.as_str()],
                format!("statica/data#{id} missing href"),
            ));
        };
        let href = aliases::resolve_path(href, aliases, site, "href")?;
        let has_locale_token = i18n::src_has_locale_token(&href);
        match filter {
            DataLinkFilter::WithoutLocaleToken if has_locale_token => continue,
            DataLinkFilter::WithLocaleTokenOnly { .. } if !has_locale_token => continue,
            _ => {}
        }
        let href = match filter {
            DataLinkFilter::WithLocaleTokenOnly { locale } => {
                i18n::interpolate_locale(&href, locale)
            }
            DataLinkFilter::WithoutLocaleToken => href,
        };
        let explicit_kind = match el
            .attr("type")
            .map(str::trim)
            .filter(|value| !value.is_empty())
        {
            Some(raw) => Some(content::DataKind::from_type_attr(raw).ok_or_else(|| {
                let type_dq = format!("type=\"{raw}\"");
                let type_sq = format!("type='{raw}'");
                site_err(
                    site,
                    &[&type_dq, &type_sq, raw],
                    format!("unsupported statica/data type `{raw}`"),
                )
            })?),
            None => None,
        };
        let cache_key = content_cache_key(site_root, page_dir, &href);
        let (kind, data) = if explicit_kind.is_none() {
            if let Some(data) = cache.get(&cache_key) {
                (inferred_data_kind(&href), Arc::clone(data))
            } else {
                let parsed = load_link_content(site_root, page_dir, &href, explicit_kind, site)?;
                let data = Arc::new(parsed.data);
                cache.insert(cache_key.clone(), Arc::clone(&data));
                (parsed.kind, data)
            }
        } else {
            // `type` changes how bytes are parsed, so explicit links stay out of the path cache.
            let parsed = load_link_content(site_root, page_dir, &href, explicit_kind, site)?;
            (parsed.kind, Arc::new(parsed.data))
        };
        out.insert(
            id.clone(),
            DataSource {
                id,
                kind,
                path: cache_key,
                data,
            },
        );
    }
    Ok(out)
}

fn load_link_content(
    site_root: &Path,
    page_dir: &Path,
    href: &str,
    explicit_kind: Option<content::DataKind>,
    site: Option<(&str, &str)>,
) -> Result<content::LoadedContent> {
    content::load_content(site_root, page_dir, href, explicit_kind).map_err(|e| match site {
        Some((file, source)) => {
            let href_dq = format!("href=\"{href}\"");
            let href_sq = format!("href='{href}'");
            Error::at(file, source, &[&href_dq, &href_sq, href], e.to_string())
        }
        None => e,
    })
}

fn inferred_data_kind(href: &str) -> content::DataKind {
    if content::is_glob_href(href) {
        content::DataKind::Glob
    } else {
        content::DataKind::from_path(Path::new(href)).unwrap_or(content::DataKind::Json)
    }
}

fn site_err(site: Option<(&str, &str)>, needles: &[&str], message: impl Into<String>) -> Error {
    match site {
        Some((file, source)) => Error::at(file, source, needles, message),
        None => Error::at_file("<unknown>", message),
    }
}

fn is_data_link(el: &Element) -> bool {
    el.is_link()
        && el
            .attr("rel")
            .is_some_and(|r| r.split_whitespace().any(|p| p == "statica/data"))
}

/// Funnel `<link rel="statica/data" id="…">` ids declared on a page.
#[must_use]
pub fn data_link_ids(doc: &Document) -> Vec<String> {
    doc.find(is_data_link)
        .into_iter()
        .filter_map(|el| el.attr("id").map(str::trim).filter(|s| !s.is_empty()))
        .map(str::to_string)
        .collect()
}

/// Look up a field, distinguishing missing (`None`) from present `null` (`Some(Null)`).
/// Non-objects yield `None` (undefined) — only objects have enumerable own properties.
pub fn read_field<'a>(value: &'a Value, field: &str) -> Option<&'a Value> {
    match value {
        Value::Object(map) => map.get(field),
        _ => None,
    }
}

/// Render a bound value for attributes / text. `null` → empty string.
/// Objects and arrays are not stringified into attrs (empty); use slots for structure.
/// Returns `None` only for object/array (not valid attr scalars).
fn value_as_str(value: &Value) -> Option<String> {
    match value {
        Value::Null => Some(String::new()),
        Value::String(s) => Some(s.clone()),
        Value::Number(n) => Some(n.to_string()),
        Value::Bool(b) => Some(b.to_string()),
        Value::Array(_) | Value::Object(_) => None,
    }
}

pub fn field_as_str(value: &Value, field: &str) -> Option<String> {
    // Route / feed keys: missing and null are absent (not empty strings).
    match read_field(value, field) {
        None | Some(Value::Null) => None,
        Some(v) => value_as_str(v),
    }
}

/// Resolve a dotted path against a bind context object.
#[must_use]
pub fn path_value<'a>(value: &'a Value, path: &str) -> Option<&'a Value> {
    let path = path.trim();
    if path.is_empty() {
        return None;
    }
    let mut cur = value;
    for part in path.split('.').filter(|p| !p.is_empty()) {
        cur = read_field(cur, part)?;
    }
    Some(cur)
}

/// Resolve `${path}` for attributes. Missing / null → empty (scope is checked statically).
pub fn path_as_str(value: &Value, path: &str) -> String {
    let path = path.trim();
    if path.is_empty() {
        return String::new();
    }
    match path_value(value, path) {
        None | Some(Value::Null) => String::new(),
        Some(v) => value_as_str(v).unwrap_or_default(),
    }
}

pub fn value_to_html(value: &Value) -> String {
    match value {
        Value::String(s) => {
            if s.contains('<') && s.contains('>') {
                s.clone()
            } else {
                escape_text(s)
            }
        }
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Null | Value::Array(_) | Value::Object(_) => String::new(),
    }
}

pub fn resolve_expr(
    expr: &str,
    current: Option<&Value>,
    local_data: &HashMap<String, DataSource>,
    parent_data: &HashMap<String, DataSource>,
) -> Result<Value> {
    let expr = expr.trim();
    if expr.is_empty() {
        return Ok(Value::Null);
    }
    if expr == "." {
        return Ok(current.cloned().unwrap_or(Value::Null));
    }
    let mut parts = expr.split('.').filter(|p| !p.is_empty());
    let first = parts
        .next()
        .ok_or_else(|| Error::at_file("<data>", "empty data expression"))?;

    let mut value = if first == "this" {
        current.cloned().unwrap_or(Value::Null)
    } else if let Some(ds) = local_data.get(first) {
        ds.value()
    } else if let Some(ds) = parent_data.get(first) {
        ds.value()
    } else if let Some(cur) = current {
        match read_field(cur, first) {
            Some(v) => v.clone(),
            None => {
                return Err(Error::at_file(
                    "<data>",
                    format!(
                        "missing data source id `{first}` (no <link rel=\"statica/data\" id=\"{first}\">)"
                    ),
                ))
            }
        }
    } else {
        return Err(Error::at_file(
            "<data>",
            format!(
                "missing data source id `{first}` (no <link rel=\"statica/data\" id=\"{first}\">)"
            ),
        ));
    };

    for part in parts {
        value = match read_field(&value, part) {
            // missing → undefined → null in the funnel (renders empty)
            Some(v) => v.clone(),
            None => Value::Null,
        };
    }
    Ok(value)
}

fn content_cache_key(site_root: &Path, page_dir: &Path, src: &str) -> PathBuf {
    if Path::new(src).is_absolute() {
        normalize(&PathBuf::from(src))
    } else {
        normalize(&aliases::resolve_local_href(site_root, page_dir, src))
    }
}

fn normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for c in path.components() {
        match c {
            Component::ParentDir => {
                out.pop();
            }
            Component::CurDir => {}
            other => out.push(other.as_os_str()),
        }
    }
    out
}

/// Collect fragment link declarations from a document.
pub fn find_fragment_links(doc: &Document) -> Vec<(String, String)> {
    doc.find(|e| {
        e.is_link()
            && e.attr("rel")
                .is_some_and(|r| r.split_whitespace().any(|p| p == "statica/fragment"))
    })
    .into_iter()
    .filter_map(|el| Some((el.attr("id")?.to_string(), el.attr("href")?.to_string())))
    .collect()
}

/// Find `<template id=…>` element.
pub fn find_template<'a>(doc: &'a Document, id: &str) -> Option<&'a Element> {
    doc.find(|e| e.is_template() && e.attr("id") == Some(id))
        .into_iter()
        .next()
}

/// Strip statica authoring tags so output is browser-valid HTML.
pub fn strip_authoring(doc: &mut Document) {
    strip_nodes(&mut doc.children);
    for child in &mut doc.children {
        if let Node::Element(el) = child {
            if el.name.eq_ignore_ascii_case("html") {
                el.attrs.shift_remove("data-bind");
            }
        }
    }
}

fn strip_nodes(nodes: &mut Vec<Node>) {
    nodes.retain(|n| match n {
        Node::Element(el) => {
            if is_data_link(el) {
                return false;
            }
            if el.is_link()
                && el
                    .attr("rel")
                    .is_some_and(|r| r.split_whitespace().any(|p| p == "statica/fragment"))
            {
                return false;
            }
            true
        }
        _ => true,
    });
    for n in nodes.iter_mut() {
        if let Node::Element(el) = n {
            strip_nodes(&mut el.children);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn path_as_str_is_lenient_at_runtime() {
        let ctx = json!({"href": null, "variant": "primary"});
        assert_eq!(path_as_str(&ctx, "variant"), "primary");
        assert_eq!(path_as_str(&ctx, "href"), "");
        assert_eq!(path_as_str(&ctx, "missing"), "");
        assert_eq!(path_as_str(&json!({"obj": {"a": 1}}), "obj"), "");
    }

    #[test]
    fn value_reads_validate_js_types() {
        assert_eq!(value_as_str(&Value::Null).as_deref(), Some(""));
        assert_eq!(value_as_str(&json!("a")).as_deref(), Some("a"));
        assert_eq!(value_as_str(&json!(1)).as_deref(), Some("1"));
        assert_eq!(value_as_str(&json!(false)).as_deref(), Some("false"));
        assert!(value_as_str(&json!({})).is_none());
        assert!(value_as_str(&json!([])).is_none());
        assert_eq!(value_to_html(&Value::Null), "");
        assert_eq!(value_to_html(&json!({"a": 1})), "");
        // field_as_str keeps null/missing as absent for route keys
        assert!(field_as_str(&json!({"slug": null}), "slug").is_none());
        assert!(field_as_str(&json!({}), "slug").is_none());
        assert_eq!(
            field_as_str(&json!({"slug": "a"}), "slug").as_deref(),
            Some("a")
        );
    }
}