diff --git a/lib/ES/Util.pm b/lib/ES/Util.pm index 906d1a83d93f7..a6aa82894e546 100644 --- a/lib/ES/Util.pm +++ b/lib/ES/Util.pm @@ -75,6 +75,9 @@ sub build_chunked { # Emulate asciidoc_dir because we use it to find shared asciidoc files # but asciidoctor doesn't support it. my $asciidoc_dir = dir('resources/asciidoc-8.6.8/')->absolute; + # We use the callouts from asciidoc so add it as a resource so we + # can find them + push @$resources, $asciidoc_dir; eval { $output = run( 'asciidoctor', '-v', '--trace', @@ -92,7 +95,8 @@ sub build_chunked { # missing attributes! # '-a' => 'attribute-missing=warn', '-a' => 'asciidoc-dir=' . $asciidoc_dir, - $resources ? ( '-a' => 'resources=' . join(',', @$resources)) : (), + '-a' => 'resources=' . join(',', @$resources), + '-a' => 'copy-callout-images=png', '--destination-dir=' . $dest, docinfo($index), $index @@ -107,7 +111,7 @@ sub build_chunked { file('resources/website_chunked.xsl')->absolute, "$dest/index.xml" ); - unlink "$dest/index.xml"; + # unlink "$dest/index.xml"; 1; } or do { $output = $@; $died = 1; }; } @@ -198,7 +202,9 @@ sub build_single { # Emulate asciidoc_dir because we use it to find shared asciidoc files # but asciidoctor doesn't support it. my $asciidoc_dir = dir('resources/asciidoc-8.6.8/')->absolute; - + # We use the callouts from asciidoc so add it as a resource so we + # can find them + push @$resources, $asciidoc_dir; eval { $output = run( 'asciidoctor', '-v', '--trace', @@ -210,7 +216,7 @@ sub build_single { '-a' => 'repo_root=' . $root_dir, $private ? () : ( '-a' => "edit_url=$edit_url" ), '-a' => 'asciidoc-dir=' . $asciidoc_dir, - $resources ? ( '-a' => 'resources=' . join(',', @$resources)) : (), + '-a' => 'resources=' . join(',', @$resources), # Disable warning on missing attributes because we have # missing attributes! # '-a' => 'attribute-missing=warn', diff --git a/resources/asciidoctor/lib/copy_images/extension.rb b/resources/asciidoctor/lib/copy_images/extension.rb index 0ee51d95c1a7c..b4a4f5de66d84 100644 --- a/resources/asciidoctor/lib/copy_images/extension.rb +++ b/resources/asciidoctor/lib/copy_images/extension.rb @@ -8,6 +8,12 @@ ## # Copies images that are referenced into the same directory as the output files. # +# It finds the images by looking in a comma separated list of directories +# defined by the `resources` attribute. +# +# It can also be configured to copy the images that number callout lists by +# setting `copy-callout-images` to the file extension of the images to copy. +# class CopyImages < TreeProcessorScaffold include Logging @@ -17,9 +23,20 @@ def initialize name end def process_block block - return unless block.context == :image - uri = block.image_uri(block.attr 'target') - return if Helpers.uriish? uri # Skip external images + if block.context == :image + uri = block.image_uri(block.attr 'target') + return if Helpers.uriish? uri # Skip external images + copy_image block, uri + elsif (extension = block.document.attr 'copy-callout-images') && + block.parent && + block.parent.context == :colist + id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) { + copy_image block, "images/icons/callouts/#{$1}.#{extension}" + } + end + end + + def copy_image block, uri return unless @copied.add? uri # Skip images we've copied before source = find_source block, uri return unless source # Skip images we can't find diff --git a/resources/asciidoctor/spec/copy_images_spec.rb b/resources/asciidoctor/spec/copy_images_spec.rb index 55b1b9b903309..9761cb06cbb0b 100644 --- a/resources/asciidoctor/spec/copy_images_spec.rb +++ b/resources/asciidoctor/spec/copy_images_spec.rb @@ -201,4 +201,145 @@ def copy_attributes copied expect(copied).to eq([]) } end + + it "copies images for callouts when requested (png)" do + copied = [] + attributes = copy_attributes copied + attributes['copy-callout-images'] = 'png' + input = <<~ASCIIDOC + == Example + ---- + foo <1> <2> + ---- + <1> words + <2> words + ASCIIDOC + expected_warnings = <<~WARNINGS + INFO: : line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png + INFO: : line 6: copying #{spec_dir}/resources/copy_images/images/icons/callouts/2.png + WARNINGS + convert input, attributes, eq(expected_warnings.strip) + expect(copied).to eq([ + ["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"], + ["images/icons/callouts/2.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/2.png"], + ]) + end + + it "copies images for callouts when requested (gif)" do + copied = [] + attributes = copy_attributes copied + attributes['copy-callout-images'] = 'gif' + input = <<~ASCIIDOC + == Example + ---- + foo <1> + ---- + <1> words + ASCIIDOC + expected_warnings = <<~WARNINGS + INFO: : line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.gif + WARNINGS + convert input, attributes, eq(expected_warnings.strip) + expect(copied).to eq([ + ["images/icons/callouts/1.gif", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.gif"], + ]) + end + + it "has a nice error message when a callout image is missing" do + copied = [] + attributes = copy_attributes copied + attributes['copy-callout-images'] = 'gif' + input = <<~ASCIIDOC + == Example + ---- + foo <1> <2> + ---- + <1> words + <2> words + ASCIIDOC + convert input, attributes, match(/ + WARN:\ :\ line\ 6:\ can't\ read\ image\ at\ any\ of\ \[ + "#{spec_dir}\/images\/icons\/callouts\/2.gif",\s + "#{spec_dir}\/resources\/images\/icons\/callouts\/2.gif",\s + .+ + "#{spec_dir}\/resources\/copy_images\/images\/icons\/callouts\/2.gif" + .+ + \]/x).and(match(/INFO: : line 5: copying #{spec_dir}\/resources\/copy_images\/images\/icons\/callouts\/1.gif/)) + expect(copied).to eq([ + ["images/icons/callouts/1.gif", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.gif"], + ]) + end + + it "only copies callout images one time" do + copied = [] + attributes = copy_attributes copied + attributes['copy-callout-images'] = 'png' + input = <<~ASCIIDOC + == Example + ---- + foo <1> + ---- + <1> words + + ---- + foo <1> + ---- + <1> words + ASCIIDOC + expected_warnings = <<~WARNINGS + INFO: : line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png + WARNINGS + convert input, attributes, eq(expected_warnings.strip) + expect(copied).to eq([ + ["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"], + ]) + end + + it "supports callout lists with multiple callouts per item" do + # This is a *super* weird case but we have it in Elasticsearch. + # The only way I can make callout lists be for two things is by making + # blocks with callouts but only having a single callout list below both. + copied = [] + attributes = copy_attributes copied + attributes['copy-callout-images'] = 'png' + input = <<~ASCIIDOC + == Example + ---- + foo <1> + ---- + + ---- + foo <1> + ---- + <1> words + ASCIIDOC + expected_warnings = <<~WARNINGS + INFO: : line 9: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png + INFO: : line 9: copying #{spec_dir}/resources/copy_images/images/icons/callouts/2.png + WARNINGS + convert input, attributes, eq(expected_warnings.strip) + expect(copied).to eq([ + ["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"], + ["images/icons/callouts/2.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/2.png"], + ]) + end + + it "doesn't copy callout images if the extension isn't set" do + copied = [] + attributes = copy_attributes copied + input = <<~ASCIIDOC + == Example + ---- + foo <1> + ---- + <1> words + + ---- + foo <1> + ---- + <1> words + ASCIIDOC + convert input, attributes + expect(copied).to eq([]) + end end \ No newline at end of file diff --git a/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.gif b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.gif new file mode 100644 index 0000000000000..3c51d740ca812 Binary files /dev/null and b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.gif differ diff --git a/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.png b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.png new file mode 100644 index 0000000000000..f37764b1f7606 Binary files /dev/null and b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.png differ diff --git a/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/2.png b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/2.png new file mode 100644 index 0000000000000..f37764b1f7606 Binary files /dev/null and b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/2.png differ diff --git a/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/3.png b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/3.png new file mode 100644 index 0000000000000..f37764b1f7606 Binary files /dev/null and b/resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/3.png differ