handle escape for defined matches only

This commit is contained in:
yggverse 2026-03-11 20:21:37 +02:00
parent 905eee0aab
commit ca29f68f69
10 changed files with 50 additions and 8 deletions

View file

@ -81,12 +81,16 @@ impl Tags {
reference::render_links(buffer, base, link_color, links);
// Cleanup unformatted escape chars
let mut cursor = buffer.start_iter();
while let Some((mut match_start, mut match_end)) =
cursor.forward_search(ESC, TextSearchFlags::CASE_INSENSITIVE, None)
{
buffer.delete(&mut match_start, &mut match_end);
cursor = match_end;
for escapes in ESCAPES {
for escape in *escapes {
let mut cursor = buffer.start_iter();
while let Some((mut match_start, mut match_end)) =
cursor.forward_search(escape, TextSearchFlags::CASE_INSENSITIVE, None)
{
buffer.delete(&mut match_start, &mut match_end);
cursor = match_end;
}
}
}
// Render placeholders
@ -99,7 +103,12 @@ impl Tags {
s = reference::strip_tags(&s);
s = strike::strip_tags(&s);
s = underline::strip_tags(&s);
s.replace(ESC, "")
for escapes in ESCAPES {
for escape in *escapes {
s = s.replace(escape, "");
}
}
s
})
}
}
@ -109,4 +118,17 @@ pub fn format_header_fragment(value: &str) -> GString {
Uri::escape_string(&value.to_lowercase().replace(" ", "-"), None, true)
}
const ESC: &str = "\\";
const ESCAPES: &[&[&str]] = &[
&["\\\n"],
bold::ESCAPES,
// same with pre
// code::ESCAPES,
header::ESCAPES,
// same with bold and reference
// list::ESCAPES,
pre::ESCAPES,
quote::ESCAPES,
reference::ESCAPES,
strike::ESCAPES,
underline::ESCAPES,
];

View file

@ -7,6 +7,8 @@ use regex::Regex;
const REGEX_BOLD: &str = r"\*\*(?P<text>[^*]+)\*\*";
pub const ESCAPES: &[&str] = &["\\*"]; // same with list
pub struct Bold(TextTag);
impl Bold {

View file

@ -12,6 +12,9 @@ use syntax::Syntax;
const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```";
// same with pre
// pub const ESCAPES: &[&str] = &["\\`"];
struct Entry {
alt: Option<String>,
data: String,

View file

@ -8,6 +8,8 @@ use std::collections::HashMap;
const REGEX_HEADER: &str = r"(?m)^(?P<level>#{1,6})\s+(?P<title>.*)$";
pub const ESCAPES: &[&str] = &["\\#"];
pub struct Header {
h1: TextTag,
h2: TextTag,

View file

@ -7,6 +7,9 @@ use regex::Regex;
const REGEX_LIST: &str =
r"(?m)^(?P<level>[ \t]*)\*[ \t]+(?:(?P<state>\[[ xX]\])[ \t]+)?(?P<text>.*)";
// same with bold and reference
// pub const ESCAPES: &[&str] = &["\\*","\\[","\\]"];
struct State(bool);
impl State {

View file

@ -10,6 +10,8 @@ const REGEX_PRE: &str = r"`(?P<text>[^`]+)`";
const TAG_FONT: &str = "monospace"; // @TODO
const TAG_SCALE: f64 = 0.9;
pub const ESCAPES: &[&str] = &["\\`"]; // same with code
pub struct Pre(TextTag);
impl Pre {

View file

@ -8,6 +8,8 @@ use regex::Regex;
const REGEX_QUOTE: &str = r"(?m)>\s*(?P<text>.*)$";
pub const ESCAPES: &[&str] = &["\\>"];
pub struct Quote(TextTag);
impl Quote {

View file

@ -12,6 +12,8 @@ const REGEX_IMAGE: &str = r"!\[(?P<alt>[^\]]*)\]\((?P<url>[^\)]+)\)";
const REGEX_IMAGE_LINK: &str =
r"\[(?P<is_img>!)\[(?P<alt>[^\]]*)\]\((?P<img_url>[^\)]+)\)\]\((?P<link_url>[^\)]+)\)";
pub const ESCAPES: &[&str] = &["\\!", "\\[", "\\]", "\\(", "\\)"];
struct Reference {
uri: Uri,
alt: String,

View file

@ -7,6 +7,8 @@ use regex::Regex;
const REGEX_STRIKE: &str = r"~~(?P<text>.+?)~~";
pub const ESCAPES: &[&str] = &["\\~"];
pub struct Strike(TextTag);
impl Strike {

View file

@ -8,6 +8,8 @@ use regex::Regex;
const REGEX_UNDERLINE: &str = r"\b_(?P<text>[^_]+)_\b";
pub const ESCAPES: &[&str] = &["\\_"];
pub struct Underline(TextTag);
impl Underline {