Skip to content

Commit

Permalink
Refactor asset path rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Aug 11, 2013
1 parent a4c18d5 commit d20a8fb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
20 changes: 7 additions & 13 deletions lib/jekyll/assets_plugin/asset_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@ module Jekyll
module AssetsPlugin
class AssetPath

attr_reader :asset
attr_writer :anchor, :query


def initialize site, pathname, *args
pathname, _, @anchor = pathname.rpartition "#" if pathname["#"]
pathname, _, @query = pathname.rpartition "?" if pathname["?"]

@asset = site.assets[pathname, *args]
@site = site

site.bundle_asset! asset
def initialize asset
@asset = asset.bundle!
end


def cachebust
@cachebust ||= @site.assets_config.cachebust
@cachebust ||= @asset.site.assets_config.cachebust
end


def path
:hard == cachebust && asset.digest_path || asset.logical_path
:hard == cachebust && @asset.digest_path || @asset.logical_path
end


def query
query = []

query << "cb=#{asset.digest}" if :soft == cachebust
query << "cb=#{@asset.digest}" if :soft == cachebust
query << @query if @query

"?#{query.join '&'}" unless query.empty?
Expand All @@ -42,7 +36,7 @@ def anchor


def to_s
"#{@site.assets_config.baseurl}/#{path}#{query}#{anchor}"
"#{@asset.site.assets_config.baseurl}/#{path}#{query}#{anchor}"
end

end
Expand Down
6 changes: 6 additions & 0 deletions lib/jekyll/assets_plugin/patches/asset_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def jekyll_assets
end


def bundle!
site.bundle_asset! self if site
self
end


def destination dest
File.join dest, site.assets_config.dirname, filename
end
Expand Down
10 changes: 8 additions & 2 deletions lib/jekyll/assets_plugin/patches/site_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def asset_files
end


def asset_path *args
AssetPath.new(self, *args).to_s
def asset_path pathname, *args
pathname, _, anchor = pathname.rpartition "#" if pathname["#"]
pathname, _, query = pathname.rpartition "?" if pathname["?"]

AssetPath.new(assets[pathname, *args]).tap{ |ap|
ap.anchor = anchor
ap.query = query
}.to_s
end


Expand Down
25 changes: 11 additions & 14 deletions lib/jekyll/assets_plugin/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@ def render_asset_path

def render_javascript
@path << ".js" if File.extname(@path).empty?

if @site.assets_config.debug
return @site.assets[@path].to_a.map{ |a|
JAVASCRIPT % @site.asset_path(a.logical_path, :bundle => false)
}.join("\n")
end

JAVASCRIPT % render_asset_path
render_tag JAVASCRIPT
end


def render_stylesheet
@path << ".css" if File.extname(@path).empty?
render_tag STYLESHEET
end


protected

if @site.assets_config.debug
return @site.assets[@path].to_a.map{ |a|
STYLESHEET % @site.asset_path(a.logical_path, :bundle => false)
}.join("\n")
end

STYLESHEET % render_asset_path
def render_tag template
asset = @site.assets[@path]
(@site.assets_config.debug ? asset.to_a : [asset]).map{ |a|
template % AssetPath.new(a).to_s
}.join("\n")
end

end
Expand Down

0 comments on commit d20a8fb

Please sign in to comment.