use regex::{Captures, Regex};
use std::collections::HashMap;
use std::sync::LazyLock;
const MATHML_NS: &str = "http://www.w3.org/1998/Math/MathML";
static VOID_ELEMENT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"<(img|br|hr|wbr|col)\b([^>]*?)\s*/?>").unwrap());
static CODE_TOOLS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"<div class="code-tools">.*?</button></div>"#).unwrap());
static ID_ATTR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" id="([^"]*)""#).unwrap());
static WEB_HREF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" href="([a-z]+:[^"]*)""#).unwrap());
const URI_ESCAPES: [(&str, &str); 7] = [
(""", "%22"),
("<", "%3C"),
(">", "%3E"),
(" ", "%20"),
("\\", "%5C"),
("^", "%5E"),
("`", "%60"),
];
static CLASS_ATTR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" class="[^"]*""#).unwrap());
static LOCAL_HREF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r##" href="#([^"]*)""##).unwrap());
pub(crate) fn from_html(html: &str) -> String {
let text: String = html.chars().filter(|&ch| xml_char(ch)).collect();
let text = CODE_TOOLS.replace_all(&text, "");
let text = VOID_ELEMENT.replace_all(&text, "<$1$2/>");
let text = WEB_HREF.replace_all(&text, |caps: &Captures<'_>| {
if !valid_host(&caps[1]) {
return String::new();
}
let uri = URI_ESCAPES
.iter()
.fold(caps[1].to_string(), |uri, (from, to)| uri.replace(from, to));
format!(" href=\"{uri}\"")
});
let text = text
.replace(" loading=\"lazy\"", "")
.replace("<math ", &format!("<math xmlns=\"{MATHML_NS}\" "));
unlink(&text)
}
pub(crate) fn ids(xhtml: &str) -> Vec<String> {
ID_ATTR.captures_iter(xhtml).map(|caps| caps[1].to_string()).collect()
}
pub(crate) fn relink(
xhtml: &str,
own: usize,
place: &HashMap<String, usize>,
file_name: impl Fn(usize) -> String,
) -> String {
let linked = LOCAL_HREF.replace_all(xhtml, |caps: &Captures<'_>| match place.get(&caps[1]) {
Some(&doc) if doc == own => caps[0].to_string(),
Some(&doc) => format!(" href=\"{}#{}\"", file_name(doc), &caps[1]),
None => String::new(),
});
unlink(&linked)
}
fn unlink(xhtml: &str) -> String {
let mut out = String::with_capacity(xhtml.len());
let mut rest = xhtml;
while let Some(start) = rest.find("<a") {
let after = &rest[start + 2..];
let Some(end) = after.find('>').filter(|_| after.starts_with([' ', '>'])) else {
out.push_str(&rest[..start + 2]);
rest = after;
continue;
};
let attrs = &after[..end];
let Some(close) = after[end..].find("</a>").map(|at| end + at) else {
out.push_str(&rest[..start + 2]);
rest = after;
continue;
};
out.push_str(&rest[..start]);
if attrs.contains(" href=\"") {
out.push_str(&rest[start..start + 2 + close + 4]);
} else {
let class = CLASS_ATTR.find(attrs).map_or("", |found| found.as_str());
out.push_str(&format!("<span{class}>{}</span>", &after[end + 1..close]));
}
rest = &after[close + 4..];
}
out.push_str(rest);
out
}
fn valid_host(uri: &str) -> bool {
let Some((_, rest)) = uri.split_once("//") else {
return true;
};
let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
let host = authority.rsplit_once('@').map_or(authority, |(_, host)| host);
let host = host.split(':').next().unwrap_or_default();
!host.is_empty()
&& host
.chars()
.all(|ch| ch.is_alphanumeric() || matches!(ch, '.' | '-' | '_'))
}
fn xml_char(ch: char) -> bool {
matches!(ch, '\t' | '\n' | '\r' | ' '..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn void_elements_are_closed() {
let html = "<p>a<img src=\"x.png\" alt=\"b\" loading=\"lazy\">c<br></p>";
assert_eq!(from_html(html), "<p>a<img src=\"x.png\" alt=\"b\"/>c<br/></p>");
}
#[test]
fn web_addresses_are_valid_uris() {
let html =
"<a href=\"https://www"\" target=\"_blank\">https://www"</a><a href=\"https://a.b/c d\">x</a>";
assert_eq!(
from_html(html),
"<span>https://www"</span><a href=\"https://a.b/c%20d\">x</a>"
);
}
#[test]
fn forbidden_characters_are_dropped() {
assert_eq!(from_html("a\u{1}b\u{c}c\u{FFFF}"), "abc");
}
#[test]
fn math_gets_its_namespace() {
assert_eq!(
from_html("<math class=\"im\"><mi>x</mi></math>"),
format!("<math xmlns=\"{MATHML_NS}\" class=\"im\"><mi>x</mi></math>")
);
}
#[test]
fn code_buttons_are_removed() {
let html = "<div class=\"code-head\"><div class=\"code-tools\"><button>A</button>\
<button><span>B</span></button></div></div><pre>x</pre>";
assert_eq!(from_html(html), "<div class=\"code-head\"></div><pre>x</pre>");
}
#[test]
fn links_point_into_other_documents() {
let place = HashMap::from([("a".to_string(), 0), ("b".to_string(), 1)]);
let xhtml = "<a href=\"#a\">1</a><a href=\"#b\">2</a><a class=\"xref\" href=\"#c\">3</a>";
assert_eq!(
relink(xhtml, 0, &place, |doc| format!("c{doc}.xhtml")),
"<a href=\"#a\">1</a><a href=\"c1.xhtml#b\">2</a><span class=\"xref\">3</span>"
);
assert_eq!(ids("<p id=\"x\">a</p><h2 id=\"y\">b</h2>"), ["x", "y"]);
}
}