use super::{Deriver, guide};
use crate::{
check::is_run,
grammar::{Block, Lang, LifecycleDimension, States},
markdown::BlockKind,
text,
};
impl Deriver<'_> {
pub(super) fn blocks(&mut self, rules: Option<&[Block]>, out: &mut Vec<String>) {
let rules = rules.unwrap_or(&[]);
self.meetings(rules);
let mut i = 0;
while i < rules.len() {
if rules[i].optional_at(self.states, &self.schema.core.lifecycle) && !self.in_optional {
let mut j = i;
while j < rules.len()
&& rules[j].optional_at(self.states, &self.schema.core.lifecycle)
{
j += 1;
}
self.optional_region(out, |d, out| {
for block in &rules[i..j] {
d.one_block(block, out);
}
});
i = j;
continue;
}
self.one_block(&rules[i], out);
i += 1;
}
}
fn one_block(&mut self, block: &Block, out: &mut Vec<String>) {
if !block.common.only.admits(self.states) || block.hard_max() == Some(0) {
return;
}
self.attempt(
block.optional_at(self.states, &self.schema.core.lifecycle),
out,
|d, out| d.block(block, out),
);
}
fn block(&mut self, block: &Block, out: &mut Vec<String>) {
let text = guide::block_text(self.schema, self.host, block);
self.guidance(text.as_deref(), out);
if block.kind == BlockKind::Group {
let inner = block.rule_id.clone().or(self.group_rule.clone());
let held = std::mem::replace(&mut self.group_rule, inner);
for _ in 0..block.min.max(1) {
self.marker(&block.common, out);
self.blocks(block.blocks.as_deref(), out);
}
self.group_rule = held;
return;
}
let units = self.units_of(block);
if block.kind != BlockKind::Code {
self.marker(&block.common, out);
}
match block.kind {
BlockKind::Prose => {
for text in units {
out.push(text);
out.push(String::new());
}
}
BlockKind::BulletList => {
for text in units {
out.push(format!("- {text}"));
}
out.push(String::new());
}
BlockKind::NumberedList => {
for (i, text) in units.iter().enumerate() {
out.push(format!("{}. {text}", i + 1));
}
out.push(String::new());
}
BlockKind::Blockquote => {
for (i, text) in units.iter().enumerate() {
if i > 0 {
out.push(">".to_string());
}
out.push(format!("> {text}"));
}
out.push(String::new());
}
BlockKind::Table => {
let columns = block
.example_columns
.as_deref()
.or(block.columns.as_deref())
.unwrap_or_default();
let names: Vec<String> = columns.iter().map(|c| c.replace('|', "\\|")).collect();
out.push(format!("| {} |", names.join(" | ")));
out.push(format!(
"| {} |",
columns
.iter()
.map(|_| "---")
.collect::<Vec<_>>()
.join(" | ")
));
for text in units {
out.push(format!("| {text} |"));
}
out.push(String::new());
}
BlockKind::Code => {
let fence = "`".repeat(block.fence.max(3));
let tag = match &block.lang {
Some(Lang::Tags(tags)) => tags.first().cloned().unwrap_or_default(),
Some(Lang::Any) | None => "text".to_string(),
};
for text in units {
self.marker(&block.common, out);
out.push(format!("{fence}{tag}"));
out.extend(text.lines().map(str::to_string));
out.push(fence.clone());
out.push(String::new());
}
}
BlockKind::ThematicBreak => {
out.push("---".to_string());
out.push(String::new());
}
BlockKind::Html | BlockKind::Image | BlockKind::Region | BlockKind::References => {
for text in units {
out.extend(text.lines().map(str::to_string));
}
out.push(String::new());
}
BlockKind::Group => unreachable!("a group is taken above"),
BlockKind::Heading => unreachable!("no schema declares a heading block"),
}
self.follows(block);
}
fn meetings(&mut self, rules: &[Block]) {
let states = self.states;
let lifecycle = &self.schema.core.lifecycle[..];
let admitted: Vec<&Block> = rules
.iter()
.filter(|b| b.common.only.admits(states))
.collect();
let banned = |b: &Block| b.hard_max() == Some(0);
let mut met: Vec<&Block> = Vec::new();
for (i, b) in admitted.iter().enumerate() {
if banned(b) {
continue;
}
for last in closings(b, states, lifecycle) {
if !is_run(last.kind) || !takes_more(last) {
continue;
}
let later = &admitted[i + 1..];
let own = std::ptr::eq(last, *b);
let opened = |next: &Block| {
openings(next, states, lifecycle).iter().any(|o| {
o.kind == last.kind && !(own && reserved(later, next, o, states, lifecycle))
})
};
let mut meets = b.kind == BlockKind::Group
&& b.hard_max().is_none_or(|m| m > 1)
&& openings(b, states, lifecycle)
.iter()
.any(|o| o.kind == last.kind);
for next in &admitted[i + 1..] {
if meets {
break;
}
if banned(next) {
continue;
}
meets = opened(next);
if !next.optional_at(states, lifecycle) {
break;
}
}
if meets && !met.iter().any(|m| std::ptr::eq(*m, last)) {
met.push(last);
}
}
}
for last in met {
let kind = last.kind.name();
self.refuse_on(
last.common.line,
"TLMD_149",
format!(
"{} `{kind}` block whose units have no shape of their own can stand directly before another `{kind}` block, which a document reads as one block, so the first takes all of it; give the units of the first block a shape that the units of the second do not have, or put the second under a heading of its own",
text::article(kind)
),
);
}
}
fn follows(&mut self, block: &Block) {
self.kinds.push(block.kind);
}
}
fn closings<'b>(
b: &'b Block,
states: &'b States,
lifecycle: &[LifecycleDimension],
) -> Vec<&'b Block> {
ends(b, states, lifecycle, true)
}
fn openings<'b>(
b: &'b Block,
states: &'b States,
lifecycle: &[LifecycleDimension],
) -> Vec<&'b Block> {
ends(b, states, lifecycle, false)
}
fn ends<'b>(
b: &'b Block,
states: &'b States,
lifecycle: &[LifecycleDimension],
last: bool,
) -> Vec<&'b Block> {
if b.kind != BlockKind::Group {
return vec![b];
}
let mut inner: Vec<&Block> = b.admitted(states).collect();
if last {
inner.reverse();
}
let mut out = Vec::new();
for i in inner {
out.extend(ends(i, states, lifecycle, last));
if !i.optional_at(states, lifecycle) {
break;
}
}
out
}
fn reserved(
later: &[&Block],
next: &Block,
o: &Block,
states: &States,
lifecycle: &[LifecycleDimension],
) -> bool {
let mut inner = next.admitted(states);
let (Some(first), Some(second)) = (inner.next(), inner.next()) else {
return false;
};
if next.kind != BlockKind::Group
|| !std::ptr::eq(first, o)
|| o.optional_at(states, lifecycle)
|| takes_more(o)
{
return false;
}
let Some(after) = second.first_position(states).map(|s| s.kind) else {
return false;
};
!first.optional_at(states, lifecycle)
&& !second.optional_at(states, lifecycle)
&& after != o.kind
&& !later
.iter()
.filter(|p| !std::ptr::eq(**p, next))
.any(|p| p.first_position(states).is_some_and(|f| f.kind == after))
}
fn takes_more(block: &Block) -> bool {
block.sequence.is_none()
&& block.rules.is_none()
&& block.each.as_ref().is_none_or(|u| {
u.shape.is_none() && u.format.is_none() && u.prohibited_patterns.is_empty()
})
}