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,57 +21,35 @@ 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() {
1 => Level::H1,
2 => Level::H2,
3 => Level::H3,
_ => return None,
};
// Init text let level = match level.len() {
let text = GString::from(text.as_str()); 1 => Level::H1,
2 => Level::H2,
3 => Level::H3,
_ => return None,
};
if text.trim().is_empty() { // Detect header value
return None; let value = parsed.get(2)?;
}
// Init markup if value.trim().is_empty() {
let markup = match level { return None;
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
return Some(Header {
// level,
text,
markup,
});
}
} }
None // not header line given // Result
Some(Header {
level,
value: String::from(value.as_str()),
})
} }
pub fn text(&self) -> &GString { pub fn level(&self) -> &Level {
&self.text &self.level
} }
pub fn markup(&self) -> &GString { pub fn value(&self) -> &str {
&self.markup self.value.as_str()
} }
} }