diff options
| -rwxr-xr-x | lib/asciidoctor.rb | 5 | ||||
| -rw-r--r-- | lib/asciidoctor/path_resolver.rb | 11 | ||||
| -rw-r--r-- | test/paths_test.rb | 4 | ||||
| -rw-r--r-- | test/substitutions_test.rb | 24 |
4 files changed, 41 insertions, 3 deletions
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb index f7be118c..6e226233 100755 --- a/lib/asciidoctor.rb +++ b/lib/asciidoctor.rb @@ -384,8 +384,9 @@ module Asciidoctor :media_blk_macro => /^(image|video|audio)::(\S+?)\[((?:\\\]|[^\]])*?)\]$/, # image:filename.png[Alt Text] + # image:http://example.com/images/filename.png[Alt Text] # image:filename.png[More [Alt\] Text] (alt text becomes "More [Alt] Text") - :image_macro => /\\?image:([^:\[]+)\[((?:\\\]|[^\]])*?)\]/, + :image_macro => /\\?image:([^:\[][^\[]*)\[((?:\\\]|[^\]])*?)\]/, # indexterm:[Tigers,Big cats] # (((Tigers,Big cats))) @@ -546,7 +547,7 @@ module Asciidoctor # http://domain # https://domain # data:info - :uri_sniff => %r{\A[[:alpha:]][[:alnum:].+-]*:}, + :uri_sniff => %r{\A[[:alpha:]][[:alnum:].+-]*:/*}, :uri_encode_chars => /[^\w\-.!~*';:@=+$,()\[\]]/ } diff --git a/lib/asciidoctor/path_resolver.rb b/lib/asciidoctor/path_resolver.rb index f03fe631..7a4e14b8 100644 --- a/lib/asciidoctor/path_resolver.rb +++ b/lib/asciidoctor/path_resolver.rb @@ -339,9 +339,14 @@ class PathResolver def web_path(target, start = nil) target = posixfy(target) start = posixfy(start) + uri_prefix = nil unless is_web_root?(target) || start.empty? target = "#{start}#{SLASH}#{target}" + if target.include?(':') && target.match(Asciidoctor::REGEXP[:uri_sniff]) + uri_prefix = $~[0] + target = target[uri_prefix.length..-1] + end end target_segments, target_root, _ = partition_path(target, true) @@ -360,7 +365,11 @@ class PathResolver accum end - join_path resolved_segments, target_root + if uri_prefix.nil? + join_path resolved_segments, target_root + else + "#{uri_prefix}#{join_path resolved_segments, target_root}" + end end end end diff --git a/test/paths_test.rb b/test/paths_test.rb index 3940c384..0a4a3e96 100644 --- a/test/paths_test.rb +++ b/test/paths_test.rb @@ -42,6 +42,10 @@ context 'Path Resolver' do assert_equal './assets/images', @resolver.web_path('./images', './assets') end + test 'target with relative path appended to url start path' do + assert_equal 'http://www.example.com/assets/images', @resolver.web_path('images', 'http://www.example.com/assets') + end + test 'normalize target' do assert_equal '../images', @resolver.web_path('../images/../images') end diff --git a/test/substitutions_test.rb b/test/substitutions_test.rb index acfbe6db..4da57d46 100644 --- a/test/substitutions_test.rb +++ b/test/substitutions_test.rb @@ -440,6 +440,30 @@ context 'Substitutions' do para.sub_macros(para.buffer.join).gsub(/>\s+</, '><') end + test 'an inline image macro with a url target should be interpreted as an image' do + para = block_from_string %(Beware of the image:http://example.com/images/tiger.png[tiger].) + assert_equal %{Beware of the <span class="image"><img src="http://example.com/images/tiger.png" alt="tiger"></span>.}, + para.sub_macros(para.buffer.join).gsub(/>\s+</, '><') + end + + test 'should prepend value of imagesdir attribute to inline image target if target is relative path' do + para = block_from_string %(Beware of the image:tiger.png[tiger].), :attributes => {'imagesdir' => './images'} + assert_equal %{Beware of the <span class="image"><img src="./images/tiger.png" alt="tiger"></span>.}, + para.sub_macros(para.buffer.join).gsub(/>\s+</, '><') + end + + test 'should not prepend value of imagesdir attribute to inline image target if target is absolute path' do + para = block_from_string %(Beware of the image:/tiger.png[tiger].), :attributes => {'imagesdir' => './images'} + assert_equal %{Beware of the <span class="image"><img src="/tiger.png" alt="tiger"></span>.}, + para.sub_macros(para.buffer.join).gsub(/>\s+</, '><') + end + + test 'should not prepend value of imagesdir attribute to inline image target if target is url' do + para = block_from_string %(Beware of the image:http://example.com/images/tiger.png[tiger].), :attributes => {'imagesdir' => './images'} + assert_equal %{Beware of the <span class="image"><img src="http://example.com/images/tiger.png" alt="tiger"></span>.}, + para.sub_macros(para.buffer.join).gsub(/>\s+</, '><') + end + test 'a block image macro should not be detected within paragraph text' do para = block_from_string(%(Not an inline image macro image::tiger.png[].)) result = para.sub_macros(para.buffer.join) |
