Class: Jekyll::Assets::HTML::Pic

Inherits:
Jekyll::Assets::HTML show all
Defined in:
lib/jekyll/assets/plugins/html/pic.rb

Instance Attribute Summary

Attributes inherited from Jekyll::Assets::HTML

#doc

Attributes inherited from Extensible

#args, #asset, #ctx, #env, #jekyll

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Jekyll::Assets::HTML

build, #initialize, make_doc, skips, wants_xml?

Methods inherited from Extensible

for_args?, for_type?, inherited, #initialize, internal!, internal?, requirements

Constructor Details

This class inherits a constructor from Jekyll::Assets::HTML

Class Method Details

.cleanup(s) ⇒ Object



101
102
103
104
105
# 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:) ⇒ Boolean

– img src src=“” @pic % –

Returns:

  • (Boolean)


110
111
112
113
114
# 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

Instance Method Details

#complex(doc) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#kvObject



80
81
82
83
84
85
86
87
88
89
90
# 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:) ⇒ Object



61
62
63
64
65
# 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?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# 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

#runObject

TODO:

this should be reworked so we don’t need to use tag to loop back in on ourselves.

– –



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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