commit d91bdac19115959d812e6eefae516c4c7a3f791f
parent 8694c58409ea3a0b33f5a5647456a699c483e53f
Author: Kyle Milz <milz@0x30.net>
Date: Wed, 17 Mar 2021 00:33:46 +0000
mk: use common suffix for all img targets
Diffstat:
M | mk/html.mk | | | 10 | ++++++---- |
A | t/img_html.t | | | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 79 insertions(+), 4 deletions(-)
diff --git a/mk/html.mk b/mk/html.mk
@@ -64,11 +64,13 @@ depend: ${SRCS:.in=.depend} _SUBDIRUSE
#
-# Generate `*_html' HTML fragments that use <img> tag to link to images.
+# Generate HTML fragments that contain <a><img> links to images.
#
-.SUFFIXES: .jpg .jpg_html
-.SUFFIXES: .png .png_html
-.jpg.jpg_html .png.png_html:
+.SUFFIXES: .img_html
+.SUFFIXES: .jpeg
+.SUFFIXES: .jpg
+.SUFFIXES: .png
+.jpeg.img_html .jpg.img_html .png.img_html:
@# Copy original image to object dir with `_cp' appended to file name.
@# Make gets confused when two files have the same names
@# in different directories.
diff --git a/t/img_html.t b/t/img_html.t
@@ -0,0 +1,73 @@
+#
+# Verify the img_html suffix generates a thumbnail and correct HTML
+# fragment.
+#
+use Imager;
+use Modern::Perl;
+use File::Spec::Functions;
+use Test::Cmd;
+use Test::File;
+use Test::File::Contents;
+use Test::More tests => 19;
+
+
+# Currently supported suffixes.
+my @sfxs = ( 'jpg', 'jpeg', 'png' );
+
+my $cmd = Test::Cmd->new( prog => '/usr/bin/make', workdir => '' );
+my $html_mk = $cmd->here . "/mk/html.mk";
+
+# Write smallest possible image for quick processing.
+my $img = Imager->new( xsize => 1, ysize => 1, channels => 4 );
+for my $sfx (@sfxs) {
+ my $ret = $img->write( file => $cmd->workdir . "/pic_$sfx.$sfx" );
+ isnt( $ret, undef, "$sfx write (err = $img->errstr)" );
+}
+
+$cmd->write( 'index.in', <<EOF );
+#include "pic_jpg.img_html"
+#include "pic_jpeg.img_html"
+#include "pic_png.img_html"
+EOF
+
+$cmd->write( 'Makefile', <<EOF );
+.include "$html_mk"
+EOF
+
+$cmd->run( args => 'depend', chdir => $cmd->curdir );
+like( $cmd->stdout, qr/.*/, 'make stdout' );
+is( $cmd->stderr, "", 'make stderr' );
+is( $? >> 8, 0, 'make exit status' );
+
+$cmd->run( chdir => $cmd->curdir );
+like( $cmd->stdout, qr/.*/, 'make stdout' );
+is( $cmd->stderr, "", 'make stderr' );
+is( $? >> 8, 0, 'make exit status' );
+
+#
+# Verify:
+# - thumbnail generated
+# - original copied
+# - html generated
+# - final html file looks good
+#
+my $index_good = "\n\n";
+
+for my $sfx (@sfxs) {
+ my $cp_file = "pic_${sfx}_cp.${sfx}";
+ my $thumb_file = "pic_${sfx}_thumb.${sfx}";
+
+ file_exists_ok( catfile( $cmd->workdir, $cp_file ) );
+ file_exists_ok( catfile( $cmd->workdir, $thumb_file ) );
+
+ my $html_good = <<EOF;
+ <a href='$cp_file'><img src='$thumb_file'
+EOF
+
+ my $html_file = catfile( $cmd->workdir, "pic_${sfx}.img_html" );
+ file_contents_eq_or_diff( $html_file, $html_good );
+
+ $index_good .= $html_good;
+}
+
+file_contents_eq_or_diff( catfile( $cmd->workdir, 'index.html' ), $index_good );