class Jekyll::Assets::Plugins::Searcher

– Searches for `<img>` that have `<img asset>` or `<img asset=“args”>` and runs them through the asset system. This allows you to use real assets inside of your Markdown pre-convert. –

Public Class Methods

new(doc) click to toggle source
# File lib/jekyll/assets/plugins/searcher.rb, line 20
def initialize(doc)
  @doc = doc
end

Public Instance Methods

run() click to toggle source

# File lib/jekyll/assets/plugins/searcher.rb, line 25
def run
  html.search("img[@asset]").each do |v|
    raise ArgumentError, "src is empty" unless v[:src]
    args = "#{v.delete('src')&.value} #{v.delete('asset')&.value}"
    pctx = ::Liquid::ParseContext.new

    attrs = v.attributes.keys
    args, = Tag.new("asset", args, pctx).render_raw(ctx)
    args.to_h(html: true).each do |k, vv|
      unless attrs.include?(k.to_s)
        v.set_attribute(k, vv)
      end
    end
  end

  out = html.to_html
  @doc.output = out
end

Private Instance Methods

ctx() click to toggle source

# File lib/jekyll/assets/plugins/searcher.rb, line 46
def ctx
  ::Liquid::Context.new({}, {}, {
    site: @doc.site,
  })
end
html() click to toggle source

# File lib/jekyll/assets/plugins/searcher.rb, line 54
def html
  @html ||= begin
    out = @doc.output
    # @see https://github.com/sparklemotion/nokogiri/issues/553
    good, buggy = Encoding::UTF_8, Encoding::ASCII_8BIT
    out = out.encode good if out.encoding == buggy
    Utils.send((@doc.output.strip =~ %r!<\!doctype\s+!i ? \
      :html : :html_fragment), out)
  end
end