Sha256: 8e80e6c3856eb8548777074ec0e7e72b5c61723ebf76ef787fef710d45e7d717

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# Frozen-string-literal: true
# Copyright: 2012 - 2018 - MIT License
# Encoding: utf-8

require "jekyll/assets"
require "nokogiri"

module Jekyll
  module Assets
    class HTML
      class IMG < HTML
        content_types %r!^image/(?\!x-icon$)[a-zA-Z0-9\-_\+]+$!

        # --
        def run
          Nokogiri::HTML::Builder.with(doc) do |d|
            if srcset?
              complex(d)
            else
              d.img(args.to_h({
                html: true, skip: HTML.skips
              }))
            end
          end
        end

        # --
        def complex(doc)
          img = doc.img @args.to_h(html: true, skip: HTML.skips)
          Array(args[:srcset][:width]).each do |w|
            w, d = w.to_s.split(%r!\s+!, 2)
            Integer(w)

            img["srcset"] ||= ""
            img["srcset"] += ", #{path(width: w)} #{d || "#{w}w"}"
            img["srcset"] = img["srcset"].gsub(
              %r!^,\s*!, "")
          end
        end

        # --
        def path(width:)
          args_ = "#{args[:argv1]} magick:resize=#{width} @path"
          Tag.new("asset", args_, Liquid::ParseContext.new)
            .render(ctx)
        end

        # --
        def srcset?
          args.key?(:srcset) && args[:srcset]
            .key?(:width)
        end

        # --
        # @example {% asset src srcset="" %}
        # @example {% asset src %}
        # --
        def self.for?(type:, args:)
          return false unless super
          return false if args.key?(:pic)
          return false if args.key?(:inline) &&
              type == "image/svg+xml"

          true
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-assets-3.0.8 lib/jekyll/assets/plugins/html/img.rb
jekyll-assets-3.0.7 lib/jekyll/assets/plugins/html/img.rb