jekyll-fridge 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fdfe9c140fa6bd4f3418b042de123efed99986a
4
- data.tar.gz: 0bcb35479db90058493b4a2025cf665e87c0b5c6
3
+ metadata.gz: e661154f081ee273810962e171ab0201c0ff8e08
4
+ data.tar.gz: e1852fd206f96a8340bbba235dc699e21e9221c3
5
5
  SHA512:
6
- metadata.gz: dbfc97e00a29be571b84eb6bd67382e15178e29039fca4b2ea50ab64d9d681b9388e12e3bfd94c7758451ce2643f3e362a076bfc00f5560a2960818708386c55
7
- data.tar.gz: f027e6a4b7f0be2de2c7f804e12ea66407cf3c945ae38bb35627f87470d741738178fa8df3115bf55a9edf5e51ca511b241de52e87c5d08c33025f87a1b67a92
6
+ metadata.gz: d1a1368960fc9036b3a55241bd0494e48ce688300a0b77b8b723c22addb0335962f93da735cabb26452ee68e6e92139a564d7b1d994f6cd4c1e3e447e3561c16
7
+ data.tar.gz: 1d6e6d66dda67d40c2d9462eea37e69a51f608ac48d6fce908c2fe17d766c7eb8d6cc19bfcf7f4ead04db9d849903181278c27fcef7dc59f5dfa63a74b16ad9d
@@ -5,7 +5,7 @@ require 'jekyll-fridge/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.add_development_dependency 'bundler', '~> 1.0'
7
7
  spec.add_dependency 'jekyll', '>= 3.0'
8
- spec.add_dependency 'fridge_api', '~> 0.2.3'
8
+ spec.add_dependency 'fridge_api', '~> 0.3'
9
9
  spec.authors = ["Mike Kruk"]
10
10
  spec.email = ['mike@ripeworks.com']
11
11
  spec.summary = %q{Jekyll plugin for building sites using Fridge content}
@@ -0,0 +1,45 @@
1
+ module Jekyll
2
+ module FridgeFilters
3
+ # Filter for fetching assets
4
+ # Writes static file to asset_dir and returns absolute file path
5
+ def fridge_asset(input)
6
+ return input unless input
7
+ if input.respond_to?('first')
8
+ input = input.first['name']
9
+ end
10
+ site = @context.registers[:site]
11
+ asset_dir = site.config['fridge'].config['asset_dir']
12
+ dest_path = File.join(site.dest, asset_dir, input)
13
+ path = File.join(asset_dir, input)
14
+
15
+ # Check if file already exists
16
+ if site.keep_files.index(path) != nil
17
+ return "/#{path}"
18
+ end
19
+
20
+ asset = site.config['fridge'].client.get("content/upload/#{input}")
21
+ return input unless asset
22
+
23
+ # play for keeps
24
+ # this is so jekyll won't clean up the file
25
+ site.keep_files << path
26
+
27
+ # write file to destination
28
+ FileUtils.mkdir_p(File.dirname(dest_path))
29
+ File.write(dest_path, asset)
30
+ "/#{path}"
31
+ end
32
+
33
+ def fridge_choices(input)
34
+ arr = input.is_a?(String) ? input.lines : input
35
+ arr.map do |line|
36
+ key, value = line.split ":"
37
+ value = key if value.nil? || !value
38
+ { "key" => key.strip, "value" => value.strip }
39
+ end
40
+ end
41
+ end
42
+
43
+ Liquid::Template.register_filter(Jekyll::FridgeFilters)
44
+ end
45
+
@@ -0,0 +1,56 @@
1
+ module Jekyll
2
+ class FridgePage < Page
3
+ def initialize(site, base, dir, content, config)
4
+ @site = site
5
+ @base = base
6
+ @dir = dir
7
+ @name = "index.html"
8
+
9
+ self.process(@name)
10
+ if content.attrs.has_key?(:template)
11
+ self.read_yaml_from_string(content.template)
12
+ else
13
+ self.read_yaml(File.join(base, '_layouts'), config['layout'])
14
+ end
15
+
16
+ self.data[config['type']] = Jekyll::Fridge::Model.new(content)
17
+ self.data['title'] = content.title
18
+ end
19
+
20
+ def read_yaml_from_string(str)
21
+ begin
22
+ self.content = str
23
+ if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
24
+ self.content = $POSTMATCH
25
+ self.data = SafeYAML.load($1)
26
+ end
27
+ rescue SyntaxError => e
28
+ Jekyll.logger.warn "YAML Exception reading custom layout: #{e.message}"
29
+ end
30
+
31
+ self.data ||= {}
32
+ end
33
+ end
34
+
35
+ class FridgePageGenerator < Generator
36
+ safe true
37
+ priority :lowest
38
+
39
+ def generate(site)
40
+ client = site.config['fridge']
41
+ site.config['fridge_collections'].each do |type, options|
42
+ options = {
43
+ 'type' => type,
44
+ 'query' => "content?type=#{type}",
45
+ 'path' => type,
46
+ 'layout' => "#{type}.html"
47
+ }.merge(options || {})
48
+
49
+ client.get(options['query']).each do |document|
50
+ slug = document.slug == 'index' ? '' : document.slug
51
+ site.pages << FridgePage.new(site, site.source, File.join(options['path'], slug), document, options)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Fridge
3
- VERSION = "0.2.1".freeze
3
+ VERSION = "1.0.0".freeze
4
4
  end
5
5
  end
data/lib/jekyll-fridge.rb CHANGED
@@ -1,42 +1,17 @@
1
1
  require 'fridge_api'
2
2
  require 'ostruct'
3
+ require 'jekyll-fridge/fridge_page'
4
+ require 'jekyll-fridge/fridge_filters'
3
5
 
4
6
  module Jekyll
5
7
 
6
- # Recursively convert hash keys to strings
7
- def self.stringify_keys_deep(h)
8
- case h
9
- when Hash
10
- Hash[
11
- h.map do |k, v|
12
- [ k.respond_to?(:to_s) ? k.to_s : k, self.stringify_keys_deep(v) ]
13
- end
14
- ]
15
- when Sawyer::Resource
16
- if self.is_fridge_object?(h)
17
- Model.new(FridgeApi::Model.new(h.to_h))
18
- else
19
- self.stringify_keys_deep(h.to_h)
20
- end
21
- when Enumerable
22
- h.map { |v| self.stringify_keys_deep(v) }
23
- else
24
- h
25
- end
26
- end
27
-
28
- # check if an object is fridge-like
29
- def self.is_fridge_object?(obj)
30
- obj.respond_to?("key?") && (obj.key?(:id) && obj.key?(:document_definition_id))
31
- end
32
-
33
- class Fridge < Generator
8
+ class FridgeGenerator < Generator
34
9
  safe true
35
10
  priority :high
36
11
 
37
12
  def generate(site)
38
13
  # Reset cache if client already exists
39
- if site.config['fridge'].kind_of?(Client)
14
+ if site.config['fridge'].kind_of?(Fridge::Client)
40
15
  site.config['fridge'].reset!()
41
16
  return
42
17
  end
@@ -50,173 +25,157 @@ module Jekyll
50
25
  api_config['asset_dir'] ||= 'assets'
51
26
 
52
27
  # set site.fridge as plugin entry
53
- site.config['fridge'] = Client.new api_config
28
+ site.config['fridge'] = Fridge::Client.new api_config
54
29
  end
55
30
  end
56
31
 
57
- class Client < Liquid::Drop
58
-
59
- attr_reader :client, :config
60
-
61
- def initialize(config)
62
- @client = FridgeApi.client({
63
- :client_id => config['client_id'],
64
- :client_secret => config['client_secret']
65
- })
66
- @config = config.delete_if { |k, v| k.to_s.match(/secret/) || v.to_s.match(/sk/) }
67
- reset!()
32
+ module Fridge
33
+ # Recursively convert hash keys to strings
34
+ def self.stringify_keys_deep(h)
35
+ case h
36
+ when Hash
37
+ Hash[
38
+ h.map do |k, v|
39
+ [ k.respond_to?(:to_s) ? k.to_s : k, self.stringify_keys_deep(v) ]
40
+ end
41
+ ]
42
+ when Sawyer::Resource
43
+ if self.is_fridge_object?(h)
44
+ Model.new(FridgeApi::Model.new(h.to_h))
45
+ else
46
+ self.stringify_keys_deep(h.to_h)
47
+ end
48
+ when Enumerable
49
+ h.map { |v| self.stringify_keys_deep(v) }
50
+ else
51
+ h
52
+ end
68
53
  end
69
54
 
70
- def get(path)
71
- return @cache[path] if @cache.has_key?(path)
72
- @cache[path] = @client.get(path)
55
+ # check if an object is fridge-like
56
+ def self.is_fridge_object?(obj)
57
+ obj.respond_to?("key?") && (obj.key?(:id) && obj.key?(:document_definition_id))
73
58
  end
74
59
 
75
- def reset!
76
- @cache = Hash.new
77
- end
60
+ class Client < Liquid::Drop
61
+ attr_reader :client, :config
78
62
 
79
- def before_method(method)
80
- # try content type
81
- type = get("types/#{method}")
82
- if type && type.kind_of?(FridgeApi::Model)
83
- return Jekyll.stringify_keys_deep(type.attrs.merge({
84
- 'content' => ContentDrop.new(self, "content", "type=#{type.slug}")
85
- }))
63
+ def initialize(config)
64
+ @client = FridgeApi.client({
65
+ :client_id => config['client_id'],
66
+ :client_secret => config['client_secret']
67
+ })
68
+ @config = config.delete_if { |k, v| k.to_s.match(/secret/) || v.to_s.match(/sk/) }
69
+ reset!()
86
70
  end
87
71
 
88
- # try user role
89
- role = get("roles/#{method}")
90
- if role && role.kind_of?(FridgeApi::Model)
91
- return Jekyll.stringify_keys_deep(role.attrs.merge({
92
- 'users' => ContentDrop.new(self, "users", "role=#{role.slug}")
93
- }))
72
+ def get(path)
73
+ return @cache[path] if @cache.has_key?(path)
74
+ @cache[path] = @client.get(path)
94
75
  end
95
76
 
96
- nil
97
- end
98
-
99
- def content
100
- ContentDrop.new(self, "content")
101
- end
102
-
103
- def collections
104
- ContentDrop.new(self, "collections")
105
- end
106
-
107
- def settings
108
- ContentDrop.new(self, "settings")
109
- end
77
+ def reset!
78
+ @cache = Hash.new
79
+ end
110
80
 
111
- def types
112
- ContentDrop.new(self, "types")
113
- end
81
+ def before_method(method)
82
+ # try content type
83
+ type = get("types/#{method}")
84
+ if type && type.kind_of?(FridgeApi::Model)
85
+ return Jekyll::Fridge.stringify_keys_deep(type.attrs.merge({
86
+ 'content' => ContentDrop.new(self, "content", "type=#{type.slug}")
87
+ }))
88
+ end
114
89
 
115
- def users
116
- ContentDrop.new(self, "users")
117
- end
90
+ # try user role
91
+ role = get("roles/#{method}")
92
+ if role && role.kind_of?(FridgeApi::Model)
93
+ return Jekyll::Fridge.stringify_keys_deep(role.attrs.merge({
94
+ 'users' => ContentDrop.new(self, "users", "role=#{role.slug}")
95
+ }))
96
+ end
118
97
 
119
- end
98
+ nil
99
+ end
120
100
 
121
- class ContentDrop < Liquid::Drop
122
- include Enumerable
101
+ def content
102
+ ContentDrop.new(self, "content")
103
+ end
123
104
 
124
- def initialize(client, base, query = "", data = nil)
125
- @client = client
126
- @base = base
127
- @query = query
128
- @data = data
129
- end
105
+ def collections
106
+ ContentDrop.new(self, "collections")
107
+ end
130
108
 
131
- def before_method(method)
132
- # check for single content item
133
- item = @client.get("#{@base}/#{method}?#{@query}")
134
- return Model.new(item) if item && item.kind_of?(FridgeApi::Model)
109
+ def settings
110
+ ContentDrop.new(self, "settings")
111
+ end
135
112
 
136
- # filter by content type
137
- if @base == "content" && @query.empty?
138
- types = @client.get("#{@base}?type=#{method}")
139
- return ContentDrop.new(@client, @base, "type=#{method}", types) if types
113
+ def types
114
+ ContentDrop.new(self, "types")
140
115
  end
141
116
 
142
- # filter by user role
143
- if @base == "users" && @query.empty?
144
- roles = @client.get("#{@base}?role=#{method}")
145
- return ContentDrop.new(@client, @base, "role=#{method}", roles) if roles
117
+ def users
118
+ ContentDrop.new(self, "users")
146
119
  end
147
120
 
148
- nil
149
121
  end
150
122
 
151
- def each &block
152
- @data ||= @client.get("#{@base}?#{@query}")
153
- @data.each do |v|
154
- m = Model.new v
155
- if block_given?
156
- block.call m
157
- else
158
- yield m
159
- end
160
- end
161
- end
123
+ class ContentDrop < Liquid::Drop
124
+ include Enumerable
162
125
 
163
- end
126
+ def initialize(client, base, query = "", data = nil)
127
+ @client = client
128
+ @base = base
129
+ @query = query
130
+ @data = data
131
+ end
164
132
 
165
- class Model
166
- def initialize(model)
167
- @model = model
168
- end
133
+ def before_method(method)
134
+ # check for single content item
135
+ item = @client.get("#{@base}/#{method}?#{@query}")
136
+ return Model.new(item) if item && item.kind_of?(FridgeApi::Model)
169
137
 
170
- def inspect
171
- @data
172
- end
138
+ # filter by content type
139
+ if @base == "content" && @query.empty?
140
+ types = @client.get("#{@base}?type=#{method}")
141
+ return ContentDrop.new(@client, @base, "type=#{method}", types) if types
142
+ end
173
143
 
174
- def to_liquid
175
- @data ||= Jekyll.stringify_keys_deep(@model.attrs)
176
- end
177
- end
144
+ # filter by user role
145
+ if @base == "users" && @query.empty?
146
+ roles = @client.get("#{@base}?role=#{method}")
147
+ return ContentDrop.new(@client, @base, "role=#{method}", roles) if roles
148
+ end
178
149
 
179
- module FridgeFilters
180
- # Filter for fetching assets
181
- # Writes static file to asset_dir and returns absolute file path
182
- def fridge_asset(input)
183
- return input unless input
184
- if input.respond_to?('first')
185
- input = input.first['name']
150
+ nil
186
151
  end
187
- site = @context.registers[:site]
188
- asset_dir = site.config['fridge'].config['asset_dir']
189
- dest_path = File.join(site.dest, asset_dir, input)
190
- path = File.join(asset_dir, input)
191
152
 
192
- # Check if file already exists
193
- if site.keep_files.index(path) != nil
194
- return "/#{path}"
153
+ def each &block
154
+ @data ||= @client.get("#{@base}?#{@query}")
155
+ @data.each do |v|
156
+ m = Model.new v
157
+ if block_given?
158
+ block.call m
159
+ else
160
+ yield m
161
+ end
162
+ end
195
163
  end
164
+ end
196
165
 
197
- asset = site.config['fridge'].client.get("content/upload/#{input}")
198
- return input unless asset
199
-
200
- # play for keeps
201
- # this is so jekyll won't clean up the file
202
- site.keep_files << path
166
+ class Model
167
+ def initialize(model)
168
+ @model = model
169
+ end
203
170
 
204
- # write file to destination
205
- FileUtils.mkdir_p(File.dirname(dest_path))
206
- File.write(dest_path, asset)
207
- "/#{path}"
208
- end
171
+ def inspect
172
+ @data
173
+ end
209
174
 
210
- def fridge_choices(input)
211
- arr = input.is_a?(String) ? input.lines : input
212
- arr.map do |line|
213
- key, value = line.split ":"
214
- value = key if value.nil? || !value
215
- { "key" => key.strip, "value" => value.strip }
175
+ def to_liquid
176
+ @data ||= Jekyll::Fridge.stringify_keys_deep(@model.attrs)
216
177
  end
217
178
  end
218
179
  end
219
-
220
180
  end
221
181
 
222
- Liquid::Template.register_filter(Jekyll::FridgeFilters)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-fridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Kruk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-19 00:00:00.000000000 Z
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.2.3
47
+ version: '0.3'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.2.3
54
+ version: '0.3'
55
55
  description: Jekyll plugin for building sites using Fridge content
56
56
  email:
57
57
  - mike@ripeworks.com
@@ -64,6 +64,8 @@ files:
64
64
  - Rakefile
65
65
  - jekyll-fridge.gemspec
66
66
  - lib/jekyll-fridge.rb
67
+ - lib/jekyll-fridge/fridge_filters.rb
68
+ - lib/jekyll-fridge/fridge_page.rb
67
69
  - lib/jekyll-fridge/version.rb
68
70
  homepage: https://github.com/fridge-cms/jekyll-fridge
69
71
  licenses: