diff options
author | James Campos <james.r.campos@gmail.com> | 2020-05-06 11:01:17 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2020-05-06 11:01:17 -0700 |
commit | 0992f5906338e7f1d87d87104623a667be8689a3 (patch) | |
tree | 10a23bcca5a66f9283844f57458668e3977b7c62 /src | |
parent | e06348bc9200bf41e0a8aaa54648f65fb4b7b39e (diff) | |
download | bk-0992f5906338e7f1d87d87104623a667be8689a3.tar.gz |
break at hyphen/em-dash
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 456f045..1677bd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,29 +184,43 @@ impl Epub { fn wrap(text: String, width: u16) -> Vec<String> { // XXX assumes a char is 1 unit wide - // TODO break at dash/hyphen let mut wrapped = Vec::new(); let mut start = 0; - let mut space = 0; + let mut brk = 0; let mut line = 0; let mut word = 0; + let mut skip = 0; for (i, c) in text.char_indices() { - if c == ' ' { - space = i; - word = 0; - } else { - word += 1; + match c { + ' ' => { + brk = i; + skip = 1; + word = 0; + } + // https://www.unicode.org/reports/tr14/ + // https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap + // currently only break at hyphen and em-dash :shrug: + '-' | '—' => { + brk = i + c.len_utf8(); + skip = 0; + word = 0; + } + _ => { + word += 1; + } } + if line == width { - wrapped.push(String::from(&text[start..space])); - start = space + 1; + wrapped.push(String::from(&text[start..brk])); + start = brk + skip; line = word; } else { line += 1; } } + wrapped.push(String::from(&text[start..])); wrapped } |