summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-09-10 23:45:21 -0600
committerGitHub <noreply@github.com>2022-09-10 23:45:21 -0600
commit07915e1d7ea94fb554d11487e18478cddb2db02f (patch)
tree88638ef60cc6912e251d624970730c936f548e6a
parent6c56c809986cb67e76f865ef324e5ff35ce5b850 (diff)
resolves #4351 force encoding of attribute data passed via CLI to UTF-8 if transcoding fails (PR #4354)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/cli/options.rb6
-rw-r--r--test/options_test.rb8
3 files changed, 14 insertions, 1 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index c31213dd..d13443a6 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -56,6 +56,7 @@ Improvements::
* Set linenums option on source block when line numbering is enabled (#3313)
* Propagate `:to_dir` option to document of AsciiDoc table cell (#4297)
* Warn if include target is remote and `allow-uri-read` attribute is not set (#2284)
+ * Force encoding of attribute data passed via CLI to UTF-8 if transcoding fails (#4351) (*zkaip*)
Bug Fixes::
diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb
index e3e0119f..bc4e46c6 100644
--- a/lib/asciidoctor/cli/options.rb
+++ b/lib/asciidoctor/cli/options.rb
@@ -91,7 +91,11 @@ module Asciidoctor
'unless either the name or value ends in @ (i.e., name@=value or name=value@)',
'may be specified more than once' do |attr|
next if (attr = attr.rstrip).empty? || attr == '='
- attr = attr.encode UTF_8 unless attr.encoding == UTF_8
+ begin
+ attr = attr.encode UTF_8
+ rescue ::EncodingError
+ attr = attr.force_encoding UTF_8
+ end unless attr.encoding == UTF_8
name, _, val = attr.partition '='
self[:attributes][name] = val
end
diff --git a/test/options_test.rb b/test/options_test.rb
index 29ffd68d..4e649998 100644
--- a/test/options_test.rb
+++ b/test/options_test.rb
@@ -146,6 +146,14 @@ context 'Options' do
assert_nil options[:attributes]
end
+ test 'should gracefully force encoding to UTF-8 if encoding on string is mislabeled' do
+ args = ['-a', ((%w(platform-name 云平台).join '=').force_encoding Encoding::ASCII_8BIT), '-']
+ options = Asciidoctor::Cli::Options.parse! args
+
+ assert_equal '云平台', options[:attributes]['platform-name']
+ assert_equal Encoding::UTF_8, options[:attributes]['platform-name'].encoding
+ end
+
test 'should allow safe mode to be specified' do
options = Asciidoctor::Cli::Options.parse! %w(-S safe test/fixtures/sample.adoc)
assert_equal Asciidoctor::SafeMode::SAFE, options[:safe]