diff --git a/src/app/browser/window/tab/item/page/content/text/markdown.rs b/src/app/browser/window/tab/item/page/content/text/markdown.rs index 5dd62197..508a93d6 100644 --- a/src/app/browser/window/tab/item/page/content/text/markdown.rs +++ b/src/app/browser/window/tab/item/page/content/text/markdown.rs @@ -112,6 +112,7 @@ impl Markdown { // * keep in order! tag::header(&buffer, &tag); + tag::quote(&buffer, &tag); reference::image_link(&buffer, &tag, base, &link_color.0, &mut links); reference::image(&buffer, &tag, base, &link_color.0, &mut links); diff --git a/src/app/browser/window/tab/item/page/content/text/markdown/tag.rs b/src/app/browser/window/tab/item/page/content/text/markdown/tag.rs index 3a814541..cc298c95 100644 --- a/src/app/browser/window/tab/item/page/content/text/markdown/tag.rs +++ b/src/app/browser/window/tab/item/page/content/text/markdown/tag.rs @@ -131,3 +131,43 @@ fn test_regex_header() { assert_eq!(&first["level"], "##"); assert_eq!(&first["title"], "Title ![alt](https://link.com)"); } + +// Quotes + +const REGEX_QUOTE: &str = r"(?m)^>\s+(?P.*)$"; + +/// 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)"); +} diff --git a/src/app/browser/window/tab/item/page/content/text/markdown/tag/quote.rs b/src/app/browser/window/tab/item/page/content/text/markdown/tag/quote.rs index 58d41e28..8b937a76 100644 --- a/src/app/browser/window/tab/item/page/content/text/markdown/tag/quote.rs +++ b/src/app/browser/window/tab/item/page/content/text/markdown/tag/quote.rs @@ -1,4 +1,4 @@ -use gtk::{TextTag, WrapMode}; +use gtk::{TextTag, WrapMode::Word, pango::Style::Italic}; pub trait Quote { fn quote() -> Self; @@ -8,7 +8,8 @@ impl Quote for TextTag { fn quote() -> Self { TextTag::builder() .left_margin(28) - .wrap_mode(WrapMode::Word) + .wrap_mode(Word) + .style(Italic) // what about the italic tags decoration? @TODO .build() } }