Skip to content

Commit 3af936a

Browse files
sztoszdoomspork
authored andcommitted
Strip CR from parsed document (#108)
* Strip CR from parsed document Remove all carriage return characters from parsed document to matanin compatibility with editors working under Windows operating system. For Windows CRLF is default line ending, and simplest way to handle that is to simply remove all CR characters by using regex. If document has only LF line endings then `convert_crlf_to_lf` will simply return original document, because nothing is going to be replaced. * Add test for CRLF support
1 parent 04f6bb7 commit 3af936a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/slime/preprocessor.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Slime.Preprocessor do
1111
document
1212
|> expand_tabs
1313
|> remove_trailing_newlines
14+
|> convert_crlf_to_lf
1415
|> split_into_lines
1516
end
1617

@@ -25,4 +26,8 @@ defmodule Slime.Preprocessor do
2526
defp split_into_lines(document) do
2627
String.split(document, "\n")
2728
end
29+
30+
defp convert_crlf_to_lf(document) do
31+
String.replace(document, ~r/\r/, "")
32+
end
2833
end

test/preprocessor_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ defmodule Slime.PreprocessorTest do
2626
" h3 Hi",
2727
]
2828
end
29+
30+
test " CRLF line endings are converted to LF" do
31+
result = "h1 Hi\r\n\th2 Bye\r\n\t\th3 Hi\r\n"
32+
|> process
33+
assert result == [
34+
"h1 Hi",
35+
" h2 Bye",
36+
" h3 Hi",
37+
]
38+
end
2939
end

0 commit comments

Comments
 (0)