use std::collections::HashSet;
use std::sync::LazyLock;
use serde_json::{Map, Value};
use crate::inline::{InlineToken, TokenType, inline_tokens};
use crate::jcs::canonical_json;
use crate::js::{JS_DOT, JS_SPACES, js_value, utf16_cmp};
use crate::pattern::Pattern;
use crate::read::Reading;
use crate::registry::{self, PRESENTATION_ATTRS, is_rule, typed_reference};
use crate::resolve::Provenance;
use crate::types::{Attrs, Document, Identity, Node, Object, Parameter};
pub type Known<'a> = &'a dyn Fn(&str, &str) -> bool;
#[derive(Clone, Default)]
pub struct TextOptions<'a> {
pub params: Option<Object>,
pub parameters: Vec<Parameter>,
pub use_prose: bool,
pub identities: Option<Vec<Identity>>,
pub known: Option<Known<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ReferenceRecord {
pub key: String,
pub kind: String,
pub label: String,
pub target: String,
pub status: String,
pub data: Option<Value>,
pub source: String,
pub sha256: String,
}
impl ReferenceRecord {
pub fn to_json(&self) -> Value {
let mut m = Map::new();
m.insert("key".into(), self.key.clone().into());
m.insert("kind".into(), self.kind.clone().into());
m.insert("label".into(), self.label.clone().into());
m.insert("target".into(), self.target.clone().into());
m.insert("status".into(), self.status.clone().into());
if let Some(d) = &self.data {
m.insert("data".into(), d.clone());
}
if !self.source.is_empty() {
m.insert("source".into(), self.source.clone().into());
}
if !self.sha256.is_empty() {
m.insert("sha256".into(), self.sha256.clone().into());
}
Value::Object(m)
}
}
struct Rx {
param_ref: Pattern,
reference: Pattern,
ticks: Pattern,
}
static RX: LazyLock<Rx> = LazyLock::new(|| Rx {
param_ref: Pattern::must(r"\$\{([A-Za-z_][A-Za-z0-9_.-]*)\}"),
reference: Pattern::must(&format!(r#"(?:^|[ \t])ref=(?:"((?:[^"\\]|\\{JS_DOT})*)"|([^{JS_SPACES}{{}}"]+))"#)),
ticks: Pattern::must("`+"),
});
fn rule_label(kind: &str) -> &'static str {
match kind {
"must" | "always" => "MUST",
"never" => "NEVER",
"should" => "SHOULD",
"avoid" => "SHOULD NOT",
"may" => "MAY",
"guardrail" => "GUARDRAIL",
_ => "",
}
}
fn heading_label(kind: &str) -> Option<&'static str> {
match kind {
"policy" => Some("Policy"),
"instruction" => Some("Instruction"),
"context" => Some("Context"),
_ => None,
}
}
const UNIT_EXCLUDED: &[&str] = &["id", "title", "class", "style"];
const RULE_EXCLUDED: &[&str] = &["id", "if", "because", "not", "class", "style"];
pub(crate) const PATCHES: &[&str] = &["replace", "extend", "remove"];
pub fn to_text(doc: &Document, opts: &TextOptions) -> String {
text_of(&doc.body, &[], &[], opts)
}
impl Reading {
pub fn text(&self, params: Option<&Object>) -> String {
to_text(
&self.document,
&TextOptions {
params: params.cloned(),
parameters: self.parameters.clone(),
use_prose: true,
identities: Some(self.identities.clone()),
known: None,
},
)
}
}
struct Ctx<'o, 'a> {
o: &'o TextOptions<'a>,
identities: Option<HashSet<String>>,
}
pub(crate) fn text_of(body: &[Node], records: &[ReferenceRecord], provenance: &[Provenance], o: &TextOptions) -> String {
let ctx = Ctx { o, identities: o.identities.as_ref().map(|ids| ids.iter().map(|i| i.id.clone()).collect()) };
let mut text = units(body, &ctx).join("\n\n");
if !records.is_empty() {
let mut sorted: Vec<&ReferenceRecord> = records.iter().collect();
sorted.sort_by(|a, b| utf16_cmp(&a.key, &b.key));
let fenced: Vec<String> = sorted.iter().map(|r| fence(&canonical_json(&r.to_json()), "json")).collect();
text.push_str("\n\n### Resolved references\n\n");
text.push_str(&fenced.join("\n\n"));
}
let changes: Vec<String> = provenance
.iter()
.filter(|p| PATCHES.contains(&p.operation.as_str()))
.map(|p| {
let mut change = format!("- {} {} (origin: {})", p.operation, p.target, p.uri);
if !p.reason.is_empty() {
change.push_str(" — ");
change.push_str(&p.reason);
}
change
})
.collect();
if !changes.is_empty() {
text.push_str("\n\n### Composition changes\n\n");
text.push_str(&changes.join("\n"));
}
format!("{}\n", text.trim_end_matches('\n'))
}
fn units(nodes: &[Node], c: &Ctx) -> Vec<String> {
nodes.iter().map(|n| if n.is_markdown() { markdown_unit(n, c) } else { directive_unit(n, c) }).filter(|u| !u.is_empty()).collect()
}
fn markdown_unit(n: &Node, c: &Ctx) -> String {
if !c.o.use_prose || n.prose {
let instance = if n.instance.is_empty() { "root" } else { &n.instance };
return trim_lf(&normalize(&n.source, c, instance)).to_string();
}
trim_lf(&n.source).to_string()
}
pub(crate) fn literal(n: &Node) -> bool {
n.kind == "example" || n.attrs.get("verbatim") == Some(&Value::Bool(true)) || registry::kind(&n.kind).is_none()
}
fn raw_children(n: &Node) -> String {
let s: String = n.children().iter().filter(|c| c.is_markdown()).map(|c| c.source.as_str()).collect();
trim_lf(&s).to_string()
}
fn directive_unit(n: &Node, c: &Ctx) -> String {
let a = &n.attrs;
let children = || if literal(n) { raw_children(n) } else { units(n.children(), c).join("\n\n") };
if is_rule(&n.kind) {
let mut label = rule_label(&n.kind).to_string();
if n.kind == "should" && a.get("not") == Some(&Value::Bool(true)) {
label = "SHOULD NOT".into();
}
if let Some(Value::String(cond)) = a.get("if") {
label = format!("{label} (if {})", interpolate(cond, c.o));
}
let mut t = format!("{label}: {}", children());
if let Some(Value::String(because)) = a.get("because") {
t.push_str("\nBECAUSE: ");
t.push_str(&interpolate(because, c.o));
}
let rest = attribute_line(a, RULE_EXCLUDED, &[]);
if !rest.is_empty() {
t.push('\n');
t.push_str(&rest);
}
return t;
}
let label = if n.kind == "example" && a.get("avoid") == Some(&Value::Bool(true)) {
"Counterexample".to_string()
} else if n.kind == "example" {
"Example".to_string()
} else if let Some(h) = heading_label(&n.kind) {
h.to_string()
} else {
n.kind.clone()
};
let title = match (a.get("title"), a.get("id")) {
(Some(Value::String(t)), _) if !t.is_empty() => t.clone(),
(_, Some(Value::String(id))) => id.clone(),
_ => n.kind.clone(),
};
let mut parts = vec![format!("### {label}: {title}")];
let more: &[&str] = if n.kind == "example" {
&["avoid"]
} else if registry::kind(&n.kind).is_some_and(|k| k.family == "layout") {
PRESENTATION_ATTRS
} else {
&[]
};
let line = attribute_line(a, UNIT_EXCLUDED, more);
if !line.is_empty() {
parts.push(line);
}
if let Some(v) = &n.value {
parts.push(fence(&v.source, &v.format));
} else if n.kind == "example" {
parts.push(fence(&raw_children(n), "markdown"));
} else {
let c = children();
if !c.is_empty() {
parts.push(c);
}
}
parts.join("\n\n")
}
fn attribute_line(a: &Attrs, excluded: &[&str], more: &[&str]) -> String {
let kept: Map<String, Value> = a
.iter()
.filter(|(k, _)| !excluded.contains(&k.as_str()) && !more.contains(&k.as_str()))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
if kept.is_empty() {
return String::new();
}
format!("Attributes: {}", canonical_json(&Value::Object(kept)))
}
fn fence(text: &str, lang: &str) -> String {
let longest = RX.ticks.find_iter(text).iter().map(|(a, b)| b - a).max().unwrap_or(0);
let marker = "`".repeat(3.max(longest + 1));
let body = if text.ends_with('\n') { text.to_string() } else { format!("{text}\n") };
format!("{marker}{lang}\n{body}{marker}")
}
fn trim_lf(s: &str) -> &str {
s.trim_matches('\n')
}
fn normalize(source: &str, c: &Ctx, instance: &str) -> String {
let tokens = inline_tokens(source);
rewrite(source, 0, source.len(), &tokens, None, c, instance)
}
fn rewrite(source: &str, from: usize, to: usize, tokens: &[InlineToken], skip: Option<usize>, c: &Ctx, instance: &str) -> String {
let mut out = String::new();
let mut at = from;
for (i, t) in tokens.iter().enumerate() {
if Some(i) == skip || t.start < at || t.end > to {
continue;
}
out.push_str(&source[at..t.start]);
out.push_str(&token_text(source, tokens, i, c, instance));
at = t.end;
}
out.push_str(&source[at..to]);
out
}
fn token_text(source: &str, tokens: &[InlineToken], i: usize, c: &Ctx, instance: &str) -> String {
let t = &tokens[i];
let written = &source[t.start..t.end];
match t.token_type {
TokenType::Param => return param_text(&t.name, c.o).unwrap_or_else(|| written.to_string()),
TokenType::Directive => {}
_ => return written.to_string(),
}
let label = rewrite(source, t.label_start, t.label_end, tokens, Some(i), c, instance);
if typed_reference(&t.kind) || t.kind == "ref" || t.kind == "cite" {
let Some(target) = ref_of(t.attributes.as_deref()) else {
return written.to_string();
};
let mut text = format!("{label} ({}; {target})", t.kind);
if unresolved_target(&target, c, instance) {
text.push_str(" [unresolved]");
}
return text;
}
label
}
fn unresolved_target(target: &str, c: &Ctx, instance: &str) -> bool {
let Some(rest) = target.strip_prefix('#') else { return false };
let path: Vec<&str> = rest.split('/').collect();
let id = path[path.len() - 1];
let path = &path[..path.len() - 1];
if let Some(known) = c.o.known {
let mut within = instance.to_string();
if !path.is_empty() {
within.push('/');
within.push_str(&path.join("/"));
}
return !known(&within, id);
}
c.identities.as_ref().is_some_and(|ids| path.is_empty() && !ids.contains(id))
}
fn ref_of(attributes: Option<&str>) -> Option<String> {
let attributes = attributes.filter(|a| !a.is_empty())?;
let m = RX.reference.captures(attributes)?;
if let Some(quoted) = m.get(attributes, 1) {
return serde_json::from_str::<String>(&format!("\"{quoted}\"")).ok();
}
m.get(attributes, 2).map(str::to_string)
}
fn param_text(name: &str, o: &TextOptions) -> Option<String> {
if let Some(v) = o.params.as_ref().and_then(|p| p.get(name)) {
return value_text(v);
}
o.parameters.iter().find(|p| p.name == name).and_then(|p| p.default.as_ref()).and_then(value_text)
}
pub fn value_text(value: &Value) -> Option<String> {
match value {
Value::Null => None,
Value::Array(list) => Some(list.iter().map(|x| escape_value(&js_value(Some(x)))).collect::<Vec<_>>().join(", ")),
Value::String(s) => Some(escape_value(s)),
v => Some(canonical_json(v)),
}
}
fn interpolate(s: &str, o: &TextOptions) -> String {
RX.param_ref.replace_all(s, |m| {
let whole = m.get(s, 0).unwrap_or("");
let name = m.get(s, 1).unwrap_or("");
param_text(name, o).unwrap_or_else(|| whole.to_string())
})
}
pub(crate) fn interpolate_values(s: &str, values: &Object) -> String {
RX.param_ref.replace_all(s, |m| {
let whole = m.get(s, 0).unwrap_or("");
let name = m.get(s, 1).unwrap_or("");
values.get(name).and_then(value_text).unwrap_or_else(|| whole.to_string())
})
}
fn escape_value(v: &str) -> String {
let mut out = String::with_capacity(v.len());
for c in v.chars() {
if "\\`*_{}[]<>|~$".contains(c) {
out.push('\\');
}
out.push(c);
}
out
}