use super::{Checker, Repair, label::Read, short};
use crate::{
grammar::{Block as Rule, Shape, Text, Unit as UnitRule, squash, task_box},
markdown::{BlockKind, Unit},
};
fn expected_text(c: &Checker<'_>, text: &Text) -> Option<String> {
c.text_of(text).map(|s| squash(&s))
}
fn repair_prefix(c: &Checker<'_>, unit: &Unit) -> Option<String> {
let raw = c.doc.text.lines().nth(unit.line.checked_sub(1)?)?;
let clean = c.doc.body.clean_line(unit.line, raw);
let line: &str = &clean;
if unit.text.is_empty() {
return Some(format!("{} ", line.trim_end()));
}
(0..=line.len())
.filter(|&i| line.is_char_boundary(i))
.find(|&i| {
let rest = line[i..].trim_end();
!rest.is_empty()
&& !rest.starts_with(char::is_whitespace)
&& unit.text.starts_with(rest)
})
.map(|i| line[..i].to_string())
}
pub(super) fn check_text(
c: &mut Checker<'_>,
unit: &Unit,
rule: &UnitRule,
block: &Rule,
read: &Read,
) {
let Some(Shape::Text(text)) = &rule.shape else {
return;
};
let Some(expected) = expected_text(c, text) else {
let Text::From(key) = text else {
return;
};
if !c.unbound_is_own_fault(key) {
return;
}
c.emit(
&rule.common,
Some(unit.line),
"TLMD_168",
format!(
"`{}` is bound to the frontmatter `{key}`, which is absent",
short(&unit.text)
),
);
return;
};
if read.squashed.text == expected {
return;
}
let (criterion, message) = match text {
Text::Literal(_) => (
"TL_74",
format!("`{}` must read `{expected}`", short(&unit.text)),
),
Text::From(key) => (
"TLMD_165",
format!(
"`{}` must equal the frontmatter `{key}`: `{expected}`",
short(&unit.text)
),
),
};
c.emit(&rule.common, Some(unit.line), criterion, message);
if let Some(prefix) = repair_prefix(c, unit) {
let at = match block.kind {
BlockKind::BulletList | BlockKind::NumberedList => task_box(&expected),
_ => 0,
};
let label = match block.label_of(Some(rule)) {
Some(_) => c.label_kept(read),
None => String::new(),
};
let text = format!("{}{label}{}", &expected[..at], &expected[at..]);
c.out.repairs.push(Repair {
line: unit.line,
end_line: unit.end_line.max(unit.line),
prefix,
text,
});
}
}