Skip to content

Commit

Permalink
Revert "Refactor front matter handling and extract behavior into load…
Browse files Browse the repository at this point in the history
…ers (#778)"

This reverts commit b22ddfe.
  • Loading branch information
jaredcwhite committed Mar 16, 2024
1 parent 0f18fb2 commit 8d20185
Show file tree
Hide file tree
Showing 31 changed files with 319 additions and 682 deletions.
2 changes: 1 addition & 1 deletion bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def resource
end

def add_resource(collection_name, path, &block) # rubocop:todo Metrics/AbcSize
data = Bridgetown::FrontMatter::RubyFrontMatter.new(scope: self).tap do |fm|
data = Bridgetown::Utils::RubyFrontMatter.new(scope: self).tap do |fm|
fm.define_singleton_method(:___) do |hsh|
hsh.each do |k, v|
fm.set k, v
Expand Down
13 changes: 2 additions & 11 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ module Bridgetown
autoload :EntryFilter, "bridgetown-core/entry_filter"
# TODO: we have too many errors! This is silly
autoload :Errors, "bridgetown-core/errors"
autoload :FrontMatter, "bridgetown-core/front_matter"
autoload :FrontmatterDefaults, "bridgetown-core/frontmatter_defaults"
autoload :FrontMatterImporter, "bridgetown-core/concerns/front_matter_importer"
autoload :GeneratedPage, "bridgetown-core/generated_page"
autoload :Hooks, "bridgetown-core/hooks"
autoload :Layout, "bridgetown-core/layout"
Expand All @@ -109,16 +110,6 @@ module Bridgetown
autoload :Watcher, "bridgetown-core/watcher"
autoload :YAMLParser, "bridgetown-core/yaml_parser"

FrontmatterDefaults = ActiveSupport::Deprecation::DeprecatedConstantProxy.new(
"FrontmatterDefaults",
"Bridgetown::FrontMatter::Defaults"
)

FrontMatterImporter = ActiveSupport::Deprecation::DeprecatedConstantProxy.new(
"FrontMatterImporter",
"Bridgetown::FrontMatter::Importer"
)

# extensions
require "bridgetown-core/commands/registrations"
require "bridgetown-core/plugin"
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def read

next if File.basename(file_path).starts_with?("_")

if label == "data" || FrontMatter::Loaders.front_matter?(full_path)
if label == "data" || Utils.has_yaml_header?(full_path) ||
Utils.has_rbfm_header?(full_path)
read_resource(full_path)
else
read_static_file(file_path, full_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module Bridgetown
module FrontMatterImporter
# Requires klass#content and klass#front_matter_line_count accessors
def self.included(klass)
klass.include Bridgetown::Utils::RubyFrontMatterDSL
end

YAML_HEADER = %r!\A---\s*\n!.freeze
YAML_BLOCK = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
RUBY_HEADER = %r!\A[~`#-]{3,}(?:ruby|<%|{%)\s*\n!.freeze
RUBY_BLOCK =
%r!#{RUBY_HEADER.source}(.*?\n?)^((?:%>|%})?[~`#-]{3,}\s*$\n?)!m.freeze

def read_front_matter(file_path) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
file_contents = File.read(
file_path, **Bridgetown::Utils.merged_file_read_opts(Bridgetown::Current.site, {})
)
yaml_content = file_contents.match(YAML_BLOCK)
if !yaml_content && Bridgetown::Current.site.config.should_execute_inline_ruby?
ruby_content = file_contents.match(RUBY_BLOCK)
end

if yaml_content
self.content = yaml_content.post_match
self.front_matter_line_count = yaml_content[1].lines.size - 1
YAMLParser.load(yaml_content[1])
elsif ruby_content
# rbfm header + content underneath
self.content = ruby_content.post_match
self.front_matter_line_count = ruby_content[1].lines.size
process_ruby_data(ruby_content[1], file_path, 2)
elsif Bridgetown::Utils.has_rbfm_header?(file_path)
process_ruby_data(File.read(file_path).lines[1..].join("\n"), file_path, 2)
elsif is_a?(Layout)
self.content = file_contents
{}
else
yaml_data = YAMLParser.load_file(file_path)
yaml_data.is_a?(Array) ? { rows: yaml_data } : yaml_data
end
end

def process_ruby_data(rubycode, file_path, starting_line)
ruby_data = instance_eval(rubycode, file_path.to_s, starting_line)
ruby_data.is_a?(Array) ? { rows: ruby_data } : ruby_data.to_h
rescue StandardError => e
raise "Ruby code isn't returning an array, or object which responds to `to_h' (#{e.message})"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def defaults_reader
@defaults_reader ||= Bridgetown::DefaultsReader.new(self)
end

# Returns the current instance of {FrontMatter::Defaults} or
# creates a new instance {FrontMatter::Defaults} if it doesn't already exist.
# Returns the current instance of {FrontmatterDefaults} or
# creates a new instance {FrontmatterDefaults} if it doesn't already exist.
#
# @return [FrontMatter::Defaults]
# Returns an instance of {FrontMatter::Defaults}
# @return [FrontmatterDefaults]
# Returns an instance of {FrontmatterDefaults}
def frontmatter_defaults
@frontmatter_defaults ||= Bridgetown::FrontMatter::Defaults.new(self)
@frontmatter_defaults ||= Bridgetown::FrontmatterDefaults.new(self)
end

# Prefix a path or paths with the {#root_dir} directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Bridgetown
class Configuration
class ConfigurationDSL < Bridgetown::FrontMatter::RubyFrontMatter
class ConfigurationDSL < Bridgetown::Utils::RubyFrontMatter
attr_reader :context

# @yieldself [ConfigurationDSL]
Expand Down
11 changes: 0 additions & 11 deletions bridgetown-core/lib/bridgetown-core/front_matter.rb

This file was deleted.

225 changes: 0 additions & 225 deletions bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb

This file was deleted.

34 changes: 0 additions & 34 deletions bridgetown-core/lib/bridgetown-core/front_matter/importer.rb

This file was deleted.

Loading

0 comments on commit 8d20185

Please sign in to comment.