From 0def02e16472d336deb4e4511757ea4b265f37de Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Sat, 6 Mar 2021 16:57:20 +0900 Subject: [PATCH] Generate .webp instead of .png --- tasks/tcardgen.rake | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tasks/tcardgen.rake b/tasks/tcardgen.rake index 2665b88..93f3ae6 100644 --- a/tasks/tcardgen.rake +++ b/tasks/tcardgen.rake @@ -1,15 +1,27 @@ #!/usr/bin/env rake +OGP_DIR = 'static/ogp/' +CONTENT_DIR = 'content/' +FONT_DIR = 'assets/font/' +CONFIG = 'assets/tcardgen.yaml' + desc 'Generate OGP images' task :tcard do - sh 'find static/ogp/ -type f -name "*.png" | grep -v "base.png" | xargs -t --no-run-if-empty rm' + sh "find #{OGP_DIR} -type f -name '*.png' | xargs -t --no-run-if-empty rm" - Dir.glob("**/*.md", File::FNM_DOTMATCH, base: 'content/').each do |article| + Dir.glob("**/*.md", File::FNM_DOTMATCH, base: CONTENT_DIR).each do |article| unless article =~ /_index\.md/ - target = File.join('content', article) - sh "tcardgen -f ~/repo/sample-font/ -o static/ogp/ -c config/tcardgen.yaml #{target}" + target = File.join(CONTENT_DIR, article) + sh "tcardgen -f #{FONT_DIR} -o #{OGP_DIR} -c #{CONFIG} #{target}" end end - #sh 'find content/ -type f -name "*.md" | xargs -t -I % tcardgen -f ~/repo/sample-font/ -o static/ogp/ -c config/tcardgen.yaml %' + Dir.glob("*.png", File::FNM_DOTMATCH, base: OGP_DIR).each do |png| + src = File.join(OGP_DIR, png) + + tmp = File.basename(src, ".*") + ".webp" + dest = File.join(OGP_DIR, tmp) + + sh "img2webp -lossy -q 50 #{src} -o #{dest}" + end end