use crate::parser::scan::{Span, is_whitespace, skip_whitespace};
pub(super) struct Autolink {
pub(super) url: String,
pub(super) text: Span,
pub(super) end: usize,
}
pub(super) fn autolink(src: &str, pos: usize) -> Option<Autolink> {
let bytes = src.as_bytes();
let inner = pos + 1;
let scheme_char = |b: &u8| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.');
let scheme = bytes
.get(inner)
.is_some_and(u8::is_ascii_alphabetic)
.then(|| {
1 + bytes[inner + 1..]
.iter()
.take(31)
.take_while(|b| scheme_char(b))
.count()
})
.filter(|&length| length >= 2 && bytes.get(inner + length) == Some(&b':'));
if let Some(length) = scheme {
let from = inner + length + 1;
let close = from
+ bytes[from..]
.iter()
.position(|&b| b == b'>' || b <= 0x1F || matches!(b, b' ' | b'<' | 0x7F))?;
return (bytes[close] == b'>').then(|| Autolink {
url: src[inner..close].to_string(),
text: Span::new(inner, close),
end: close + 1,
});
}
let end = email(bytes, inner)?;
Some(Autolink {
url: format!("mailto:{}", &src[inner..end]),
text: Span::new(inner, end),
end: end + 1,
})
}
pub(crate) fn is_autolink_email(text: &str) -> bool {
let closed = format!("{text}>");
email(closed.as_bytes(), 0) == Some(text.len())
}
fn email(bytes: &[u8], start: usize) -> Option<usize> {
let atext = |b: u8| matches!(b, b'#'..=b'\'' | b'*' | b'+' | b'-'..=b'9' | b'=' | b'?' | b'A'..=b'Z' | b'^'..=b'~');
let mut index = start;
while bytes.get(index).copied().is_some_and(atext) {
index += 1;
}
if index == start || bytes.get(index) != Some(&b'@') {
return None;
}
index += 1;
loop {
let label = index;
if !bytes.get(index).is_some_and(u8::is_ascii_alphanumeric) {
return None;
}
while bytes
.get(index)
.is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'-')
{
index += 1;
}
if index - label > 63 || bytes[index - 1] == b'-' {
return None;
}
match bytes.get(index) {
Some(b'.') => index += 1,
Some(b'>') => return Some(index),
_ => return None,
}
}
}
#[derive(Default)]
pub(super) struct Misses {
comment: Option<usize>,
instruction: Option<usize>,
cdata: Option<usize>,
declaration: Option<usize>,
}
fn find_from(src: &str, from: usize, terminator: &str, miss: &mut Option<usize>) -> Option<usize> {
if miss.is_some_and(|missed| from >= missed) {
return None;
}
let found = src[from..].find(terminator).map(|index| from + index);
if found.is_none() {
*miss = Some(from);
}
found
}
pub(super) fn inline_html(src: &str, pos: usize, misses: &mut Misses) -> Option<usize> {
let rest = &src[pos..];
let bytes = rest.as_bytes();
if rest.starts_with("<!--") {
if rest.starts_with("<!-->") {
return Some(pos + 5);
}
if rest.starts_with("<!--->") {
return Some(pos + 6);
}
return find_from(src, pos + 4, "-->", &mut misses.comment).map(|index| index + 3);
}
if rest.starts_with("<?") {
return find_from(src, pos + 2, "?>", &mut misses.instruction).map(|index| index + 2);
}
if rest.starts_with("<![CDATA[") {
return find_from(src, pos + 9, "]]>", &mut misses.cdata).map(|index| index + 3);
}
if rest.starts_with("<!") && bytes.get(2).is_some_and(u8::is_ascii_alphabetic) {
return find_from(src, pos, ">", &mut misses.declaration).map(|index| index + 1);
}
if rest.starts_with("</") {
return closing_tag(bytes).map(|length| pos + length);
}
open_tag(bytes).map(|length| pos + length)
}
fn tag_name(bytes: &[u8]) -> usize {
if !bytes.first().is_some_and(u8::is_ascii_alphabetic) {
return 0;
}
bytes
.iter()
.take_while(|b| b.is_ascii_alphanumeric() || **b == b'-')
.count()
}
fn closing_tag(bytes: &[u8]) -> Option<usize> {
let name = tag_name(&bytes[2..]);
if name == 0 {
return None;
}
let index = skip_whitespace(bytes, 2 + name);
(bytes.get(index) == Some(&b'>')).then_some(index + 1)
}
fn open_tag(bytes: &[u8]) -> Option<usize> {
let name = tag_name(&bytes[1..]);
if name == 0 {
return None;
}
let mut index = 1 + name;
loop {
let after_space = skip_whitespace(bytes, index);
let has_space = after_space > index;
match bytes.get(after_space)? {
b'>' => return Some(after_space + 1),
b'/' => return (bytes.get(after_space + 1) == Some(&b'>')).then_some(after_space + 2),
byte if has_space && (byte.is_ascii_alphabetic() || matches!(byte, b'_' | b':')) => {
index = attribute(bytes, after_space)?;
}
_ => return None,
}
}
}
fn attribute(bytes: &[u8], start: usize) -> Option<usize> {
let name = bytes[start..]
.iter()
.take_while(|b| b.is_ascii_alphanumeric() || matches!(**b, b'_' | b'.' | b':' | b'-'))
.count();
let end = start + name;
let after_space = skip_whitespace(bytes, end);
if bytes.get(after_space) != Some(&b'=') {
return Some(end);
}
let value = skip_whitespace(bytes, after_space + 1);
match bytes.get(value)? {
quote @ (b'"' | b'\'') => {
let close = bytes[value + 1..].iter().position(|b| b == quote)?;
Some(value + 1 + close + 1)
}
_ => {
let length = bytes[value..]
.iter()
.take_while(|&&b| !is_whitespace(b) && !matches!(b, b'"' | b'\'' | b'=' | b'<' | b'>' | b'`'))
.count();
(length > 0).then_some(value + length)
}
}
}