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 all 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
27 changes: 27 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,27 @@
# 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
return unless File.directory?(site.source)

entries = Dir.chdir(site.source) 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
1 change: 1 addition & 0 deletions bridgetown-core/test/source/src/_posts/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby3: groovy
1 change: 1 addition & 0 deletions bridgetown-core/test/source/src/_posts/es/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby3: trippin
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
layout: default
title: Further Nested
---

url: {{ page.url }}
date: {{ page.date }}
id: {{ page.id }}
17 changes: 17 additions & 0 deletions bridgetown-core/test/test_defaults_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "helper"

class TestDefaultsReader < BridgetownUnitTest
def setup
@reader = DefaultsReader.new(fixture_site)
@reader.read
end

context "default files" do
should "be loaded" do
assert_equal "groovy", @reader.path_defaults[fixture_site.source + "/_posts/"][:ruby3]
assert_equal "trippin", @reader.path_defaults[fixture_site.source + "/_posts/es/"][:ruby3]
end
end
end
1 change: 1 addition & 0 deletions bridgetown-core/test/test_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ def select; end
"categories" => [
"publish_test",
],
"ruby3" => "groovy",
"layout" => "default",
"title" => "Publish",
"category" => "publish_test",
Expand Down
15 changes: 15 additions & 0 deletions bridgetown-core/test/test_front_matter_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,19 @@ class TestFrontMatterDefaults < BridgetownUnitTest
assert(@site.posts.find { |page| page.data["date"] == date })
end
end

context "A site with front matter data cascade" do
setup do
@site = fixture_site
@site.process
end

should "have a post with a value from the defaults file" do
assert(@site.posts.find { |page| page.data[:title] == "Post with Permalink" }.data[:ruby3] == "groovy")
end

should "have an overridden value in a subtree" do
assert(@site.posts.find { |page| page.data[:title] == "Further Nested" }.data[:ruby3] == "trippin")
end
end
end
2 changes: 1 addition & 1 deletion bridgetown-core/test/test_generated_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestGeneratedSite < BridgetownUnitTest
end

should "ensure post count is as expected" do
assert_equal 60, @site.posts.size
assert_equal 61, @site.posts.size
end

should "insert site.posts into the index" do
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/test/test_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def read_posts
context "creating sites" do
setup do
@site = Site.new(site_configuration)
@num_invalid_posts = 5
@num_invalid_posts = 7
end

teardown do
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