//! Minimal HTML shell for assembled pages / parts. use serde_json::{json, Map, Value}; use crate::db; use crate::table::{as_bind, normalize_ref, normalize_slot, parse_site_path, project_rows, SitePath}; fn esc(s: &str) -> String { s.replace('&', "&") .replace('<', "<") .replace('>', ">") .replace('"', """) } fn text(v: &Value) -> String { match v { Value::Null => String::new(), Value::String(s) => s.clone(), Value::Bool(b) => b.to_string(), Value::Number(n) => n.to_string(), other => other.to_string(), } } fn class_attr(css: &str) -> String { if css.is_empty() { String::new() } else { format!(" class=\"{}\"", esc(css)) } } fn is_simple_ident(s: &str) -> bool { !s.is_empty() && s.chars() .all(|c| c.is_ascii_alphanumeric() || c == '_' || !c.is_ascii()) } /// True when `back` is a DB field path (`table.col` / `mod.table.col`), not a URL or site path. fn is_db_bind_back(back: &str) -> bool { let s = normalize_ref(back); if s.is_empty() || s.contains("://") || s.starts_with('/') || s.starts_with('#') || s.starts_with('?') { return false; } match parse_site_path(&s) { SitePath::DbField { .. } => true, SitePath::LibMember { lib, member } => is_simple_ident(&lib) && is_simple_ident(&member), SitePath::Plain(p) => { let parts: Vec<&str> = p.split('.').filter(|x| !x.is_empty()).collect(); parts.len() == 2 && parts.iter().all(|x| is_simple_ident(x)) } } } fn resolve_links(raw: Option<&Value>, db_url: Option<&str>) -> Vec<(String, String)> { let Some(raw) = raw else { return Vec::new(); }; let binds = as_bind(raw); let arr = binds.as_array().cloned().unwrap_or_default(); if arr.is_empty() { return Vec::new(); } let has_db = arr .iter() .any(|b| b.get("back").and_then(|v| v.as_str()).is_some_and(is_db_bind_back)); if has_db { let Some(url) = db_url else { return Vec::new(); }; let table = crate::table::bind_table_name(&arr); let Some(table) = table else { return Vec::new(); }; let data = select_page_data(url, &table, raw, 200); let rows = data .get("rows") .and_then(|v| v.as_array()) .cloned() .unwrap_or_default(); let projected = project_rows(&arr, &rows); let mut out = Vec::new(); for it in projected.as_array().cloned().unwrap_or_default() { let m = it.as_object().cloned().unwrap_or_default(); let label = m .get("title") .or_else(|| m.get("label")) .map(text) .unwrap_or_default(); let href = m .get("href") .or_else(|| m.get("path")) .map(text) .unwrap_or_else(|| "#".into()); if !label.is_empty() { out.push((label, href)); } } return out; } // static: front=label back=href (URLs with `.` must stay here) arr.into_iter() .map(|b| { let label = b.get("front").map(text).unwrap_or_default(); let href = b.get("back").map(text).unwrap_or_else(|| "#".into()); (label, href) }) .filter(|(l, _)| !l.is_empty()) .collect() } fn render_ul(links: &[(String, String)], class: &str) -> String { let mut s = format!("