jekyll-contentblocks 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +39 -22
- data/Rakefile +2 -15
- data/gemfiles/jekyll_1.0.4.gemfile +2 -0
- data/gemfiles/jekyll_1.1.2.gemfile +2 -0
- data/gemfiles/jekyll_1.2.1.gemfile +2 -0
- data/gemfiles/jekyll_1.3.1.gemfile +2 -0
- data/gemfiles/jekyll_1.4.3.gemfile +2 -0
- data/gemfiles/jekyll_1.5.1.gemfile +2 -0
- data/gemfiles/jekyll_2.0.3.gemfile +2 -0
- data/gemfiles/jekyll_2.1.1.gemfile +2 -0
- data/gemfiles/jekyll_2.2.0.gemfile +2 -0
- data/gemfiles/jekyll_2.3.0.gemfile +2 -0
- data/gemfiles/jekyll_2.4.0.gemfile +2 -0
- data/gemfiles/jekyll_2.5.3.gemfile +2 -0
- data/jekyll-contentblocks.gemspec +1 -1
- data/lib/jekyll-contentblocks.rb +12 -5
- data/lib/jekyll/content_block_tag.rb +28 -0
- data/lib/{jekyll-contentblocks → jekyll/contentblocks}/version.rb +2 -1
- data/lib/{jekyll-contentblocks → jekyll}/convertible.rb +0 -0
- data/lib/{jekyll-contentblocks → jekyll/tags}/content_block.rb +1 -6
- data/lib/{jekyll-contentblocks → jekyll/tags}/content_for.rb +1 -6
- data/lib/jekyll/tags/if_has_content.rb +14 -0
- data/lib/jekyll/tags/if_not_has_content.rb +14 -0
- data/spec/jekyll_content_blocks_spec.rb +40 -0
- data/spec/spec_helper.rb +17 -0
- data/test/_layouts/default.html +23 -2
- metadata +14 -8
- data/lib/jekyll-contentblocks/common.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddeace684edb5376fdab5c280c88ced16731264b
|
4
|
+
data.tar.gz: d770dffb2fb6b82e75888a2d502e02177a713864
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06022becaeac04a221c70de8c345612cabbd96c61832faefb965085514a56265bfe916aa2476c54c7e8613de3a97db53d4032bc71b5a14e8e8441ab64bb07d1b
|
7
|
+
data.tar.gz: 0e8281ad795ef26b00cd522c73603a7b5adb37a2f6d1a0aaa54b5c4308ecf17975db901f2ed8b8c54e450c702d47016656ed6f78791ee00759314a978cf9e9af
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -42,17 +42,17 @@ require "jekyll-contentblocks"
|
|
42
42
|
In your layout files, define `contentblock` blocks that say where content will end up. For example, say the file `_layouts/default.html` looks like this:
|
43
43
|
```html
|
44
44
|
<html>
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
45
|
+
<head>
|
46
|
+
{% contentblock scripts %}
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<div class="main">
|
50
|
+
{{ content }}
|
51
|
+
</div>
|
52
|
+
<div class="sidebar">
|
53
|
+
{% contentblock sidebar %}
|
54
|
+
</div>
|
55
|
+
</body>
|
56
56
|
</html>
|
57
57
|
```
|
58
58
|
|
@@ -74,21 +74,27 @@ Here is my post content.
|
|
74
74
|
|
75
75
|
Note that we didn't add anything to the `scripts` block in the post. That's OK, content blocks without any content will be ignored.
|
76
76
|
|
77
|
-
### Checking if
|
77
|
+
### Checking if a block has content
|
78
78
|
|
79
|
-
We might want to check if the particular contentblock
|
80
|
-
|
81
|
-
* [Capture](http://docs.shopify.com/themes/liquid-basics/logic) contents of the `sidebar` contentblock to a variable `result`
|
82
|
-
* If `result` is not empty, output its contents surrounded with desired markup
|
79
|
+
We might want to check if the particular contentblock has content before using it in our template.
|
80
|
+
To do this, use the `ifhascontent` tag:
|
83
81
|
|
84
82
|
```liquid
|
85
|
-
{%
|
83
|
+
{% ifhascontent javascripts %}
|
84
|
+
<script type="text/javascript>
|
85
|
+
{% contentfor javascripts %}
|
86
|
+
</script>
|
87
|
+
{% endifhascontent %}
|
88
|
+
```
|
89
|
+
|
90
|
+
Similarly, there's the opposite tag, `ifnothascontent`:
|
86
91
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
+
```liquid
|
93
|
+
{% ifnothascontent sidebar %}
|
94
|
+
<div>
|
95
|
+
This is our default sidebar.
|
96
|
+
</div>
|
97
|
+
{% endifnothascontent %}
|
92
98
|
```
|
93
99
|
|
94
100
|
## Contributing
|
@@ -98,3 +104,14 @@ We might want to check if the particular contentblock exists before using it in
|
|
98
104
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
99
105
|
4. Push to the branch (`git push origin my-new-feature`)
|
100
106
|
5. Create new Pull Request
|
107
|
+
|
108
|
+
### Running the tests
|
109
|
+
|
110
|
+
Try to make sure that your changes work with all of the latest point releases
|
111
|
+
of Jekyll. To do this, run the test suite:
|
112
|
+
|
113
|
+
```bash
|
114
|
+
> bundle
|
115
|
+
> bundle exec appraisal install
|
116
|
+
> bundle exec appraisal rpsec
|
117
|
+
```
|
data/Rakefile
CHANGED
@@ -1,19 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
4
5
|
|
5
|
-
|
6
|
-
task :test do
|
7
|
-
require 'jekyll'
|
8
|
-
system 'rm -rf test/_site'
|
9
|
-
version = `jekyll --version`.strip
|
10
|
-
system 'jekyll build -s test/ -d test/_site &> /dev/null'
|
11
|
-
unless system 'grep \'<h2 id=.sidebar.>SIDEBAR</h2>\' test/_site/index.html &> /dev/null'
|
12
|
-
fail "#{version} failed!"
|
13
|
-
end
|
14
|
-
unless system 'grep "<p>3</p>" test/_site/index.html &> /dev/null'
|
15
|
-
fail "#{version} failed!"
|
16
|
-
end
|
17
|
-
puts "#{version} pass!"
|
18
|
-
end
|
19
|
-
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'jekyll
|
4
|
+
require 'jekyll/contentblocks/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "jekyll-contentblocks"
|
data/lib/jekyll-contentblocks.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
|
5
|
-
require
|
1
|
+
require 'jekyll'
|
2
|
+
require 'jekyll/convertible'
|
3
|
+
require File.expand_path('../jekyll/convertible', __FILE__)
|
4
|
+
|
5
|
+
require 'jekyll/contentblocks/version'
|
6
|
+
require 'jekyll/content_block_tag'
|
7
|
+
require 'jekyll/tags/content_for'
|
8
|
+
require 'jekyll/tags/if_has_content'
|
9
|
+
require 'jekyll/tags/if_not_has_content'
|
10
|
+
require 'jekyll/tags/content_block'
|
6
11
|
|
7
12
|
Liquid::Template.register_tag('contentfor', Jekyll::Tags::ContentFor)
|
13
|
+
Liquid::Template.register_tag('ifhascontent', Jekyll::Tags::IfHasContent)
|
14
|
+
Liquid::Template.register_tag('ifnothascontent', Jekyll::Tags::IfNotHasContent)
|
8
15
|
Liquid::Template.register_tag('contentblock', Jekyll::Tags::ContentBlock)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ContentBlockTag
|
3
|
+
def initialize(tag_name, block_name, tokens)
|
4
|
+
super
|
5
|
+
@block_name = get_content_block_name(tag_name, block_name)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def get_content_block_name(tag_name, block_name)
|
11
|
+
block_name = (block_name || '').strip
|
12
|
+
if block_name == ''
|
13
|
+
raise SyntaxError.new("No block name given in #{tag_name} tag")
|
14
|
+
end
|
15
|
+
block_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def block_has_content?(context)
|
19
|
+
block_content = content_for_block(context).join
|
20
|
+
!(block_content.nil? || block_content.empty?)
|
21
|
+
end
|
22
|
+
|
23
|
+
def content_for_block(context)
|
24
|
+
context.environments.first['contentblocks'] ||= {}
|
25
|
+
context.environments.first['contentblocks'][@block_name] ||= []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
File without changes
|
@@ -1,12 +1,7 @@
|
|
1
1
|
module Jekyll
|
2
2
|
module Tags
|
3
3
|
class ContentBlock < Liquid::Tag
|
4
|
-
include ::Jekyll::
|
5
|
-
|
6
|
-
def initialize(tag_name, block_name, tokens)
|
7
|
-
super
|
8
|
-
@block_name = get_content_block_name(tag_name, block_name)
|
9
|
-
end
|
4
|
+
include ::Jekyll::ContentBlockTag
|
10
5
|
|
11
6
|
def render(context)
|
12
7
|
block_content = content_for_block(context).join
|
@@ -1,14 +1,9 @@
|
|
1
1
|
module Jekyll
|
2
2
|
module Tags
|
3
3
|
class ContentFor < Liquid::Block
|
4
|
-
include ::Jekyll::
|
4
|
+
include ::Jekyll::ContentBlockTag
|
5
5
|
alias_method :render_block, :render
|
6
6
|
|
7
|
-
def initialize(tag_name, block_name, tokens)
|
8
|
-
super
|
9
|
-
@block_name = get_content_block_name(tag_name, block_name)
|
10
|
-
end
|
11
|
-
|
12
7
|
def render(context)
|
13
8
|
content_for_block(context) << render_block(context)
|
14
9
|
''
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module Tags
|
3
|
+
class IfHasContent < Liquid::Block
|
4
|
+
include ::Jekyll::ContentBlockTag
|
5
|
+
alias_method :render_block, :render
|
6
|
+
|
7
|
+
def render(context)
|
8
|
+
if block_has_content?(context)
|
9
|
+
render_block(context)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module Tags
|
3
|
+
class IfNotHasContent < Liquid::Block
|
4
|
+
include ::Jekyll::ContentBlockTag
|
5
|
+
alias_method :render_block, :render
|
6
|
+
|
7
|
+
def render(context)
|
8
|
+
unless block_has_content?(context)
|
9
|
+
render_block(context)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jekyll::ContentBlocks do
|
4
|
+
jekyll_version = `jekyll --version`.strip
|
5
|
+
puts jekyll_version
|
6
|
+
|
7
|
+
context "against #{jekyll_version}" do
|
8
|
+
before(:all) do
|
9
|
+
expect(generate_test_site).to be true
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:page) { load_index_html.html }
|
13
|
+
let(:sidebar) { page.css('body > div > h2#sidebar').first }
|
14
|
+
|
15
|
+
it 'renders the sidebar content block' do
|
16
|
+
expect(sidebar).not_to be_nil
|
17
|
+
expect(sidebar.text).to eq 'SIDEBAR'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'renders Liquid within a content block' do
|
21
|
+
rendered_liquid = sidebar.css('+ p')
|
22
|
+
expect(rendered_liquid).not_to be_nil
|
23
|
+
expect(rendered_liquid.text).to eq '3'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'does not render a block without content' do
|
27
|
+
expect(page.css('head > style')).to be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'ifnothascontent' do
|
31
|
+
it 'renders defaults when content is not supplied' do
|
32
|
+
expect(page.css('div#footer')).not_to be_empty
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'does not render when there is content' do
|
36
|
+
expect(page.css('div[class=sidebar-default]')).to be_empty
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module SpecHelpers
|
4
|
+
def generate_test_site
|
5
|
+
system 'rm -rf test/_site'
|
6
|
+
system 'jekyll build -s test/ -d test/_site &> /dev/null'
|
7
|
+
end
|
8
|
+
|
9
|
+
def load_index_html
|
10
|
+
index_html = File.read('test/_site/index.html')
|
11
|
+
Nokogiri::Slop(index_html)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.include(SpecHelpers)
|
17
|
+
end
|
data/test/_layouts/default.html
CHANGED
@@ -1,12 +1,33 @@
|
|
1
1
|
<!doctype html>
|
2
2
|
<html lang="en">
|
3
3
|
<head>
|
4
|
+
{% ifhascontent css %}
|
5
|
+
<style type="text/css">
|
6
|
+
{% contentblock css %}
|
7
|
+
</style>
|
8
|
+
{% endifhascontent %}
|
4
9
|
</head>
|
10
|
+
|
5
11
|
<body>
|
6
12
|
{{ content }}
|
7
|
-
|
8
|
-
|
13
|
+
|
14
|
+
{% ifhascontent sidebar %}
|
15
|
+
<div>
|
16
|
+
{% contentblock sidebar %}
|
17
|
+
</div>
|
18
|
+
{% endifhascontent %}
|
19
|
+
|
20
|
+
{% ifnothascontent sidebar %}
|
21
|
+
<div class="sidebar-default">
|
22
|
+
This is the default sidebar
|
23
|
+
</div>
|
24
|
+
{% endifnothascontent %}
|
25
|
+
|
26
|
+
{% ifnothascontent footer %}
|
27
|
+
<div id="footer">
|
28
|
+
This is the default footer.
|
9
29
|
</div>
|
30
|
+
{% endifnothascontent %}
|
10
31
|
</body>
|
11
32
|
</html>
|
12
33
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-contentblocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rusty Geldmacher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -52,11 +52,15 @@ files:
|
|
52
52
|
- gemfiles/jekyll_2.5.3.gemfile
|
53
53
|
- jekyll-contentblocks.gemspec
|
54
54
|
- lib/jekyll-contentblocks.rb
|
55
|
-
- lib/jekyll
|
56
|
-
- lib/jekyll
|
57
|
-
- lib/jekyll
|
58
|
-
- lib/jekyll
|
59
|
-
- lib/jekyll
|
55
|
+
- lib/jekyll/content_block_tag.rb
|
56
|
+
- lib/jekyll/contentblocks/version.rb
|
57
|
+
- lib/jekyll/convertible.rb
|
58
|
+
- lib/jekyll/tags/content_block.rb
|
59
|
+
- lib/jekyll/tags/content_for.rb
|
60
|
+
- lib/jekyll/tags/if_has_content.rb
|
61
|
+
- lib/jekyll/tags/if_not_has_content.rb
|
62
|
+
- spec/jekyll_content_blocks_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
60
64
|
- test/_layouts/default.html
|
61
65
|
- test/_plugins/bundler.rb
|
62
66
|
- test/index.md
|
@@ -79,11 +83,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
83
|
version: '0'
|
80
84
|
requirements: []
|
81
85
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.2.2
|
83
87
|
signing_key:
|
84
88
|
specification_version: 4
|
85
89
|
summary: A Jekyll plugin kind of like Rails' content_for
|
86
90
|
test_files:
|
91
|
+
- spec/jekyll_content_blocks_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
87
93
|
- test/_layouts/default.html
|
88
94
|
- test/_plugins/bundler.rb
|
89
95
|
- test/index.md
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Jekyll
|
2
|
-
module ContentBlocks
|
3
|
-
module Common
|
4
|
-
def get_content_block_name(tag_name, block_name)
|
5
|
-
block_name = (block_name || '').strip
|
6
|
-
if block_name == ''
|
7
|
-
raise SyntaxError.new("No block name given in #{tag_name} tag")
|
8
|
-
end
|
9
|
-
block_name
|
10
|
-
end
|
11
|
-
|
12
|
-
def content_for_block(context)
|
13
|
-
context.environments.first['contentblocks'] ||= {}
|
14
|
-
context.environments.first['contentblocks'][@block_name] ||= []
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|