diff --git a/src/text/md.rs b/src/text/md.rs index 5dfacec..6e56756 100644 --- a/src/text/md.rs +++ b/src/text/md.rs @@ -1,11 +1,10 @@ use hypertext::Renderable; use once_cell::sync::Lazy; -use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd}; +use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd, TextMergeStream}; +use regex::Regex; use crate::ts; -use super::ruby; - static OPTS: Lazy = Lazy::new(|| Options::empty() @@ -33,7 +32,8 @@ static KATEX_B: Lazy = Lazy::new(|| pub fn parse(text: &str) -> String { - let stream = Parser::new_ext(text, *OPTS) + let stream = Parser::new_ext(text, *OPTS); + let stream = TextMergeStream::new(stream) .map(make_math) .map(make_emoji) .collect::>(); @@ -83,20 +83,50 @@ fn make_code(es: Vec) -> Vec { buff } +static RE_RUBY: Lazy = Lazy::new(|| + Regex::new(r"\[([^\]]+)\]\{([^}]+)\}").unwrap() +); + +#[derive(Debug)] +enum Annotated<'a> { + Text(&'a str), + Ruby(&'a str, &'a str), +} + + +fn annotate(input: &str) -> Vec { + let mut parts: Vec = Vec::new(); + let mut last_index = 0; + + for cap in RE_RUBY.captures_iter(input) { + let text = cap.get(1).unwrap().as_str(); + let ruby = cap.get(2).unwrap().as_str(); + let index = cap.get(0).unwrap().start(); + + if index > last_index { + parts.push(Annotated::Text(&input[last_index..index])); + } + + parts.push(Annotated::Ruby(text, ruby)); + last_index = cap.get(0).unwrap().end(); + } + + if last_index < input.len() { + parts.push(Annotated::Text(&input[last_index..])); + } + + parts +} + fn make_ruby(event: Event) -> Vec { match event { - Event::Text(text) => { - let mut buff = Vec::new(); - - for item in ruby::annotate(&text) { - match item { - ruby::Annotated::Text(text) => buff.push(Event::Text(text.to_owned().into())), - ruby::Annotated::Ruby(t, f) => buff.push(Event::InlineHtml(format!("{t}({f})").into())), - }; - } - - buff - }, + Event::Text(ref text) => annotate(&text) + .into_iter() + .map(|el| match el { + Annotated::Text(text) => Event::Text(text.to_owned().into()), + Annotated::Ruby(t, f) => Event::InlineHtml(format!("{t}({f})").into()), + }) + .collect(), _ => vec![event], } } diff --git a/src/text/mod.rs b/src/text/mod.rs index 65919e6..0301ec5 100644 --- a/src/text/mod.rs +++ b/src/text/mod.rs @@ -1,2 +1 @@ pub mod md; -pub mod ruby; diff --git a/src/text/ruby.rs b/src/text/ruby.rs deleted file mode 100644 index ec4df02..0000000 --- a/src/text/ruby.rs +++ /dev/null @@ -1,38 +0,0 @@ -use once_cell::sync::Lazy; -use regex::Regex; - - -static RE_RUBY: Lazy = Lazy::new(|| - Regex::new(r"\[([^\]]+)\]\{([^}]+)\}").unwrap() -); - -#[derive(Debug)] -pub(crate) enum Annotated<'a> { - Text(&'a str), - Ruby(&'a str, &'a str), -} - - -pub fn annotate(input: &str) -> Vec { - let mut parts: Vec = Vec::new(); - let mut last_index = 0; - - for cap in RE_RUBY.captures_iter(input) { - let text = cap.get(1).unwrap().as_str(); - let ruby = cap.get(2).unwrap().as_str(); - let index = cap.get(0).unwrap().start(); - - if index > last_index { - parts.push(Annotated::Text(&input[last_index..index])); - } - - parts.push(Annotated::Ruby(text, ruby)); - last_index = cap.get(0).unwrap().end(); - } - - if last_index < input.len() { - parts.push(Annotated::Text(&input[last_index..])); - } - - parts -}