use std::sync::atomic::{AtomicBool, Ordering};
use crate::style::Color;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Span {
pub text: String,
pub bold: bool,
pub italic: bool,
pub underline: bool,
pub strikethrough: bool,
pub color: Option<Color>,
pub link: Option<String>,
}
impl Span {
pub fn new(text: impl Into<String>) -> Span {
Span {
text: text.into(),
..Span::default()
}
}
pub fn bold(mut self) -> Span {
self.bold = true;
self
}
pub fn italic(mut self) -> Span {
self.italic = true;
self
}
pub fn underline(mut self) -> Span {
self.underline = true;
self
}
pub fn strikethrough(mut self) -> Span {
self.strikethrough = true;
self
}
pub fn color(mut self, color: Color) -> Span {
self.color = Some(color);
self
}
pub fn link(mut self, url: impl Into<String>) -> Span {
self.link = Some(url.into());
self
}
}
pub const MARKUP_CAP: usize = 64 * 1024;
pub fn parse_markup(body: &str) -> Vec<Span> {
static CUT_LOGGED: AtomicBool = AtomicBool::new(false);
let mut body = body;
if body.len() > MARKUP_CAP {
let mut end = MARKUP_CAP;
while !body.is_char_boundary(end) {
end -= 1;
}
if !CUT_LOGGED.swap(true, Ordering::Relaxed) {
tracing::warn!(
bytes = body.len(),
"a markup body is over 64 KiB, so only its first 64 KiB are shown"
);
}
body = &body[..end];
}
let mut out = Markup::default();
let mut rest = body;
while let Some(at) = rest.find(['<', '&']) {
out.text(&rest[..at]);
rest = &rest[at..];
let used = if rest.starts_with('&') {
entity(rest).map(|(ch, len)| {
out.push(ch.encode_utf8(&mut [0; 4]));
len
})
} else {
tag(rest).map(|(tag, len)| {
out.tag(tag);
len
})
};
let used = used.unwrap_or_else(|| {
out.push(&rest[..1]);
1
});
rest = &rest[used..];
}
out.text(rest);
out.spans
}
#[derive(Default)]
struct Markup {
spans: Vec<Span>,
open: Vec<usize>,
at: [Vec<usize>; 4],
links: Vec<(usize, Option<String>)>,
opened: usize,
last: Option<(bool, bool, bool, Option<usize>)>,
}
const B: usize = 0;
const I: usize = 1;
const U: usize = 2;
const A: usize = 3;
impl Markup {
fn text(&mut self, text: &str) {
if !text.is_empty() {
self.push(text);
}
}
fn push(&mut self, text: &str) {
let open = |kind: usize| !self.at[kind].is_empty();
let (number, link) = match self.links.last() {
Some((number, Some(link))) => (Some(*number), Some(link)),
_ => (None, None),
};
let style = (open(B), open(I), open(U) || link.is_some(), number);
let same = match (self.last, self.spans.last()) {
(Some(last), _) if last == style => true,
(Some((bold, italic, underline, _)), Some(span)) => {
(bold, italic, underline) == (style.0, style.1, style.2) && span.link.as_ref() == link
}
_ => false,
};
if let (true, Some(last)) = (same, self.spans.last_mut()) {
last.text.push_str(text);
} else {
self.spans.push(Span {
text: text.to_owned(),
bold: style.0,
italic: style.1,
underline: style.2,
strikethrough: false,
color: None,
link: link.cloned(),
});
}
self.last = Some(style);
}
fn tag(&mut self, tag: Tag) {
match tag {
Tag::Open(kind, link) => {
self.at[kind].push(self.open.len());
self.open.push(kind);
if kind == A {
self.links.push((self.opened, link));
self.opened += 1;
}
}
Tag::Close(kind) => {
let Some(&from) = self.at[kind].last() else {
return;
};
while self.open.len() > from {
let closed = self.open.pop().expect("open holds the tag closed");
self.at[closed].pop();
if closed == A {
self.links.pop();
}
}
}
Tag::Image(alt) => self.text(&alt),
Tag::Unknown => {}
}
}
}
enum Tag {
Open(usize, Option<String>),
Close(usize),
Image(String),
Unknown,
}
fn tag(text: &str) -> Option<(Tag, usize)> {
let bytes = text.as_bytes();
let mut at = 1;
let close = bytes.get(at) == Some(&b'/');
if close {
at += 1;
}
let name_start = at;
if !bytes.get(at)?.is_ascii_alphabetic() {
return None;
}
while bytes.get(at).is_some_and(u8::is_ascii_alphanumeric) {
at += 1;
}
let name = text[name_start..at].to_ascii_lowercase();
let mut attributes = Vec::new();
let mut empty = false;
loop {
while bytes.get(at).is_some_and(u8::is_ascii_whitespace) {
at += 1;
}
match bytes.get(at)? {
b'>' => {
at += 1;
break;
}
b'/' if bytes.get(at + 1) == Some(&b'>') => {
at += 2;
empty = true;
break;
}
_ if !bytes[at - 1].is_ascii_whitespace() => return None,
_ => {
let (attribute, len) = attribute(&text[at..])?;
attributes.push(attribute);
at += len;
}
}
}
let value = |key: &str| {
attributes
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case(key))
.map(|(_, value)| value.clone())
};
let tag = match (close, name.as_str()) {
(false, "img") => Tag::Image(value("alt").unwrap_or_default()),
_ if empty => Tag::Unknown,
(false, "a") => Tag::Open(A, value("href").filter(|href| safe_link(href))),
(false, "b") => Tag::Open(B, None),
(false, "i") => Tag::Open(I, None),
(false, "u") => Tag::Open(U, None),
(true, "a") => Tag::Close(A),
(true, "b") => Tag::Close(B),
(true, "i") => Tag::Close(I),
(true, "u") => Tag::Close(U),
_ => Tag::Unknown,
};
Some((tag, at))
}
fn attribute(text: &str) -> Option<((String, String), usize)> {
let name_len = text
.find(|ch: char| ch.is_ascii_whitespace() || matches!(ch, '=' | '>' | '/' | '<' | '"' | '\''))
.unwrap_or(text.len());
if name_len == 0 {
return None;
}
let name = text[..name_len].to_owned();
let rest = &text[name_len..];
let Some(rest) = rest.strip_prefix('=') else {
return Some(((name, String::new()), name_len));
};
let (raw, len) = match rest.chars().next()? {
quote @ ('"' | '\'') => {
let end = rest[1..].find([quote, '<'])? + 1;
if !rest[end..].starts_with(quote) {
return None;
}
(&rest[1..end], end + 1)
}
_ => {
let end = rest
.find(|ch: char| ch.is_ascii_whitespace() || matches!(ch, '>' | '<' | '"' | '\''))
.unwrap_or(rest.len());
(&rest[..end], end)
}
};
if rest[len..].is_empty() {
return None;
}
Some(((name, decode(raw)), name_len + 1 + len))
}
fn decode(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find('&') {
out.push_str(&rest[..at]);
rest = &rest[at..];
match entity(rest) {
Some((ch, len)) => {
out.push(ch);
rest = &rest[len..];
}
None => {
out.push('&');
rest = &rest[1..];
}
}
}
out.push_str(rest);
out
}
fn entity(text: &str) -> Option<(char, usize)> {
let end = text.bytes().take(12).position(|byte| byte == b';')?;
let ch = match &text[1..end] {
"amp" => '&',
"lt" => '<',
"gt" => '>',
"quot" => '"',
"apos" => '\'',
name => {
let number = name.strip_prefix('#')?;
let (digits, radix) = match number.strip_prefix(['x', 'X']) {
Some(hex) => (hex, 16),
None => (number, 10),
};
if !digits.chars().all(|ch| ch.is_digit(radix)) {
return None;
}
let code = u32::from_str_radix(digits, radix).ok()?;
char::from_u32(code).filter(|&ch| ch != '\0')?
}
};
Some((ch, end + 1))
}
fn safe_link(href: &str) -> bool {
["http:", "https:", "mailto:"].iter().any(|scheme| {
href
.get(..scheme.len())
.is_some_and(|start| start.eq_ignore_ascii_case(scheme))
})
}