jekyll-strapi 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 714d228234e6f85718179694c095944fad199cf3d9215c294f39450297b6f129
4
+ data.tar.gz: 490043482646332491d08dcecceae88fbe6eace1bb347ff2a9f3a60f2802e95e
5
+ SHA512:
6
+ metadata.gz: 16210fe380082a464f1384ab5b6492cb6bc7ae165a9b24d0139263e76a0f2b7bcd1495affb52c23f9899b42d1c2380d3912baa0cea42009f3eacca35f6fdef2d
7
+ data.tar.gz: 898dd3592c5897a434222ddb03f66f43b22ca0ab4bbce7ef07500ef5ebf5d762b05ccf3548632ced26a4a0b814b4223ec6b89eac57f309706a47520145e4bc50
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2015-2018 Strapi Solutions.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # jekyll-strapi
2
+
3
+ ## Install
4
+
5
+ Add the "jekyll-strapi" gem to your Gemfile:
6
+
7
+ ```
8
+ gem "jekyll-strapi"
9
+ ```
10
+
11
+ Then add "jekyll-strapi" to your plugins in `_config.yml`:
12
+
13
+ ```
14
+ plugins:
15
+ - jekyll-strapi
16
+ ```
17
+
18
+ ## Configuration
19
+
20
+ ```yaml
21
+ strapi:
22
+ # Your API endpoint (optional, default to http://localhost:1337)
23
+ endpoint: http://localhost:1337
24
+ # Collections, key is used to access in the strapi.collections
25
+ # template variable
26
+ collections:
27
+ # Example for a "articles" collection
28
+ articles:
29
+ # Collection name (optional)
30
+ type: article
31
+ # Permalink used to generate the output files (eg. /articles/:id).
32
+ permalink: /articles/:id/
33
+ # Layout file for this collection
34
+ layout: strapi_article.html
35
+ # Generate output files or not (default: false)
36
+ output: true
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ This plugin provides the `strapi` template variable. This template provides access to the collections defined in the configuration.
42
+
43
+ ### Using Collections
44
+
45
+ Collections are accessed by their name in `strapi.collections`. The `articles` collections is available at `strapi.collections.articles`.
46
+
47
+ To list all documents of the collection:
48
+
49
+ ```
50
+ {% for post in strapi.collections.articles %}
51
+ <article>
52
+ <header>
53
+ {{ post.title }}
54
+ </header>
55
+ <div class="body">
56
+ {{ post.content }}
57
+ </div>
58
+ </article>
59
+ {% endfor %}
60
+ ```
@@ -0,0 +1,6 @@
1
+ require 'jekyll/strapi/collection'
2
+ require 'jekyll/strapi/drops'
3
+ require 'jekyll/strapi/generator'
4
+ require 'jekyll/strapi/hooks'
5
+ require 'jekyll/strapi/site'
6
+ require 'jekyll/strapi/version'
@@ -0,0 +1,52 @@
1
+ require "net/http"
2
+ require "ostruct"
3
+ require "json"
4
+
5
+ module Jekyll
6
+ module Strapi
7
+ class StrapiCollection
8
+ attr_accessor :collection_name, :config
9
+
10
+ def initialize(site, collection_name, config)
11
+ @site = site
12
+ @collection_name = collection_name
13
+ @config = config
14
+ end
15
+
16
+ def generate?
17
+ @config['output'] || false
18
+ end
19
+
20
+ def each
21
+ # Initialize the HTTP query
22
+ endpoint = URI.parse(@site.endpoint)
23
+ uri = URI::HTTP.build({:host => endpoint.host,
24
+ :port => endpoint.port,
25
+ :path => "/#{@config['type'] || @collection_name}",
26
+ :query => "_limit=10000"})
27
+
28
+ # Get entries
29
+ response = Net::HTTP.get_response(uri)
30
+
31
+ # Check response code
32
+ if response.code == "200"
33
+ result = JSON.parse(response.body, object_class: OpenStruct)
34
+ elsif response.code == "401"
35
+ raise "The Strapi server sent a error with the following status: #{response.code}. Please make sure you authorized the API access in the Users & Permissions section of the Strapi admin panel."
36
+ else
37
+ raise "The Strapi server sent a error with the following status: #{response.code}. Please make sure it is correctly running."
38
+ end
39
+
40
+ # Add necessary properties
41
+ result.each do |document|
42
+ document.type = collection_name
43
+ document.collection = collection_name
44
+ document.id ||= document._id
45
+ document.url = @site.strapi_link_resolver(collection_name, document)
46
+ end
47
+
48
+ result.each {|x| yield(x)}
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,62 @@
1
+ module Jekyll
2
+ module Strapi
3
+ # Strapi Document access in Liquid
4
+ class StrapiDocumentDrop < Liquid::Drop
5
+ attr_accessor :document
6
+
7
+ def initialize(document)
8
+ @document = document
9
+ end
10
+
11
+ def [](attribute)
12
+ value = @document[attribute]
13
+
14
+ case value
15
+ when OpenStruct
16
+ StrapiDocumentDrop.new(value)
17
+ when Array
18
+ StrapiCollectionDrop.new(value)
19
+ else
20
+ value
21
+ end
22
+ end
23
+ end
24
+
25
+ # Handles a single Strapi collection in Liquid
26
+ class StrapiCollectionDrop < Liquid::Drop
27
+ def initialize(collection)
28
+ @collection = collection
29
+ end
30
+
31
+ def to_liquid
32
+ results = []
33
+ @collection.each do |result|
34
+ results << StrapiDocumentDrop.new(result)
35
+ end
36
+ results
37
+ end
38
+ end
39
+
40
+ # Handles Strapi collections in Liquid, and creates the collection on demand
41
+ class StrapiCollectionsDrop < Liquid::Drop
42
+ def initialize(collections)
43
+ @collections = collections
44
+ end
45
+
46
+ def [](collection_name)
47
+ StrapiCollectionDrop.new(@collections[collection_name])
48
+ end
49
+ end
50
+
51
+ # Main Liquid Drop, handles lazy loading of collections to drops
52
+ class StrapiDrop < Liquid::Drop
53
+ def initialize(site)
54
+ @site = site
55
+ end
56
+
57
+ def collections
58
+ @collections ||= StrapiCollectionsDrop.new(@site.strapi_collections)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,49 @@
1
+ module Jekyll
2
+ module Strapi
3
+ # Generates pages for all collections with have the "generate" option set to True
4
+ class StrapiGenerator < Generator
5
+ safe true
6
+
7
+ def generate(site)
8
+ site.strapi_collections.each do |collection_name, collection|
9
+ if collection.generate?
10
+ collection.each do |document|
11
+ site.pages << StrapiPage.new(site, site.source, document, collection)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class StrapiPage < Page
19
+ def initialize(site, base, document, collection)
20
+ @site = site
21
+ @base = base
22
+ @collection = collection
23
+ @document = document
24
+
25
+ @dir = @collection.config['output_dir'] || collection.collection_name
26
+ # Default file name, can be overwritten by permalink frontmatter setting
27
+ @name = "#{document.id}.html"
28
+
29
+ self.process(@name)
30
+ self.read_yaml(File.join(base, "_layouts"), @collection.config['layout'])
31
+
32
+ # Use the permalink collection setting if it is set
33
+ if @collection.config.key? 'permalink'
34
+ self.data['permalink'] = @collection.config['permalink']
35
+ end
36
+
37
+ self.data['document'] = StrapiDocumentDrop.new(@document)
38
+ end
39
+
40
+ def url_placeholders
41
+ requiredValues = @document.to_h.select {|k, v|
42
+ v.class == String and @collection.config['permalink'].include? k.to_s
43
+ }
44
+
45
+ Utils.deep_merge_hashes(requiredValues, super)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ module Jekyll
2
+ module Strapi
3
+ # Add Strapi Liquid variables to all templates
4
+ Jekyll::Hooks.register :site, :pre_render do |site, payload|
5
+ payload['strapi'] = StrapiDrop.new(site)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ module Jekyll
2
+ # Add helper methods for dealing with Strapi to the Site class
3
+ class Site
4
+ def strapi
5
+ return nil unless has_strapi?
6
+ end
7
+
8
+ def strapi_collections
9
+ return Array.new unless has_strapi_collections?
10
+ @strapi_collections ||= Hash[@config['strapi']['collections'].map {|name, config| [name, Strapi::StrapiCollection.new(self, name, config)]}]
11
+ end
12
+
13
+ def has_strapi?
14
+ @config['strapi'] != nil
15
+ end
16
+
17
+ def has_strapi_collections?
18
+ has_strapi? and @config['strapi']['collections'] != nil
19
+ end
20
+
21
+ def endpoint
22
+ has_strapi? and @config['strapi']['endpoint'] or "http://localhost:1337"
23
+ end
24
+
25
+ def strapi_link_resolver(collection = nil, document = nil)
26
+ return "/" unless collection != nil and @config['strapi']['collections'][collection]['permalink'] != nil
27
+
28
+ url = Jekyll::URL.new(
29
+ :template => @config['strapi']['collections'][collection]['permalink'],
30
+ :placeholders => {
31
+ :id => document.id,
32
+ :uid => document.uid,
33
+ :slug => document.slug,
34
+ :type => document.type
35
+ }
36
+ )
37
+
38
+ url.to_s
39
+ end
40
+
41
+ def strapi_collection(collection_name)
42
+ strapi_collections[collection_name]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Strapi
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-strapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Strapi Solutions
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ description: " A Jekyll plugin for retrieving content from a Strapi API\n"
56
+ email:
57
+ - hi@strapi.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/jekyll-strapi.rb
65
+ - lib/jekyll/strapi/collection.rb
66
+ - lib/jekyll/strapi/drops.rb
67
+ - lib/jekyll/strapi/generator.rb
68
+ - lib/jekyll/strapi/hooks.rb
69
+ - lib/jekyll/strapi/site.rb
70
+ - lib/jekyll/strapi/version.rb
71
+ homepage: https://github.com/strapi/jekyll-strapi
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.7.6
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Strapi.io integration for Jekyll
95
+ test_files: []