lib/asciidoctor/helpers.rb in asciidoctor-2.0.10 vs lib/asciidoctor/helpers.rb in asciidoctor-2.0.11

- old
+ new

@@ -54,29 +54,31 @@ # whitespace from every line. # # If a BOM is found at the beginning of the data, a best attempt is made to # encode it to UTF-8 from the specified source encoding. # - # data - the source data Array to prepare (no nil entries allowed) + # data - the source data Array to prepare (no nil entries allowed) + # trim_end - whether to trim whitespace from the end of each line; + # (true cleans all whitespace; false only removes trailing newline) (default: true) # # returns a String Array of prepared lines - def prepare_source_array data + def prepare_source_array data, trim_end = true return [] if data.empty? if (leading_2_bytes = (leading_bytes = (first = data[0]).unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE data[0] = first.byteslice 2, first.bytesize # NOTE you can't split a UTF-16LE string using .lines when encoding is UTF-8; doing so will cause this line to fail - return data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16LE).rstrip } + return trim_end ? data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16LE).rstrip } : data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16LE).chomp } elsif leading_2_bytes == BOM_BYTES_UTF_16BE data[0] = first.byteslice 2, first.bytesize - return data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16BE).rstrip } + return trim_end ? data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16BE).rstrip } : data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16BE).chomp } elsif leading_bytes == BOM_BYTES_UTF_8 data[0] = first.byteslice 3, first.bytesize end if first.encoding == UTF_8 - data.map {|line| line.rstrip } + trim_end ? data.map {|line| line.rstrip } : data.map {|line| line.chomp } else - data.map {|line| (line.encode UTF_8).rstrip } + trim_end ? data.map {|line| (line.encode UTF_8).rstrip } : data.map {|line| (line.encode UTF_8).chomp } end end # Internal: Prepare the source data String for parsing. # @@ -84,14 +86,16 @@ # removes any trailing whitespace from every line. # # If a BOM is found at the beginning of the data, a best attempt is made to # encode it to UTF-8 from the specified source encoding. # - # data - the source data String to prepare + # data - the source data String to prepare + # trim_end - whether to trim whitespace from the end of each line; + # (true cleans all whitespace; false only removes trailing newline) (default: true) # # returns a String Array of prepared lines - def prepare_source_string data + def prepare_source_string data, trim_end = true return [] if data.nil_or_empty? if (leading_2_bytes = (leading_bytes = data.unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE data = (data.byteslice 2, data.bytesize).encode UTF_8, ::Encoding::UTF_16LE elsif leading_2_bytes == BOM_BYTES_UTF_16BE data = (data.byteslice 2, data.bytesize).encode UTF_8, ::Encoding::UTF_16BE @@ -99,10 +103,14 @@ data = data.byteslice 3, data.bytesize data = data.encode UTF_8 unless data.encoding == UTF_8 elsif data.encoding != UTF_8 data = data.encode UTF_8 end - [].tap {|lines| data.each_line {|line| lines << line.rstrip } } + if trim_end + [].tap {|lines| data.each_line {|line| lines << line.rstrip } } + else + [].tap {|lines| data.each_line {|line| lines << line.chomp } } + end end # Internal: Efficiently checks whether the specified String resembles a URI # # Uses the Asciidoctor::UriSniffRx regex to check whether the String begins