Sha256: 7633681d2d2a1d6f2988082550207a0046d65a90adf948fb04ec3c1f0da8ea5c

Contents?: true

Size: 891 Bytes

Versions: 8

Compression:

Stored size: 891 Bytes

Contents

module SmallCage
  class CacheFilter

    def initialize(opts)
    end

    def after_rendering_filter(obj, str)
      str.gsub %r{(\s(?:src|href)=["'])(?!https?://)([^"']+--latest\.(?:css|js|png|gif|jpg))(["'])} do
        pre  = $1
        path = $2
        pro  = $3
        dir  = obj["dirs"][path[0] == ?/ ? 0 : -1]["path"]
        pre + find_latest(dir, path) + pro
      end
    end

    def find_latest(dir, path)
      relpath = path[0] == ?/ ? path[1..-1] : path
      return path unless (dir + relpath).exist?
      
      rex = /^(.+)--latest(\.[^.]+)$/
      pattern    = relpath.to_s.sub(rex, '\1-*\2')
      
      entry = nil
      Dir.chdir(dir) do
        entry = Dir.glob(pattern).reject {|f| f == relpath }.sort.last
      end
      return path unless entry
      
      entry = "/" + entry if path[0] == ?/ 
      return entry
    end
    private :find_latest
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
bluemark-smallcage-0.1.4 project/cache/_smc/filters/cache_filter.rb
bluemark-smallcage-0.1.5 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.9 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.8 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.7 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.6 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.5 project/cache/_smc/filters/cache_filter.rb
smallcage-0.1.4 project/cache/_smc/filters/cache_filter.rb