mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-24 18:14:14 +00:00
add list and quote tag support
This commit is contained in:
parent
86347c42b9
commit
d646c95563
@ -3,6 +3,8 @@ mod widget;
|
|||||||
|
|
||||||
use parser::header::Header;
|
use parser::header::Header;
|
||||||
use parser::link::Link;
|
use parser::link::Link;
|
||||||
|
use parser::list::List;
|
||||||
|
use parser::quote::Quote;
|
||||||
use widget::Widget;
|
use widget::Widget;
|
||||||
|
|
||||||
use adw::StyleManager;
|
use adw::StyleManager;
|
||||||
@ -10,6 +12,7 @@ use gtk::{
|
|||||||
gdk::{BUTTON_MIDDLE, BUTTON_PRIMARY},
|
gdk::{BUTTON_MIDDLE, BUTTON_PRIMARY},
|
||||||
gio::SimpleAction,
|
gio::SimpleAction,
|
||||||
glib::{GString, TimeZone, Uri},
|
glib::{GString, TimeZone, Uri},
|
||||||
|
pango::Style,
|
||||||
prelude::{ActionExt, TextBufferExt, TextBufferExtManual, TextViewExt, ToVariant, WidgetExt},
|
prelude::{ActionExt, TextBufferExt, TextBufferExtManual, TextViewExt, ToVariant, WidgetExt},
|
||||||
EventControllerMotion, GestureClick, TextBuffer, TextTag, TextView, TextWindowType, WrapMode,
|
EventControllerMotion, GestureClick, TextBuffer, TextTag, TextView, TextWindowType, WrapMode,
|
||||||
};
|
};
|
||||||
@ -130,6 +133,42 @@ impl Reader {
|
|||||||
continue;
|
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,
|
// Nothing match custom tags above,
|
||||||
// just append plain text covered in empty tag (to handle controller events properly)
|
// just append plain text covered in empty tag (to handle controller events properly)
|
||||||
let tag = TextTag::builder().wrap_mode(WrapMode::Word).build();
|
let tag = TextTag::builder().wrap_mode(WrapMode::Word).build();
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
pub mod header;
|
pub mod header;
|
||||||
pub mod link;
|
pub mod link;
|
||||||
|
pub mod list;
|
||||||
|
pub mod quote;
|
||||||
|
@ -39,7 +39,7 @@ impl Header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Result
|
// Result
|
||||||
Some(Header {
|
Some(Self {
|
||||||
level,
|
level,
|
||||||
value: GString::from(value.as_str()),
|
value: GString::from(value.as_str()),
|
||||||
})
|
})
|
||||||
|
@ -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()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -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()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user