use crate::docs::html::match_tag;
use crate::docs::indented::indent_columns;
const MAX_INDENT: usize = 3;
const RAW_NAMES: [&str; 4] = ["pre", "script", "style", "textarea"];
const BLOCK_NAMES: [&str; 62] = [
"address",
"article",
"aside",
"base",
"basefont",
"blockquote",
"body",
"caption",
"center",
"col",
"colgroup",
"dd",
"details",
"dialog",
"dir",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"frame",
"frameset",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hr",
"html",
"iframe",
"legend",
"li",
"link",
"main",
"menu",
"menuitem",
"nav",
"noframes",
"ol",
"optgroup",
"option",
"p",
"param",
"search",
"section",
"summary",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"title",
"tr",
"track",
"ul",
];
enum End {
Marker(&'static [&'static str]),
Blank,
}
pub fn mask<S: AsRef<str>>(lines: &[S], fenced: &[bool]) -> Vec<bool> {
let mut out = vec![false; lines.len()];
let mut paragraph = false;
let mut index = 0;
while index < lines.len() {
let text = lines[index].as_ref();
if fenced[index] || text.trim().is_empty() {
paragraph = false;
index += 1;
continue;
}
if let Some(close) = comment_end(lines, index) {
paragraph = false;
index = close + 1;
continue;
}
match block_end(lines, index, paragraph) {
Some(last) => {
out[index..=last].fill(true);
paragraph = false;
index = last + 1;
}
None => {
paragraph = true;
index += 1;
}
}
}
out
}
fn unindented(text: &str) -> Option<&str> {
(indent_columns(text) <= MAX_INDENT).then(|| text.trim_start_matches([' ', '\t']))
}
fn comment_end<S: AsRef<str>>(lines: &[S], open: usize) -> Option<usize> {
unindented(lines[open].as_ref())?.strip_prefix("<!--")?;
(open..lines.len()).find(|i| lines[*i].as_ref().contains("-->"))
}
fn block_end<S: AsRef<str>>(lines: &[S], start: usize, paragraph: bool) -> Option<usize> {
let rest = unindented(lines[start].as_ref())?;
match kind(rest, paragraph)? {
End::Marker(markers) => (start..lines.len()).find(|i| {
let line = lines[*i].as_ref().as_bytes();
markers.iter().any(|marker| {
line.windows(marker.len())
.any(|window| window.eq_ignore_ascii_case(marker.as_bytes()))
})
}),
End::Blank => Some(
(start..lines.len())
.find(|i| lines[*i].as_ref().trim().is_empty())
.unwrap_or(lines.len())
- 1,
),
}
}
fn kind(rest: &str, paragraph: bool) -> Option<End> {
let after = rest.strip_prefix('<')?;
if let Some(declaration) = after.strip_prefix('!') {
return if declaration.starts_with("[CDATA[") {
Some(End::Marker(&["]]>"]))
} else if declaration.starts_with(|c: char| c.is_ascii_alphabetic()) {
Some(End::Marker(&[">"]))
} else {
None
};
}
if after.starts_with('?') {
return Some(End::Marker(&["?>"]));
}
if named(after, &RAW_NAMES, &[" ", "\t", ">"]) {
return Some(End::Marker(&[
"</pre>",
"</script>",
"</style>",
"</textarea>",
]));
}
let name = after.strip_prefix('/').unwrap_or(after);
if named(name, &BLOCK_NAMES, &[" ", "\t", ">", "/>"]) {
return Some(End::Blank);
}
(!paragraph && is_tag_alone(rest)).then_some(End::Blank)
}
fn named(text: &str, names: &[&str], followers: &[&str]) -> bool {
let end = text
.bytes()
.position(|b| !b.is_ascii_alphanumeric())
.unwrap_or(text.len());
names
.iter()
.any(|name| text[..end].eq_ignore_ascii_case(name))
&& (end == text.len() || followers.iter().any(|f| text[end..].starts_with(f)))
}
fn is_tag_alone(rest: &str) -> bool {
let name = rest[1..].strip_prefix('/').unwrap_or(&rest[1..]);
let closing = name.len() != rest.len() - 1;
let end = name
.bytes()
.position(|b| !(b.is_ascii_alphanumeric() || b == b'-'))
.unwrap_or(name.len());
if !closing
&& RAW_NAMES
.iter()
.any(|raw| name[..end].eq_ignore_ascii_case(raw))
{
return false;
}
let chars: Vec<char> = rest.chars().collect();
match_tag(&chars, 0)
.is_some_and(|close| chars[close + 1..].iter().all(|c| matches!(c, ' ' | '\t')))
}