jekyll-embed 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3570efa12f8e10276e3b2140358f20a82820b75f
4
+ data.tar.gz: a1770e1332ca513221bc81696d806847eb9b34ea
5
+ SHA512:
6
+ metadata.gz: 6f9a4c99ebf9ddc029add33225dcc1449fa5ddeb8e3f9bf9c88ac4ccaa663cac54cb6bf86dde7ba7e6fe9ebe0bd378bc9ecc8b0e9736b0f1f58848a596cde36d
7
+ data.tar.gz: f7110bb883a8ec8f72843c33927a1bbbc621846badc255a1864ba161359bb89c023c1e589e5a5212a194d5cb5ba5ca1be098ccd86662bf25579600a31bed8386
@@ -0,0 +1,4 @@
1
+ /_site/
2
+ /.bundle/
3
+ /Gemfile.lock
4
+ /pkg/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Greg Scott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Jekyll::Embed
2
+
3
+ Use links to build Jekyll page data.
4
+
5
+ ## Installation
6
+
7
+ Follow [Jekyll's instructions for installing Jekyll plugins](http://jekyllrb.com/docs/plugins/#installing-a-plugin). The **Jekyll::Embed** plugin is available from the `jekyll-embed` gem.
8
+
9
+ ## Usage
10
+
11
+ **Jekyll::Embed** relies on two special fields in the Jekyll page front matter to do its thing - `_links` and `_embedded`. These concepts come from the [Hypermedia Application Language (HAL)](http://stateless.co/hal_specification.html) media type specification.
12
+
13
+ * `_links` is a container of links to other resources
14
+ * `_embedded` is a container of other resources
15
+
16
+ **Jekyll::Embed** finds resources using `_links` and puts them in `_embedded`.
17
+
18
+ > For the purposes of this plugin, a resource and Jekyll page (specifically its front matter, aka data) can be considered the same thing.
19
+
20
+ ### Steps
21
+
22
+ 1. Define link objects to other resources in a `_links` field as part of the Jekyll page's front matter (aka data). Every link object should have `title` and `href` fields, but this plugin only needs `href` to work.
23
+ 2. Use the embedded resources in the Jekyll page.
24
+
25
+ In the following example, Jill has links to her friends Bob and Jack inside of `_links` defined in the `people/jill.md` page.
26
+
27
+ ```yaml
28
+ # people/jill.md (front matter only)
29
+
30
+ title: Jill
31
+ age: 6
32
+ _links:
33
+ friends:
34
+ - title: Bob
35
+ href: /people/bob
36
+
37
+ - title: Jack
38
+ href: /people/jack
39
+ ```
40
+
41
+ Below is the data that will be available to the `people/jill.md` page during the Jekyll build process. Notice that Jill's friends Bob and Jack are now included in their entirety in `_embedded` and are available to be displayed on the Jill's page.
42
+
43
+ ```yaml
44
+ # people/jill.md (front matter only)
45
+
46
+ title: Jill
47
+ age: 6
48
+ _links:
49
+ friends:
50
+ - title: Bob
51
+ href: /people/bob
52
+
53
+ - title: Jack
54
+ href: /people/jack
55
+
56
+ _embedded:
57
+ friends:
58
+ - title: Bob
59
+ age: 5
60
+ _links:
61
+ friends:
62
+ - title: Jill
63
+ href: /people/jill
64
+
65
+ - title: Jack
66
+ age: 7
67
+ _links:
68
+ friends:
69
+ - title: Jill
70
+ href: /people/jill
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it (https://github.com/gregoryjscott/jekyll-embed/fork).
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
78
+ 4. Push to the branch (`git push origin my-new-feature`).
79
+ 5. Create a new Pull Request.
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Run tests'
4
+ task :test do
5
+ system 'ruby -Ilib test/jekyll/embed_test.rb'
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll/embed/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = 'jekyll-embed'
9
+ spec.version = Jekyll::Embed::VERSION
10
+ spec.authors = ['Greg Scott']
11
+ spec.email = ['i@gregoryjscott.com']
12
+ spec.summary = %q{Uses links to build Jekyll page data.}
13
+ spec.homepage = 'https://github.com/gregoryjscott/jekyll-embed'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'jekyll', '~> 2.5'
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ require 'jekyll'
2
+ require 'jekyll/embed/version'
3
+ require 'jekyll/embed/loader'
@@ -0,0 +1,74 @@
1
+ module Jekyll
2
+ module Embed
3
+ class Loader < Jekyll::Generator
4
+
5
+ def generate(site)
6
+ @site = site
7
+ Dir.chdir(@site.source) { embed }
8
+ end
9
+
10
+ def embed
11
+ capture_resources
12
+
13
+ @site.pages.each do |page|
14
+ embed_resources(page)
15
+ end
16
+ end
17
+
18
+ def embed_resources(page)
19
+ links = page.data['_links']
20
+ return if links.nil?
21
+
22
+ embedded = get_embedded(page)
23
+
24
+ links.keys.each do |key|
25
+ link_object = links[key]
26
+ if link_object.kind_of?(Array)
27
+ embed_many(embedded, key, link_object)
28
+ else
29
+ embed_single(embedded, key, link_object)
30
+ end
31
+ end
32
+ end
33
+
34
+ def embed_many(embedded, key, link_objects)
35
+ embedded[key] = [] if embedded[key].nil?
36
+
37
+ link_objects.each do |link_object|
38
+ resource = find_resource(link_object['href'])
39
+ embedded[key] << resource['data'] unless resource.nil?
40
+ end
41
+ end
42
+
43
+ def embed_single(embedded, key, link_object)
44
+ resource = find_resource(link_object['href'])
45
+ embedded[key] = resource['data'] unless resource.nil?
46
+ end
47
+
48
+ def get_embedded(page)
49
+ page.data['_embedded'] = { } if page.data['_embedded'].nil?
50
+ page.data['_embedded']
51
+ end
52
+
53
+ def capture_resources
54
+ @resources = []
55
+ @site.pages.each do |page|
56
+ @resources << {
57
+ 'href' => page.url,
58
+ 'data' => clone(page.data)
59
+ }
60
+ end
61
+ end
62
+
63
+ def find_resource(url)
64
+ @resources.detect { |resource| url == resource['href'] }
65
+ end
66
+
67
+ def clone(resource)
68
+ json = JSON.dump(resource)
69
+ JSON.parse(json)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Embed
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: Bob
3
+ age: 5
4
+
5
+ _links:
6
+ brother:
7
+ title: Jack
8
+ href: /people/jack/
9
+
10
+ friends:
11
+ - title: Jill
12
+ href: /people/jill/
13
+ ---
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: Jack
3
+ age: 7
4
+
5
+ _links:
6
+ brother:
7
+ title: Bob
8
+ href: /people/bob/
9
+
10
+ friends:
11
+ - title: Jill
12
+ href: /people/jill/
13
+ ---
@@ -0,0 +1,15 @@
1
+ ---
2
+ title: Jill
3
+ age: 6
4
+
5
+ _links:
6
+ friends:
7
+ - title: Bob
8
+ href: /people/bob/
9
+
10
+ - title: Jack
11
+ href: /people/jack/
12
+
13
+ - title: Fake
14
+ href: /people/fake/
15
+ ---
@@ -0,0 +1,86 @@
1
+ require 'jekyll/embed'
2
+ require 'minitest/autorun'
3
+
4
+ describe 'Jekyll::Embed' do
5
+
6
+ let(:config) do
7
+ Jekyll.configuration({
8
+ 'source' => 'test/fixtures',
9
+ 'permalink' => 'pretty',
10
+ 'quiet' => true
11
+ })
12
+ end
13
+
14
+ let(:site) { Jekyll::Site.new(config) }
15
+
16
+ before(:each) do
17
+ site.process
18
+ end
19
+
20
+ it 'embeds local linked resource' do
21
+ assert_has_brother('Bob', 'Jack')
22
+ assert_has_brother('Jack', 'Bob')
23
+ end
24
+
25
+ it 'embeds local linked resource array' do
26
+ assert_has_friends('Bob', ['Jill'])
27
+ assert_has_friends('Jill', ['Bob', 'Jack'])
28
+ assert_has_friends('Jack', ['Jill'])
29
+ end
30
+
31
+ it 'does not embed previously embedded resources' do
32
+ check_embedded_for_embedded('Bob')
33
+ check_embedded_for_embedded('Jill')
34
+ check_embedded_for_embedded('Jack')
35
+ end
36
+
37
+ def assert_has_brother(name, expected_brother)
38
+ embedded_brother = get_embedded(name)['brother']
39
+
40
+ failure = "#{name} has wrong brother. Expected #{expected_brother}."
41
+ assert brother?(embedded_brother, expected_brother), failure
42
+ end
43
+
44
+ def assert_has_friends(name, expected_friends)
45
+ friends = get_embedded(name)['friends']
46
+
47
+ failure = "#{name} should have #{expected_friends.count} friends."
48
+ assert_equal expected_friends.count, friends.count, failure
49
+
50
+ expected_friends.each do |expected|
51
+ failure = "#{name} has the wrong friends. Expected #{expected}."
52
+ assert friend?(friends, expected), failure
53
+ end
54
+ end
55
+
56
+ def check_embedded_for_embedded(name)
57
+ embedded = get_embedded(name)
58
+
59
+ brother = embedded['brother']
60
+ failure = 'Embedded resources should not contain other embedded resources.'
61
+ assert_nil brother['_embedded'], failure unless brother.nil?
62
+
63
+ embedded['friends'].each do |friend|
64
+ assert_nil friend['_embedded'], failure
65
+ end
66
+ end
67
+
68
+ def get_embedded(name)
69
+ path = File.join('people', "#{name.downcase}.md")
70
+ page = site.pages.detect { |page| page.path == path }
71
+ page.data['_embedded']
72
+ end
73
+
74
+ def brother?(brother, name)
75
+ brother['title'] == name
76
+ end
77
+
78
+ def friend?(friends, name)
79
+ !find_friend(friends, name).nil?
80
+ end
81
+
82
+ def find_friend(friends, name)
83
+ friends.detect { |friend| friend['title'] == name }
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-embed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greg Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jekyll
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ description:
56
+ email:
57
+ - i@gregoryjscott.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - jekyll-embed.gemspec
68
+ - lib/jekyll/embed.rb
69
+ - lib/jekyll/embed/loader.rb
70
+ - lib/jekyll/embed/version.rb
71
+ - test/fixtures/people/bob.md
72
+ - test/fixtures/people/jack.md
73
+ - test/fixtures/people/jill.md
74
+ - test/jekyll/embed_test.rb
75
+ homepage: https://github.com/gregoryjscott/jekyll-embed
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Uses links to build Jekyll page data.
99
+ test_files:
100
+ - test/fixtures/people/bob.md
101
+ - test/fixtures/people/jack.md
102
+ - test/fixtures/people/jill.md
103
+ - test/jekyll/embed_test.rb