use std::ops::Range;
use pulldown_cmark::Tag;
use super::{Block, BlockKind, Blocks, Cut, Opening, kind_of};
#[derive(Default)]
pub(super) struct Items {
kind: Option<BlockKind>,
number: Option<u64>,
paragraphs: bool,
children: Vec<Item>,
last: Option<usize>,
}
struct Item {
start: usize,
content: Option<usize>,
opening: Opening,
}
impl Items {
pub(super) fn of(tag: &Tag<'_>, blocks: Blocks) -> Self {
let kind = Some(kind_of(tag)).filter(|kind| {
let cut = match kind {
BlockKind::List => blocks.lists,
BlockKind::BlockQuote => blocks.quotes,
BlockKind::DefinitionList => blocks.definitions,
BlockKind::Table => blocks.tables,
_ => Cut::Whole,
};
cut == Cut::Split
});
let number = match tag {
Tag::List(start) => *start,
_ => None,
};
Self { kind, number, paragraphs: false, children: Vec::new(), last: None }
}
pub(super) fn start(&mut self, tag: &Tag<'_>, range: &Range<usize>) {
self.last = Some(range.end);
let opening = match (self.kind, tag) {
(Some(BlockKind::List), Tag::Item) => match self.number.as_mut() {
Some(next) => {
*next += 1;
Opening::Number(*next - 1)
}
None => Opening::Plain,
},
(Some(BlockKind::BlockQuote), _)
| (Some(BlockKind::DefinitionList), Tag::DefinitionListTitle)
| (Some(BlockKind::Table), Tag::TableHead | Tag::TableRow) => Opening::Plain,
_ => {
self.inside(Some(tag), range);
return;
}
};
self.children.push(Item { start: range.start, content: None, opening });
self.inside(Some(tag), range);
}
pub(super) fn inner(&mut self, tag: &Tag<'_>) {
if self.kind == Some(BlockKind::List) && matches!(tag, Tag::Paragraph) {
self.paragraphs = true;
}
}
pub(super) fn inside(&mut self, tag: Option<&Tag<'_>>, range: &Range<usize>) {
let container = matches!(
tag,
Some(
Tag::List(_)
| Tag::Item
| Tag::BlockQuote(_)
| Tag::DefinitionList
| Tag::DefinitionListDefinition
| Tag::FootnoteDefinition(_)
)
);
if let Some(item) = self.children.last_mut().filter(|_| !container) {
item.content = Some(item.content.map_or(range.end, |end| end.max(range.end)));
}
}
pub(super) fn leaf(&mut self, range: &Range<usize>) {
if self.kind == Some(BlockKind::BlockQuote) {
self.children.push(Item {
start: range.start,
content: Some(range.end),
opening: Opening::Plain,
});
}
}
pub(super) fn end(&self, kind: BlockKind, start: usize, end: usize) -> usize {
match (kind, self.last) {
(BlockKind::DefinitionList, Some(last)) => last.clamp(start, end),
_ => end,
}
}
pub(super) fn blocks(&self, source: &str, whole: &Block) -> Option<Vec<Block>> {
if self.children.len() < 2 {
return None;
}
let starts: Vec<usize> =
self.children.iter().map(|item| line_start(source, item.start).max(whole.range.start)).collect();
let ends: Vec<usize> = self
.children
.iter()
.zip(&starts)
.enumerate()
.map(|(index, (item, &start))| {
let next = starts.get(index + 1).copied().unwrap_or(whole.range.end);
let end = match self.kind {
Some(BlockKind::DefinitionList) => next,
_ => item.content.unwrap_or_else(|| line_end(source, start)),
};
let end = end.clamp(start, next);
source.get(start..end).map_or(end, |text| start + text.trim_end_matches(['\n', '\r']).len())
})
.collect();
let joined =
matches!(self.kind, Some(BlockKind::List | BlockKind::DefinitionList | BlockKind::Table));
let blocks = self.children.iter().zip(starts.iter().zip(&ends)).enumerate().map(
|(index, (item, (&start, &end)))| Block {
range: start..end,
kind: whole.kind,
opening: item.opening.clone(),
top: whole.top,
joined: index > 0 && joined,
loose: self.paragraphs,
},
);
Some(blocks.collect())
}
}
fn line_start(source: &str, at: usize) -> usize {
source.get(..at).and_then(|before| before.rfind('\n')).map_or(0, |newline| newline + 1)
}
fn line_end(source: &str, at: usize) -> usize {
source.get(at..).and_then(|after| after.find('\n')).map_or(source.len(), |newline| at + newline)
}