Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test if vips or convert are installed #299

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions lib/jekyll_picture_tag/parsers/image_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ def handler_for(format)

# Returns array of formats that vips can save to
def vips_formats
@vips_formats ||= `vips -l`
.split('/n')
.select { |line| line.include? 'ForeignSave' }
.flat_map { |line| line.scan(/\.[a-z]{1,5}/) }
.map { |format| format.strip.delete_prefix('.') }
.uniq
if command?("vips")
@vips_formats ||= `vips -l`
.split("/n")
.select { |line| line.include? "ForeignSave" }
.flat_map { |line| line.scan(/\.[a-z]{1,5}/) }
.map { |format| format.strip.delete_prefix(".") }
.uniq
else
@vips_formats = []
end
end

# Returns an array of formats that imagemagick can handle.
def magick_formats
@magick_formats ||= `convert -version`
.scan(/Delegates.*/)
.first
.delete_prefix('Delegates (built-in):')
.split
if command?("convert")
@magick_formats ||= `convert -version`
.scan(/Delegates.*/)
.first
.delete_prefix("Delegates (built-in):")
.split
else
@magick_formats = []
end
end

# Returns an array of all known names of a format, for the purposes of
Expand All @@ -41,16 +49,28 @@ def all_names(format)
private

def error_string(format)
<<~HEREDOC
No support for generating #{format} files in this environment!
Libvips known savers: #{vips_formats.join(', ')}
Imagemagick known savers: #{magick_formats.join(', ')}
HEREDOC
str = []
str << "No support for generating \"#{format}\" files in this environment!"
if command?("vips")
str << "Libvips (installed) supports: \"#{vips_formats.join(", ")}\"."
else
str << "Libvips is not installed."
end
if command?("convert")
str << "Imagemagick (installed) supports: \"#{magick_formats.join(", ")}\"."
else
str << "Imagemagick is not installed."
end
str.join(" ")
end

def alternates
[%w[jpg jpeg], %w[avif heic heif]]
end

def command?(command)
system("which #{command} > /dev/null 2>&1")
end
end
end
end
Loading