fix: don't add spacing after text ending with punctuation
1 file changed, 14 insertions(+), 4 deletions(-)
changed files
M format.go → format.go
@@ -75,8 +75,14 @@ r, _ := utf8.DecodeRuneInString(s) return r } +func getLastRune(s string) rune { + r, _ := utf8.DecodeLastRuneInString(s) + return r +} + func hasSingleTextChild(n *html.Node) bool { - return n != nil && n.FirstChild != nil && n.FirstChild == n.LastChild && n.FirstChild.Type == html.TextNode + return n != nil && n.FirstChild != nil && n.FirstChild == n.LastChild && + n.FirstChild.Type == html.TextNode } func printNode(w io.Writer, n *html.Node, pre bool, level int) (err error) {@@ -121,7 +127,8 @@ } else { if _, err = fmt.Fprint(w, s); err != nil { return } - if !hasSingleTextChild(n.Parent) { + if !hasSingleTextChild(n.Parent) && + (n.NextSibling == nil || !unicode.IsPunct(getLastRune(strings.TrimSpace(s)))) { if _, err = fmt.Fprint(w, "\n"); err != nil { return }@@ -129,8 +136,11 @@ } } } case html.ElementNode: - if err = printIndent(w, level); err != nil { - return + if n.PrevSibling == nil || + (n.PrevSibling.Type != html.TextNode || !unicode.IsPunct(getLastRune(strings.TrimSpace(n.PrevSibling.Data)))) { + if err = printIndent(w, level); err != nil { + return + } } if _, err = fmt.Fprintf(w, "<%s", n.Data); err != nil { return