mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 08:35:28 +00:00
add quote support
This commit is contained in:
parent
c732964494
commit
cab1610e1f
3 changed files with 44 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -131,3 +131,43 @@ fn test_regex_header() {
|
|||
assert_eq!(&first["level"], "##");
|
||||
assert_eq!(&first["title"], "Title ");
|
||||
}
|
||||
|
||||
// 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 ")
|
||||
.collect();
|
||||
|
||||
let first = cap.get(0).unwrap();
|
||||
assert_eq!(&first[0], "> Some quote with ");
|
||||
assert_eq!(&first["text"], "Some quote with ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue