Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process data cascade for folder-based frontmatter defaults #139

Merged
merged 3 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module Bridgetown
autoload :Cache, "bridgetown-core/cache"
autoload :CollectionReader, "bridgetown-core/readers/collection_reader"
autoload :DataReader, "bridgetown-core/readers/data_reader"
autoload :DefaultsReader, "bridgetown-core/readers/defaults_reader"
autoload :LayoutReader, "bridgetown-core/readers/layout_reader"
autoload :PostReader, "bridgetown-core/readers/post_reader"
autoload :PageReader, "bridgetown-core/readers/page_reader"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def config=(config)
@config
end

def defaults_reader
@defaults_reader ||= DefaultsReader.new(self)
end

# Returns the current instance of {FrontmatterDefaults} or
# creates a new instance {FrontmatterDefaults} if it doesn't already exist.
#
Expand Down
17 changes: 17 additions & 0 deletions bridgetown-core/lib/bridgetown-core/frontmatter_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def ensure_time!(set)
#
# Returns the default value or nil if none was found
def find(path, type, setting)
merged_data = {}
merge_data_cascade_for_path(path, merged_data)
return merged_data[setting] if merged_data.key?(setting)

value = nil
old_scope = nil

Expand All @@ -74,6 +78,9 @@ def find(path, type, setting)
# Returns a hash with all default values (an empty hash if there are none)
def all(path, type)
defaults = {}

merge_data_cascade_for_path(path, defaults)

old_scope = nil
matching_sets(path, type).each do |set|
if has_precedence?(old_scope, set["scope"])
Expand All @@ -88,6 +95,16 @@ def all(path, type)

private

def merge_data_cascade_for_path(path, merged_data)
absolute_path = @site.in_source_dir(path)
@site.defaults_reader.path_defaults
.select { |k, _v| absolute_path.include? k }
.sort_by { |k, _v| k.length }
.each do |defaults|
merged_data.merge!(defaults[1])
end
end
jaredcwhite marked this conversation as resolved.
Show resolved Hide resolved

# Checks if a given default setting scope matches the given path and type
#
# scope - the hash indicating the scope, as defined in bridgetown.config.yml
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(site)
# Returns nothing.
# rubocop:disable Metrics/AbcSize
def read
@site.defaults_reader.read
@site.layouts = LayoutReader.new(site).read
read_directories
read_included_excludes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class CollectionReader
SPECIAL_COLLECTIONS = %w(posts data).freeze

attr_reader :site, :content

def initialize(site)
@site = site
@content = {}
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/readers/data_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class DataReader
attr_reader :site, :content

def initialize(site)
@site = site
@content = ActiveSupport::HashWithIndifferentAccess.new
Expand Down
25 changes: 25 additions & 0 deletions bridgetown-core/lib/bridgetown-core/readers/defaults_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Bridgetown
class DefaultsReader
attr_reader :site, :path_defaults

def initialize(site)
@site = site
@path_defaults = ActiveSupport::HashWithIndifferentAccess.new
end

def read
jaredcwhite marked this conversation as resolved.
Show resolved Hide resolved
entries = Dir.chdir(site.in_source_dir) do
Dir["**/_defaults.{yaml,yml,json}"]
end

entries.each do |entry|
path = @site.in_source_dir(entry)
@path_defaults[File.dirname(path) + File::SEPARATOR] = SafeYAML.load_file(path)
end

@path_defaults
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class LayoutReader
attr_reader :site

def initialize(site)
@site = site
@layouts = {}
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/readers/page_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class PageReader
attr_reader :site, :dir, :unfiltered_content

def initialize(site, dir)
@site = site
@dir = dir
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/readers/post_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class PostReader
attr_reader :site, :unfiltered_content

def initialize(site)
@site = site
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class StaticFileReader
attr_reader :site, :dir, :unfiltered_content

def initialize(site, dir)
@site = site
@dir = dir
Expand Down
19 changes: 0 additions & 19 deletions bridgetown-website/bridgetown.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ collections:
name: Documentation
foo: bar

defaults:
- scope:
path: ""
values:
image: /images/bridgetown-logo-twitter-card.jpg
- scope:
path: _docs
values:
layout: docs
- scope:
path: _posts
values:
layout: post
category: news
- scope:
path: _posts/drafts
values:
published: false

pagination:
enabled: true

Expand Down
1 change: 1 addition & 0 deletions bridgetown-website/src/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image: /images/bridgetown-logo-twitter-card.jpg
1 change: 1 addition & 0 deletions bridgetown-website/src/_docs/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout: docs
2 changes: 2 additions & 0 deletions bridgetown-website/src/_posts/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
layout: post
category: news
1 change: 1 addition & 0 deletions bridgetown-website/src/_posts/drafts/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
published: false