Skip to content

Commit 4c008b0

Browse files
committed
resolves asciidoctor#3248 allow encoding of include file to be specified using encoding attribute
1 parent c5b4390 commit 4c008b0

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGELOG.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Improvements::
3535

3636
* Preserve guard around XML-style callout when icons are not enabled (#3319)
3737
* Remove redundant test for halign and valign attributes on table cell in DocBook converter
38+
* Allow encoding of include file to be specified using encoding attribute (#3248)
3839

3940
Build / Infrastructure::
4041

lib/asciidoctor/reader.rb

+5
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,11 @@ def preprocess_include_directive target, attrlist
10601060
return inc_path
10611061
end
10621062

1063+
if (enc = parsed_attrs['encoding']) && (::Encoding.find enc rescue nil)
1064+
(read_mode_params = read_mode.split ':')[1] = enc
1065+
read_mode = read_mode_params.join ':'
1066+
end unless RUBY_ENGINE_OPAL
1067+
10631068
inc_linenos = inc_tags = nil
10641069
if attrlist
10651070
if parsed_attrs.key? 'lines'

test/fixtures/iso-8859-1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
O� est l'h�pital ?

test/reader_test.rb

+41
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,47 @@ class ReaderTest < Minitest::Test
730730
assert_equal ['last line'], doc.blocks[2].lines
731731
end
732732

733+
test 'should fail to read include file if not UTF-8 encoded and encoding is not specified' do
734+
input = <<~'EOS'
735+
....
736+
include::fixtures/iso-8859-1.txt[]
737+
....
738+
EOS
739+
740+
assert_raises StandardError, 'invalid byte sequence in UTF-8' do
741+
doc = document_from_string input, safe: :safe, base_dir: DIRNAME
742+
assert_equal 1, doc.blocks.size
743+
refute_equal ['Où est l\'hôpital ?'], doc.blocks[0].lines
744+
doc.convert
745+
end
746+
end
747+
748+
test 'should ignore encoding attribute if value is not an valid encoding' do
749+
input = <<~'EOS'
750+
....
751+
include::fixtures/encoding.adoc[tag=romé,encoding=iso-1000-1]
752+
....
753+
EOS
754+
755+
doc = document_from_string input, safe: :safe, base_dir: DIRNAME
756+
assert_equal 1, doc.blocks.size
757+
assert_equal doc.blocks[0].lines[0].encoding, Encoding::UTF_8
758+
assert_equal ['Gregory Romé has written an AsciiDoc plugin for the Redmine project management application.'], doc.blocks[0].lines
759+
end
760+
761+
test 'should use encoding specified by encoding attribute when reading include file' do
762+
input = <<~'EOS'
763+
....
764+
include::fixtures/iso-8859-1.txt[encoding=iso-8859-1]
765+
....
766+
EOS
767+
768+
doc = document_from_string input, safe: :safe, base_dir: DIRNAME
769+
assert_equal 1, doc.blocks.size
770+
assert_equal doc.blocks[0].lines[0].encoding, Encoding::UTF_8
771+
assert_equal ['Où est l\'hôpital ?'], doc.blocks[0].lines
772+
end
773+
733774
test 'unresolved target referenced by include directive is skipped when optional option is set' do
734775
input = <<~'EOS'
735776
include::fixtures/{no-such-file}[opts=optional]

0 commit comments

Comments
 (0)