add quote support

This commit is contained in:
yggverse 2026-03-09 03:00:58 +02:00
parent c732964494
commit cab1610e1f
3 changed files with 44 additions and 2 deletions

View file

@ -112,6 +112,7 @@ impl Markdown {
// * keep in order! // * keep in order!
tag::header(&buffer, &tag); tag::header(&buffer, &tag);
tag::quote(&buffer, &tag);
reference::image_link(&buffer, &tag, base, &link_color.0, &mut links); reference::image_link(&buffer, &tag, base, &link_color.0, &mut links);
reference::image(&buffer, &tag, base, &link_color.0, &mut links); reference::image(&buffer, &tag, base, &link_color.0, &mut links);

View file

@ -131,3 +131,43 @@ fn test_regex_header() {
assert_eq!(&first["level"], "##"); assert_eq!(&first["level"], "##");
assert_eq!(&first["title"], "Title ![alt](https://link.com)"); assert_eq!(&first["title"], "Title ![alt](https://link.com)");
} }
// Quotes
const REGEX_QUOTE: &str = r"(?m)^>\s+(?P<text>.*)$";
/// Apply quote `Tag` to given `TextBuffer`
pub fn quote(buffer: &TextBuffer, tag: &Tag) {
let (start, end) = buffer.bounds();
let full_content = buffer.text(&start, &end, true).to_string();
let matches: Vec<_> = Regex::new(REGEX_QUOTE)
.unwrap()
.captures_iter(&full_content)
.collect();
for cap in matches.into_iter().rev() {
let full_match = cap.get(0).unwrap();
let start_char_offset = full_content[..full_match.start()].chars().count() as i32;
let end_char_offset = full_content[..full_match.end()].chars().count() as i32;
let mut start_iter = buffer.iter_at_offset(start_char_offset);
let mut end_iter = buffer.iter_at_offset(end_char_offset);
buffer.delete(&mut start_iter, &mut end_iter);
buffer.insert_with_tags(&mut start_iter, &cap["text"], &[&tag.quote])
}
}
#[test]
fn test_regex_quote() {
let cap: Vec<_> = Regex::new(REGEX_QUOTE)
.unwrap()
.captures_iter(r"> Some quote with ![img](https://link.com)")
.collect();
let first = cap.get(0).unwrap();
assert_eq!(&first[0], "> Some quote with ![img](https://link.com)");
assert_eq!(&first["text"], "Some quote with ![img](https://link.com)");
}

View file

@ -1,4 +1,4 @@
use gtk::{TextTag, WrapMode}; use gtk::{TextTag, WrapMode::Word, pango::Style::Italic};
pub trait Quote { pub trait Quote {
fn quote() -> Self; fn quote() -> Self;
@ -8,7 +8,8 @@ impl Quote for TextTag {
fn quote() -> Self { fn quote() -> Self {
TextTag::builder() TextTag::builder()
.left_margin(28) .left_margin(28)
.wrap_mode(WrapMode::Word) .wrap_mode(Word)
.style(Italic) // what about the italic tags decoration? @TODO
.build() .build()
} }
} }