Skip to content

Commit

Permalink
fix(renderer): poor handling of windows carriage return
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaLohinski committed Nov 23, 2024
1 parent 4039065 commit 8d56e4e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
8 changes: 4 additions & 4 deletions exec/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ func (r *Renderer) Visit(node nodes.Node) (nodes.Visitor, error) {
return nil, nil
case *nodes.Data:
output := n.Data.Val
if n.RemoveFirstLineReturn && output[0:1] == "\n" {
if n.RemoveFirstLineReturn && (output[0:1] == "\n" || output[0:2] == "\r\n") {
output = output[1:]
}
if n.Trim.Left {
output = strings.TrimLeft(output, " \n\t")
output = strings.TrimLeft(output, " \r\n\t")
}
if n.Trim.Right {
output = strings.TrimRight(output, " \n\t")
output = strings.TrimRight(output, " \r\n\t")
}
if n.RemoveTrailingWhiteSpaceFromLastLine {
lines := strings.Split(output, "\n")
lines = append(lines[0:len(lines)-1], strings.TrimRight(lines[len(lines)-1], " \n\t"))
lines = append(lines[0:len(lines)-1], strings.TrimRight(lines[len(lines)-1], " \n\t\r"))
output = strings.Join(lines, "\n")
}
_, err := io.WriteString(r.Output, output)
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
lineReturnWithOnlyWhiteSpace = regexp.MustCompile("^(\n|\r)[ \t]*$")
lineReturnWithOnlyWhiteSpace = regexp.MustCompile("^(\n|\r\n)[ \t]*$")
)

type ControlStructureGetter interface {
Expand Down
5 changes: 0 additions & 5 deletions tokens/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,6 @@ func isSpace(r rune) bool {
return r == ' ' || r == '\t'
}

// isEndOfLine reports whether r is an end-of-line character.
func isEndOfLine(r rune) bool {
return r == '\r' || r == '\n'
}

// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
Expand Down

0 comments on commit 8d56e4e

Please sign in to comment.