From 0bf5a161d19ad9e6fd5d130b74233e486ce1105e Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Thu, 31 Jan 2019 16:18:31 -0500 Subject: [PATCH 1/5] Asciidoctor: Copy callout icons Adds support for copying callout icons into the directory containing the built documents. Without this callout icons all 404 which is sad because they are pretty. --- lib/ES/Util.pm | 14 +- .../asciidoctor/lib/copy_images/extension.rb | 20 ++- .../asciidoctor/spec/copy_images_spec.rb | 126 ++++++++++++++++++ .../copy_images/images/icons/callouts/1.gif | Bin 0 -> 43 bytes .../copy_images/images/icons/callouts/1.png | Bin 0 -> 70 bytes .../copy_images/images/icons/callouts/2.png | Bin 0 -> 70 bytes .../copy_images/images/icons/callouts/3.png | Bin 0 -> 70 bytes resources/asciidoctor/spec/spec_helper.rb | 2 +- 8 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.gif create mode 100644 resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/1.png create mode 100644 resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/2.png create mode 100644 resources/asciidoctor/spec/resources/copy_images/images/icons/callouts/3.png 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 803a002a8765c..fccd8e7519c54 100644 --- a/resources/asciidoctor/lib/copy_images/extension.rb +++ b/resources/asciidoctor/lib/copy_images/extension.rb @@ -17,9 +17,23 @@ 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 + else + extension = block.attr 'copy-callout-images' + if block.parent && block.parent.context == :colist + id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) { + copy_image block, "images/icons/callouts/#{$1}.#{extension}" + } + else + return + end + 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 8300141a52a41..155be203eaddc 100644 --- a/resources/asciidoctor/spec/copy_images_spec.rb +++ b/resources/asciidoctor/spec/copy_images_spec.rb @@ -194,4 +194,130 @@ def copy_attributes copied expect(copied).to eq([]) } end + + it "copies a 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 images/icons/callouts/1.png + INFO: : line 6: copying 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 a 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 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 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 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 images/icons/callouts/1.png + INFO: : line 9: copying 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 + + 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 0000000000000000000000000000000000000000..3c51d740ca8120d8d798f4b7060f586a1fdfdbff GIT binary patch literal 43 mcmZ?wbhEHbWMp7u_`m=Kia%KxK};PG0g_>0Vsc?*um%8taRh7t literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..f37764b1f7606623616dcdc169cc858273ea2d94 GIT binary patch literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9xZYBRYe;|OLfu)tPp=D){ QB2a?C)78&qol`;+0Lr!y6951J literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..f37764b1f7606623616dcdc169cc858273ea2d94 GIT binary patch literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9xZYBRYe;|OLfu)tPp=D){ QB2a?C)78&qol`;+0Lr!y6951J literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..f37764b1f7606623616dcdc169cc858273ea2d94 GIT binary patch literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9xZYBRYe;|OLfu)tPp=D){ QB2a?C)78&qol`;+0Lr!y6951J literal 0 HcmV?d00001 diff --git a/resources/asciidoctor/spec/spec_helper.rb b/resources/asciidoctor/spec/spec_helper.rb index e365e5ef3fe6b..ceb58b4ff7424 100644 --- a/resources/asciidoctor/spec/spec_helper.rb +++ b/resources/asciidoctor/spec/spec_helper.rb @@ -33,7 +33,7 @@ def convert input, extra_attributes = {}, warnings_matcher = eq('') } warnings_string = logger.messages .map { |l| "#{l[:severity]}: #{l[:message].inspect}" } - .join('\n') + .join("\n") expect(warnings_string).to warnings_matcher result end From 589a55b893774def850cf100c24f766cd16e4e01 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 4 Feb 2019 10:11:12 -0500 Subject: [PATCH 2/5] Fixup test --- resources/asciidoctor/spec/copy_images_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/resources/asciidoctor/spec/copy_images_spec.rb b/resources/asciidoctor/spec/copy_images_spec.rb index b87db5c8eecca..a19fdfe1e1055 100644 --- a/resources/asciidoctor/spec/copy_images_spec.rb +++ b/resources/asciidoctor/spec/copy_images_spec.rb @@ -215,8 +215,8 @@ def copy_attributes copied <2> words ASCIIDOC expected_warnings = <<~WARNINGS - INFO: : line 5: copying images/icons/callouts/1.png - INFO: : line 6: copying images/icons/callouts/2.png + 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([ @@ -237,7 +237,7 @@ def copy_attributes copied <1> words ASCIIDOC expected_warnings = <<~WARNINGS - INFO: : line 5: copying images/icons/callouts/1.gif + 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([ @@ -264,7 +264,7 @@ def copy_attributes copied .+ "#{spec_dir}\/resources\/copy_images\/images\/icons\/callouts\/2.gif" .+ - \]/x).and(match(/INFO: : line 5: copying images\/icons\/callouts\/1.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"], ]) @@ -287,7 +287,7 @@ def copy_attributes copied <1> words ASCIIDOC expected_warnings = <<~WARNINGS - INFO: : line 5: copying images/icons/callouts/1.png + 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([ @@ -314,8 +314,8 @@ def copy_attributes copied <1> words ASCIIDOC expected_warnings = <<~WARNINGS - INFO: : line 9: copying images/icons/callouts/1.png - INFO: : line 9: copying images/icons/callouts/2.png + 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([ From 52951c0aed9ac299884bd93d1f550cf27249e918 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 4 Feb 2019 10:41:17 -0500 Subject: [PATCH 3/5] Fixup behavior when not asked to copy callout images --- .../asciidoctor/lib/copy_images/extension.rb | 4 +--- resources/asciidoctor/spec/copy_images_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/resources/asciidoctor/lib/copy_images/extension.rb b/resources/asciidoctor/lib/copy_images/extension.rb index 298ff32df7cec..e71fd5b9d6e1f 100644 --- a/resources/asciidoctor/lib/copy_images/extension.rb +++ b/resources/asciidoctor/lib/copy_images/extension.rb @@ -23,12 +23,10 @@ def process_block block copy_image block, uri else extension = block.attr 'copy-callout-images' - if block.parent && block.parent.context == :colist + if extension && block.parent && block.parent.context == :colist id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) { copy_image block, "images/icons/callouts/#{$1}.#{extension}" } - else - return end end end diff --git a/resources/asciidoctor/spec/copy_images_spec.rb b/resources/asciidoctor/spec/copy_images_spec.rb index a19fdfe1e1055..7803ed6c2dee8 100644 --- a/resources/asciidoctor/spec/copy_images_spec.rb +++ b/resources/asciidoctor/spec/copy_images_spec.rb @@ -325,6 +325,21 @@ def copy_attributes copied 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 From cd5c22b272f50d34a35372aa079dabc015227fb3 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 4 Feb 2019 10:49:16 -0500 Subject: [PATCH 4/5] Document --- .../asciidoctor/lib/copy_images/extension.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/resources/asciidoctor/lib/copy_images/extension.rb b/resources/asciidoctor/lib/copy_images/extension.rb index e71fd5b9d6e1f..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 @@ -21,13 +27,12 @@ def process_block block uri = block.image_uri(block.attr 'target') return if Helpers.uriish? uri # Skip external images copy_image block, uri - else - extension = block.attr 'copy-callout-images' - if extension && block.parent && block.parent.context == :colist - id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) { - copy_image block, "images/icons/callouts/#{$1}.#{extension}" - } - end + 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 From 413edf971e237b480466514911a13762723f59e4 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 4 Feb 2019 11:10:10 -0500 Subject: [PATCH 5/5] Fix remaining oopsies --- resources/asciidoctor/spec/copy_images_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/asciidoctor/spec/copy_images_spec.rb b/resources/asciidoctor/spec/copy_images_spec.rb index 7803ed6c2dee8..9761cb06cbb0b 100644 --- a/resources/asciidoctor/spec/copy_images_spec.rb +++ b/resources/asciidoctor/spec/copy_images_spec.rb @@ -202,7 +202,7 @@ def copy_attributes copied } end - it "copies a images for callouts when requested (png)" do + it "copies images for callouts when requested (png)" do copied = [] attributes = copy_attributes copied attributes['copy-callout-images'] = 'png' @@ -225,7 +225,7 @@ def copy_attributes copied ]) end - it "copies a images for callouts when requested (gif)" do + it "copies images for callouts when requested (gif)" do copied = [] attributes = copy_attributes copied attributes['copy-callout-images'] = 'gif'