diff options
author | Nick White <git@njw.name> | 2020-08-25 15:56:55 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-08-25 15:56:55 +0100 |
commit | 496fe0af1cb212a6a4af932b6c188bf082d15383 (patch) | |
tree | 42010406d561ed26c6f4288445adbef2aec1c8ad | |
parent | 8077bcde1f07d697d78f478f04e251b96bff3bbf (diff) |
Fixes to dehyphenate
- Ensure a final hyphen on the last word of a page isn't removed
- Only try to add a next word if there is one to take
- Ensure that if a single word is on the following line, which is
taken, then the line is blanked
-rw-r--r-- | cmd/dehyphenate/main.go | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/cmd/dehyphenate/main.go b/cmd/dehyphenate/main.go index ed8eb13..afd1aac 100644 --- a/cmd/dehyphenate/main.go +++ b/cmd/dehyphenate/main.go @@ -68,11 +68,16 @@ func main() { for i, line := range lines { words := strings.Split(line, " ") last := words[len(words)-1] - if len(last) > 0 && last[len(last) - 1] == '-' { + // the - 2 here is to account for a trailing newline and counting from zero + if len(last) > 0 && last[len(last) - 1] == '-' && i < len(lines) - 2 { nextwords := strings.Split(lines[i+1], " ") - line = line[0:len(line)-1] + nextwords[0] + if len(nextwords) > 0 { + line = line[0:len(line)-1] + nextwords[0] + } if len(nextwords) > 1 { lines[i+1] = strings.Join(nextwords[1:], " ") + } else { + lines[i+1] = "" } } newlines = append(newlines, line) |