jekyll-prismic 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGZhMmE5MjVhNWZiYjU0YWI3MzIyMDJkMzg5MjQ4ZTRkNjQyZjBmYQ==
5
+ data.tar.gz: !binary |-
6
+ MGFiYmZiZWVhNmRkNWZjMTU1ZDY2NDQ2ZGJkZWYzNjFiODMyZWUxZg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Zjg1ZjFiMDhkN2RlNDE4ODZkM2NjZDE2ZDNkZWRkMjNkM2U5NTJlYTY2YTFm
10
+ NDY3MjI2NDk2ZDQxY2FhYmZkYTM0YWVjYTc0M2VmZGI3MDRkZGFkNTEzNWQy
11
+ ZjEwMDU1OTAzZmE0MTE2NjhhZmEwNWVmMDAxOWRiZTIzNTgwMDM=
12
+ data.tar.gz: !binary |-
13
+ NDUyYmFmOWU1M2Y3Yzc1MmI2NzIzNzBhYTBlOGUxOWIzNDk1ZjhlNzZjZmRj
14
+ MmE1N2JhZDcxZTAzOGNiNDVmZGEyNzRjYTMzNDNjMDRlOGE5OGJkNDNlYjc3
15
+ MDQ0M2I4ZTE3MjVmYjg4YWQ0Nzc5MmUzYmQyNWM4MWJiNTAzNjE=
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2015 Christoph Hochstrasser <me@christophh.net>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the “Software”), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # jekyll-prismic
2
+
3
+ ## Install
4
+
5
+ Add the "jekyll-prismic" gem to your Gemfile:
6
+
7
+ ```
8
+ gem "jekyll-prismic"
9
+ ```
10
+
11
+ Then add "jekyll-prismic" to your gems in `_config.yml`:
12
+
13
+ ```
14
+ gems:
15
+ - jekyll-prismic
16
+ ```
17
+
18
+ ## Configuration
19
+
20
+ ```
21
+ prismic:
22
+ # Your repository endpoint
23
+ endpoint: https://lesbonneschoses.prismic.io/api
24
+ access_token: 'Your access token if the repo is private'
25
+ # Link resolving, key should match a document type in your repo
26
+ links:
27
+ post:
28
+ permalink: /posts/:slug-:id/
29
+ # Collections, key is used to access in the site.prismic.collections
30
+ # template variable
31
+ collections:
32
+ # Example for a "posts" collection
33
+ posts:
34
+ # Query the documents by type (optional)
35
+ type: post
36
+ # Collection name (optional)
37
+ form: posts
38
+ # Layout file for this collection
39
+ layout: prismic_post.html
40
+ # Additional queries
41
+ query:
42
+ - ["missing", "my.post.allow_comments"]
43
+ output: true # Generate output files or not (default: false)
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ This plugin provides the `site.prismic` template variable. This template provides access to tags, bookmarks, and the collections defined in the configuration.
49
+
50
+ ### Using Collections
51
+
52
+ Collections are accessed by their name in `site.prismic.collections`. The `posts` collections is available at `site.prismic.collections.posts`.
53
+
54
+ To list all documents of the collection:
55
+
56
+ ```
57
+ {% for post in site.prismic.collection.posts %}
58
+ <article>
59
+ <header>
60
+ {{ post.fragments.title.html }}
61
+ </header>
62
+ <div class="body"
63
+ {{ post.fragments.body.html }}
64
+ </div>
65
+ </article>
66
+ {% endfor %}
67
+ ```
@@ -0,0 +1,9 @@
1
+ require 'prismic'
2
+ require 'jekyll/prismic/version'
3
+ require 'jekyll/prismic/helper'
4
+ require 'jekyll/prismic/collection'
5
+ require 'jekyll/prismic/drops'
6
+ require 'jekyll/prismic/generator'
7
+ require 'jekyll/prismic/filters'
8
+ require 'jekyll/prismic/hooks'
9
+ require 'jekyll/prismic/site'
@@ -0,0 +1,53 @@
1
+ module Jekyll
2
+ module Prismic
3
+ class PrismicCollection
4
+ attr_accessor :collection_name, :config
5
+
6
+ def initialize(site, collection_name, config)
7
+ @cache = []
8
+ @site = site
9
+ @collection_name = collection_name
10
+ @config = config
11
+ @config['output_dir'] = collection_name unless config.key? 'output_dir'
12
+ end
13
+
14
+ def generate?
15
+ @config['output'] || false
16
+ end
17
+
18
+ def each
19
+ if @cache.length > 0
20
+ @cache.each { |v| yield v }
21
+ return
22
+ end
23
+
24
+ queries = []
25
+ queries << ::Prismic::Predicates::at('document.type', @config['type']) if @config['type']
26
+
27
+ form = PrismicHelper.api.form(@config['form'] || "everything").ref(PrismicHelper.ref)
28
+
29
+ if @config['query'] != nil and @config['query'].length > 0
30
+ @config['query'].each do |query|
31
+ queries << query
32
+ end
33
+ end
34
+
35
+ form.query(*queries)
36
+
37
+ begin
38
+ response = form.submit()
39
+
40
+ begin
41
+ response.results.each do |result|
42
+ @cache << result
43
+ yield result
44
+ end
45
+ response = form.page(response.next_page).submit() if response.next_page != nil
46
+ end while response.next_page != nil
47
+ rescue ::Prismic::SearchForm::FormSearchException => error
48
+ Jekyll.logger.warn "prismic:collection:#{@collection_name}", "Not found"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,228 @@
1
+ module Jekyll
2
+ module Prismic
3
+ # Prismic Document access in Liquid
4
+ class PrismicDocumentDrop < Liquid::Drop
5
+ attr_accessor :document
6
+
7
+ def initialize(document, link_resolver)
8
+ @document = document
9
+ @link_resolver = link_resolver
10
+ end
11
+
12
+ def id
13
+ @document.id
14
+ end
15
+
16
+ def type
17
+ @document.type
18
+ end
19
+
20
+ def uid
21
+ @document.uid
22
+ end
23
+
24
+ def slug
25
+ @document.slug
26
+ end
27
+
28
+ def slugs
29
+ @document.slugs
30
+ end
31
+
32
+ def tags
33
+ @document.tags
34
+ end
35
+
36
+ def href
37
+ @document.href
38
+ end
39
+
40
+ def fragments
41
+ PrismicFragmentsDrop.new(@document.fragments, @link_resolver)
42
+ end
43
+ end
44
+
45
+ class PrismicFragmentsDrop < Liquid::Drop
46
+ def initialize(fragments, link_resolver)
47
+ @fragments = fragments
48
+ @link_resolver = link_resolver
49
+ end
50
+
51
+ def [](fragment_id)
52
+ fragment = @fragments[fragment_id]
53
+
54
+ case fragment
55
+ when ::Prismic::Fragments::DocumentLink then
56
+ PrismicDocumentLinkFragmentDrop.new(fragment, @link_resolver)
57
+ when ::Prismic::Fragments::Link then
58
+ PrismicLinkFragmentDrop.new(fragment, @link_resolver)
59
+ when ::Prismic::Fragments::Group then
60
+ PrismicGroupFragmentDrop.new(fragment, @link_resolver)
61
+ when ::Prismic::Fragments::GroupDocument then
62
+ PrismicGroupDocumentFragmentDrop.new(fragment, @link_resolver)
63
+ when ::Prismic::Fragments::Multiple then
64
+ PrismicMultipleFragmentDrop.new(fragment, @link_resolver)
65
+ when ::Prismic::Fragments::Color then
66
+ PrismicColorFragmentDrop.new(fragment, @link_resolver)
67
+ when ::Prismic::Fragments::StructuredText then
68
+ PrismicStructuredTextFragmentDrop.new(fragment, @link_resolver)
69
+ else
70
+ PrismicFragmentDrop.new(fragment, @link_resolver)
71
+ end
72
+ end
73
+ end
74
+
75
+ class PrismicFragmentDrop < Liquid::Drop
76
+ def initialize(fragment, link_resolver)
77
+ @fragment = fragment
78
+ @link_resolver = link_resolver
79
+ end
80
+
81
+ def [](attribute)
82
+ case attribute
83
+ when "html" then
84
+ @fragment.as_html(@link_resolver)
85
+ when "text" then
86
+ @fragment.as_text
87
+ else
88
+ @fragment.send(attribute.to_sym)
89
+ end
90
+ end
91
+ end
92
+
93
+ class PrismicGroupFragmentDrop < PrismicFragmentDrop
94
+ def [](attribute)
95
+ case attribute
96
+ when "group_documents" then
97
+ docs = []
98
+ @fragment.each do |group_document|
99
+ docs << PrismicGroupDocumentFragmentDrop.new(group_document, @link_resolver)
100
+ end
101
+ docs
102
+ else
103
+ super
104
+ end
105
+ end
106
+ end
107
+
108
+ class PrismicGroupDocumentFragmentDrop < PrismicFragmentDrop
109
+ def to_liquid
110
+ {
111
+ "fragments" => PrismicFragmentsDrop.new(@fragment.fragments, @link_resolver)
112
+ }
113
+ end
114
+ end
115
+
116
+ class PrismicMultipleFragmentDrop < PrismicFragmentDrop
117
+ def to_liquid
118
+ {
119
+ "fragments" => PrismicFragmentsDrop.new(@fragment.fragments, @link_resolver)
120
+ }
121
+ end
122
+ end
123
+
124
+ class PrismicLinkFragmentDrop < PrismicFragmentDrop
125
+ def [](attribute)
126
+ case attribute
127
+ when "start_html" then
128
+ @fragment.start_html(@link_resolver)
129
+ when "url" then
130
+ @fragment.url(@link_resolver)
131
+ else
132
+ super
133
+ end
134
+ end
135
+
136
+ def url
137
+ @fragment.url(@link_resolver)
138
+ end
139
+ end
140
+
141
+ class PrismicDocumentLinkFragmentDrop < PrismicLinkFragmentDrop
142
+ def [](attribute)
143
+ case attribute
144
+ when "fragments" then
145
+ PrismicFragmentsDrop.new(@fragment.fragments, @link_resolver)
146
+ else
147
+ super
148
+ end
149
+ end
150
+ end
151
+
152
+ class PrismicColorFragmentDrop < PrismicFragmentDrop
153
+ def [](attribute)
154
+ case attribute
155
+ when "rgb" then
156
+ @fragment.asRGB
157
+ else
158
+ super
159
+ end
160
+ end
161
+ end
162
+
163
+ class PrismicStructuredTextFragmentDrop < PrismicFragmentDrop
164
+ end
165
+
166
+ # Handles a single Prismic collection in Liquid
167
+ class PrismicCollectionDrop < Liquid::Drop
168
+ def initialize(collection, link_resolver)
169
+ @collection = collection
170
+ @link_resolver = link_resolver
171
+ end
172
+
173
+ def to_liquid
174
+ results = []
175
+ @collection.each { |result| results << PrismicDocumentDrop.new(result, @link_resolver) }
176
+ results
177
+ end
178
+ end
179
+
180
+ # Handles Prismic collections in Liquid, and creates the collection on demand
181
+ class PrismicCollectionsDrop < Liquid::Drop
182
+ def initialize(collections, link_resolver)
183
+ @collections = collections
184
+ @link_resolver = link_resolver
185
+ end
186
+
187
+ def [](collection_name)
188
+ PrismicCollectionDrop.new(@collections[collection_name], @link_resolver)
189
+ end
190
+ end
191
+
192
+ # Main Liquid Drop, handles lazy loading of collections, tags, and bookmarks
193
+ # and conversion to drops
194
+ class PrismicDrop < Liquid::Drop
195
+ def initialize(site)
196
+ @site = site
197
+ end
198
+
199
+ def collections
200
+ @collections ||= PrismicCollectionsDrop.new(@site.prismic_collections, @site.prismic_link_resolver)
201
+ end
202
+
203
+ def tags
204
+ @tags ||= @site.prismic.tags
205
+ end
206
+
207
+ def bookmarks
208
+ @bookmarks ||= PrismicBookmarksDrop.new(@site, @site.prismic.bookmarks)
209
+ end
210
+ end
211
+
212
+ # Handles Prismic bookmarks in Liquid, and fetches the documents on demand
213
+ class PrismicBookmarksDrop < Liquid::Drop
214
+ def initialize(site, bookmarks)
215
+ @cache = {}
216
+ @site = site
217
+ @bookmarks = bookmarks
218
+ end
219
+
220
+ def [](bookmark)
221
+ unless @cache.key? bookmark
222
+ @cache[bookmark] = PrismicDocumentDrop.new(PrismicHelper.find_document(@bookmarks[bookmark]), @site.prismic_link_resolver)
223
+ end
224
+ @cache[bookmark]
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,23 @@
1
+ module Jekyll
2
+ module Prismic
3
+ module PrismicFilter
4
+ def prismic_document(input)
5
+ document = PrismicHelper.find_document(input)
6
+ PrismicDocumentDrop.new(document, @context.registers[:site].prismic_link_resolver)
7
+ end
8
+
9
+ # Uses the link resolver to link to a document
10
+ def prismic_link_to(input)
11
+ site = @context.registers[:site]
12
+
13
+ if input.is_a? PrismicDocumentDrop
14
+ return site.prismic_link_resolver.link_to(input.document)
15
+ end
16
+
17
+ input.url if input.respond_to? :url
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Liquid::Template.register_filter(Jekyll::Prismic::PrismicFilter)
@@ -0,0 +1,53 @@
1
+ module Jekyll
2
+ module Prismic
3
+ # Generates pages for all collections with have the "generate" option set to True
4
+ class PrismicGenerator < Generator
5
+ safe true
6
+
7
+ def generate(site)
8
+ site.prismic_collections.each do |collection_name, collection|
9
+ if collection.generate?
10
+ collection.each do |document|
11
+ site.pages << PrismicPage.new(site, site.source, document, collection.config)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class PrismicPage < Page
19
+ def initialize(site, base, document, config)
20
+ @site = site
21
+ @base = base
22
+ @dir = config['output_dir']
23
+ # Default file name, can be overwritten by permalink frontmatter setting
24
+ @name = "#{document.slug}-#{document.id}.html"
25
+ @document = document
26
+
27
+ self.process(@name)
28
+ self.read_yaml(File.join(base, "_layouts"), config['layout'])
29
+ # Use the permalink collection setting if it is set
30
+ self.data['permalink'] = config['permalink'] if config.key? 'permalink'
31
+ self.data['document'] = document
32
+ end
33
+
34
+ def url_placeholders
35
+ Utils.deep_merge_hashes({
36
+ :slug => @document.slug,
37
+ :id => @document.id,
38
+ :uid => @document.uid,
39
+ :type => @document.type
40
+ }, super)
41
+ end
42
+
43
+ def render(layouts, site_payload)
44
+ payload = Utils.deep_merge_hashes({
45
+ "document" => PrismicDocumentDrop.new(@document, @site.prismic_link_resolver),
46
+ "page" => self.to_liquid
47
+ }, site_payload)
48
+
49
+ do_layout(payload, layouts)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ module Jekyll
2
+ module Prismic
3
+ class PrismicHelper
4
+ def self.config
5
+ @@config ||= Jekyll.configuration()['prismic']
6
+ end
7
+
8
+ def self.api
9
+ return if config == nil
10
+
11
+ @@api ||= ::Prismic.api(config['endpoint'], config['access_token'])
12
+ end
13
+
14
+ def self.ref
15
+ @@ref ||= api.refs[config['ref']] || api.master_ref
16
+ end
17
+
18
+ def self.find_document(id)
19
+ @@document_cache ||= {}
20
+
21
+ if @@document_cache.key? id
22
+ return @@document_cache[id]
23
+ end
24
+
25
+ begin
26
+ response = api.form('everything')
27
+ .query(::Prismic::Predicates::at('document.id', id))
28
+ .submit(ref)
29
+
30
+ @@document_cache[id] = response.results.first
31
+ rescue ::Prismic::SearchForm::FormSearchException
32
+ nil
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ module Jekyll
2
+ module Prismic
3
+ # Add Prismic Liquid variables to all templates
4
+ Jekyll::Hooks.register :site, :pre_render do |site, payload|
5
+ payload['site']['prismic'] = PrismicDrop.new(site)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ module Jekyll
2
+ # Add helper methods for dealing with Prismic to the Site class
3
+ class Site
4
+ def prismic
5
+ Jekyll::Prismic::PrismicHelper.api
6
+ end
7
+
8
+ def prismic_collections
9
+ if @config['prismic'] != nil and @config['prismic']['collections'] != nil
10
+ @prismic_collections ||= Hash[@config['prismic']['collections'].map { |name, config| [name, Prismic::PrismicCollection.new(self, name, config)] }]
11
+ else
12
+ []
13
+ end
14
+ end
15
+
16
+ def prismic_ref
17
+ Jekyll::Prismic::PrismicHelper.ref
18
+ end
19
+
20
+ def prismic_link_resolver
21
+ @prismic_link_resolver ||= ::Prismic.link_resolver prismic_ref do |link|
22
+ if @config['prismic'] != nil and @config['prismic']['links'] != nil
23
+ url = nil
24
+
25
+ @config['prismic']['links'].each do |type, link_config|
26
+ if (link.is_a? ::Prismic::Fragments::DocumentLink or link.is_a? ::Prismic::Document) and type == link.type
27
+ url = Jekyll::URL.new(:template => link_config['permalink'], :placeholders => {
28
+ :id => link.id,
29
+ :uid => link.uid,
30
+ :slug => link.slug,
31
+ :type => link.type
32
+ })
33
+ end
34
+ end
35
+
36
+ url.to_s
37
+ end
38
+ end
39
+ end
40
+
41
+ def prismic_collection(collection_name)
42
+ prismic_collections[collection_name]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Prismic
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-prismic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Christoph Hochstrasser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prismic.io
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ description: ! ' A Jekyll plugin for retrieving content from the Prismic.io API
42
+
43
+ '
44
+ email:
45
+ - me@christophh.net
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - LICENSE
51
+ - README.md
52
+ - lib/jekyll-prismic.rb
53
+ - lib/jekyll/prismic/collection.rb
54
+ - lib/jekyll/prismic/drops.rb
55
+ - lib/jekyll/prismic/filters.rb
56
+ - lib/jekyll/prismic/generator.rb
57
+ - lib/jekyll/prismic/helper.rb
58
+ - lib/jekyll/prismic/hooks.rb
59
+ - lib/jekyll/prismic/site.rb
60
+ - lib/jekyll/prismic/version.rb
61
+ homepage: http://github.com/CHH/jekyll-prismic/
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.5
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Prismic.io integration for Jekyll
85
+ test_files: []