Class: CloudCannonJekyll::Collections

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcannon-jekyll/generators/collections.rb

Overview

Helper functions for generating collection configuration and summaries

Instance Method Summary collapse

Constructor Details

#initialize(site, config) ⇒ Collections

Returns a new instance of Collections.



15
16
17
18
19
20
21
22
23
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 15

def initialize(site, config)
  @site = site
  @config = config
  @reader = Reader.new(site)
  @collections_dir = Paths.collections_dir(site)
  @data_dir = Paths.data_dir(site)
  @split_posts = group_by_category_folder(all_posts, 'posts')
  @split_drafts = group_by_category_folder(all_drafts, 'drafts')
end

Instance Method Details

#all_draftsObject



298
299
300
301
302
303
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 298

def all_drafts
  drafts_paths.reduce([]) do |drafts, drafts_path|
    base_path = drafts_path.gsub(%r{(^|/)_drafts}, '')
    drafts + @reader.read_drafts(base_path)
  end
end

#all_pagesObject



305
306
307
308
309
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 305

def all_pages
  pages = @site.pages.select { |doc| allowed_page?(doc) }
  static_pages = @site.static_files.select { |doc| allowed_static_file?(doc) }
  pages + static_pages
end

#all_postsObject



293
294
295
296
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 293

def all_posts
  posts = @site.posts || @site.collections['posts']
  posts.class.method_defined?(:docs) ? posts.docs : posts
end

#allowed_document?(doc) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
317
318
319
320
321
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 311

def allowed_document?(doc)
  if Jekyll.const_defined?(:PageWithoutAFile) && doc.instance_of?(Jekyll::PageWithoutAFile)
    false
  elsif doc.instance_of?(Jekyll::Page)
    allowed_page?(doc)
  elsif doc.instance_of?(Jekyll::StaticFile)
    allowed_static_file?(doc)
  else
    true
  end
end

#allowed_page?(page) ⇒ Boolean

Returns:

  • (Boolean)


323
324
325
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 323

def allowed_page?(page)
  page.html? || page.url.end_with?('/')
end

#allowed_static_file?(static_file) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 327

def allowed_static_file?(static_file)
  STATIC_EXTENSIONS.include?(static_file.extname)
end

#collections_config_path_map(collections_config) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 215

def collections_config_path_map(collections_config)
  unsorted = collections_config.map do |key, collection_config|
    {
      key: key,
      path: "/#{collection_config['path']}/".sub(%r{/+}, '/')
    }
  end

  unsorted.sort_by { |pair| pair[:path].length }.reverse
end

#data_files?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 331

def data_files?
  @reader.read_data(@data_dir)&.keys&.any?
end

#document_collection_key(doc, path_map) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 226

def document_collection_key(doc, path_map)
  path = "/#{File.join(@collections_dir, doc.relative_path)}/".sub(%r{/+}, '/')

  collection_path_pair = path_map.find do |pair|
    path.start_with? pair[:path]
  end

  collection_path_pair[:key] if collection_path_pair
end

#document_data(doc) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 252

def document_data(doc)
  data = if legacy_doc?(doc)
           legacy_document_data(doc)
         elsif doc.respond_to?(:data)
           doc.data.dup
         else
           {}
         end

  data.delete('excerpt')
  defaults = @site.frontmatter_defaults.all(doc.relative_path, document_type(doc))
  defaults.merge(data)
end

#document_path(doc) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 270

def document_path(doc)
  path = if doc.respond_to?(:collection) && doc.collection
           File.join(@collections_dir, doc.relative_path)
         else
           doc.relative_path
         end

  path.sub(%r{^/+}, '')
end

#document_to_json(doc, collection) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 280

def document_to_json(doc, collection)
  base = document_data(doc).merge(
    {
      'path' => document_path(doc),
      'url' => document_url(doc),
      'collection' => collection
    }
  )

  base['id'] = doc.id if doc.respond_to? :id
  base
end

#document_type(doc) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 203

def document_type(doc)
  if IS_JEKYLL_2_X_X && (doc.instance_of?(Jekyll::Post) || doc.instance_of?(Jekyll::Draft))
    :posts
  elsif doc.respond_to?(:type)
    doc.type
  elsif doc.respond_to?(:collection)
    doc.collection.label.to_sym
  elsif doc.instance_of?(Jekyll::Page)
    :pages
  end
end

#document_url(doc) ⇒ Object



266
267
268
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 266

def document_url(doc)
  doc.respond_to?(:url) ? doc.url : doc.relative_path
end

#drafts_pathsObject



116
117
118
119
120
121
122
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 116

def drafts_paths
  paths = @split_posts.keys.map do |key|
    File.join('/', @collections_dir, key.sub(/posts$/, '_drafts'))
  end

  paths.empty? ? [File.join('/', @collections_dir, '_drafts')] : paths
end

#each_document(&block) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 124

def each_document(&block)
  @site.pages.each(&block)
  @site.static_files.each(&block)
  @site.collections.each_value { |coll| coll.docs.each(&block) }
  all_drafts.each(&block)

  # Jekyll 2.x.x doesn't have posts in site.collections
  all_posts.each(&block) if IS_JEKYLL_2_X_X
end

#generate_collections(collections_config) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 134

def generate_collections(collections_config)
  assigned_pages = {}
  collections = {}

  path_map = collections_config_path_map(collections_config)

  each_document do |doc|
    next unless allowed_document?(doc)

    key = document_collection_key(doc, path_map)

    unless key
      Logger.warn "⚠️ No collection for #{doc.relative_path.bold}"
      next
    end

    if collections_config.dig(key, 'parser') == false
      Logger.warn "⚠️ Ignoring #{doc.relative_path.bold} in #{key.bold} collection"
      next
    end

    collections[key] ||= []

    begin
      jsonified = document_to_json(doc, key)
      JSON.generate(jsonified) # this is here to catch JSON errors per file
      collections[key].push(jsonified)
    rescue
      Logger.warn "⚠️ Failed to parse #{doc.relative_path.bold}"
    end


    assigned_pages[doc.relative_path] = true if doc.instance_of?(Jekyll::Page)
  end

  collections_config.each_key do |key|
    next if key == 'data'

    collections[key] ||= []
  end

  if collections.key?('pages') && collections['pages'].empty?
    all_pages.each do |page|
      assigned = assigned_pages[page.relative_path]
      collections['pages'].push(document_to_json(page, 'pages')) unless assigned
    end
  end

  collections
end

#generate_collections_configObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 35

def generate_collections_config
  collections = @site.config['collections'] || {}
  collections_config = @config['collections_config'] || {}

  return collections_config if @config['collections_config_override']

  if collections.is_a?(Array)
    collections = collections.each_with_object({}) { |key, memo| memo[key] = {} }
  end

  defaults = {
    'data' => {
      'path' => @data_dir,
      'output' => false
    },
    'posts' => {
      'output' => true
    },
    'drafts' => {
      'output' => !!@site.show_drafts
    }
  }

  unless collections.key?('pages') && !collections['pages'].empty?
    defaults['pages'] = {
      'path' => '',
      'output' => true,
      'filter' => 'strict'
    }
  end

  collection_keys = (collections_config.keys + defaults.keys + collections.keys).uniq

  collection_keys.each do |key|
    processed = (defaults[key] || {})
                .merge(collections[key] || {})
                .merge(collections_config[key] || {})

    processed['output'] ||= false
    processed['auto_discovered'] = !collections_config.key?(key)
    processed['path'] ||= generate_collections_config_path(key)
    processed['path'] = processed['path'].sub(%r{^/+}, '')

    Config.rename_legacy_collection_config_keys(processed)

    collections_config[key] = processed
  end

  @split_posts.each_key do |key|
    posts_path = @split_posts[key]&.first&.relative_path&.sub(%r{(^|/)_posts.*}, '\1_posts')
    next unless posts_path

    defaults = {
      'auto_discovered' => !collections_config.key?(key),
      'path' => File.join(@collections_dir, posts_path).sub(%r{^/+}, ''),
      'output' => true
    }

    collections_config[key] = defaults.merge(collections_config[key] || {})
  end

  @split_drafts.each_key do |key|
    drafts_path = @split_drafts[key]&.first&.relative_path&.sub(%r{(^|/)_drafts.*}, '\1_drafts')
    next unless drafts_path

    defaults = {
      'auto_discovered' => !collections_config.key?(key),
      'path' => File.join(@collections_dir, drafts_path).sub(%r{^/+}, ''),
      'output' => !!@site.show_drafts
    }

    collections_config[key] = defaults.merge(collections_config[key] || {})
  end

  # Remove auto-discovered collections with duplicate paths
  set_paths = collections_config.reject { |_, v| v['auto_discovered'] }.map { |_, v| v['path'] }
  collections_config.delete_if { |_, v| v['auto_discovered'] && set_paths.include?(v['path']) }

  collections_config
end

#generate_collections_config_path(key) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 25

def generate_collections_config_path(key)
  if key.end_with?("/posts")
    File.join(@collections_dir, key.sub(/\/posts$/, "/_posts"))
  elsif key.end_with?("/drafts")
    File.join(@collections_dir, key.sub(/\/drafts$/, "/_drafts"))
  else
    File.join(@collections_dir, "_#{key}")
  end
end

#group_by_category_folder(collection, key) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 335

def group_by_category_folder(collection, key)
  split_path = "/_#{key}/"
  collection.group_by do |doc|
    parts = doc.relative_path.split(split_path)
    if parts.length > 1
      File.join(parts.first, key).sub(%r{^/+}, '')
    else
      key
    end
  end
end

#legacy_doc?(doc) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 247

def legacy_doc?(doc)
  (IS_JEKYLL_3_04_X && doc.instance_of?(Jekyll::Document) && doc.collection.label == 'posts') ||
    (IS_JEKYLL_2_X_X && (doc.instance_of?(Jekyll::Draft) || doc.instance_of?(Jekyll::Post)))
end

#legacy_document_data(doc) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 236

def legacy_document_data(doc)
  legacy_data = {}
  legacy_data['categories'] = doc.categories if doc.respond_to?(:categories)
  legacy_data['tags'] = doc.tags if doc.respond_to?(:tags)
  legacy_data['date'] = doc.date if doc.respond_to?(:date)

  data = doc.data.merge(legacy_data)
  data['slug'] = doc.slug if doc.respond_to?(:slug)
  data
end

#remove_empty_collection_config(collections_config, collections) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 185

def remove_empty_collection_config(collections_config, collections)
  collections_config.each do |key, collection_config|
    should_delete = if key == 'data'
                      !data_files?
                    else
                      collections[key]&.empty? && collection_config['auto_discovered']
                    end

    if should_delete
      Logger.info "📂 #{'Ignored'.yellow} #{key.bold} collection"
      collections_config.delete(key)
    else
      count = collections[key]&.length || 0
      Logger.info "📁 Processed #{key.bold} collection with #{count} files"
    end
  end
end