From 496fe0af1cb212a6a4af932b6c188bf082d15383 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 25 Aug 2020 15:56:55 +0100 Subject: 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 --- cmd/dehyphenate/main.go | 9 +++++++-- 1 file 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) -- cgit v1.2.1-24-ge1ad