Permalink
Browse files

Cleanup & small style fix

  • Loading branch information...
1 parent 00fd208 commit 93b9351a0f612e9acb4d484d50eebc1ffe5033f7 @ixti ixti committed Feb 2, 2013
View
2 lib/jekyll-assets.rb
@@ -1 +1 @@
-require 'jekyll/assets_plugin'
+require "jekyll/assets_plugin"
View
12 lib/jekyll/assets_plugin.rb
@@ -1,8 +1,4 @@
-require 'jekyll'
-require 'liquid'
-
-
-require 'jekyll/assets_plugin/site_patch'
-require 'jekyll/assets_plugin/filters'
-require 'jekyll/assets_plugin/tag'
-require 'jekyll/assets_plugin/version'
+require "jekyll/assets_plugin/site_patch"
+require "jekyll/assets_plugin/filters"
+require "jekyll/assets_plugin/tag"
+require "jekyll/assets_plugin/version"
View
19 lib/jekyll/assets_plugin/asset_file.rb
@@ -1,38 +1,38 @@
-# stdlib
-require 'set'
-
-
module Jekyll
module AssetsPlugin
- # Represents single asset that can be used as StaticFile for Site
- #
class AssetFile
- class NotFound < StandardError; end
@@mtimes = Hash.new
+
attr_reader :asset
+
def initialize site, asset
@site, @asset = site, asset
end
+
def destination dest
File.join(dest, @site.assets_config.dirname, @asset.digest_path)
end
+
def path
@asset.pathname.to_s
end
+
def mtime
@asset.mtime.to_i
end
+
def modified?
@@mtimes[path] != mtime
end
+
def write dest
dest_path = destination dest
@@ -43,6 +43,7 @@ def write dest
true
end
+
def == other
case other
when AssetFile then same_asset? other.asset
@@ -51,15 +52,19 @@ def == other
end
end
+
def to_s
"#<Jekyll::AssetsPlugin::AssetFile:#{asset.logical_path}>"
end
+
protected
+
def same_asset? other
other.pathname.cleanpath == asset.pathname.cleanpath
end
+
end
end
end
View
47 lib/jekyll/assets_plugin/configuration.rb
@@ -1,72 +1,39 @@
# stdlib
-require 'ostruct'
+require "ostruct"
module Jekyll
module AssetsPlugin
- # Plugin configuration class.
- #
- #
- # ##### sources
- #
- # Pathnames where to find assets relative to the root of the site.
- #
- # Default: ['_assets/javascripts', '_assets/stylesheets', '_assets/images']
- #
- #
- # ##### compress
- #
- # Sets compressors for the specific types of file: `js`, or `css`.
- #
- # Possible variants:
- #
- # css => 'yui', 'sass', nil
- # js => 'yui', 'uglifier', nil
- #
- # Default: { 'css' => nil, 'js' => nil } (no compression at all)
- #
- #
- # ##### dirname
- #
- # Destination pathname of processed assets relative to the compiled site
- # root (which is `_site` by default).
- #
- # Default: 'assets'
- #
- #
- # ##### baseurl
- #
- # Base URL for assets paths. By default equals dirname surrunded with
- # slashes. You ight want to change it if your blog has baseurl configuration
- # and served not from the root of the server, or if you want to keep your
- # assets on CDN.
- #
class Configuration
DEFAULTS = {
- :dirname => 'assets',
+ :dirname => "assets",
:sources => %w{_assets/javascripts _assets/stylesheets _assets/images},
:compress => { :css => nil, :js => nil }
}.freeze
+
def initialize config = {}
@data = OpenStruct.new DEFAULTS.merge(config)
@data.sources = [ @data.sources ] if @data.sources.is_a? String
@data.compress = OpenStruct.new @data.compress
- @data.dirname = @data.dirname.gsub(/^\/+|\/+$/, '')
+ @data.dirname = @data.dirname.gsub(/^\/+|\/+$/, "")
# if baseurl not given - autoguess base on dirname
@data.baseurl ||= "/#{@data.dirname}/".squeeze '/'
end
+
def baseurl
@data.baseurl.chomp "/"
end
+
def js_compressor
@data.compress.js ? @data.compress.js.to_sym : false
end
+
def css_compressor
@data.compress.css ? @data.compress.css.to_sym : false
end
View
16 lib/jekyll/assets_plugin/environment.rb
@@ -1,15 +1,23 @@
# 3rd-party
-require 'sprockets'
+require "sprockets"
# internal
-require 'jekyll/assets_plugin/asset_file'
-require 'jekyll/assets_plugin/liquid_processor'
+require "jekyll/assets_plugin/asset_file"
+require "jekyll/assets_plugin/liquid_processor"
module Jekyll
module AssetsPlugin
class Environment < Sprockets::Environment
+
+ class AssetNotFound < StandardError
+ def initialize path
+ super "Couldn't find file '#{path}'"
+ end
+ end
+
+
autoload :ContextPatch, "jekyll/assets_plugin/environment/context_patch"
@@ -38,7 +46,7 @@ def initialize site
def find_asset path, *args
- super or raise AssetFile::NotFound, "couldn't find file '#{path}'"
+ super or raise AssetNotFound, path
end
View
2 lib/jekyll/assets_plugin/filters.rb
@@ -5,6 +5,7 @@
module Jekyll
module AssetsPlugin
module Filters
+
%w{ asset asset_path javascript stylesheet }.each do |name|
module_eval <<-RUBY, __FILE__, __LINE__
def #{name} path # def stylesheet logical_path
@@ -13,6 +14,7 @@ def #{name} path # def stylesheet logical_path
end # end
RUBY
end
+
end
end
end
View
7 lib/jekyll/assets_plugin/liquid_processor.rb
@@ -1,15 +1,10 @@
# 3rd-party
-require 'liquid'
require 'tilt'
module Jekyll
module AssetsPlugin
- class LiquidProcessor < Tilt::Template
- def prepare
- @engine = Liquid::Template.parse data
- end
-
+ class LiquidProcessor < Tilt::LiquidTemplate
def evaluate context, locals, &block
@engine.render locals, {
:filters => [Jekyll::Filters],
View
4 lib/jekyll/assets_plugin/renderer.rb
@@ -23,14 +23,14 @@ def render_asset_path
def render_javascript
- @path << '.js' if File.extname(@path).empty?
+ @path << ".js" if File.extname(@path).empty?
JAVASCRIPT % render_asset_path
end
def render_stylesheet
- @path << '.css' if File.extname(@path).empty?
+ @path << ".css" if File.extname(@path).empty?
STYLESHEET % render_asset_path
end
View
13 lib/jekyll/assets_plugin/site_patch.rb
@@ -1,22 +1,25 @@
+# 3rd-party
+require "jekyll"
+
+
# internal
-require 'jekyll/assets_plugin/configuration'
-require 'jekyll/assets_plugin/environment'
+require "jekyll/assets_plugin/configuration"
+require "jekyll/assets_plugin/environment"
module Jekyll
module AssetsPlugin
- # Patch that provides #assets and #assets_config to Site
- #
module SitePatch
def assets_config
- @assets_config ||= Configuration.new(self.config['assets'] || {})
+ @assets_config ||= Configuration.new(self.config["assets"] || {})
end
def assets
@assets ||= Environment.new self
end
+
end
end
end
View
44 lib/jekyll/assets_plugin/tag.rb
@@ -1,55 +1,19 @@
# 3rd-party
-require 'liquid'
+require "liquid"
# internal
-require 'jekyll/assets_plugin/renderer'
+require "jekyll/assets_plugin/renderer"
module Jekyll
module AssetsPlugin
- # Class that implements some useful liquid tags:
- #
- #
- # ##### stylesheet
- #
- # Renders `<link>` tag for a logical path:
- #
- # {% stylesheet foo.css %}
- # # => '<link type="text/css" rel="stylesheet"
- # href="/assets/foo-b39e528efc3afe2def4bbc39de17f2b82cd8bd0d.css">
- #
- # You may omit extension so the following will give same result as above:
- #
- # {% stylesheet foo %}
- #
- #
- # ##### javascript
- #
- # Renders `<script>` tag for a logical path:
- #
- # {% javascript foo.js %}
- # # => '<script type="text/javascript"
- # src="/assets/foo-b39e528efc3afe2def4bbc39de17f2b82cd8bd0d.js">
- # </script>
- #
- # You may omit extension so the following will give same result as above:
- #
- # {% javascript foo %}
- #
- #
- # ##### asset_path
- #
- # Renders asset path for an asset (useful for images):
- #
- # {% asset_path foo.jpg %}
- # # => '/assets/foo-b39e528efc3afe2def4bbc39de17f2b82cd8bd0d.jpg"
- #
- #
class Tag < Liquid::Tag
+
def render context
Renderer.new(context, @markup).send :"render_#{@tag_name}"
end
+
end
end
end
View
8 spec/lib/jekyll-assets/bourbon_spec.rb
@@ -1,11 +1,11 @@
-require 'spec_helper'
-require 'jekyll-assets/bourbon'
+require "spec_helper"
+require "jekyll-assets/bourbon"
module Jekyll::AssetsPlugin
- describe 'Bourbon integration' do
+ describe "Bourbon integration" do
it "should globally append bourbon paths into Sprockets environment" do
- asset = @site.assets['vendor/bourbon.css'].to_s
+ asset = @site.assets["vendor/bourbon.css"].to_s
asset.should =~ /-webkit-box-shadow/
asset.should =~ /box-shadow/
View
8 spec/lib/jekyll-assets/compass_spec.rb
@@ -1,11 +1,11 @@
-require 'spec_helper'
-require 'jekyll-assets/compass'
+require "spec_helper"
+require "jekyll-assets/compass"
module Jekyll::AssetsPlugin
- describe 'Compass integration' do
+ describe "Compass integration" do
it "should globally append compass paths into Sprockets environment" do
- asset = @site.assets['vendor/compass.css'].to_s
+ asset = @site.assets["vendor/compass.css"].to_s
asset.should =~ /-webkit-box-shadow/
asset.should =~ /-moz-box-shadow/
View
6 spec/lib/jekyll/assets_plugin/asset_file_spec.rb
@@ -1,10 +1,10 @@
-require 'spec_helper'
+require "spec_helper"
module Jekyll::AssetsPlugin
describe AssetFile do
- context '#destination' do
- let(:file) { AssetFile.new(@site, @site.assets['app.css']) }
+ context "#destination" do
+ let(:file) { AssetFile.new(@site, @site.assets["app.css"]) }
subject { file.destination @dest.to_s }
it { should match %r{/app-[0-9a-f]{32}\.css$} }
end
View
54 spec/lib/jekyll/assets_plugin/configuration_spec.rb
@@ -1,86 +1,86 @@
-require 'spec_helper'
+require "spec_helper"
module Jekyll::AssetsPlugin
describe Configuration do
- context 'with defaults' do
+ context "with defaults" do
let(:config){ Configuration.new }
- context 'output assets dirname' do
+ context "output assets dirname" do
subject { config.dirname }
it { should == Configuration::DEFAULTS[:dirname] }
end
- context 'assets baseurl' do
+ context "assets baseurl" do
subject { config.baseurl }
it { should == "/" + Configuration::DEFAULTS[:dirname] }
end
- context 'sources list' do
+ context "sources list" do
subject { config.sources }
it { should =~ Configuration::DEFAULTS[:sources] }
end
- context 'js compressor' do
+ context "js compressor" do
subject { config.compress.js }
it { should be_nil }
end
- context 'css compressor' do
+ context "css compressor" do
subject { config.compress.css }
it { should be_nil }
end
end
- it 'should override specified options and leave defaults for missing' do
+ it "should override specified options and leave defaults for missing" do
config = Configuration.new({
:sources => %w{abc},
- :compress => { :css => 'sass' }
+ :compress => { :css => "sass" }
})
- config.dirname.should == 'assets'
+ config.dirname.should == "assets"
config.sources.should =~ %w{abc}
config.compress.js.should be_nil
- config.compress.css.should == 'sass'
+ config.compress.css.should == "sass"
end
- context '#baseurl' do
- it 'should respect explicit overrides' do
+ context "#baseurl" do
+ it "should respect explicit overrides" do
Configuration.new({
- :dirname => 'foo',
- :baseurl => '/bar/'
- }).baseurl.should == '/bar'
+ :dirname => "foo",
+ :baseurl => "/bar/"
+ }).baseurl.should == "/bar"
end
- it 'should be auto-guessed from dirname' do
+ it "should be auto-guessed from dirname" do
Configuration.new({
- :dirname => 'foo'
- }).baseurl.should == '/foo'
+ :dirname => "foo"
+ }).baseurl.should == "/foo"
end
end
- context '#js_compressor' do
- context 'when js compressor is given as "uglify"' do
- let(:config){ Configuration.new(:compress => {:js => 'uglify'}) }
+ context "#js_compressor" do
+ context "when js compressor is given as `uglify`" do
+ let(:config){ Configuration.new(:compress => {:js => "uglify"}) }
subject { config.js_compressor }
it { should be :uglify }
end
- context 'otherwise' do
+ context "otherwise" do
let(:config){ Configuration.new }
subject { config.js_compressor }
it { should be_false }
end
end
- context '#css_compressor' do
- context 'when js compressor is given as "sass"' do
- let(:config){ Configuration.new(:compress => {:css => 'sass'}) }
+ context "#css_compressor" do
+ context "when css compressor is given as `sass`" do
+ let(:config){ Configuration.new(:compress => {:css => "sass"}) }
subject { config.css_compressor }
it { should be :sass }
end
- context 'otherwise' do
+ context "otherwise" do
let(:config){ Configuration.new }
subject { config.css_compressor }
it { should be_false }
View
8 spec/lib/jekyll/assets_plugin/filters_spec.rb
@@ -27,7 +27,7 @@ def tag_re name
context "when <file> does not exists" do
subject { render("{{ 'not-found.css' | stylesheet }}") }
- it { should match "Liquid error: couldn't find file 'not-found.css'" }
+ it { should match "Liquid error: Couldn't find file 'not-found.css'" }
end
end
@@ -49,7 +49,7 @@ def tag_re name
context "when <file> does not exists" do
subject { render("{{ 'not-found.js' | javascript }}") }
- it { should match "Liquid error: couldn't find file 'not-found.js'" }
+ it { should match "Liquid error: Couldn't find file 'not-found.js'" }
end
end
@@ -61,7 +61,7 @@ def tag_re name
context "when <file> does not exists" do
subject { render("{{ 'not-found.css' | asset_path }}") }
- it { should match "Liquid error: couldn't find file 'not-found.css'" }
+ it { should match "Liquid error: Couldn't find file 'not-found.css'" }
end
context "with baseurl given as /foobar/" do
@@ -79,7 +79,7 @@ def tag_re name
context "when <file> does not exists" do
subject { render("{{ 'not-found.js' | asset }}") }
- it { should match "Liquid error: couldn't find file 'not-found.js'" }
+ it { should match "Liquid error: Couldn't find file 'not-found.js'" }
end
end
end
View
38 spec/lib/jekyll/assets_plugin/site_patch_spec.rb
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require "spec_helper"
module Jekyll::AssetsPlugin
@@ -13,9 +13,9 @@ def initialize
def config
@config ||= {
- 'dirname' => 'foobar',
- 'assets' => {
- 'sources' => [ 'foobar', '_assets' ]
+ "dirname" => "foobar",
+ "assets" => {
+ "sources" => [ "foobar", "_assets" ]
}
}
end
@@ -26,26 +26,26 @@ def source
end.new
end
- context '#assets' do
+ context "#assets" do
subject { site.assets }
it { should be_a_kind_of Sprockets::Environment }
- context 'calling #asset_path within assets' do
- context 'when requested file not found' do
- it 'should raise a NotFound error' do
+ context "calling #asset_path within assets" do
+ context "when requested file not found" do
+ it "should raise a NotFound error" do
Proc.new do
site.assets["should_fail.css"]
- end.should raise_error(AssetFile::NotFound)
+ end.should raise_error(Environment::AssetNotFound)
end
end
- context 'when requested file found' do
- it 'should have proper asset path' do
+ context "when requested file found" do
+ it "should have proper asset path" do
noise_img_re = %r{url\(/assets/noise-[a-f0-9]{32}\.png\)}
site.assets["app.css"].to_s.should match(noise_img_re)
end
- it 'should be appended to the static files list' do
+ it "should be appended to the static files list" do
asset = site.assets["app.css"] # make sure main asset was compiled
asset = site.assets["noise.png"]
@@ -54,24 +54,24 @@ def source
end
end
- context 'with Liquid markup within assets' do
- it 'should be rendered' do
+ context "with Liquid markup within assets" do
+ it "should be rendered" do
site.assets["app.js"].to_s.should match(/noise-[a-f0-9]{32}\.png/)
end
end
end
- context '#assets_config' do
+ context "#assets_config" do
subject { site.assets_config }
it { should be_an_instance_of Configuration }
- it 'should been populated with `assets` section of config' do
- site.assets_config.dirname.should_not == 'foobar'
- site.assets_config.sources.should include 'foobar'
+ it "should been populated with `assets` section of config" do
+ site.assets_config.dirname.should_not == "foobar"
+ site.assets_config.sources.should include "foobar"
end
end
- it 'should be included into Jekyll::Site' do
+ it "should be included into Jekyll::Site" do
Jekyll::Site.included_modules.should include SitePatch
end
end
View
64 spec/lib/jekyll/assets_plugin/tag_spec.rb
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require "spec_helper"
module Jekyll::AssetsPlugin
@@ -9,77 +9,77 @@ def render content
Liquid::Template.parse(content).render({}, context)
end
- context '{% stylesheet <file> %}' do
+ context "{% stylesheet <file> %}" do
def tag_re name
file = "/assets/#{name}-[a-f0-9]{32}\.css"
Regexp.new "^#{Renderer::STYLESHEET % file}$"
end
- context 'when <file> exists' do
- subject { render('{% stylesheet app.css %}') }
+ context "when <file> exists" do
+ subject { render("{% stylesheet app.css %}") }
it { should match tag_re("app") }
end
- context 'when <file> extension is omited' do
- subject { render('{% stylesheet app %}') }
+ context "when <file> extension is omited" do
+ subject { render("{% stylesheet app %}") }
it { should match tag_re("app") }
end
- context 'when <file> does not exists' do
- subject { render('{% stylesheet not-found.css %}') }
- it { should match "Liquid error: couldn't find file 'not-found.css'" }
+ context "when <file> does not exists" do
+ subject { render("{% stylesheet not-found.css %}") }
+ it { should match "Liquid error: Couldn't find file 'not-found.css'" }
end
end
- context '{% javascript <file> %}' do
+ context "{% javascript <file> %}" do
def tag_re name
file = "/assets/#{name}-[a-f0-9]{32}\.js"
Regexp.new "^#{Renderer::JAVASCRIPT % file}$"
end
- context 'when <file> exists' do
- subject { render('{% javascript app.js %}') }
+ context "when <file> exists" do
+ subject { render("{% javascript app.js %}") }
it { should match tag_re("app") }
end
- context 'when <file> extension omited' do
- subject { render('{% javascript app %}') }
+ context "when <file> extension omited" do
+ subject { render("{% javascript app %}") }
it { should match tag_re("app") }
end
- context 'when <file> does not exists' do
- subject { render('{% javascript not-found.js %}') }
- it { should match "Liquid error: couldn't find file 'not-found.js'" }
+ context "when <file> does not exists" do
+ subject { render("{% javascript not-found.js %}") }
+ it { should match "Liquid error: Couldn't find file 'not-found.js'" }
end
end
- context '{% asset_path <file.ext> %}' do
- context 'when <file> exists' do
- subject { render('{% asset_path app.css %}') }
+ context "{% asset_path <file.ext> %}" do
+ context "when <file> exists" do
+ subject { render("{% asset_path app.css %}") }
it { should match(%r{^/assets/app-[a-f0-9]{32}\.css$}) }
end
- context 'when <file> does not exists' do
- subject { render('{% asset_path not-found.js %}') }
- it { should match "Liquid error: couldn't find file 'not-found.js'" }
+ context "when <file> does not exists" do
+ subject { render("{% asset_path not-found.js %}") }
+ it { should match "Liquid error: Couldn't find file 'not-found.js'" }
end
- context 'with baseurl given as /foobar/' do
- before { context[:registers][:site].assets_config.baseurl = '/foobar/' }
- subject { render('{% asset_path app.css %}') }
+ context "with baseurl given as /foobar/" do
+ before { context[:registers][:site].assets_config.baseurl = "/foobar/" }
+ subject { render("{% asset_path app.css %}") }
it { should match(%r{^/foobar/app-[a-f0-9]{32}\.css$}) }
end
end
- context '{% asset <file.ext> %}' do
- context 'when <file> exists' do
- subject { render('{% asset app.css %}') }
+ context "{% asset <file.ext> %}" do
+ context "when <file> exists" do
+ subject { render("{% asset app.css %}") }
it { should match(/body \{ background-image: url\(.+?\) \}/) }
end
- context 'when <file> does not exists' do
- subject { render('{% asset_path not-found.js %}') }
- it { should match "Liquid error: couldn't find file 'not-found.js'" }
+ context "when <file> does not exists" do
+ subject { render("{% asset_path not-found.js %}") }
+ it { should match "Liquid error: Couldn't find file 'not-found.js'" }
end
end
end
View
16 spec/spec_helper.rb
@@ -1,24 +1,24 @@
-require 'rubygems'
+require "rubygems"
-require 'jekyll'
-require 'liquid'
-require 'sprockets'
+require "jekyll"
+require "liquid"
+require "sprockets"
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
-Dir[File.expand_path('../support', __FILE__) + "/**/*.rb"].each {|f| require f}
+Dir[File.expand_path("../support", __FILE__) + "/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
config.include Jekyll::AssetsPlugin::RSpecHelpers
config.before(:all) do
- @dest = fixtures_path.join('_site')
+ @dest = fixtures_path.join("_site")
@site = Jekyll::Site.new(Jekyll.configuration({
- 'source' => fixtures_path.to_s,
- 'destination' => @dest.to_s
+ "source" => fixtures_path.to_s,
+ "destination" => @dest.to_s
}))
@dest.rmtree if @dest.exist?
View
4 spec/support/fixtures_path.rb
@@ -1,12 +1,12 @@
-require 'pathname'
+require "pathname"
module Jekyll::AssetsPlugin
module RSpecHelpers
extend self
def fixtures_path
- @fixtures_path ||= Pathname.new(__FILE__).parent.parent.join('fixtures')
+ @fixtures_path ||= Pathname.new(__FILE__).parent.parent.join("fixtures")
end
end
end

0 comments on commit 93b9351

Please sign in to comment.