jekyll-strapi-custom 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbaa922e8fd1d6fdba4d3b76b5ccfde1b660c52a2fde0fff4b23a890625e7ea2
4
+ data.tar.gz: 427a33b3a10325c123d5f41b2b44888bdcda370a7eb83c829f410613165249b7
5
+ SHA512:
6
+ metadata.gz: d192214409a84ac8a1463fbf917c15ddcef35277a03787b00b722350d770cbbacca9aeb98f30f117683fdd7bde2cea022b21cbb51dea13ca6da9935472a818ac
7
+ data.tar.gz: fead3e0ff0cd0e962ec07c89d8711ca76a76894c2f8623d59f4662e8c95fbd2f722a203961d5bcaf5a4967b7d79fad9bef75bafb6d74c0d78e097dc83666450a
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2015-2019 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.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # jekyll-strapi
2
+
3
+ ## Install
4
+
5
+ This is a Custom Jekyll-strapi gem for Use in Jekyll version > 4.0.
6
+ Since the Official jekyll-strapi gem has not been developed for the past 2 years. I Forked it and Made some Changes to Work with Latest Jekyll Versions.
7
+
8
+ Add the following to your Gemfile:
9
+ ```
10
+ gem "jekyll-strapi", github:tks18/jekyll-strapi
11
+ ```
12
+ Then add "jekyll-strapi" to your plugins in `_config.yml`:
13
+
14
+ ```
15
+ plugins:
16
+ - jekyll-strapi
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ ```yaml
22
+ strapi:
23
+ # Your API endpoint (optional, default to http://localhost:1337)
24
+ endpoint: http://localhost:1337
25
+ # Collections, key is used to access in the strapi.collections
26
+ # template variable
27
+ collections:
28
+ # Example for a "articles" collection
29
+ articles:
30
+ # Collection name (optional)
31
+ type: article
32
+ # Permalink used to generate the output files (eg. /articles/:id).
33
+ permalink: /articles/:id/
34
+ # Layout file for this collection
35
+ layout: strapi_article.html
36
+ # Generate output files or not (default: false)
37
+ output: true
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ This plugin provides the `strapi` template variable. This template provides access to the collections defined in the configuration.
43
+
44
+ ### Using Collections
45
+
46
+ Collections are accessed by their name in `strapi.collections`. The `articles` collections is available at `strapi.collections.articles`.
47
+
48
+ To list all documents of the collection:
49
+
50
+ ```
51
+ {% for post in strapi.collections.articles %}
52
+ <article>
53
+ <header>
54
+ {{ post.title }}
55
+ </header>
56
+ <div class="body">
57
+ {{ post.content }}
58
+ </div>
59
+ </article>
60
+ {% endfor %}
61
+ ```
@@ -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,48 @@
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
+ path = "/#{@config['type'] || @collection_name}?_limit=10000"
23
+ uri = URI("#{@site.endpoint}#{path}")
24
+ Jekyll.logger.info "Jekyll Strapi:", "Fetching entries from #{uri}"
25
+ # Get entries
26
+ response = Net::HTTP.get_response(uri)
27
+ # Check response code
28
+ if response.code == "200"
29
+ result = JSON.parse(response.body, object_class: OpenStruct)
30
+ elsif response.code == "401"
31
+ 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."
32
+ else
33
+ raise "The Strapi server sent a error with the following status: #{response.code}. Please make sure it is correctly running."
34
+ end
35
+
36
+ # Add necessary properties
37
+ result.each do |document|
38
+ document.type = collection_name
39
+ document.collection = collection_name
40
+ document.id ||= document._id
41
+ document.url = @site.strapi_link_resolver(collection_name, document)
42
+ end
43
+
44
+ result.each {|x| yield(x)}
45
+ end
46
+ end
47
+ end
48
+ 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.to_s,
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.5"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-strapi-custom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Sudharshan TK
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-23 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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 Custom Jekyll plugin for retrieving content from a Strapi API
56
+ for Latest Jekyll Versions\n"
57
+ email:
58
+ - tksudharshan@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.md
64
+ - README.md
65
+ - lib/jekyll-strapi-custom.rb
66
+ - lib/jekyll/strapi/collection.rb
67
+ - lib/jekyll/strapi/drops.rb
68
+ - lib/jekyll/strapi/generator.rb
69
+ - lib/jekyll/strapi/hooks.rb
70
+ - lib/jekyll/strapi/site.rb
71
+ - lib/jekyll/strapi/version.rb
72
+ homepage: https://github.com/tks18/jekyll-strapi
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Strapi.io integration for Jekyll v4.0
95
+ test_files: []