jekyll-assets 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.3.0 (2013-01-08)
2
+
3
+ * Add complimentary filters (same names as tags).
4
+
5
+
1
6
  ### 0.2.3 (2013-01-07)
2
7
 
3
8
  * Add built-in bourbon support as: `require "jekyll-assets/bourbon"`.
data/README.md CHANGED
@@ -60,13 +60,28 @@ into `_plugins/ext.rb` file:
60
60
  require "jekyll-assets"
61
61
  ```
62
62
 
63
- Once plugin installed, you'll have following liquid tags available:
63
+ Once plugin installed, you'll have following Liquid tags available:
64
64
 
65
65
  - `{% javascript app %}`: Generates `<script>` tag for `app.js`
66
66
  - `{% stylesheet app %}`: Generates `<link>` tag for `app.css`
67
67
  - `{% asset_path logo.png %}`: Returns _resulting_ URL for `logo.png`
68
68
  - `{% asset app.css %}`: Returns _compiled_ body of `app.css`
69
69
 
70
+ Also you'll have complimentary Liquid filters as well:
71
+
72
+ - `{{ 'app' | javascript }}`: Generates `<script>` tag for `app.js`
73
+ - `{{ 'app' | stylesheet }}`: Generates `<link>` tag for `app.css`
74
+ - `{{ 'logo.png' | asset_path }}`: Returns _resulting_ URL for `logo.png`
75
+ - `{{ 'app.css' | asset }}`: Returns _compiled_ body of `app.css`
76
+
77
+ Filters are used mostly to render tag (or asset source) using variable that
78
+ holds value of asset logical path rather than specifiyng it directly. Here's
79
+ an example that speaks for itself:
80
+
81
+ ```
82
+ {% if page.custom_css %}{{ page.custom_css | stylesheet }}{% endif %}
83
+ ```
84
+
70
85
  All compiled assets will be stored under `assets/` dir of generated site.
71
86
 
72
87
  Pipeline assets should be under your sources directory of Jekyll site. When a
@@ -3,6 +3,7 @@ require 'liquid'
3
3
 
4
4
 
5
5
  require 'jekyll/assets_plugin/site_patch'
6
+ require 'jekyll/assets_plugin/filters'
6
7
  require 'jekyll/assets_plugin/tag'
7
8
  require 'jekyll/assets_plugin/version'
8
9
 
@@ -10,7 +11,10 @@ require 'jekyll/assets_plugin/version'
10
11
  Jekyll::Site.send :include, Jekyll::AssetsPlugin::SitePatch
11
12
 
12
13
 
13
- Liquid::Template.register_tag('javascript', Jekyll::AssetsPlugin::Tag)
14
- Liquid::Template.register_tag('stylesheet', Jekyll::AssetsPlugin::Tag)
15
- Liquid::Template.register_tag('asset_path', Jekyll::AssetsPlugin::Tag)
16
- Liquid::Template.register_tag('asset', Jekyll::AssetsPlugin::Tag)
14
+ Liquid::Template.register_filter Jekyll::AssetsPlugin::Filters
15
+
16
+
17
+ Liquid::Template.register_tag 'javascript', Jekyll::AssetsPlugin::Tag
18
+ Liquid::Template.register_tag 'stylesheet', Jekyll::AssetsPlugin::Tag
19
+ Liquid::Template.register_tag 'asset_path', Jekyll::AssetsPlugin::Tag
20
+ Liquid::Template.register_tag 'asset', Jekyll::AssetsPlugin::Tag
@@ -28,8 +28,8 @@ module Jekyll
28
28
  #
29
29
  # ##### dirname
30
30
  #
31
- # Pathname of the destination of generated (bundled) assets relative to the
32
- # destination of the root.
31
+ # Destination pathname of processed assets relative to the compiled site
32
+ # root (which is `_site` by default).
33
33
  #
34
34
  # Default: 'assets'
35
35
  #
@@ -0,0 +1,18 @@
1
+ # internal
2
+ require 'jekyll/assets_plugin/renderer'
3
+
4
+
5
+ module Jekyll
6
+ module AssetsPlugin
7
+ module Filters
8
+ %w{ asset asset_path javascript stylesheet }.each do |name|
9
+ module_eval <<-RUBY, __FILE__, __LINE__
10
+ def #{name} path # def stylesheet logical_path
11
+ r = Renderer.new @context, path # r = Renderer.new @context, path
12
+ r.render_#{name} # r.render_stylesheet
13
+ end # end
14
+ RUBY
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ module Jekyll
2
+ module AssetsPlugin
3
+ class Renderer
4
+ STYLESHEET = '<link rel="stylesheet" type="text/css" href="%s">'
5
+ JAVASCRIPT = '<script type="text/javascript" src="%s"></script>'
6
+
7
+ def initialize context, logical_path
8
+ @site = context.registers[:site]
9
+ @path = logical_path.strip
10
+ end
11
+
12
+ def render_asset
13
+ asset.to_s
14
+ end
15
+
16
+ def render_asset_path
17
+ unless @site.static_files.include? asset
18
+ @site.static_files << AssetFile.new(@site, asset)
19
+ end
20
+
21
+ return "#{@site.assets_config.baseurl.chomp '/'}/#{asset.digest_path}"
22
+ end
23
+
24
+ def render_javascript
25
+ @path << '.js' if File.extname(@path).empty?
26
+
27
+ JAVASCRIPT % render_asset_path
28
+ end
29
+
30
+ def render_stylesheet
31
+ @path << '.css' if File.extname(@path).empty?
32
+
33
+ STYLESHEET % render_asset_path
34
+ end
35
+
36
+ protected
37
+
38
+ def asset
39
+ unless @asset ||= @site.assets[@path]
40
+ raise AssetFile::NotFound, "couldn't find file '#{@path}'"
41
+ end
42
+
43
+ @asset
44
+ end
45
+ end
46
+ end
47
+ end
48
+
@@ -2,8 +2,8 @@
2
2
  require 'liquid'
3
3
 
4
4
 
5
- # stdlib
6
- require 'set'
5
+ # internal
6
+ require 'jekyll/assets_plugin/renderer'
7
7
 
8
8
 
9
9
  module Jekyll
@@ -47,67 +47,8 @@ module Jekyll
47
47
  #
48
48
  #
49
49
  class Tag < Liquid::Tag
50
- STYLESHEET = '<link rel="stylesheet" type="text/css" href="%s">'
51
- JAVASCRIPT = '<script type="text/javascript" src="%s"></script>'
52
- EXTENSIONS = { 'stylesheet' => '.css', 'javascript' => '.js' }
53
-
54
- @@errors = Set.new
55
-
56
- def initialize tag_name, logical_path, tokens
57
- super
58
-
59
- @logical_path = logical_path.strip
60
-
61
- # append auto-guessed extension if needed
62
- @logical_path << default_extension if File.extname(@logical_path).empty?
63
- end
64
-
65
50
  def render context
66
- send :"render_#{@tag_name}", context
67
- end
68
-
69
- protected
70
-
71
- def default_extension
72
- EXTENSIONS[@tag_name].to_s
73
- end
74
-
75
- def with_asset context, &block
76
- site = context.registers[:site]
77
- path = @logical_path
78
- asset = site.assets[path]
79
-
80
- raise AssetFile::NotFound, "couldn't find file '#{path}'" unless asset
81
-
82
- yield asset, site
83
- end
84
-
85
- def render_asset context
86
- with_asset context do |asset|
87
- return asset.to_s
88
- end
89
- end
90
-
91
- def render_asset_path context
92
- with_asset context do |asset, site|
93
- unless site.static_files.include? asset
94
- site.static_files << AssetFile.new(site, asset)
95
- end
96
-
97
- return "#{site.assets_config.baseurl.chomp '/'}/#{asset.digest_path}"
98
- end
99
- end
100
-
101
- def render_javascript context
102
- if url = render_asset_path(context)
103
- return JAVASCRIPT % [url]
104
- end
105
- end
106
-
107
- def render_stylesheet context
108
- if url = render_asset_path(context)
109
- return STYLESHEET % [url]
110
- end
51
+ Renderer.new(context, @markup).send :"render_#{@tag_name}"
111
52
  end
112
53
  end
113
54
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module AssetsPlugin
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,86 @@
1
+ require "spec_helper"
2
+
3
+
4
+ module Jekyll::AssetsPlugin
5
+ describe Filters do
6
+ let(:context) { { :registers => { :site => @site } } }
7
+
8
+ def render content
9
+ Liquid::Template.parse(content).render({}, context)
10
+ end
11
+
12
+ context "{{ '<file>' | stylesheet }}" do
13
+ def tag_re name
14
+ file = "/assets/#{name}-[a-f0-9]{32}\.css"
15
+ Regexp.new "^#{Renderer::STYLESHEET % file}$"
16
+ end
17
+
18
+ context "when <file> exists" do
19
+ subject { render("{{ 'app.css' | stylesheet }}") }
20
+ it { should match tag_re("app") }
21
+ end
22
+
23
+ context "when <file> extension is omited" do
24
+ subject { render("{{ 'app' | stylesheet }}") }
25
+ it { should match tag_re("app") }
26
+ end
27
+
28
+ context "when <file> does not exists" do
29
+ subject { render("{{ 'not-found.css' | stylesheet }}") }
30
+ it { should match "Liquid error: couldn't find file 'not-found.css'" }
31
+ end
32
+ end
33
+
34
+ context "{{ '<file>' | javascript }}" do
35
+ def tag_re name
36
+ file = "/assets/#{name}-[a-f0-9]{32}\.js"
37
+ Regexp.new "^#{Renderer::JAVASCRIPT % file}$"
38
+ end
39
+
40
+ context "when <file> exists" do
41
+ subject { render("{{ 'app.js' | javascript }}") }
42
+ it { should match tag_re("app") }
43
+ end
44
+
45
+ context "when <file> extension omited" do
46
+ subject { render("{{ 'app' | javascript }}") }
47
+ it { should match tag_re("app") }
48
+ end
49
+
50
+ context "when <file> does not exists" do
51
+ subject { render("{{ 'not-found.js' | javascript }}") }
52
+ it { should match "Liquid error: couldn't find file 'not-found.js'" }
53
+ end
54
+ end
55
+
56
+ context "{{ '<file.ext>' | asset_path }}" do
57
+ context "when <file> exists" do
58
+ subject { render("{{ 'app.css' | asset_path }}") }
59
+ it { should match(%r{^/assets/app-[a-f0-9]{32}\.css$}) }
60
+ end
61
+
62
+ context "when <file> does not exists" do
63
+ subject { render("{{ 'not-found.css' | asset_path }}") }
64
+ it { should match "Liquid error: couldn't find file 'not-found.css'" }
65
+ end
66
+
67
+ context "with baseurl given as /foobar/" do
68
+ before { context[:registers][:site].assets_config.baseurl = "/foobar/" }
69
+ subject { render("{{ 'app.css' | asset_path }}") }
70
+ it { should match(%r{^/foobar/app-[a-f0-9]{32}\.css$}) }
71
+ end
72
+ end
73
+
74
+ context "{{ '<file.ext>' | asset }}" do
75
+ context "when <file> exists" do
76
+ subject { render("{{ 'app.css' | asset }}") }
77
+ it { should match(/body \{ background-image: url\(.+?\) \}/) }
78
+ end
79
+
80
+ context "when <file> does not exists" do
81
+ subject { render("{{ 'not-found.js' | asset }}") }
82
+ it { should match "Liquid error: couldn't find file 'not-found.js'" }
83
+ end
84
+ end
85
+ end
86
+ end
@@ -12,25 +12,20 @@ module Jekyll::AssetsPlugin
12
12
  context '{% stylesheet <file> %}' do
13
13
  def tag_re name
14
14
  file = "/assets/#{name}-[a-f0-9]{32}\.css"
15
- Regexp.new "^#{Tag::STYLESHEET % file}$"
15
+ Regexp.new "^#{Renderer::STYLESHEET % file}$"
16
16
  end
17
17
 
18
- context 'when <file> is bundled' do
18
+ context 'when <file> exists' do
19
19
  subject { render('{% stylesheet app.css %}') }
20
20
  it { should match tag_re("app") }
21
21
  end
22
22
 
23
- context 'when <file> is not explicitly bundled' do
24
- subject { render('{% stylesheet vapor.css %}') }
25
- it { should match tag_re("vapor") }
26
- end
27
-
28
23
  context 'when <file> extension is omited' do
29
24
  subject { render('{% stylesheet app %}') }
30
25
  it { should match tag_re("app") }
31
26
  end
32
27
 
33
- context 'when <file> is not found' do
28
+ context 'when <file> does not exists' do
34
29
  subject { render('{% stylesheet not-found.css %}') }
35
30
  it { should match "Liquid error: couldn't find file 'not-found.css'" }
36
31
  end
@@ -39,55 +34,32 @@ module Jekyll::AssetsPlugin
39
34
  context '{% javascript <file> %}' do
40
35
  def tag_re name
41
36
  file = "/assets/#{name}-[a-f0-9]{32}\.js"
42
- Regexp.new "^#{Tag::JAVASCRIPT % file}$"
37
+ Regexp.new "^#{Renderer::JAVASCRIPT % file}$"
43
38
  end
44
39
 
45
- context 'when <file> is bundled' do
40
+ context 'when <file> exists' do
46
41
  subject { render('{% javascript app.js %}') }
47
42
  it { should match tag_re("app") }
48
43
  end
49
44
 
50
- context 'when <file> is not explicitly bundled' do
51
- subject { render('{% javascript vapor.js %}') }
52
- it { should match tag_re("vapor") }
53
- end
54
-
55
45
  context 'when <file> extension omited' do
56
46
  subject { render('{% javascript app %}') }
57
47
  it { should match tag_re("app") }
58
48
  end
59
49
 
60
- context 'when <file> is not found' do
50
+ context 'when <file> does not exists' do
61
51
  subject { render('{% javascript not-found.js %}') }
62
52
  it { should match "Liquid error: couldn't find file 'not-found.js'" }
63
53
  end
64
54
  end
65
55
 
66
56
  context '{% asset_path <file.ext> %}' do
67
- context 'when <file> is bundled' do
57
+ context 'when <file> exists' do
68
58
  subject { render('{% asset_path app.css %}') }
69
59
  it { should match(%r{^/assets/app-[a-f0-9]{32}\.css$}) }
70
60
  end
71
61
 
72
- context 'when <file> is not explicitly bundled, but required' do
73
- subject { render('{% asset_path vapor.js %}') }
74
- it { should match(%r{^/assets/vapor-[a-f0-9]{32}\.js$}) }
75
-
76
- it "should be appended to the static files list" do
77
- asset = context[:registers][:site].assets["vapor.js"]
78
-
79
- context[:registers][:site].static_files.include?(asset).should be_true
80
- end
81
-
82
- it "should be bundled file automagically upon site#write" do
83
- context[:registers][:site].cleanup
84
- context[:registers][:site].write
85
-
86
- File.exist?("#{fixtures_path.join '_site'}#{subject}").should be_true
87
- end
88
- end
89
-
90
- context 'when <file> is not found' do
62
+ context 'when <file> does not exists' do
91
63
  subject { render('{% asset_path not-found.js %}') }
92
64
  it { should match "Liquid error: couldn't find file 'not-found.js'" }
93
65
  end
@@ -105,7 +77,7 @@ module Jekyll::AssetsPlugin
105
77
  it { should match(/body \{ background-image: url\(.+?\) \}/) }
106
78
  end
107
79
 
108
- context 'when <file> is not found' do
80
+ context 'when <file> does not exists' do
109
81
  subject { render('{% asset_path not-found.js %}') }
110
82
  it { should match "Liquid error: couldn't find file 'not-found.js'" }
111
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-07 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jekyll
@@ -164,6 +164,8 @@ files:
164
164
  - lib/jekyll/assets_plugin.rb
165
165
  - lib/jekyll/assets_plugin/asset_file.rb
166
166
  - lib/jekyll/assets_plugin/configuration.rb
167
+ - lib/jekyll/assets_plugin/filters.rb
168
+ - lib/jekyll/assets_plugin/renderer.rb
167
169
  - lib/jekyll/assets_plugin/site_patch.rb
168
170
  - lib/jekyll/assets_plugin/tag.rb
169
171
  - lib/jekyll/assets_plugin/version.rb
@@ -184,6 +186,7 @@ files:
184
186
  - spec/lib/jekyll-assets/compass_spec.rb
185
187
  - spec/lib/jekyll/assets_plugin/asset_file_spec.rb
186
188
  - spec/lib/jekyll/assets_plugin/configuration_spec.rb
189
+ - spec/lib/jekyll/assets_plugin/filters_spec.rb
187
190
  - spec/lib/jekyll/assets_plugin/site_patch_spec.rb
188
191
  - spec/lib/jekyll/assets_plugin/tag_spec.rb
189
192
  - spec/spec_helper.rb
@@ -211,7 +214,7 @@ rubyforge_project:
211
214
  rubygems_version: 1.8.23
212
215
  signing_key:
213
216
  specification_version: 3
214
- summary: jekyll-assets-0.2.3
217
+ summary: jekyll-assets-0.3.0
215
218
  test_files:
216
219
  - spec/fixtures/.gitignore
217
220
  - spec/fixtures/_assets/app.css.erb
@@ -230,6 +233,7 @@ test_files:
230
233
  - spec/lib/jekyll-assets/compass_spec.rb
231
234
  - spec/lib/jekyll/assets_plugin/asset_file_spec.rb
232
235
  - spec/lib/jekyll/assets_plugin/configuration_spec.rb
236
+ - spec/lib/jekyll/assets_plugin/filters_spec.rb
233
237
  - spec/lib/jekyll/assets_plugin/site_patch_spec.rb
234
238
  - spec/lib/jekyll/assets_plugin/tag_spec.rb
235
239
  - spec/spec_helper.rb