parent
981a78f36e
commit
a33a05a60a
@ -0,0 +1,36 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use rocket_dyn_templates::tera::{self, Value};
|
||||||
|
|
||||||
|
pub fn furigana_to_html(text: &str) -> String {
|
||||||
|
// Original regular expression: \[([^\]]*)\]{([^\}]*)}
|
||||||
|
// https://regexr.com/6dspa
|
||||||
|
// https://blog.elnu.com/2022/01/furigana-in-markdown-using-regular-expressions/
|
||||||
|
// The regex crate users curly braces {} as repetition qualifiers,
|
||||||
|
// so { needs to be escaped as \{
|
||||||
|
// Curly brace literals \{ need to have their backslash escaped as \\{
|
||||||
|
// TODO: Modify so <span lang="ja"> only wraps continuous sections of furigana
|
||||||
|
let re = Regex::new(r"\[([^\]]*)\]\{([^\\}]*)}").unwrap();
|
||||||
|
format!("<span lang=\"ja\">{}</span>", re.replace_all(text, "<ruby>$1<rp>(</rp><rt>$2</rt><rp>)</rp></ruby>"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn furigana_filter(value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
|
||||||
|
Ok(Value::String(furigana_to_html(value.as_str().expect("The furigana input must be a string"))))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn furigana_to_html() {
|
||||||
|
use super::furigana_to_html;
|
||||||
|
assert_eq!(
|
||||||
|
furigana_to_html("[振]{ふ}り[仮]{が}[名]{な}"),
|
||||||
|
"<span lang=\"ja\">\
|
||||||
|
<ruby>振<rp>(</rp><rt>ふ</rt><rp>)</rp></ruby>り\
|
||||||
|
<ruby>仮<rp>(</rp><rt>が</rt><rp>)</rp></ruby>\
|
||||||
|
<ruby>名<rp>(</rp><rt>な</rt><rp>)</rp></ruby>\
|
||||||
|
</span>"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue