use std::collections::HashSet;
use crate::options::Options;
use crate::tokenize::{Token, TokenizeState, tokenize, tokenize_with_state};
pub const DEFAULT_SKIP_TAGS: [&str; 7] =
["code", "pre", "script", "style", "kbd", "samp", "textarea"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HtmlOptions {
tag: String,
class_name: Option<String>,
rest_tag: Option<String>,
rest_class_name: Option<String>,
ignore_html_tags: bool,
skip_tags: Vec<String>,
}
impl Default for HtmlOptions {
fn default() -> Self {
Self::new()
}
}
impl HtmlOptions {
#[must_use]
pub fn new() -> Self {
Self {
tag: "b".to_owned(),
class_name: None,
rest_tag: None,
rest_class_name: None,
ignore_html_tags: true,
skip_tags: DEFAULT_SKIP_TAGS
.iter()
.map(|&tag| tag.to_owned())
.collect(),
}
}
#[must_use]
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.tag = tag.into();
self
}
#[must_use]
pub fn class_name(mut self, class_name: impl Into<String>) -> Self {
self.class_name = Some(class_name.into());
self
}
#[must_use]
pub fn rest_tag(mut self, rest_tag: impl Into<String>) -> Self {
self.rest_tag = Some(rest_tag.into());
self
}
#[must_use]
pub fn rest_class_name(mut self, rest_class_name: impl Into<String>) -> Self {
self.rest_class_name = Some(rest_class_name.into());
self
}
#[must_use]
pub const fn ignore_html_tags(mut self, ignore_html_tags: bool) -> Self {
self.ignore_html_tags = ignore_html_tags;
self
}
#[must_use]
pub fn skip_tags<I, S>(mut self, skip_tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.skip_tags = skip_tags.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn get_tag(&self) -> &str {
&self.tag
}
#[must_use]
pub fn get_class_name(&self) -> Option<&str> {
self.class_name.as_deref()
}
#[must_use]
pub fn get_rest_tag(&self) -> Option<&str> {
self.rest_tag.as_deref()
}
#[must_use]
pub fn get_rest_class_name(&self) -> Option<&str> {
self.rest_class_name.as_deref()
}
#[must_use]
pub const fn get_ignore_html_tags(&self) -> bool {
self.ignore_html_tags
}
#[must_use]
pub fn get_skip_tags(&self) -> &[String] {
&self.skip_tags
}
}
#[must_use]
pub fn escape_html(text: &str) -> String {
let mut out = String::with_capacity(text.len());
push_escaped(&mut out, text);
out
}
fn push_escaped(out: &mut String, text: &str) {
for c in text.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
_ => out.push(c),
}
}
}
fn markup_end(text: &str, pos: usize) -> Option<usize> {
let bytes = text.as_bytes();
let rest = &text[pos..];
match bytes[pos] {
b'<' => {
if let Some(body) = rest.strip_prefix("<!--") {
if let Some(end) = body.find("-->") {
return Some(pos + 4 + end + 3);
}
}
if let Some(body) = rest.strip_prefix("<![CDATA[") {
if let Some(end) = body.find("]]>") {
return Some(pos + 9 + end + 3);
}
}
match bytes.get(pos + 1) {
Some(&next) if matches!(next, b'/' | b'!' | b'?') || next.is_ascii_alphabetic() => {
rest.find('>').map(|end| pos + end + 1)
}
_ => None,
}
}
b'&' => {
let body = &bytes[pos + 1..];
let mut i;
if body.first() == Some(&b'#') {
let hex = matches!(body.get(1), Some(b'x' | b'X'));
i = if hex { 2 } else { 1 };
let digits_start = i;
while body.get(i).is_some_and(|b| {
if hex {
b.is_ascii_hexdigit()
} else {
b.is_ascii_digit()
}
}) {
i += 1;
}
if i == digits_start {
return None;
}
} else {
if !body.first().is_some_and(u8::is_ascii_alphabetic) {
return None;
}
i = 1;
while body.get(i).is_some_and(u8::is_ascii_alphanumeric) {
i += 1;
}
}
(body.get(i) == Some(&b';')).then_some(pos + 1 + i + 1)
}
_ => None,
}
}
fn tag_name(raw: &str) -> Option<(bool, String)> {
let body = raw.strip_prefix('<')?.trim_start();
let (closing, body) = match body.strip_prefix('/') {
Some(body) => (true, body.trim_start()),
None => (false, body),
};
if !body.starts_with(|c: char| c.is_ascii_alphabetic()) {
return None;
}
let end = body
.find(|c: char| c.is_whitespace() || c == '/' || c == '>')
.unwrap_or(body.len());
Some((closing, body[..end].to_lowercase()))
}
fn is_self_closing(raw: &str) -> bool {
raw.strip_suffix('>')
.is_some_and(|body| body.trim_end().ends_with('/'))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Chunk<'a> {
Raw(&'a str),
Prose(&'a str),
}
fn split_markup<'a>(text: &'a str, skipped: &HashSet<String>) -> Vec<Chunk<'a>> {
let mut chunks = Vec::new();
let mut skip_name: Option<String> = None; let mut skip_depth = 0_usize; let mut cursor = 0;
let mut pos = 0;
let bytes = text.as_bytes();
while pos < bytes.len() {
if bytes[pos] != b'<' && bytes[pos] != b'&' {
pos += 1;
continue;
}
let Some(end) = markup_end(text, pos) else {
pos += 1;
continue;
};
if pos > cursor {
let prose = &text[cursor..pos];
chunks.push(if skip_name.is_some() {
Chunk::Raw(prose)
} else {
Chunk::Prose(prose)
});
}
let raw = &text[pos..end];
chunks.push(Chunk::Raw(raw));
cursor = end;
pos = end;
let Some((closing, name)) = tag_name(raw) else {
continue;
};
if is_self_closing(raw) {
continue;
}
match &skip_name {
None => {
if !closing && skipped.contains(&name) {
skip_name = Some(name);
skip_depth = 1;
}
}
Some(current) if *current == name => {
if closing {
skip_depth -= 1;
if skip_depth == 0 {
skip_name = None;
}
} else {
skip_depth += 1;
}
}
Some(_) => {}
}
}
if cursor < text.len() {
let prose = &text[cursor..];
chunks.push(if skip_name.is_some() {
Chunk::Raw(prose)
} else {
Chunk::Prose(prose)
});
}
chunks
}
fn push_wrapped(out: &mut String, tag: Option<&str>, class_name: Option<&str>, body: &str) {
let Some(tag) = tag else {
push_escaped(out, body);
return;
};
out.push('<');
out.push_str(tag);
if let Some(class_name) = class_name {
out.push_str(" class=\"");
push_escaped(out, class_name);
out.push('"');
}
out.push('>');
push_escaped(out, body);
out.push_str("</");
out.push_str(tag);
out.push('>');
}
fn render(out: &mut String, tokens: &[Token<'_>], html: &HtmlOptions) {
for token in tokens {
match *token {
Token::Word {
fixation,
fixation_text,
rest_text,
..
} if fixation > 0 => {
push_wrapped(
out,
Some(html.get_tag()),
html.get_class_name(),
fixation_text,
);
if !rest_text.is_empty() {
push_wrapped(
out,
html.get_rest_tag(),
html.get_rest_class_name(),
rest_text,
);
}
}
_ => push_escaped(out, token.text()),
}
}
}
#[must_use]
pub fn to_html(text: &str, options: &Options, html: &HtmlOptions) -> String {
let mut out = String::with_capacity(text.len() * 2);
if !html.ignore_html_tags {
render(&mut out, &tokenize(text, options), html);
return out;
}
let skipped: HashSet<String> = html
.skip_tags
.iter()
.map(String::as_str)
.chain(Some(html.tag.as_str()))
.chain(html.rest_tag.as_deref())
.map(str::to_lowercase)
.collect();
let mut state = TokenizeState::default();
for chunk in split_markup(text, &skipped) {
match chunk {
Chunk::Raw(raw) => out.push_str(raw),
Chunk::Prose(prose) => {
let tokens = tokenize_with_state(prose, options, &mut state);
render(&mut out, &tokens, html);
}
}
}
out
}
#[must_use]
pub fn to_markdown(text: &str, options: &Options, marker: &str) -> String {
let mut out = String::with_capacity(text.len() * 2);
for token in tokenize(text, options) {
match token {
Token::Word {
fixation,
fixation_text,
rest_text,
..
} if fixation > 0 => {
out.push_str(marker);
out.push_str(fixation_text);
out.push_str(marker);
out.push_str(rest_text);
}
_ => out.push_str(token.text()),
}
}
out
}