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

feat: allow template to use data directly instead of resource.data #622

Merged
merged 6 commits into from
Sep 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def configure_payload(content = nil)
payload["paginator"] = document.respond_to?(:paginator) ? document.paginator.to_liquid : nil
payload["layout"] = @layout ? @layout.to_liquid.merge({ data: @layout.data }) : {}
payload["content"] = content
payload["data"] = payload["page"].data
end

def liquid_context
Expand Down
85 changes: 37 additions & 48 deletions bridgetown-core/lib/bridgetown-core/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ class Reader
# @return [Bridgetown::Site]
attr_reader :site

# @param site [Bridgetown::Site]
def initialize(site)
@site = site
end

# Read Site data from disk and load it into internal data structures.
#
# Returns nothing.
# Read data and resources from disk and load it into internal data structures.
# @return [void]
def read
site.defaults_reader.read
site.data = site.collections.data.read.merge_data_resources
read_layouts
read_directories
read_includes
sort_files!
site.data = site.collections.data.read.merge_data_resources
read_collections
Bridgetown::PluginManager.source_manifests.select(&:content).each do |manifest|
PluginContentReader.new(site, manifest).read
end
end

# Read in layouts
# @see LayoutReader
# @return [void]
def read_layouts
site.layouts = LayoutReader.new(site).read
end

# Read in collections (other than the data collection)
# @return [void]
def read_collections
site.collections.each_value do |collection|
next if collection.data?
Expand All @@ -38,6 +43,7 @@ def read_collections
end

# Sorts generated pages and static files.
# @return [void]
def sort_files!
site.generated_pages.sort_by!(&:name)
site.static_files.sort_by!(&:relative_path)
Expand All @@ -47,44 +53,41 @@ def sort_files!
# that will become part of the site according to the rules in
# filter_entries.
#
# dir - The String relative path of the directory to read. Default: ''.
#
# Returns nothing.
# @param dir [String] relative path of the directory to read. Default: ''
# @return [void]
def read_directories(dir = "")
base = site.in_source_dir(dir)

return unless File.directory?(base)

dot_dirs = []
dot_pages = []
dot_static_files = []
entries_dirs = []
entries_pages = []
entries_static_files = []

dot = Dir.chdir(base) { filter_entries(Dir.entries("."), base) }
dot.each do |entry|
entries = Dir.chdir(base) { filter_entries(Dir.entries("."), base) }
entries.each do |entry|
file_path = @site.in_source_dir(base, entry)
if File.directory?(file_path)
dot_dirs << entry
entries_dirs << entry
elsif Utils.has_yaml_header?(file_path) || Utils.has_rbfm_header?(file_path)
dot_pages << entry
entries_pages << entry
else
dot_static_files << entry
entries_static_files << entry
end
end

retrieve_dirs(base, dir, dot_dirs)
retrieve_pages(dir, dot_pages)
retrieve_static_files(dir, dot_static_files)
retrieve_dirs(dir, entries_dirs)
retrieve_pages(dir, entries_pages)
retrieve_static_files(dir, entries_static_files)
end

# Recursively traverse directories with the read_directories function.
#
# base - The String representing the site's base directory.
# dir - The String representing the directory to traverse down.
# dot_dirs - The Array of subdirectories in the dir.
#
# Returns nothing.
def retrieve_dirs(_base, dir, dot_dirs)
dot_dirs.each do |file|
# @param dir [String] the directory to traverse down
# @param entries_dirs [Array<String>] subdirectories in the directory
# @return [void]
def retrieve_dirs(dir, entries_dirs)
entries_dirs.each do |file|
dir_path = site.in_source_dir(dir, file)
rel_path = File.join(dir, file)
@site.reader.read_directories(rel_path) unless @site.dest.chomp("/") == dir_path
Expand All @@ -94,12 +97,11 @@ def retrieve_dirs(_base, dir, dot_dirs)
# Retrieve all the pages from the current directory,
# add them to the site and sort them.
#
# dir - The String representing the directory retrieve the pages from.
# dot_pages - The Array of pages in the dir.
#
# Returns nothing.
def retrieve_pages(dir, dot_pages)
dot_pages.each do |page_path|
# @param dir [String] the directory to retrieve the pages from
# @param entries_pages [Array<String>] page paths in the directory
# @return [void]
def retrieve_pages(dir, entries_pages)
entries_pages.each do |page_path|
site.collections.pages.read_resource(site.in_source_dir(dir, page_path))
end
end
Expand All @@ -122,8 +124,8 @@ def retrieve_static_files(dir, files)
# or are excluded in the site configuration, unless they are web server
# files such as '.htaccess'.
#
# entries - The Array of String file/directory entries to filter.
# base_directory - The string representing the optional base directory.
# @param entries [Array<String>] file/directory entries to filter
# @param base_directory [String] optional base directory
#
# Returns the Array of filtered entries.
def filter_entries(entries, base_directory = nil)
Expand All @@ -132,8 +134,8 @@ def filter_entries(entries, base_directory = nil)

# Read the entries from a particular directory for processing
#
# dir - The String representing the relative path of the directory to read.
# subfolder - The String representing the directory to read.
# @param dir [String] parent directory
# @param subfolder [String] the directory to read
#
# Returns the list of entries to process
def get_entries(dir, subfolder)
Expand All @@ -146,19 +148,6 @@ def get_entries(dir, subfolder)

private

# Internal
#
# Determine if the directory is supposed to contain posts.
# If the user has defined a custom collections_dir, then attempt to read
# posts only from within that directory.
#
# Returns true if a custom collections_dir has been set but current directory lies
# outside that directory.
def outside_configured_directory?(dir)
collections_dir = site.config["collections_dir"]
!collections_dir.empty? && !dir.start_with?("/#{collections_dir}")
end

def read_includes
site.config.include.each do |entry|
next if entry == ".htaccess"
Expand Down
14 changes: 13 additions & 1 deletion bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Bridgetown
module Resource
class Base
class Base # rubocop:todo Metrics/ClassLength
include Comparable
include Bridgetown::Publishable
include Bridgetown::LayoutPlaceable
Expand Down Expand Up @@ -300,6 +300,7 @@ def previous_resource

def ensure_default_data
determine_locale
merge_requested_site_data

slug = if matches = relative_path.to_s.match(DATE_FILENAME_MATCHER) # rubocop:disable Lint/AssignmentInCondition
set_date_from_string(matches[1]) unless data.date
Expand All @@ -314,6 +315,17 @@ def ensure_default_data
data.title ||= Bridgetown::Utils.titleize_slug(slug)
end

# Lets you put `site.data.foo.bar` in a front matter variable and it will then get swapped
# out for the actual site data
def merge_requested_site_data
data.each do |k, v|
next unless v.is_a?(String) && v.starts_with?("site.data.")

data_path = v.delete_prefix("site.data.")
data[k] = site.data.dig(*data_path.split("."))
end
end

def set_date_from_string(new_date) # rubocop:disable Naming/AccessorMethodName
return unless new_date.is_a?(String)

Expand Down
4 changes: 4 additions & 0 deletions bridgetown-core/lib/bridgetown-core/ruby_template_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def initialize(convertible)
@site = page.site
end

def data
resource.data
end

def partial(_partial_name = nil, **_options)
raise "Must be implemented in a subclass"
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!doctype html>
<html lang="<%= site.locale %>">
<head>
<%= render "head", metadata: site.metadata, title: resource.data.title %>
<%= render "head", metadata: site.metadata, title: data.title %>
</head>
<body class="<%= resource.data.layout %> <%= resource.data.page_class %>">
<body class="<%= data.layout %> <%= data.page_class %>">
<%= render Shared::Navbar.new(metadata: site.metadata, resource: resource) %>

<main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1><%= resource.data.title %></h1>
<h1><%= data.title %></h1>

<%= yield %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1><%= resource.data.title %></h1>
<h1><%= data.title %></h1>

<%= yield %>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!doctype html>
<html lang="{{ site.locale }}">
<head>
{% render "head", metadata: site.metadata, title: resource.data.title %}
{% render "head", metadata: site.metadata, title: data.title %}
</head>
<body class="{{ resource.data.layout }} {{ resource.data.page_class }}">
<body class="{{ data.layout }} {{ data.page_class }}">
{% render "navbar", metadata: site.metadata, resource: resource %}

<main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1>{{ page.title }}</h1>
<h1>{{ data.title }}</h1>

{{ content }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1>{{ page.title }}</h1>
<h1>{{ data.title }}</h1>

{{ content }}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!doctype html>
<html lang="{%= site.locale %}">
<head>
{%@ "head", metadata: site.metadata, title: resource.data.title %}
{%@ "head", metadata: site.metadata, title: data.title %}
</head>
<body class="{{ resource.data.layout }} {{ resource.data.page_class }}">
<body class="{{ data.layout }} {{ data.page_class }}">
{%@ Shared::Navbar metadata: site.metadata, resource: resource %}

<main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1>{{ resource.data.title }}</h1>
<h1>{{ data.title }}</h1>

{%= yield %}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
layout: default
---

<h1>{{ resource.data.title }}</h1>
<h1>{{ data.title }}</h1>

{%= yield %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ locale_overrides:
title: "Sur mesure"
---

{% if site.locale == "en" %}English:{% elsif site.locale == "fr" %}French:{% endif %} {{ resource.data.title }}
{% if site.locale == "en" %}English:{% elsif site.locale == "fr" %}French:{% endif %} {{ data.title }}

{{ site.locale | t }}: {{ "test.name" | t }}
2 changes: 1 addition & 1 deletion bridgetown-core/test/source/src/_layouts/erblayout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout_var:

<div>Test? <%= layout[:layout_var].first %></div>

<h1><%= page.data.title %></h1>
<h1><%= data.title %></h1>

<%= yield %>

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/test/source/src/_layouts/serblayout.serb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout_var:

<div>Test? {%= layout[:layout_var].first %}</div>

<h1>{{ page.data.title }}</h1>
<h1>{{ data.title }}</h1>

{%= yield %}

Expand Down
Loading