diff --git a/src/lib.rs b/src/lib.rs index 2ba555b..61a82af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -441,22 +441,45 @@ fn apply_furigana<'a>( if pitches[0] == 0 { // 平板. let (s, k) = out.last_mut().unwrap(); + let mark = if k.is_empty() { + // If we're putting this on a non-furigana character, add an + // extra level of furigana to make the formatting consistent. + &[ + "", + " o", + ] + } else { + &["", "o"] + }; let text = if k.is_empty() { s } else { k }; + if text.len() >= 3 && text.is_char_boundary(text.len() - 3) { - text.insert_str(text.len() - 3, ""); - text.insert_str(text.len(), ""); + text.insert_str(text.len() - 3, mark[0]); + text.insert_str(text.len(), mark[1]); } } else { // Everything else. let mut byte_idx = accent::accent_number_to_byte_idx(kana, pitches[0]).unwrap(); for (s, k) in out.iter_mut() { + let mark = if k.is_empty() { + // If we're putting this on a non-furigana character, add an + // extra level of furigana to make the formatting consistent. + &[ + "", + " ", + ] + } else { + &["", ""] + }; let text = if k.is_empty() { s } else { k }; + if byte_idx < text.len() && text.is_char_boundary(byte_idx) && text.is_char_boundary(byte_idx + 3) { - text.insert_str(byte_idx + 3, ""); - text.insert_str(byte_idx, ""); + text.insert_str(byte_idx + 3, mark[1]); + text.insert_str(byte_idx, mark[0]); + break; } byte_idx -= text.len(); } @@ -744,7 +767,7 @@ mod tests { ); assert_eq!( furi_2, - r#"のはいね!"# + r#" のはいね!"# ); } @@ -792,7 +815,7 @@ mod tests { // Ichidan verb. Should only get pitch accent marking in full dictionary form. assert_eq!( gen.add_html_furigana("食べる"), - r#"る"# + r#" る"# ); assert_eq!( gen.add_html_furigana("食べます"), @@ -806,7 +829,7 @@ mod tests { // Godan verb. Should only get pitch accent marking in full dictionary form. assert_eq!( gen.add_html_furigana("泳ぐ"), - r#"ぐ"# + r#"ぐ"# ); assert_eq!( gen.add_html_furigana("泳が"), @@ -818,13 +841,13 @@ mod tests { ); assert_eq!( gen.add_html_furigana("泳ぎ"), - r#"オヨ"# + r#"オヨ "# ); // I-adjective. Should only get pitch accent marking in full dictionary form. assert_eq!( gen.add_html_furigana("早い"), - r#"い"# + r#"い"# ); assert_eq!( gen.add_html_furigana("早く"), @@ -834,19 +857,23 @@ mod tests { // Other. Should always get pitch accent markings. assert_eq!( gen.add_html_furigana("少し"), - r#"し"# + r#"し"# ); assert_eq!( gen.add_html_furigana("綺麗"), - r#"綺麗レイ"# + r#"綺麗レイ"# ); assert_eq!( gen.add_html_furigana("平板"), - r#"平板ヘイバ"# + r#"平板ヘイバo"# ); assert_eq!( gen.add_html_furigana("他"), - r#""# + r#"o"# + ); + assert_eq!( + gen.add_html_furigana("中"), + r#""# ); } }