Skip to content

Commit

Permalink
Make some refactoring and bump version to 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
maksugr committed Jul 24, 2022
1 parent 668964f commit 4eae60a
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "text-to-sounds"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
authors = ["maksugr <[email protected]>"]
description = "Text-to-sounds parsing tool."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ In order to use this crate, you have to add it under `[dependencies]` to your `C

```toml
[dependencies]
text-to-sounds = "1.1.0"
text-to-sounds = "1.1.1"
```

## Javascript / WASM
Expand Down
2 changes: 1 addition & 1 deletion pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ In order to use this crate, you have to add it under `[dependencies]` to your `C

```toml
[dependencies]
text-to-sounds = "1.1.0"
text-to-sounds = "1.1.1"
```

## Javascript / WASM
Expand Down
2 changes: 1 addition & 1 deletion pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"maksugr <[email protected]>"
],
"description": "Text-to-sounds parsing tool.",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT OR Apache-2.0",
"repository": {
"type": "git",
Expand Down
Binary file modified pkg/text_to_sounds_bg.wasm
Binary file not shown.
48 changes: 11 additions & 37 deletions src/highlighter.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
use crate::scanner::Scanner;
use crate::sound::SoundKind;
use crate::utils::any_letter;

const NON_BREAKABLE_HTML_CHAR: char = '\u{a0}';
const PUNCTUATION_CHARS: [char; 7] = ['.', ',', ';', '!', '?', ':', '-'];

fn is_punctuation(c: &char) -> bool {
PUNCTUATION_CHARS.iter().any(|cc| cc == c)
}

fn is_first(scanner: &Scanner) -> bool {
let prev_char = scanner.peek_prev();

scanner.is_first()
|| prev_char == &' '
|| prev_char == &NON_BREAKABLE_HTML_CHAR
|| is_punctuation(prev_char)
}

fn is_last(scanner: &Scanner) -> bool {
let next_char = scanner.peek_next();

scanner.is_last()
|| next_char == &' '
|| next_char == &NON_BREAKABLE_HTML_CHAR
|| is_punctuation(next_char)
}

fn highlight_two_letters(
first_letter: &char,
Expand Down Expand Up @@ -60,7 +34,7 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {

while !scanner.is_done() {
match scanner.peek() {
letter @ ('c' | 'C') if !is_last(&scanner) && any_letter(vec!['h', 'H'], &scanner) => {
letter @ ('c' | 'C') if !scanner.is_last() && scanner.is_next_any(vec!['h', 'H']) => {
let next_letter = scanner.peek_next();

highlight_two_letters(letter, next_letter, SoundKind::Ch, &mut result_text);
Expand All @@ -69,11 +43,11 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {
scanner.pop();
}
letter @ ('p' | 'P' | 't' | 'T' | 'c' | 'C')
if is_first(&scanner) || is_last(&scanner) =>
if scanner.is_first() || scanner.is_last() =>
{
if (letter == &'t' || letter == &'T')
&& !is_last(&scanner)
&& any_letter(vec!['h', 'H'], &scanner)
&& !scanner.is_last()
&& scanner.is_next_any(vec!['h', 'H'])
{
let next_letter = scanner.peek_next();

Expand All @@ -93,15 +67,15 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {

scanner.pop();
}
letter @ ('t' | 'T') if any_letter(vec!['h', 'H'], &scanner) => {
letter @ ('t' | 'T') if scanner.is_next_any(vec!['h', 'H']) => {
let next_letter = scanner.peek_next();

highlight_two_letters(letter, next_letter, SoundKind::Th, &mut result_text);

scanner.pop();
scanner.pop();
}
letter @ ('w' | 'W') if is_first(&scanner) => {
letter @ ('w' | 'W') if scanner.is_first() => {
result_text.push_str(&format!(
"<span class='{:?}'>{}</span>",
SoundKind::W,
Expand All @@ -110,7 +84,7 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {

scanner.pop();
}
letter @ ('v' | 'V') if is_first(&scanner) => {
letter @ ('v' | 'V') if scanner.is_first() => {
result_text.push_str(&format!(
"<span class='{:?}'>{}</span>",
SoundKind::V,
Expand All @@ -120,7 +94,7 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {
scanner.pop();
}
letter @ ('n' | 'N')
if !is_last(&scanner) && any_letter(vec!['g', 'G', 'k', 'K'], &scanner) =>
if !scanner.is_last() && scanner.is_next_any(vec!['g', 'G', 'k', 'K']) =>
{
let next_letter = scanner.peek_next();

Expand All @@ -129,7 +103,7 @@ pub fn highlight<T: AsRef<str>>(text: T) -> String {
scanner.pop();
scanner.pop();
}
letter @ ('j' | 'J') if is_first(&scanner) => {
letter @ ('j' | 'J') if scanner.is_first() => {
result_text.push_str(&format!(
"<span class='{:?}'>{}</span>",
SoundKind::Dj,
Expand Down Expand Up @@ -205,7 +179,7 @@ mod highlight {
}

#[test]
fn it_should_highlight_with_non_breakable_chars() {
fn it_should_highlight_with_non_breakable_char() {
assert_eq!(
highlight("Put\u{a0}W"),
"<span class='Ptk'>P</span>u<span class='Ptk'>t</span>\u{a0}<span class='W'>W</span>"
Expand All @@ -214,7 +188,7 @@ mod highlight {
}

#[test]
fn it_should_highlight_with_punctuation_chars() {
fn it_should_highlight_with_punctuation_char() {
assert_eq!(
highlight("what!the such-exp:the going?Jhon much; Going."),
"<span class='W'>w</span>ha<span class='Ptk'>t</span>!<span class='Th'>th</span>e su<span class='Ch'>ch</span>-ex<span class='Ptk'>p</span>:<span class='Th'>th</span>e goi<span class='Ng'>ng</span>?<span class='Dj'>J</span>hon mu<span class='Ch'>ch</span>; Goi<span class='Ng'>ng</span>."
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//!
//! ```toml
//! [dependencies]
//! text-to-sounds = "1.1.0"
//! text-to-sounds = "1.1.1"
//! ```
//!
//! ## Examples
Expand Down Expand Up @@ -83,7 +83,6 @@ mod parser;
mod scanner;
mod serializer;
mod sound;
mod utils;
mod wasm;

pub use crate::highlighter::highlight;
Expand Down
9 changes: 4 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::scanner::Scanner;
use crate::sound::{Sound, SoundKind};
use crate::utils::any_letter;

fn add_sound_from_two_letters(
first_letter: &char,
Expand Down Expand Up @@ -63,7 +62,7 @@ pub fn parse<T: AsRef<str>>(text: T) -> Vec<Sound> {
while !scanner.is_done() {
match scanner.peek() {
letter @ ('c' | 'C')
if !scanner.is_last() && any_letter(vec!['h', 'H'], &scanner) =>
if !scanner.is_last() && scanner.is_next_any(vec!['h', 'H']) =>
{
let next_letter = scanner.peek_next();

Expand All @@ -77,7 +76,7 @@ pub fn parse<T: AsRef<str>>(text: T) -> Vec<Sound> {
{
if (letter == &'t' || letter == &'T')
&& !scanner.is_last()
&& any_letter(vec!['h', 'H'], &scanner)
&& scanner.is_next_any(vec!['h', 'H'])
{
let next_letter = scanner.peek_next();

Expand All @@ -92,7 +91,7 @@ pub fn parse<T: AsRef<str>>(text: T) -> Vec<Sound> {
sounds.push(Sound::new(SoundKind::Ptk, letter.to_string()));
scanner.pop();
}
letter @ ('t' | 'T') if any_letter(vec!['h', 'H'], &scanner) => {
letter @ ('t' | 'T') if scanner.is_next_any(vec!['h', 'H']) => {
let next_letter = scanner.peek_next();

add_sound_from_two_letters(letter, next_letter, SoundKind::Th, &mut sounds);
Expand All @@ -109,7 +108,7 @@ pub fn parse<T: AsRef<str>>(text: T) -> Vec<Sound> {
scanner.pop();
}
letter @ ('n' | 'N')
if !scanner.is_last() && any_letter(vec!['g', 'G', 'k', 'K'], &scanner) =>
if !scanner.is_last() && scanner.is_next_any(vec!['g', 'G', 'k', 'K']) =>
{
let next_letter = scanner.peek_next();

Expand Down
Loading

0 comments on commit 4eae60a

Please sign in to comment.