mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 17:45:28 +00:00
move regex logic, add annotation tag, add some tests
This commit is contained in:
parent
e61b6c400a
commit
5675809320
1 changed files with 66 additions and 9 deletions
|
|
@ -1,5 +1,10 @@
|
||||||
use gtk::glib::{Uri, UriFlags};
|
use gtk::glib::{Uri, UriFlags};
|
||||||
|
|
||||||
|
pub const REGEX_LINK: &str = r"\[(?P<text>[^\]]+)\]\((?P<url>[^\)]+)\)";
|
||||||
|
|
||||||
|
pub const REGEX_IMAGE_LINK: &str =
|
||||||
|
r"\[(?P<is_img>!)\[(?P<alt>[^\]]+)\]\((?P<img_url>[^\)]+)\)\]\((?P<link_url>[^\)]+)\)";
|
||||||
|
|
||||||
pub struct Reference {
|
pub struct Reference {
|
||||||
pub uri: Uri,
|
pub uri: Uri,
|
||||||
pub alt: String,
|
pub alt: String,
|
||||||
|
|
@ -53,22 +58,74 @@ impl Reference {
|
||||||
pub fn into_buffer(
|
pub fn into_buffer(
|
||||||
self,
|
self,
|
||||||
buffer: >k::TextBuffer,
|
buffer: >k::TextBuffer,
|
||||||
|
position: &mut gtk::TextIter,
|
||||||
link_color: >k::gdk::RGBA,
|
link_color: >k::gdk::RGBA,
|
||||||
tag: &super::Tag,
|
tag: &super::Tag,
|
||||||
|
is_annotation: bool,
|
||||||
links: &mut std::collections::HashMap<gtk::TextTag, Uri>,
|
links: &mut std::collections::HashMap<gtk::TextTag, Uri>,
|
||||||
) {
|
) {
|
||||||
use gtk::prelude::{TextBufferExt, TextBufferExtManual};
|
use gtk::{TextTag, WrapMode, prelude::TextBufferExtManual};
|
||||||
let a = gtk::TextTag::builder()
|
let a = if is_annotation {
|
||||||
.foreground_rgba(link_color)
|
buffer.insert_with_tags(position, " ", &[]);
|
||||||
// .foreground_rgba(&adw::StyleManager::default().accent_color_rgba())
|
TextTag::builder()
|
||||||
// @TODO adw 1.6 / ubuntu 24.10+
|
.foreground_rgba(link_color)
|
||||||
.sentence(true)
|
// .foreground_rgba(&adw::StyleManager::default().accent_color_rgba())
|
||||||
.wrap_mode(gtk::WrapMode::Word)
|
// @TODO adw 1.6 / ubuntu 24.10+
|
||||||
.build();
|
.pixels_above_lines(4)
|
||||||
|
.pixels_below_lines(4)
|
||||||
|
.rise(5000)
|
||||||
|
.scale(0.8)
|
||||||
|
.wrap_mode(WrapMode::Word)
|
||||||
|
.build()
|
||||||
|
} else {
|
||||||
|
TextTag::builder()
|
||||||
|
.foreground_rgba(link_color)
|
||||||
|
// .foreground_rgba(&adw::StyleManager::default().accent_color_rgba())
|
||||||
|
// @TODO adw 1.6 / ubuntu 24.10+
|
||||||
|
.sentence(true)
|
||||||
|
.wrap_mode(WrapMode::Word)
|
||||||
|
.build()
|
||||||
|
};
|
||||||
if !tag.text_tag_table.add(&a) {
|
if !tag.text_tag_table.add(&a) {
|
||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
buffer.insert_with_tags(&mut buffer.end_iter(), &self.alt, &[&a]);
|
buffer.insert_with_tags(position, &self.alt, &[&a]);
|
||||||
links.insert(a, self.uri);
|
links.insert(a, self.uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_regex_link() {
|
||||||
|
let cap: Vec<_> = regex::Regex::new(REGEX_LINK)
|
||||||
|
.unwrap()
|
||||||
|
.captures_iter(r#"[link1](https://link1.com) [link2](https://link2.com)"#)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let first = cap.get(0).unwrap();
|
||||||
|
assert_eq!(&first["text"], "link1");
|
||||||
|
assert_eq!(&first["url"], "https://link1.com");
|
||||||
|
|
||||||
|
let second = cap.get(1).unwrap();
|
||||||
|
assert_eq!(&second["text"], "link2");
|
||||||
|
assert_eq!(&second["url"], "https://link2.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_regex_image_link() {
|
||||||
|
let cap: Vec<_> = regex::Regex::new(
|
||||||
|
REGEX_IMAGE_LINK,
|
||||||
|
)
|
||||||
|
.unwrap().captures_iter(
|
||||||
|
r#"[](https://image2.com) [](https://image4.com)"#
|
||||||
|
).collect();
|
||||||
|
|
||||||
|
let first = cap.get(0).unwrap();
|
||||||
|
assert_eq!(&first["alt"], "image1");
|
||||||
|
assert_eq!(&first["img_url"], "https://image1.com");
|
||||||
|
assert_eq!(&first["link_url"], "https://image2.com");
|
||||||
|
|
||||||
|
let second = cap.get(1).unwrap();
|
||||||
|
assert_eq!(&second["alt"], "image3");
|
||||||
|
assert_eq!(&second["img_url"], "https://image3.com");
|
||||||
|
assert_eq!(&second["link_url"], "https://image4.com");
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue