Skip to content

Commit

Permalink
resolves asciidoctor#4550 turn off system-dependent newline conversio…
Browse files Browse the repository at this point in the history
…n when writing files
  • Loading branch information
mojavelinux committed Feb 18, 2024
1 parent 38f464f commit feb7c8e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Enhancements::

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*)
* Don't promote level-0 special section at start of document to document title (#4151)
* Disallow the use of dot (`.`) in the name of a named element attribute (#4147)
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 @@ -1179,13 +1179,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 output.include? ?\r
lines = output.split ?\n
assert_equal lines.map(&:length), lines.map(&:rstrip).map(&:length)
end

Expand Down Expand Up @@ -1548,6 +1548,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 output.include? ?\n
refute output.include? ?\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
6 changes: 6 additions & 0 deletions test/invoker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@
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 contents.include? ?\n
refute contents.include? ?\r
refute contents.end_with? ?\n
end
ensure
FileUtils.rm_f sample_outpath
FileUtils.rm_f asciidoctor_stylesheet
Expand Down

0 comments on commit feb7c8e

Please sign in to comment.