Skip to content

Commit d99cef4

Browse files
committed
initial commit
0 parents  commit d99cef4

File tree

16 files changed

+370
-0
lines changed

16 files changed

+370
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spec/examples.txt
2+
*.gem

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

.rubocop.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
inherit_gem:
2+
jekyll: .rubocop.yml
3+
4+
AllCops:
5+
Exclude:
6+
- vendor/**/*
7+
8+
Metrics/BlockLength:
9+
Exclude:
10+
- spec/**/*

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rvm:
2+
- 2.2
3+
before_install: gem install bundler
4+
language: ruby
5+
script: script/cibuild
6+
sudo: false
7+
cache: bundler

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
PATH
2+
remote: .
3+
specs:
4+
jekyll-include-cache (0.1.0)
5+
jekyll (~> 3.3)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
addressable (2.5.0)
11+
public_suffix (~> 2.0, >= 2.0.2)
12+
ast (2.3.0)
13+
colorator (1.1.0)
14+
diff-lcs (1.2.5)
15+
ffi (1.9.14)
16+
forwardable-extended (2.6.0)
17+
jekyll (3.3.1)
18+
addressable (~> 2.4)
19+
colorator (~> 1.0)
20+
jekyll-sass-converter (~> 1.0)
21+
jekyll-watch (~> 1.1)
22+
kramdown (~> 1.3)
23+
liquid (~> 3.0)
24+
mercenary (~> 0.3.3)
25+
pathutil (~> 0.9)
26+
rouge (~> 1.7)
27+
safe_yaml (~> 1.0)
28+
jekyll-sass-converter (1.5.0)
29+
sass (~> 3.4)
30+
jekyll-watch (1.5.0)
31+
listen (~> 3.0, < 3.1)
32+
kramdown (1.13.1)
33+
liquid (3.0.6)
34+
listen (3.0.8)
35+
rb-fsevent (~> 0.9, >= 0.9.4)
36+
rb-inotify (~> 0.9, >= 0.9.7)
37+
mercenary (0.3.6)
38+
parser (2.3.3.1)
39+
ast (~> 2.2)
40+
pathutil (0.14.0)
41+
forwardable-extended (~> 2.6)
42+
powerpack (0.1.1)
43+
public_suffix (2.0.4)
44+
rainbow (2.1.0)
45+
rb-fsevent (0.9.8)
46+
rb-inotify (0.9.7)
47+
ffi (>= 0.5.0)
48+
rouge (1.11.1)
49+
rspec (3.5.0)
50+
rspec-core (~> 3.5.0)
51+
rspec-expectations (~> 3.5.0)
52+
rspec-mocks (~> 3.5.0)
53+
rspec-core (3.5.4)
54+
rspec-support (~> 3.5.0)
55+
rspec-expectations (3.5.0)
56+
diff-lcs (>= 1.2.0, < 2.0)
57+
rspec-support (~> 3.5.0)
58+
rspec-mocks (3.5.0)
59+
diff-lcs (>= 1.2.0, < 2.0)
60+
rspec-support (~> 3.5.0)
61+
rspec-support (3.5.0)
62+
rubocop (0.46.0)
63+
parser (>= 2.3.1.1, < 3.0)
64+
powerpack (~> 0.1)
65+
rainbow (>= 1.99.1, < 3.0)
66+
ruby-progressbar (~> 1.7)
67+
unicode-display_width (~> 1.0, >= 1.0.1)
68+
ruby-progressbar (1.8.1)
69+
safe_yaml (1.0.4)
70+
sass (3.4.22)
71+
unicode-display_width (1.1.2)
72+
73+
PLATFORMS
74+
ruby
75+
76+
DEPENDENCIES
77+
jekyll-include-cache!
78+
rspec (~> 3.5)
79+
rubocop (~> 0.43)
80+
81+
BUNDLED WITH
82+
1.13.6

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Jekyll Include Cache
2+
3+
*A Jekyll plugin to cache the rendering of Liquid includes*
4+
5+
## What it does
6+
7+
If you have a computationally expensive include (such as a sidebar or navigation), Jekyll Include Cache renders the include once, and then reuses the output any time that includes is called with the same arguments, potentially speeding up your site's build significantly.
8+
9+
## Usage
10+
11+
1. Add the following to your site's Gemfile:
12+
13+
```ruby
14+
gem 'jekyll-include-cache'
15+
```
16+
17+
2. Add the following to your site's config file:
18+
19+
```yml
20+
gems:
21+
- jekyll-include_cache
22+
```
23+
24+
3. Replace `{% include foo.html %}` in your template with `{% include_cached foo.html %}`
25+
26+
## One potential gotcha
27+
28+
For Jekyll Include Cache to work, you cannot rely on the page context to pass variables to your include (e.g., `assign foo=bar` or `page.title`). Instead, you must explicitly pass all variables to the include as arguments, and reference them within the include as `include.foo` (instead of `page.foo` or just `foo`).
29+
30+
### Good
31+
32+
In your template:
33+
34+
```liquid
35+
{% include_cached shirt.html size=medium color=red %}
36+
```
37+
38+
In your include:
39+
40+
```liquid
41+
Buy our {{ include.color }} shirt in {{ include.size }}!
42+
```
43+
44+
### Bad
45+
46+
In your template:
47+
48+
```liquid
49+
{% assign color=blue %}
50+
{% include_cached shirt.html %}
51+
```
52+
53+
In your include:
54+
55+
```liquid
56+
Buy our {{ color }} shirt in {{ page.size }}!
57+
```

jekyll-include-cache.gemspec

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# encoding: utf-8
2+
3+
$:.unshift File.expand_path('../lib', __FILE__)
4+
require 'jekyll-include-cache/version'
5+
6+
Gem::Specification.new do |s|
7+
s.name = "jekyll-include-cache"
8+
s.version = JekyllIncludeCache::VERSION
9+
s.authors = ["Ben Balter"]
10+
s.email = ["ben.balter@github.com"]
11+
s.homepage = "https://github.com/benbalter/jekyll-include-cache"
12+
s.summary = "A Jekyll plugin to cache the rendering of Liquid includes"
13+
14+
s.files = `git ls-files app lib`.split("\n")
15+
s.platform = Gem::Platform::RUBY
16+
s.require_paths = ['lib']
17+
s.license = "MIT"
18+
19+
s.add_dependency "jekyll", "~> 3.3"
20+
s.add_development_dependency "rubocop", "~> 0.43"
21+
s.add_development_dependency "rspec", "~> 3.5"
22+
end

lib/jekyll-include-cache.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "jekyll"
2+
3+
module JekyllIncludeCache
4+
autoload :Tag, "jekyll-include-cache/tag"
5+
end
6+
7+
Liquid::Template.register_tag("include_cached", JekyllIncludeCache::Tag)

lib/jekyll-include-cache/tag.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "digest/md5"
2+
3+
module JekyllIncludeCache
4+
class Tag < Jekyll::Tags::IncludeTag
5+
def render(context)
6+
path = path(context)
7+
params = parse_params(context) if @params
8+
return unless path
9+
10+
key = key(path, params)
11+
cached = cache(context)[key]
12+
13+
if cached
14+
cached
15+
else
16+
cache(context)[key] = super
17+
end
18+
end
19+
20+
private
21+
22+
def path(context)
23+
site = context.registers[:site]
24+
file = render_variable(context) || @file
25+
locate_include_file(context, file, site.safe)
26+
end
27+
28+
def key(path, params)
29+
Digest::MD5.hexdigest(path.to_s + params.to_s)
30+
end
31+
32+
def cache(context)
33+
context.registers[:cached_includes] ||= {}
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)