drop pango markup features

This commit is contained in:
yggverse 2024-10-12 08:51:05 +03:00
parent 6aedb21794
commit adcf05f43a

View File

@ -1,4 +1,4 @@
use gtk::glib::{gformat, markup_escape_text, GString, Regex, RegexCompileFlags, RegexMatchFlags}; use gtk::glib::{Regex, RegexCompileFlags, RegexMatchFlags};
pub enum Level { pub enum Level {
H1, H1,
@ -7,13 +7,12 @@ pub enum Level {
} }
pub struct Header { pub struct Header {
// level: Level, value: String,
text: GString, level: Level,
markup: GString,
} }
impl Header { impl Header {
pub fn from(line: &str) -> Option<Header> { pub fn from(line: &str) -> Option<Self> {
// Parse line // Parse line
let parsed = Regex::split_simple( let parsed = Regex::split_simple(
r"^(#{1,3})\s*(.+)$", r"^(#{1,3})\s*(.+)$",
@ -22,10 +21,9 @@ impl Header {
RegexMatchFlags::DEFAULT, RegexMatchFlags::DEFAULT,
); );
// Validate match results // Detect header level
if let Some(text) = parsed.get(2) { let level = parsed.get(1)?;
if let Some(level) = parsed.get(1) {
// Init level
let level = match level.len() { let level = match level.len() {
1 => Level::H1, 1 => Level::H1,
2 => Level::H2, 2 => Level::H2,
@ -33,46 +31,25 @@ impl Header {
_ => return None, _ => return None,
}; };
// Init text // Detect header value
let text = GString::from(text.as_str()); let value = parsed.get(2)?;
if text.trim().is_empty() { if value.trim().is_empty() {
return None; return None;
} }
// Init markup
let markup = match level {
Level::H1 => gformat!(
"<span size=\"xx-large\">{}</span>\n",
markup_escape_text(&text)
),
Level::H2 => gformat!(
"<span size=\"x-large\">{}</span>\n",
markup_escape_text(&text)
),
Level::H3 => gformat!(
"<span size=\"large\">{}</span>\n",
markup_escape_text(&text)
),
};
// Result // Result
return Some(Header { Some(Header {
// level, level,
text, value: String::from(value.as_str()),
markup, })
});
}
} }
None // not header line given pub fn level(&self) -> &Level {
&self.level
} }
pub fn text(&self) -> &GString { pub fn value(&self) -> &str {
&self.text self.value.as_str()
}
pub fn markup(&self) -> &GString {
&self.markup
} }
} }