Skip to content

Commit

Permalink
backport fix for #4550 turn off system-dependent newline conversion w…
Browse files Browse the repository at this point in the history
…hen writing files
  • Loading branch information
mojavelinux committed Feb 19, 2024
1 parent 8c64c07 commit f1069d8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project utilizes semantic versioning.

Compliance::

* Turn off system-dependent newline conversion when writing files; don't convert line feeds to system-dependent newline (#4550)
* Support logger in Ruby 3.3 by instantiating super class (#4493) (*@mtasaka*)

Improvements::
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def define key, value
URI_READ_MODE = FILE_READ_MODE

# The mode to use when opening a file for writing
FILE_WRITE_MODE = RUBY_ENGINE_OPAL ? 'w' : 'w:utf-8'
FILE_WRITE_MODE = RUBY_ENGINE_OPAL ? 'w' : 'wb:utf-8'

# The default document type
# Can influence markup generated by the converters
Expand Down
23 changes: 20 additions & 3 deletions test/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1168,13 +1168,13 @@ def for name
assert_xpath '/html/body/*[@id="header"]/h1[text() = "Document Title"]', output, 1
end

test 'lines in output should be separated by line feed' do
test 'lines in output should be separated by line feed (universal newline)' do
sample_input_path = fixture_path('sample.adoc')

output = Asciidoctor.convert_file sample_input_path, standalone: true, to_file: false
refute_empty output
lines = output.split("\n")
assert_equal lines.size, output.split(/\r\n|\r|\n/).size
refute_includes output, ?\r
lines = output.split ?\n
assert_equal lines.map(&:length), lines.map(&:rstrip).map(&:length)
end

Expand Down Expand Up @@ -1539,6 +1539,23 @@ def for name
end
end

test 'should write file in bin mode and thus not convert line feeds to system-dependent newline' do
sample_input_path = fixture_path 'sample.adoc'
sample_output_path = fixture_path 'sample.html'
begin
Asciidoctor.convert_file sample_input_path
assert_path_exists sample_output_path
output = File.read sample_output_path, mode: Asciidoctor::FILE_READ_MODE
refute_empty output
assert_includes output, ?\n
refute_includes output, ?\r
assert_includes output, %(<\/body>\n<\/html>)
refute output.end_with? ?\n
ensure
FileUtils.rm sample_output_path
end
end

test 'should resolve :to_dir option correctly when both :to_dir and :to_file options are set to an absolute path' do
begin
sample_input_path = fixture_path 'sample.adoc'
Expand Down
12 changes: 9 additions & 3 deletions test/invoker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,15 @@
coderay_stylesheet = fixture_path 'coderay-asciidoctor.css'
begin
invoke_cli %W(-o #{sample_outpath} -a linkcss -a source-highlighter=coderay), 'source-block.adoc'
assert File.exist?(sample_outpath)
assert File.exist?(asciidoctor_stylesheet)
assert File.exist?(coderay_stylesheet)
assert_path_exists(sample_outpath)
assert_path_exists(asciidoctor_stylesheet)
assert_path_exists(coderay_stylesheet)
[sample_outpath, asciidoctor_stylesheet, coderay_stylesheet].each do |path|
contents = File.read path, mode: Asciidoctor::FILE_READ_MODE
assert_includes contents, ?\n
refute_includes contents, ?\r
refute contents.end_with? ?\n
end
ensure
FileUtils.rm_f(sample_outpath)
FileUtils.rm_f(asciidoctor_stylesheet)
Expand Down

0 comments on commit f1069d8

Please sign in to comment.