//! Minimal HTML shell for assembled pages / parts.
use serde_json::{json, Map, Value};
use crate::db;
use crate::markdown;
use crate::table::{
as_bind, as_nav_rows, nav_label_href, nav_media_when_class, 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))
}
}
}
#[derive(Clone, Debug)]
struct NavLink {
label: String,
href: String,
class: String,
media: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NavWhen {
Always,
Hide,
Auth,
Guest,
}
fn parse_nav_when(raw: &str) -> NavWhen {
let t = raw.trim();
if t.is_empty()
|| matches!(
t,
"*" | "always" | "Always" | "真" | "yes" | "on" | "show" | "all"
)
{
return NavWhen::Always;
}
if matches!(
t,
"hide" | "never" | "off" | "no" | "假" | "否" | "0" | "false" | "False"
) {
return NavWhen::Hide;
}
if matches!(
t,
"auth" | "user" | "登录" | "已登录" | "logged_in" | "logged-in"
) {
return NavWhen::Auth;
}
if matches!(
t,
"guest" | "anon" | "anonymous" | "访客" | "匿名" | "未登录"
) {
return NavWhen::Guest;
}
// Unknown token: keep visible (forward-compatible).
NavWhen::Always
}
/// `Some(true)` logged in, `Some(false)` guest, `None` unknown (do not filter auth/guest).
fn page_auth_state(page: Option<&Value>) -> Option {
let page = page?;
if let Some(v) = page
.get("_nav_user")
.or_else(|| page.get("user"))
.or_else(|| page.get("username"))
.or_else(|| page.get("_user"))
{
match v {
Value::Null => Some(false),
Value::Bool(b) => Some(*b),
Value::String(s) => Some(!s.trim().is_empty()),
Value::Number(n) => Some(n.as_i64().unwrap_or(0) != 0),
_ => Some(true),
}
} else if let Some(v) = page.get("_logged_in").or_else(|| page.get("logged_in")) {
match v {
Value::Bool(b) => Some(*b),
Value::String(s) => Some(matches!(
s.trim(),
"1" | "true" | "True" | "yes" | "on" | "真" | "是"
)),
Value::Number(n) => Some(n.as_i64().unwrap_or(0) != 0),
_ => None,
}
} else {
None
}
}
fn nav_when_visible(when: NavWhen, auth: Option) -> bool {
match when {
NavWhen::Always => true,
NavWhen::Hide => false,
NavWhen::Auth => auth.unwrap_or(true), // unknown → keep (compat)
NavWhen::Guest => match auth {
Some(logged_in) => !logged_in,
None => true,
},
}
}
fn resolve_links(raw: Option<&Value>, db_url: Option<&str>, page: Option<&Value>) -> Vec {
let Some(raw) = raw else {
return Vec::new();
};
let auth = page_auth_state(page);
let binds = as_bind(raw);
let arr = binds.as_array().cloned().unwrap_or_default();
let rows: Vec