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,13 +81,17 @@ impl Tags {
reference::render_links(buffer, base, link_color, links); reference::render_links(buffer, base, link_color, links);
// Cleanup unformatted escape chars // Cleanup unformatted escape chars
for escapes in ESCAPES {
for escape in *escapes {
let mut cursor = buffer.start_iter(); let mut cursor = buffer.start_iter();
while let Some((mut match_start, mut match_end)) = while let Some((mut match_start, mut match_end)) =
cursor.forward_search(ESC, TextSearchFlags::CASE_INSENSITIVE, None) cursor.forward_search(escape, TextSearchFlags::CASE_INSENSITIVE, None)
{ {
buffer.delete(&mut match_start, &mut match_end); buffer.delete(&mut match_start, &mut match_end);
cursor = match_end; cursor = match_end;
} }
}
}
// Render placeholders // Render placeholders
self.code.render(buffer); self.code.render(buffer);
@ -99,7 +103,12 @@ impl Tags {
s = reference::strip_tags(&s); s = reference::strip_tags(&s);
s = strike::strip_tags(&s); s = strike::strip_tags(&s);
s = underline::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) 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>[^*]+)\*\*"; const REGEX_BOLD: &str = r"\*\*(?P<text>[^*]+)\*\*";
pub const ESCAPES: &[&str] = &["\\*"]; // same with list
pub struct Bold(TextTag); pub struct Bold(TextTag);
impl Bold { impl Bold {

View file

@ -12,6 +12,9 @@ use syntax::Syntax;
const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```"; const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```";
// same with pre
// pub const ESCAPES: &[&str] = &["\\`"];
struct Entry { struct Entry {
alt: Option<String>, alt: Option<String>,
data: 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>.*)$"; const REGEX_HEADER: &str = r"(?m)^(?P<level>#{1,6})\s+(?P<title>.*)$";
pub const ESCAPES: &[&str] = &["\\#"];
pub struct Header { pub struct Header {
h1: TextTag, h1: TextTag,
h2: TextTag, h2: TextTag,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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