use tramlines_core::frontmatter::Host as FrontmatterHost;
use tramlines_md::{
check::{Host, ScaffoldFront, Subject},
markdown::Dialect,
};
use crate::{
grammar::{Dispatch, SokfSchema, States},
markdown::SOKF,
model::frontmatter::CORE_KEYS,
};
pub struct SokfScaffold<'a> {
schema: &'a SokfSchema,
}
impl<'a> SokfScaffold<'a> {
pub fn new(schema: &'a SokfSchema) -> Self {
SokfScaffold { schema }
}
}
fn slugified(type_name: &str) -> String {
let mut out = String::new();
for (i, ch) in type_name.chars().enumerate() {
if ch.is_uppercase() && i > 0 {
out.push('-');
}
out.extend(ch.to_lowercase());
}
if out.is_empty() {
"document".into()
} else {
out
}
}
fn guidance_comment(text: &str) -> String {
let one: Vec<&str> = text.split_whitespace().collect();
format!("# {} {}", SOKF.spell("guidance"), one.join(" "))
}
impl FrontmatterHost for SokfScaffold<'_> {
fn scalar_format(&self, _: &str, _: &str) -> Option<Result<(), String>> {
None
}
fn reserved_keys(&self) -> &[&str] {
CORE_KEYS
}
}
impl Host for SokfScaffold<'_> {
fn dialect(&self) -> Dialect {
SOKF
}
fn image(&self, _: &str) -> Subject {
Subject::Unknown
}
fn region(&self, _: &str) -> Subject {
Subject::Unknown
}
fn scaffold_front(&self, states: &States) -> Option<ScaffoldFront> {
let Dispatch::Type(type_name) = &self.schema.dispatch else {
return None;
};
let template = self.schema.core.core.id_template();
let id = match template {
Some(t) => t
.render_with(1, t.has_slug().then_some("example"), states)
.unwrap_or_else(|_| slugified(type_name)),
None => format!("{}-example", slugified(type_name)),
};
let slug = match template {
Some(t) if t.has_slug() => "example".to_string(),
_ => id.clone(),
};
let title = crate::scaffold::derived_title(&slug);
let mut trailing = Vec::new();
let edges: Vec<_> = self
.schema
.links
.iter()
.filter(|r| r.common.only.admits(states))
.collect();
if !edges.is_empty() {
trailing.push("links:".to_string());
for rule in edges {
for _ in 0..rule.min.max(1) {
if let Some(g) = &rule.guidance {
trailing.push(format!(" {}", guidance_comment(g)));
}
trailing.push(format!(" - rel: {}", rule.rel));
trailing.push(" to: target-id".to_string());
}
}
}
Some(ScaffoldFront {
leading: vec![
("type".into(), Some(type_name.clone())),
("id".into(), Some(id)),
("title".into(), Some(title)),
],
trailing,
})
}
}