use tramlines_core::frontmatter::Host as FrontmatterHost;
use tramlines_md::{
check::{Host, LabelFacts, LimitFacts, ScaffoldFront, Subject},
markdown::Dialect,
};
use super::Words;
use crate::{
grammar::{Dispatch, LimitKind, LinkRule, SokfSchema, States, squash},
markdown::SOKF,
model::frontmatter::CORE_KEYS,
};
pub struct SokfScaffold<'a> {
schema: &'a SokfSchema,
words: Option<&'a Words<'a>>,
skeleton: bool,
}
impl<'a> SokfScaffold<'a> {
pub fn new(schema: &'a SokfSchema) -> Self {
SokfScaffold {
schema,
words: None,
skeleton: false,
}
}
pub fn with_words(schema: &'a SokfSchema, words: &'a Words<'a>) -> Self {
SokfScaffold {
schema,
words: Some(words),
skeleton: false,
}
}
pub fn for_skeleton(schema: &'a SokfSchema, words: &'a Words<'a>) -> Self {
SokfScaffold {
schema,
words: Some(words),
skeleton: true,
}
}
}
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
}
}
pub(super) fn guidance_comment(text: &str) -> String {
let one: Vec<&str> = text.split_whitespace().collect();
format!("# {} {}", SOKF.spell("guidance"), one.join(" "))
}
pub fn link_text(rule: &LinkRule, words: Option<&Words<'_>>) -> Option<String> {
let limit = rule.max.iter().flat_map(|l| {
let lead = words
.and_then(|w| w.link_lead(l, &rule.rel))
.filter(|w| !squash(w).is_empty())
.unwrap_or_else(|| {
let kind = match l.kind {
LimitKind::Soft => "soft",
LimitKind::Hard => "hard",
};
format!("`{kind}: {}`, `when-at-limit`:", l.count)
});
[lead, l.when_at_limit.clone()]
});
let words: Vec<String> = rule
.guidance
.iter()
.cloned()
.chain(limit)
.map(|t| squash(&t))
.filter(|t| !t.is_empty())
.collect();
(!words.is_empty()).then(|| words.join(" "))
}
pub fn link_example(rule: &LinkRule) -> String {
format!("links:\n - rel: {}\n to: target-id", rule.rel)
}
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 limit_lead(&self, facts: &LimitFacts<'_>) -> Option<String> {
self.words?.limit_lead(facts)
}
fn label_words(&self, facts: &LabelFacts<'_>) -> Option<String> {
self.words?.label(facts.key)
}
fn scaffold_label(&self, _labels: &[String]) -> Option<String> {
Some("agent".to_string())
}
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.written_links(states).collect();
if self.skeleton {
if edges.iter().any(|r| r.min > 0) {
trailing.push("links:".to_string());
}
} else if !edges.is_empty() {
trailing.push("links:".to_string());
for rule in edges {
for _ in 0..rule.min.max(1) {
if let Some(g) = link_text(rule, self.words) {
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,
})
}
}