cloudcannon-jekyll 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,232 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "jekyll"
4
-
5
- module CloudCannonJekyll
6
- # Filter for converting Jekyll objects into JSON
7
- module JsonifyFilter
8
- STATIC_EXTENSIONS = [".html", ".htm"].freeze
9
-
10
- CC_JSONIFY_KEY_SWAPS = {
11
- "collections" => {
12
- "_sort_key" => "_sort-key",
13
- "_subtext_key" => "_subtext-key",
14
- "_image_key" => "_image-key",
15
- "_image_size" => "_image-size",
16
- "_singular_name" => "_singular-name",
17
- "_singular_key" => "_singular-key",
18
- "_disable_add" => "_disable-add",
19
- "_add_options" => "_add-options",
20
- },
21
- }.freeze
22
-
23
- @simple_types = [
24
- String,
25
- Numeric,
26
- Integer,
27
- Float,
28
- Date,
29
- Time,
30
- NilClass,
31
- Object.const_defined?("Fixnum") ? Fixnum : nil,
32
- ].compact.freeze
33
-
34
- @document_types = [
35
- Jekyll::Document,
36
- Jekyll::Page,
37
- Jekyll::VERSION.start_with?("2.") ? Jekyll::Post : nil,
38
- ].compact.freeze
39
-
40
- def self.document_type?(input)
41
- @document_types.include?(input.class)
42
- end
43
-
44
- def self.simple_type?(input)
45
- @simple_types.include?(input.class) || [true, false].include?(input)
46
- end
47
-
48
- def self.static_file_to_json(input, depth, max_depth)
49
- path = input.relative_path.sub(%r!^\/+!, "")
50
- url = Jekyll::VERSION.start_with?("2.") ? "/#{path}" : input.url
51
-
52
- out = [
53
- "\"path\": #{JsonifyFilter.to_json(path, depth, max_depth)}",
54
- "\"url\": #{JsonifyFilter.to_json(url, depth, max_depth)}",
55
- ]
56
-
57
- "{#{out.join(",")}}"
58
- end
59
-
60
- def self.document_data_to_a(data, prevent, depth, max_depth)
61
- prevent += %w(content output next previous excerpt)
62
-
63
- out = data.map do |key, value|
64
- next if prevent.include? key
65
-
66
- prevent.push key
67
- next_max_depth = key == "_array_structures" ? 20 : max_depth
68
- "#{key.to_json}: #{JsonifyFilter.to_json(value, depth, next_max_depth)}"
69
- end
70
-
71
- out.compact
72
- end
73
-
74
- def self.legacy_post_to_json(input, depth, max_depth)
75
- prevent = %w(dir name path url date id categories tags)
76
-
77
- out = [
78
- "\"name\": #{JsonifyFilter.to_json(input.name, depth, max_depth)}",
79
- "\"path\": #{JsonifyFilter.to_json(input.path, depth, max_depth)}",
80
- "\"url\": #{JsonifyFilter.to_json(input.url, depth, max_depth)}",
81
- "\"date\": #{JsonifyFilter.to_json(input.date, depth, max_depth)}",
82
- "\"id\": #{JsonifyFilter.to_json(input.id, depth, max_depth)}",
83
- "\"categories\": #{JsonifyFilter.to_json(input.categories, depth, max_depth)}",
84
- "\"tags\": #{JsonifyFilter.to_json(input.tags, depth, max_depth)}",
85
- ]
86
-
87
- out += JsonifyFilter.document_data_to_a(input.data, prevent, depth, max_depth)
88
- "{#{out.join(",")}}"
89
- end
90
-
91
- def self.page_to_json(input, depth, max_depth)
92
- prevent = %w(dir name path url)
93
-
94
- out = [
95
- "\"name\": #{JsonifyFilter.to_json(input.name, depth, max_depth)}",
96
- "\"path\": #{JsonifyFilter.to_json(input.path, depth, max_depth)}",
97
- "\"url\": #{JsonifyFilter.to_json(input.url, depth, max_depth)}",
98
- ]
99
-
100
- # Merge Jekyll Defaults into data for pages (missing at v3.8.5)
101
- defaults = input.site.frontmatter_defaults.all(input.relative_path, :pages).tap do |default|
102
- default.delete("date")
103
- end
104
-
105
- data = Jekyll::Utils.deep_merge_hashes(defaults, input.data)
106
-
107
- out += JsonifyFilter.document_data_to_a(data, prevent, depth, max_depth)
108
- "{#{out.join(",")}}"
109
- end
110
-
111
- def self.document_path(input)
112
- collections_dir = input.site.config["collections_dir"] || ""
113
- if input.collection && !collections_dir.empty?
114
- "#{collections_dir}/#{input.relative_path}"
115
- else
116
- input.relative_path
117
- end
118
- end
119
-
120
- def self.document_to_json(input, depth, max_depth)
121
- prevent = %w(dir relative_path url collection)
122
-
123
- out = [
124
- "\"path\": #{JsonifyFilter.to_json(JsonifyFilter.document_path(input), depth, max_depth)}",
125
- "\"url\": #{JsonifyFilter.to_json(input.url, depth, max_depth)}",
126
- ]
127
-
128
- unless input.collection.nil?
129
- collection_json = JsonifyFilter.to_json(input.collection.label, depth, max_depth)
130
- out.push("\"collection\": #{collection_json}")
131
- end
132
-
133
- if input.respond_to? :id
134
- out.push("\"id\": #{JsonifyFilter.to_json(input.id, depth, max_depth)}")
135
- end
136
-
137
- out += JsonifyFilter.document_data_to_a(input.data, prevent, depth, max_depth)
138
- "{#{out.join(",")}}"
139
- end
140
-
141
- def self.array_to_json(input, depth, max_depth, key_swaps = {})
142
- array = input.map do |value|
143
- JsonifyFilter.to_json(value, depth, max_depth, key_swaps)
144
- end
145
-
146
- "[#{array.join(",")}]"
147
- end
148
-
149
- def self.hash_to_json(input, depth, max_depth, key_swaps = {})
150
- out = input.map do |key, value|
151
- next_max_depth = key == "_array_structures" ? 20 : max_depth
152
- string_key = (key_swaps[key] || key).to_s.to_json
153
- "#{string_key}: #{JsonifyFilter.to_json(value, depth, next_max_depth, key_swaps)}"
154
- end
155
-
156
- "{#{out.join(",")}}"
157
- end
158
-
159
- def self.config_to_select_data_json(input, depth)
160
- prevent = %w(source destination collections_dir cache_dir plugins_dir layouts_dir data_dir
161
- includes_dir collections safe include exclude keep_files encoding markdown_ext
162
- strict_front_matter show_drafts limit_posts future unpublished whitelist
163
- plugins markdown highlighter lsi excerpt_separator incremental detach port host
164
- baseurl show_dir_listing permalink paginate_path timezone quiet verbose defaults
165
- liquid kramdown title url description uploads_dir _comments _options _editor
166
- _explore _source_editor _array_structures maruku redcloth rdiscount redcarpet
167
- gems plugins cloudcannon _collection_groups)
168
-
169
- out = input.map do |key, value|
170
- next unless value.is_a?(Array) || value.is_a?(Hash)
171
- next if prevent.include? key
172
-
173
- prevent.push key
174
- "#{key.to_s.to_json}: #{JsonifyFilter.to_json(value, depth)}"
175
- end
176
-
177
- out.compact!
178
-
179
- "{#{out.join(",")}}" if out.any?
180
- end
181
-
182
- def self.to_json(input, depth, max_depth = 9, key_swaps = {})
183
- depth += 1
184
-
185
- if depth > max_depth || (depth > 3 && JsonifyFilter.document_type?(input))
186
- '"MAXIMUM_DEPTH"'
187
- elsif JsonifyFilter.simple_type?(input)
188
- input.to_json
189
- elsif input.is_a?(Jekyll::StaticFile)
190
- JsonifyFilter.static_file_to_json(input, depth, max_depth)
191
- elsif input.is_a?(Jekyll::Page)
192
- JsonifyFilter.page_to_json(input, depth, max_depth)
193
- elsif Jekyll::VERSION.start_with?("2.") && input.is_a?(Jekyll::Post)
194
- JsonifyFilter.legacy_post_to_json(input, depth, max_depth)
195
- elsif input.is_a?(Jekyll::Document)
196
- JsonifyFilter.document_to_json(input, depth, max_depth)
197
- elsif input.is_a?(Array)
198
- JsonifyFilter.array_to_json(input, depth, max_depth, key_swaps)
199
- elsif input.is_a?(Hash)
200
- JsonifyFilter.hash_to_json(input, depth, max_depth, key_swaps)
201
- else
202
- input.class.to_s.prepend("UNSUPPORTED:").to_json
203
- end
204
- end
205
-
206
- def cc_static_files_jsonify(input)
207
- out = input.map do |page|
208
- JsonifyFilter.to_json(page, 1) if STATIC_EXTENSIONS.include?(page.extname)
209
- end
210
-
211
- out.compact!
212
-
213
- "[#{out.join(",")}]"
214
- end
215
-
216
- def cc_select_data_jsonify(input)
217
- if input.key? "_select_data"
218
- JsonifyFilter.to_json(input["_select_data"], 0)
219
- else
220
- JsonifyFilter.config_to_select_data_json(input, 0)
221
- end
222
- end
223
-
224
- def cc_jsonify(input, key_swaps_key = nil, max_depth = 8)
225
- if CC_JSONIFY_KEY_SWAPS.key? key_swaps_key
226
- JsonifyFilter.to_json(input, 0, max_depth, CC_JSONIFY_KEY_SWAPS[key_swaps_key])
227
- else
228
- JsonifyFilter.to_json(input, 0, max_depth)
229
- end
230
- end
231
- end
232
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "jekyll"
4
- require_relative "readers/old-data-reader"
5
- require_relative "readers/data-reader"
6
-
7
- module CloudCannonJekyll
8
- # Wraps read functions into one class
9
- class Reader
10
- attr_reader :site
11
-
12
- def initialize(site)
13
- @site = site
14
- end
15
-
16
- def read_data(dir = "_data")
17
- # DataReader doesn't exist in old versions of Jekyll
18
- if Jekyll::VERSION.start_with? "2."
19
- CloudCannonJekyll::OldDataReader.new(@site).read(dir)
20
- else
21
- CloudCannonJekyll::DataReader.new(@site).read(dir)
22
- end
23
- end
24
-
25
- def read_drafts(dir = "")
26
- # PostReader doesn't exist in old versions of Jekyll
27
- if Jekyll::VERSION.start_with? "2."
28
- @site.read_content(dir, "_drafts", Jekyll::Draft)
29
- else
30
- Jekyll::PostReader.new(@site).read_drafts(dir)
31
- end
32
- end
33
-
34
- def read_posts(dir = "")
35
- # PostReader doesn't exist in old versions of Jekyll
36
- if Jekyll::VERSION.start_with? "2."
37
- @site.read_content(dir, "_posts", Jekyll::Post)
38
- else
39
- Jekyll::PostReader.new(@site).read_posts(dir)
40
- end
41
- end
42
- end
43
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- unless Jekyll::VERSION.start_with? "2."
4
- require "jekyll"
5
-
6
- module CloudCannonJekyll
7
- # Reads data files and creates a collections-style hash representation
8
- class DataReader < Jekyll::DataReader
9
- # Determines how to read a data file.
10
- # This is overridden return a hash instead of reading the file.
11
- #
12
- # Returns a hash with the path to the data file.
13
- def read_data_file(path)
14
- {
15
- "path" => path,
16
- }
17
- end
18
- end
19
- end
20
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if Jekyll::VERSION.start_with? "2."
4
- require "jekyll"
5
-
6
- module CloudCannonJekyll
7
- # Reads data files and creates a collections-style hash representation
8
- # Aims to replicate the data reading logic in Jekyll 2.5
9
- class OldDataReader
10
- attr_reader :site
11
-
12
- def initialize(site)
13
- @site = site
14
- @safe = site.safe
15
- @content = {}
16
- end
17
-
18
- def read(dir)
19
- base = Jekyll.sanitized_path(@site.source, dir)
20
- read_data_to(base, @content)
21
- @content
22
- end
23
-
24
- def read_data_to(dir, data)
25
- return unless File.directory?(dir) && (!@safe || !File.symlink?(dir))
26
-
27
- entries = Dir.chdir(dir) do
28
- Dir["*.{yaml,yml,json,csv}"] + Dir["*"].select { |fn| File.directory?(fn) }
29
- end
30
-
31
- entries.each do |entry|
32
- path = Jekyll.sanitized_path(dir, entry)
33
- next if File.symlink?(path) && @safe
34
-
35
- key = sanitize_filename(File.basename(entry, ".*"))
36
- if File.directory?(path)
37
- read_data_to(path, data[key] = {})
38
- else
39
- data[key] = read_data_file(path)
40
- end
41
- end
42
- end
43
-
44
- def read_data_file(path)
45
- {
46
- "path" => path,
47
- }
48
- end
49
-
50
- def sanitize_filename(name)
51
- name.gsub!(%r![^\w\s_-]+!, "")
52
- name.gsub!(%r!(^|\b\s)\s+($|\s?\b)!, '\\1\\2')
53
- name.gsub(%r!\s+!, "_")
54
- end
55
- end
56
- end
57
- end
data/script/ci-smoke-test DELETED
@@ -1,10 +0,0 @@
1
- #!/bin/sh
2
-
3
- set -ex
4
-
5
- # Checks to see the build is likely to fail on Travis.
6
- # Duplicated the versions from .travis.yml
7
-
8
- JEKYLL_VERSION=2.4.0 bundle update && $(dirname "$0")/test &&
9
- JEKYLL_VERSION=3.0.0 bundle update && $(dirname "$0")/test &&
10
- JEKYLL_VERSION=3.2.1 bundle update && $(dirname "$0")/test