Sha256: d85366d5e3b752c1d2c953066b76a0b31e4978d952dc06faa004a5b8095ead9e

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

# Add assets (by name) from the current Liquidum site
#
# == Basic usage:
#    {%asset 'test.png'%}
#
# == Advanced usage:
#    {%asset 'test.png' height="72px"%}
#    {%asset 'test.png' style="height: 72px;"%}
#
# Note: It will only look at published assets
class AssetTag < LiquidumTag
  include Rails.application.routes.url_helpers

  def render(context)
    super

    current_content = context.registers['content']

    content = current_content.site.contents.published.located(argv1, restricted: false).first

    return "<!-- asset '#{argv1}' not found -->" unless content

    full_path = content.full_path
    if File.basename(full_path).include?('/_')
      full_path = context.registers['controller'].helpers.scribo.content_path(content)
    end

    case content&.media_type
    when 'image'
      %[<img] +
        attr_str(:src, arg(:src), full_path) +
        attr_str(:alt, content.properties['title'], content.properties['title']) +
        attr_str(:title, content.properties['caption'], content.properties['caption']) +
        attr_str(:width, arg(:width)) +
        attr_str(:height, arg(:height)) +
        attr_str(:style, arg(:style)) +
        %[/>]
    else
      if content&.content_type == 'text/css'
        %[<link rel="stylesheet" type="text/css"] +
          attr_str(:href, arg(:href), full_path) +
          %[/>]
      elsif content&.content_type == 'application/javascript'
        %[<script] +
          attr_str(:src, arg(:src), full_path) +
          %[/></script>]
      else
        "<!-- unknown asset: #{argv1} -->"
      end
    end
  end
end

Liquid::Template.register_tag('asset', AssetTag)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
scribo-1.0.44 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.43 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.42 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.41 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.40 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.39 lib/scribo/liquid/tags/asset_tag.rb
scribo-1.0.38 lib/scribo/liquid/tags/asset_tag.rb