Sha256: 794e23e5492727d508b69c8e978d98d5ac4d6b54befcb2a4f0a918e851ed968c

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 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{^(?:[^:]+:)?//(?:[^./]+\.)+[^./]+/}
      PARAMS_RE  = / (?: "(?<path>[^"]+)" | '(?<path>[^']+)' | (?<path>[^ ]+) )
                     (?<attrs>.*)
                   /x

      attr_reader :site, :path, :attrs

      def initialize(context, params)
        @site = context.registers[:site]

        match = params.strip.match PARAMS_RE

        @path  = match["path"]
        @attrs = match["attrs"]
      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

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-assets-0.11.0 lib/jekyll/assets_plugin/renderer.rb
jekyll-assets-0.10.1 lib/jekyll/assets_plugin/renderer.rb