add list and quote tag support

This commit is contained in:
yggverse 2024-10-14 08:15:57 +03:00
parent 86347c42b9
commit d646c95563
5 changed files with 100 additions and 1 deletions

View File

@ -3,6 +3,8 @@ mod widget;
use parser::header::Header;
use parser::link::Link;
use parser::list::List;
use parser::quote::Quote;
use widget::Widget;
use adw::StyleManager;
@ -10,6 +12,7 @@ use gtk::{
gdk::{BUTTON_MIDDLE, BUTTON_PRIMARY},
gio::SimpleAction,
glib::{GString, TimeZone, Uri},
pango::Style,
prelude::{ActionExt, TextBufferExt, TextBufferExtManual, TextViewExt, ToVariant, WidgetExt},
EventControllerMotion, GestureClick, TextBuffer, TextTag, TextView, TextWindowType, WrapMode,
};
@ -130,6 +133,42 @@ impl Reader {
continue;
}
// Is list
if let Some(list) = List::from(line) {
// Build tag from level parsed
let tag = TextTag::builder().wrap_mode(gtk::WrapMode::Word).build();
// Register tag in buffer
buffer.tag_table().add(&tag);
// Append value to buffer
buffer.insert(&mut buffer.end_iter(), "");
buffer.insert_with_tags(&mut buffer.end_iter(), list.value.as_str(), &[&tag]);
buffer.insert(&mut buffer.end_iter(), "\n");
// Skip other actions for this line
continue;
}
// Is quote
if let Some(quote) = Quote::from(line) {
// Build tag from level parsed
let tag = TextTag::builder()
.style(Style::Italic)
.wrap_mode(gtk::WrapMode::Word)
.build();
// Register tag in buffer
buffer.tag_table().add(&tag);
// Append value to buffer
buffer.insert_with_tags(&mut buffer.end_iter(), quote.value.as_str(), &[&tag]);
buffer.insert(&mut buffer.end_iter(), "\n");
// Skip other actions for this line
continue;
}
// Nothing match custom tags above,
// just append plain text covered in empty tag (to handle controller events properly)
let tag = TextTag::builder().wrap_mode(WrapMode::Word).build();

View File

@ -1,2 +1,4 @@
pub mod header;
pub mod link;
pub mod list;
pub mod quote;

View File

@ -39,7 +39,7 @@ impl Header {
}
// Result
Some(Header {
Some(Self {
level,
value: GString::from(value.as_str()),
})

View File

@ -0,0 +1,29 @@
use gtk::glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags};
pub struct List {
pub value: GString,
}
impl List {
pub fn from(line: &str) -> Option<Self> {
// Parse line
let regex = Regex::split_simple(
r"^\*\s*(.+)$",
line,
RegexCompileFlags::DEFAULT,
RegexMatchFlags::DEFAULT,
);
// Detect value
let value = regex.get(1)?;
if value.trim().is_empty() {
return None;
}
// Result
Some(Self {
value: GString::from(value.as_str()),
})
}
}

View File

@ -0,0 +1,29 @@
use gtk::glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags};
pub struct Quote {
pub value: GString,
}
impl Quote {
pub fn from(line: &str) -> Option<Self> {
// Parse line
let regex = Regex::split_simple(
r"^>\s*(.+)$",
line,
RegexCompileFlags::DEFAULT,
RegexMatchFlags::DEFAULT,
);
// Detect value
let value = regex.get(1)?;
if value.trim().is_empty() {
return None;
}
// Result
Some(Self {
value: GString::from(value.as_str()),
})
}
}