Sha256: cbf4133507e39f152ed75fc955a79ce632e04643ec5c0fb7fece0bd38a14c026

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Jekyll
  module AssetsPlugin
    class Renderer
      STYLESHEET = '<link rel="stylesheet" href="%{path}"%{attrs}>'
      JAVASCRIPT = '<script src="%{path}"%{attrs}></script>'
      IMAGE      = '<img src="%{path}"%{attrs}>'

      URI_RE     = %r{^(?:[^:]+:)?//(?:[^./]+\.)+[^./]+/}

      def initialize(context, logical_path)
        @site   = context.registers[:site]
        @path   = logical_path.strip

        @path, _, @attrs = @path.partition(" ") if @path[" "]
      end

      def render_asset
        fail "Can't render remote asset: #{@path}" if remote?
        @site.assets[@path].to_s
      end

      def render_asset_path
        return @path if remote?
        @site.asset_path @path
      end

      def render_javascript
        render_tag JAVASCRIPT, ".js"
      end

      def render_stylesheet
        render_tag STYLESHEET, ".css"
      end

      def render_image
        render_tag IMAGE
      end

      private

      def render_tag(template, extension = "")
        return format(template, :path => @path, :attrs => @attrs) if remote?

        @path << extension if extension.to_s != File.extname(@path)

        asset = @site.assets[@path]
        tags  = (@site.assets_config.debug ? asset.to_a : [asset]).map do |a|
          format template, :path => AssetPath.new(a).to_s, :attrs => @attrs
        end

        tags.join "\n"
      end

      def remote?
        @path =~ URI_RE
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-assets-0.9.1 lib/jekyll/assets_plugin/renderer.rb