Skip to content

Commit

Permalink
Assign files to collections based on paths rather than matching jekyl…
Browse files Browse the repository at this point in the history
…l collection
  • Loading branch information
rphillips-cc committed Dec 19, 2022
1 parent 299e5ed commit c95eedd
Show file tree
Hide file tree
Showing 47 changed files with 2,421 additions and 470 deletions.
108 changes: 77 additions & 31 deletions lib/cloudcannon-jekyll/generators/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def initialize(site, config)

def generate_collections_config
collections = @site.config['collections'] || {}
input_collections_config = @config['collections_config'] || {}
collections_config = input_collections_config.reject { |_, v| v == false }
collections_config = @config['collections_config'] || {}

return collections_config if @config['collections_config_override']

Expand Down Expand Up @@ -54,11 +53,9 @@ def generate_collections_config
}
end

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

collection_keys.each do |key|
next if input_collections_config[key] == false

processed = (defaults[key] || {})
.merge(collections[key] || {})
.merge(collections_config[key] || {})
Expand All @@ -74,8 +71,6 @@ def generate_collections_config
end

@split_posts.each_key do |key|
next if input_collections_config[key] == false

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

Expand All @@ -89,8 +84,6 @@ def generate_collections_config
end

@split_drafts.each_key do |key|
next if input_collections_config[key] == false

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

Expand All @@ -114,33 +107,53 @@ def drafts_paths
paths.empty? ? [File.join('/', @collections_dir, '_drafts')] : paths
end

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

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

collections_config.each_key do |key|
next if key == 'data'
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] ||= []
collections[key].push(document_to_json(doc, key))

next if collections_config.dig(key, 'parser') == false
assigned_pages[doc.relative_path] = true if doc.instance_of?(Jekyll::Page)
end

collections[key] = if key == 'posts' || key.end_with?('/posts')
@split_posts[key]
elsif key == 'drafts' || key.end_with?('/drafts')
@split_drafts[key]
else
@site.collections[key]&.docs
end
collections_config.each_key do |key|
next if key == 'data'

collections[key] ||= []
collections[key] = collections[key].map do |doc|
document_to_json(doc, key)
end
end

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

Expand All @@ -152,7 +165,7 @@ def remove_empty_collection_config(collections_config, collections)
should_delete = if key == 'data'
!data_files?
else
collections[key].empty? && collection_config['auto_discovered']
collections[key]&.empty? && collection_config['auto_discovered']
end

if should_delete
Expand All @@ -177,6 +190,27 @@ def document_type(doc)
end
end

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

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

def legacy_document_data(doc)
legacy_data = {}
legacy_data['categories'] = doc.categories if doc.respond_to?(:categories)
Expand Down Expand Up @@ -247,15 +281,27 @@ def all_drafts
end

def all_pages
html_pages = @site.pages.select do |page|
page.html? || page.url.end_with?('/')
end
pages = @site.pages.select { |doc| allowed_page?(doc) }
static_pages = @site.static_files.select { |doc| allowed_static_file?(doc) }
pages + static_pages
end

static_pages = @site.static_files.select do |static_page|
STATIC_EXTENSIONS.include?(static_page.extname)
def allowed_document?(doc)
if doc.instance_of?(Jekyll::Page)
allowed_page?(doc)
elsif doc.instance_of?(Jekyll::StaticFile)
allowed_static_file?(doc)
else
true
end
end

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

html_pages + static_pages
def allowed_static_file?(static_file)
STATIC_EXTENSIONS.include?(static_file.extname)
end

def data_files?
Expand Down
4 changes: 4 additions & 0 deletions lib/cloudcannon-jekyll/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ class Logger
def self.info(str)
Jekyll.logger.info('CloudCannon:', str)
end

def self.warn(str)
Jekyll.logger.warn('CloudCannon:', str)
end
end
end
2 changes: 1 addition & 1 deletion script/test-all-versions
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ JEKYLL_VERSION=2.4.0 bundle update && $(dirname "$0")/test &&
JEKYLL_VERSION=3.0.0 bundle update && $(dirname "$0")/test &&
JEKYLL_VERSION=3.3.1 bundle update && $(dirname "$0")/test &&
JEKYLL_VERSION=3.8.5 bundle update && $(dirname "$0")/test &&
JEKYLL_VERSION=4.2.1 bundle update && $(dirname "$0")/test
JEKYLL_VERSION=4.3.1 bundle update && $(dirname "$0")/test

84 changes: 3 additions & 81 deletions spec/expected/2.4.0/ignored-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,6 @@
"auto_discovered": true,
"path": "_data",
"output": false
},
"drafts": {
"auto_discovered": true,
"path": "_drafts",
"output": false
},
"other/posts": {
"auto_discovered": true,
"path": "other/_posts",
"output": true
},
"other/drafts": {
"auto_discovered": true,
"path": "other/_drafts",
"output": false
}
},
"collection_groups": [
Expand All @@ -91,6 +76,8 @@
}
],
"collections": {
"posts": [],
"drafts": [],
"staff_members": [],
"pages": [
{
Expand Down Expand Up @@ -172,68 +159,6 @@
"url": "/static-page.html",
"collection": "pages"
}
],
"drafts": [
{
"layout": "post",
"categories": [
"business"
],
"title": "WIP",
"date": "2016-08-10 00:00:00 +0000",
"author_staff_member": "jane-doe",
"image": null,
"slug": "incomplete",
"tags": [

],
"path": "_drafts/incomplete.md",
"url": "/business/2016/08/10/incomplete/",
"collection": "drafts",
"id": "/business/2016/08/10/incomplete"
}
],
"other/posts": [
{
"layout": "post",
"categories": [
"other",
"stuff"
],
"title": "Category test",
"author": "/staff-members/jane-doe",
"image": null,
"large_header": false,
"date": "2020-08-10 00:00:00 +0000",
"slug": "category-test",
"tags": [

],
"path": "other/_posts/2020-08-10-category-test.md",
"url": "/other/stuff/2020/08/10/category-test/",
"collection": "other/posts",
"id": "/other/stuff/2020/08/10/category-test"
}
],
"other/drafts": [
{
"layout": "post",
"categories": [
"other"
],
"title": "Other WIP",
"author": "/staff-members/jane-doe",
"image": "/uploads/example-upload.png",
"slug": "wip",
"tags": [

],
"date": "2024-01-01 00:00:00 +1300",
"path": "other/_drafts/wip.md",
"url": "/other/2024/01/01/wip/",
"collection": "other/drafts",
"id": "/other/2024/01/01/wip"
}
]
},
"data": {
Expand All @@ -252,10 +177,7 @@
"title": "Links"
}
],
"categories": [
"stuff",
"other"
],
"categories": [],
"tags": []
},
"source": "/spec/fixtures/ignored-collection",
Expand Down
Loading

0 comments on commit c95eedd

Please sign in to comment.