use super::markdown_transformers::{
ElementTransformer, MultilineElementTransformer, TextFormatTransformer, TextMatchTransformer,
Transformer,
};
use super::reg_exp::{RegExp, in_table, is_js_space};
use super::reg_exp_tables::{PUNCTUATION, SPACE_SEPARATOR, SYMBOL};
use crate::NodeRef;
use crate::nodes::line_break::LineBreakNode;
use crate::nodes::paragraph::ParagraphNode;
use crate::nodes::tab::TabNode;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
pub(crate) fn re(source: &'static str, flags: &'static str) -> RegExp {
thread_local! {
static CACHE: RefCell<HashMap<(&'static str, &'static str), RegExp>> =
RefCell::new(HashMap::new());
}
CACHE.with(|cache| {
cache
.borrow_mut()
.entry((source, flags))
.or_insert_with(|| RegExp::of(source, flags))
.clone()
})
}
pub(crate) fn units(text: &str) -> Vec<u16> {
text.encode_utf16().collect()
}
pub(crate) fn string(units: &[u16]) -> String {
String::from_utf16_lossy(units)
}
pub(crate) fn unit_at(text: &[u16], index: isize) -> Option<u32> {
if index < 0 {
return None;
}
text.get(index as usize).map(|u| u32::from(*u))
}
pub(crate) fn char_string(character: u32) -> String {
char::from_u32(character).map_or_else(|| "\u{FFFD}".to_owned(), String::from)
}
pub(crate) fn char_units(character: u32) -> usize {
if character > 0xFFFF { 2 } else { 1 }
}
pub(crate) fn is_space(character: u32) -> bool {
is_js_space(character)
}
pub(crate) fn trim_start(text: &str) -> &str {
text.trim_start_matches(|c: char| is_js_space(c as u32))
}
pub(crate) fn trim_end(text: &str) -> &str {
text.trim_end_matches(|c: char| is_js_space(c as u32))
}
pub(crate) fn trim(text: &str) -> &str {
trim_end(trim_start(text))
}
pub(crate) fn has_non_space(text: &str) -> bool {
text.chars().any(|c| !is_js_space(c as u32))
}
pub(crate) fn index_by<T: Clone>(
list: &[T],
callback: impl Fn(&T) -> Option<String>,
) -> Vec<(String, Vec<T>)> {
let mut index: Vec<(String, Vec<T>)> = Vec::new();
for item in list {
let Some(key) = callback(item).filter(|k| !k.is_empty()) else {
continue;
};
match index.iter_mut().find(|(k, _)| *k == key) {
Some((_, items)) => items.push(item.clone()),
None => index.push((key, vec![item.clone()])),
}
}
index
}
#[derive(Clone, Default)]
pub(crate) struct ByType {
pub element: Vec<Rc<ElementTransformer>>,
pub multiline_element: Vec<Rc<MultilineElementTransformer>>,
pub text_format: Vec<Rc<TextFormatTransformer>>,
pub text_match: Vec<Rc<TextMatchTransformer>>,
}
pub(crate) fn transformers_by_type(transformers: &[Transformer]) -> ByType {
let mut by_type = ByType::default();
for transformer in transformers {
match transformer {
Transformer::Element(t) => by_type.element.push(t.clone()),
Transformer::MultilineElement(t) => by_type.multiline_element.push(t.clone()),
Transformer::TextFormat(t) => by_type.text_format.push(t.clone()),
Transformer::TextMatch(t) => by_type.text_match.push(t.clone()),
}
}
by_type
}
fn is_punctuation(character: u32) -> bool {
in_table(PUNCTUATION, character) || in_table(SYMBOL, character)
}
fn is_whitespace(character: u32) -> bool {
matches!(character, 0x09 | 0x0A | 0x0C | 0x0D) || in_table(SPACE_SEPARATOR, character)
}
const WHITESPACE_CLASS: u8 = 0;
const PUNCTUATION_CLASS: u8 = 1;
const OTHER_CLASS: u8 = 2;
fn classify(character: Option<u32>) -> u8 {
match character {
None => WHITESPACE_CLASS,
Some(c) if is_whitespace(c) => WHITESPACE_CLASS,
Some(c) if is_punctuation(c) => PUNCTUATION_CLASS,
Some(_) => OTHER_CLASS,
}
}
pub(crate) fn is_flanked_by(primary: Option<u32>, secondary: Option<u32>) -> bool {
is_flanked_by_class(classify(primary), classify(secondary))
}
fn is_flanked_by_class(primary: u8, secondary: u8) -> bool {
primary == OTHER_CLASS || (primary == PUNCTUATION_CLASS && secondary != OTHER_CLASS)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct EmphasisSides {
pub can_open: bool,
pub can_close: bool,
}
pub(crate) fn get_emphasis_sides(
marker: u32,
before: Option<u32>,
after: Option<u32>,
) -> EmphasisSides {
let before_class = classify(before);
let after_class = classify(after);
let is_left = is_flanked_by_class(after_class, before_class);
let is_right = is_flanked_by_class(before_class, after_class);
if marker == u32::from(b'_') {
EmphasisSides {
can_close: is_right && (!is_left || after_class == PUNCTUATION_CLASS),
can_open: is_left && (!is_right || before_class == PUNCTUATION_CLASS),
}
} else {
EmphasisSides {
can_close: is_right,
can_open: is_left,
}
}
}
pub(crate) fn is_punctuation_or_space(character: Option<u32>) -> bool {
classify(character) != OTHER_CLASS
}
fn as_micromark_reads(character: Option<u32>) -> Option<u32> {
character.map(|c| {
if c > 0xFFFF {
u32::from(b'a')
} else if matches!(c, 0x0B | 0x2028 | 0x2029 | 0xFEFF) {
u32::from(b' ')
} else {
c
}
})
}
pub(crate) fn is_portably_flanked_by(primary: Option<u32>, secondary: Option<u32>) -> bool {
is_flanked_by(primary, secondary)
&& is_flanked_by(as_micromark_reads(primary), as_micromark_reads(secondary))
}
pub(crate) fn get_portable_emphasis_sides(
marker: u32,
before: Option<u32>,
after: Option<u32>,
) -> EmphasisSides {
let sides = get_emphasis_sides(marker, before, after);
let micromark = get_emphasis_sides(
marker,
as_micromark_reads(before),
as_micromark_reads(after),
);
EmphasisSides {
can_close: sides.can_close && micromark.can_close,
can_open: sides.can_open && micromark.can_open,
}
}
pub(crate) fn is_letter_for_some_reader(character: u32) -> bool {
!is_punctuation_or_space(Some(character))
|| !is_punctuation_or_space(as_micromark_reads(Some(character)))
}
pub(crate) fn character_at(text: &[u16], index: isize) -> Option<u32> {
let unit = unit_at(text, index)?;
if (0xD800..0xDC00).contains(&unit)
&& let Some(low) = unit_at(text, index + 1).filter(|l| (0xDC00..0xE000).contains(l))
{
return Some(0x10000 + ((unit - 0xD800) << 10) + (low - 0xDC00));
}
Some(unit)
}
pub(crate) fn character_before(text: &[u16], index: isize) -> Option<u32> {
match character_at(text, index - 2) {
Some(pair) if pair > 0xFFFF => Some(pair),
_ => unit_at(text, index - 1),
}
}
pub(crate) fn to_character_reference(character: u32) -> String {
format!("&#{character};")
}
pub(crate) fn is_empty_paragraph(node: &NodeRef) -> bool {
if !node.get_latest().is::<ParagraphNode>() {
return false;
}
match node.get_first_child() {
None => true,
Some(first) => {
node.get_children_size() == 1
&& first.is_text()
&& re(r"^\s{0,3}$", "").test(&first.get_text_content())
}
}
}
fn decode_character_reference(digits: Option<&str>, hex: Option<&str>) -> String {
let code_point = match (digits, hex) {
(Some(d), _) => d.parse::<u64>().unwrap_or(u64::MAX),
(None, Some(h)) => u64::from_str_radix(h, 16).unwrap_or(u64::MAX),
(None, None) => 0,
};
if code_point == 0 || (0xD800..=0xDFFF).contains(&code_point) || code_point > 0x10FFFF {
"\u{FFFD}".to_owned()
} else {
char_string(code_point as u32)
}
}
pub(crate) fn unescape_text(value: &str) -> String {
if !value.contains('\\') && !value.contains('&') {
return value.to_owned();
}
re(
r"\\([!-/:-@[-`{-~])|&#(?:(\d{1,7})|[xX]([\da-fA-F]{1,6}));",
"g",
)
.replace_with(value, |m| match m.get(1) {
Some(escaped) => escaped.to_owned(),
None => decode_character_reference(m.get(2), m.get(3)),
})
}
pub(crate) fn is_escaped(text: &[u16], index: usize) -> bool {
let mut backslashes = 0;
let mut i = index;
while i > 0 && text.get(i - 1) == Some(&u16::from(b'\\')) {
backslashes += 1;
i -= 1;
}
backslashes % 2 == 1
}
pub(crate) fn get_delimiter_run_edge(text: &[u16], index: usize, step: isize) -> usize {
let delimiter = if step == 1 {
unit_at(text, index as isize - 1)
} else {
unit_at(text, index as isize)
};
let mut edge = index;
if step == 1 {
while edge < text.len() && Some(u32::from(text[edge])) == delimiter {
edge += 1;
}
} else {
while edge > 0
&& Some(u32::from(text[edge - 1])) == delimiter
&& !is_escaped(text, edge - 1)
{
edge -= 1;
}
}
edge
}
pub(crate) fn is_line_break_node(node: &NodeRef) -> bool {
node.get_latest().is::<LineBreakNode>()
}
pub(crate) fn is_tab_node(node: &NodeRef) -> bool {
node.get_latest().is::<TabNode>()
}
pub(crate) fn is_paragraph_node(node: &NodeRef) -> bool {
node.get_latest().is::<ParagraphNode>()
}
pub(crate) fn text_format(node: &NodeRef) -> f64 {
node.text_data().get_format()
}
pub(crate) fn index_of(text: &[u16], search: &[u16], from: usize) -> Option<usize> {
if search.len() > text.len() || from > text.len() - search.len() {
return None;
}
(from..=text.len() - search.len()).find(|&i| &text[i..i + search.len()] == search)
}
pub(crate) fn last_index_of(text: &[u16], search: &[u16]) -> Option<usize> {
if search.len() > text.len() {
return None;
}
(0..=text.len() - search.len())
.rev()
.find(|&i| &text[i..i + search.len()] == search)
}