jekyll-strapi-next 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: 87a9e3078465aec7b90b79c0fff7140cf932bfccc3ec1c557f1dd3523ca33c2e
4
+ data.tar.gz: 5a6f371954c5583081892a3d57b19fd5d36d18b1a3dde00aa2a2c4ba278c1184
5
+ SHA512:
6
+ metadata.gz: 4fd15cd359a51cc99cd9739520ecf8738d388a1c46a61fbb2515c0763fad0ea47ccee8ba91c7af85411975c2450991b62d784f0e79b7a6f3170c77763ec0b068
7
+ data.tar.gz: 2d09671f2d690cc34c51e11e816fd10a8f331bd5145e4cb28ab3cde3157fee9b8508b6f52b6e306c32e9876a495a153b9ef428655f1b237ca8d95c66c392c75d
@@ -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.
@@ -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
+ path = "/#{@config['type'] || @collection_name}"
23
+ uri = URI("#{@site.endpoint}#{path}")
24
+ if @config['query']
25
+ uri += @config['query']
26
+ p uri
27
+ end
28
+ Jekyll.logger.info "Jekyll Strapi:", "Fetching entries from #{uri}"
29
+ # Get entries
30
+ response = Net::HTTP.get_response(uri)
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.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.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-strapi-next
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: 2020-11-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: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
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 with query
56
+ parameter\n"
57
+ email:
58
+ - hi@strapi.io
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.md
64
+ - README.md
65
+ - lib/jekyll-strapi.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/strapi/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
95
+ test_files: []