Class: Middleman::Extensions::AssetHash::AssetHashManager

Inherits:
Object
  • Object
show all
Defined in:
middleman-more/lib/middleman-more/extensions/asset_hash.rb

Instance Method Summary (collapse)

Constructor Details

- (AssetHashManager) initialize(app, exts)

A new instance of AssetHashManager



21
22
23
24
# File 'middleman-more/lib/middleman-more/extensions/asset_hash.rb', line 21

def initialize(app, exts)
  @app = app
  @exts = exts
end

Instance Method Details

- (void) manipulate_resource_list(resources)

This method returns an undefined value.

Update the main sitemap resource list



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'middleman-more/lib/middleman-more/extensions/asset_hash.rb', line 28

def manipulate_resource_list(resources)
  resources.each do |resource|
    if @exts.include? resource.ext
      # figure out the path Sprockets would use for this asset
      if resource.ext == '.js'
        sprockets_path = resource.path.sub(@app.js_dir,'').sub(/^\//,'')
      elsif resource.ext == '.css'
        sprockets_path = resource.path.sub(@app.css_dir,'').sub(/^\//,'')
      end

      # See if Sprockets knows about the file
      asset = @app.sprockets.find_asset(sprockets_path) if sprockets_path

      if asset # if it's a Sprockets asset, ask sprockets for its digest
        digest = asset.digest[0..7]
      elsif resource.template? # if it's a template, render it out
        digest = Digest::SHA1.hexdigest(resource.render)[0..7]
      else # if it's a static file, just hash it
        digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7]
      end

      resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
    end
  end
end