Skip to content

Commit

Permalink
feat: normalise line breaks and path separators to Linux style, on Wi…
Browse files Browse the repository at this point in the history
…ndows, fixes #539 (#696)
  • Loading branch information
a-h authored Apr 22, 2024
1 parent 210391f commit 41d5003
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.664
0.2.668
2 changes: 2 additions & 0 deletions cmd/templ/generatecmd/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func (h *FSEventHandler) generate(ctx context.Context, fileName string) (goUpdat
if err != nil {
return false, false, nil, fmt.Errorf("failed to get relative path for %q: %w", fileName, err)
}
// Convert Windows file paths to Unix-style for consistency.
relFilePath = filepath.ToSlash(relFilePath)

var b bytes.Buffer
sourceMap, literals, err := generator.Generate(t, &b, append(h.genOpts, generator.WithFileName(relFilePath))...)
Expand Down
2 changes: 1 addition & 1 deletion parser/v2/textparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/a-h/parse"
)

var tagTemplOrNewLine = parse.Any(parse.Rune('<'), parse.Rune('{'), parse.Rune('}'), parse.Rune('\n'))
var tagTemplOrNewLine = parse.Any(parse.Rune('<'), parse.Rune('{'), parse.Rune('}'), parse.String("\r\n"), parse.Rune('\n'))

var textParser = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
from := pi.Position()
Expand Down
16 changes: 16 additions & 0 deletions parser/v2/textparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ func TestTextParser(t *testing.T) {
Value: "abcdef&#x20;ghijk",
},
},
{
name: "Multiline text is colected line by line",
input: "Line 1\nLine 2",
expected: Text{
Value: "Line 1",
TrailingSpace: "\n",
},
},
{
name: "Multiline text is colected line by line (Windows)",
input: "Line 1\r\nLine 2",
expected: Text{
Value: "Line 1",
TrailingSpace: "\n",
},
},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit 41d5003

Please sign in to comment.