Skip to content

Commit

Permalink
backport fix for #4461 escape spaces in include target when generatin…
Browse files Browse the repository at this point in the history
…g link from include directive
  • Loading branch information
mojavelinux committed Jun 5, 2023
1 parent db6de25 commit 560df47
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ This project utilizes semantic versioning.
// tag::compact[]
== Unreleased

_No changes since previous release._
Bug Fixes::

* Escape spaces in include target (using inline passthrough) when generating link from include directive (#4461)

== 2.0.20 (2023-05-18) - @mojavelinux

Expand Down
6 changes: 5 additions & 1 deletion lib/asciidoctor/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ def preprocess_include_directive target, attrlist
# if running in SafeMode::SECURE or greater, don't process this directive
# however, be friendly and at least make it a link to the source document
elsif doc.safe >= SafeMode::SECURE
expanded_target = %(pass:c[#{expanded_target}]) if expanded_target.include? ' '
# FIXME we don't want to use a link macro if we are in a verbatim context
replace_next_line %(link:#{expanded_target}[role=include])
elsif @maxdepth
Expand Down Expand Up @@ -1238,7 +1239,10 @@ def preprocess_include_directive target, attrlist
def resolve_include_path target, attrlist, attributes
doc = @document
if (Helpers.uriish? target) || (::String === @dir ? nil : (target = %(#{@dir}/#{target})))
return replace_next_line %(link:#{target}[role=include]) unless doc.attr? 'allow-uri-read'
unless doc.attr? 'allow-uri-read'
target = %(pass:c[#{target}]) if target.include? ' '
return replace_next_line %(link:#{target}[role=include])
end
if doc.attr? 'cache-uri'
# caching requires the open-uri-cached gem to be installed
# processing will be automatically aborted if these libraries can't be opened
Expand Down
14 changes: 14 additions & 0 deletions test/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ class ReaderTest < Minitest::Test
assert_equal 'link:include-file.adoc[role=include]', reader.read_line
end

test 'should escape spaces in target when generating link from include directive' do
input = 'include::foo bar baz.adoc[]'
doc = Asciidoctor::Document.new input
reader = doc.reader
assert_equal 'link:pass:c[foo bar baz.adoc][role=include]', reader.read_line
end

test 'should replace include directive with link macro if safe mode allows it, but allow-uri-read is not set' do
using_memory_logger do |logger|
input = 'include::https://example.org/dist/info.adoc[]'
Expand All @@ -662,6 +669,13 @@ class ReaderTest < Minitest::Test
end
end

test 'should escape spaces in target when generating link from remote include directive' do
input = 'include::https://example.org/no such file.adoc[]'
doc = Asciidoctor::Document.new input, safe: :safe
reader = doc.reader
assert_equal 'link:pass:c[https://example.org/no such file.adoc][role=include]', reader.read_line
end

test 'include directive is enabled when safe mode is less than SECURE' do
input = 'include::fixtures/include-file.adoc[]'
doc = document_from_string input, safe: :safe, standalone: false, base_dir: DIRNAME
Expand Down

0 comments on commit 560df47

Please sign in to comment.