summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor.rb2
-rw-r--r--test/api_test.rb23
-rw-r--r--test/invoker_test.rb6
4 files changed, 28 insertions, 4 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index f1232928..f58d472c 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -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)
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb
index 386b90c4..aa19583d 100644
--- a/lib/asciidoctor.rb
+++ b/lib/asciidoctor.rb
@@ -216,7 +216,7 @@ module Asciidoctor
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
diff --git a/test/api_test.rb b/test/api_test.rb
index 91839bbf..ba96ab5a 100644
--- a/test/api_test.rb
+++ b/test/api_test.rb
@@ -1179,13 +1179,13 @@ context 'API' do
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
@@ -1548,6 +1548,23 @@ context 'API' do
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'
diff --git a/test/invoker_test.rb b/test/invoker_test.rb
index a5e960a7..c46c7a25 100644
--- a/test/invoker_test.rb
+++ b/test/invoker_test.rb
@@ -426,6 +426,12 @@ context 'Invoker' do
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