class Jekyll::Assets::HTML::Pic

Public Class Methods

cleanup(s) click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 101
def self.cleanup(s)
  s.gsub(%r!<(picture)>(.+)<\/\1>!) do |v|
    v.gsub(%r!</source>!, "")
  end
end
for?(args:, type:) click to toggle source

– {% img src src=“” @pic %} –

Calls superclass method Jekyll::Assets::Extensible::for?
# File lib/jekyll/assets/plugins/html/pic.rb, line 110
def self.for?(args:, type:)
  return false unless super
  return false unless args.key?(:pic)
  true
end

Public Instance Methods

complex(doc) click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 36
def complex(doc)
  @args[:img] ||= {}
  args = @args.to_h(html: true, skip: HTML.skips)
  @args[:img][:src] = @args[:src]
  _, sources = kv

  doc.picture @args[:picture] do
    Array(sources).each do |w|
      w, d = w.to_s.split(%r!\s+!, 2)
      Integer(w)

      source({
        width: w,
        args: args.dup,
        src: path(width: w),
        density: d,
        doc: doc,
      })
    end

    doc.img(@args[:img])
  end
end
kv() click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 80
def kv
  @kv ||= begin
    src = args[:srcset]

    out = [:"max-width", src[:"max-width"]] if src[:"max-width"]
    out = [:"min-width", src[:"min-width"]] if src[:"min-width"] && !out
    out = [:width, src[:width]] if src[:width] && !out

    out
  end
end
path(width:) click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 61
def path(width:)
  args_ = "#{args[:argv1]} magick:resize=#{width} @path"
  Tag.new("asset", args_, Liquid::ParseContext.new)
    .render(ctx)
end
proper_pic?() click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 93
def proper_pic?
  args.key?(:srcset) && (
    args[:srcset].key?(:"max-width") ||
    args[:srcset].key?(:"min-width") ||
    args[:srcset].key?(:width))
end
run() click to toggle source

– @todo this should be reworked so we don't need to use

tag to loop back in on ourselves.

# File lib/jekyll/assets/plugins/html/pic.rb, line 18
def run
  args[:picture] ||= {}
  Nokogiri::HTML::Builder.with(doc) do |d|
    if proper_pic?
      complex(d)
    else
      d.picture args[:picture] do
        d.img args.to_h({
          html: true, skip: HTML.skips + %i(
            picture
          )
        })
      end
    end
  end
end
source(doc:, args:, src:, width:, density:) click to toggle source

# File lib/jekyll/assets/plugins/html/pic.rb, line 68
def source(doc:, args:, src:, width:, density:)
  args.delete(:src)
  k, = kv

  args[:srcset] = "#{src} #{density || "#{width}w"}"
  args[:media]  = "(#{k}: #{width}px)" unless k == :width \
    || args[:media]

  doc.source args
end