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

Default to using ERB templates, the config YAML is now optional #888

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ module Bridgetown
autoload :Layout, "bridgetown-core/layout"
autoload :LayoutPlaceable, "bridgetown-core/concerns/layout_placeable"
autoload :LayoutReader, "bridgetown-core/readers/layout_reader"
autoload :LiquidRenderable, "bridgetown-core/concerns/liquid_renderable"
autoload :Localizable, "bridgetown-core/concerns/localizable"
autoload :LiquidRenderer, "bridgetown-core/liquid_renderer"
autoload :LogAdapter, "bridgetown-core/log_adapter"
Expand Down
19 changes: 17 additions & 2 deletions bridgetown-core/lib/bridgetown-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,25 @@ def write?
!!metadata.fetch("output", false)
end

# Used by Resource's permalink processor
# Used by Resources permalink processors
# @return [String]
def default_permalink
metadata.fetch("permalink", "/:locale/:collection/:path/")
metadata.fetch("permalink", default_configured_permalink)
end

# What the configured permalink should be absent of user overrides
# @return [String]
def default_configured_permalink
@default_configured_permalink ||= case label
when "data"
nil
when "posts"
site.config.permalink
when "pages"
"/:locale/:path/"
else
"/:locale/:collection/:path/"
end
end

# Extract options for this collection from the site configuration.
Expand Down
28 changes: 11 additions & 17 deletions bridgetown-core/lib/bridgetown-core/commands/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def self.banner
desc: "Comma separated list of bundled configurations to perform"
class_option :templates,
aliases: "-t",
banner: "liquid|erb|serbea",
desc: "Preferred template engine (defaults to Liquid)"
banner: "erb|serbea|liquid",
desc: "Preferred template engine (defaults to ERB)"
class_option :"frontend-bundling",
aliases: "-e",
banner: "esbuild",
Expand Down Expand Up @@ -121,12 +121,12 @@ def create_site(new_site_path)
copy_file("frontend/styles/syntax-highlighting.css")

case options["templates"]
when "erb"
setup_erb_templates
when "serbea"
setup_serbea_templates
else
when "liquid"
setup_liquid_templates
else # default if no option specififed
setup_erb_templates
end

postcss_option ? configure_postcss : configure_sass
Expand All @@ -140,29 +140,23 @@ def setup_erb_templates
directory "TEMPLATES/erb/_layouts", "src/_layouts"
directory "TEMPLATES/erb/_components", "src/_components"
directory "TEMPLATES/erb/_partials", "src/_partials"
gsub_file "bridgetown.config.yml", %r!permalink: pretty\n!, <<~YML
permalink: pretty
template_engine: erb
YML
end

def setup_serbea_templates
directory "TEMPLATES/serbea/_layouts", "src/_layouts"
directory "TEMPLATES/serbea/_components", "src/_components"
directory "TEMPLATES/serbea/_partials", "src/_partials"
gsub_file "bridgetown.config.yml", %r!permalink: pretty\n!, <<~YML
permalink: pretty
template_engine: serbea
YML
gsub_file "config/initializers.rb", %r!template_engine "erb"\n!, <<~RUBY
template_engine "serbea"
RUBY
end

def setup_liquid_templates
directory "TEMPLATES/liquid/_layouts", "src/_layouts"
directory "TEMPLATES/liquid/_components", "src/_components"
gsub_file "bridgetown.config.yml", %r!permalink: pretty\n!, <<~YML
permalink: pretty
template_engine: liquid
YML
gsub_file "config/initializers.rb", %r!template_engine "erb"\n!, <<~RUBY
template_engine "liquid"
RUBY
end

def configure_sass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module LayoutPlaceable
# Returns false if the document is an asset file or if the front matter
# specifies `layout: none`
def place_in_layout?
!(yaml_file? || no_layout?)
no_layout?.!
end

def no_layout?
Expand Down
30 changes: 0 additions & 30 deletions bridgetown-core/lib/bridgetown-core/concerns/liquid_renderable.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def config=(config)
configure_component_paths
configure_file_read_opts

# TODO: see if we can just get rid of this. It's only used by GeneratedPage
self.permalink_style = (config["permalink"] || "pretty").to_sym
end

Expand Down
36 changes: 30 additions & 6 deletions bridgetown-core/lib/bridgetown-core/concerns/site/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,43 @@ def execute_inline_ruby_for_layouts!
end
end

def matched_converters_for_convertible(convertible)
def matched_converters_for_convertible(convertible) # rubocop:todo Metrics
@layout_converters ||= {}

if convertible.is_a?(Bridgetown::Layout) && @layout_converters[convertible]
return @layout_converters[convertible]
end

matches = converters.select do |converter|
if converter.method(:matches).arity == 1
converter.matches(convertible.extname)
else
converter.matches(convertible.extname, convertible)
if convertible.is_a?(Bridgetown::GeneratedPage) && convertible.original_resource
convertible = convertible.original_resource
end

directly_matched_template_engine = nil
matches = converters.map do |converter|
result = [
converter,
converter.matches(convertible.extname, convertible),
converter.determine_template_engine(convertible),
]

directly_matched_template_engine = converter if result[1] && converter.class.template_engine

result
end

matches = matches.filter_map do |result|
converter, ext_matched, engine_matched = result
next nil if !ext_matched && !engine_matched

next nil if !ext_matched &&
engine_matched && directly_matched_template_engine &&
converter != directly_matched_template_engine

if !convertible.data["template_engine"] && engine_matched
convertible.data["template_engine"] = converter.class.template_engine
end

converter
end

@layout_converters[convertible] = matches if convertible.is_a?(Bridgetown::Layout)
Expand Down
99 changes: 22 additions & 77 deletions bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def initialize(*)
"eager_load_paths" => [],
"autoloader_collapsed_paths" => [],
"additional_watch_paths" => [],
"defaults" => [],

# Handling Reading
"include" => [".htaccess", "_redirects", ".well-known"],
Expand All @@ -61,26 +62,20 @@ def initialize(*)
"ruby_in_front_matter" => true,

# Conversion
"content_engine" => "resource",
"template_engine" => "erb",
"markdown" => "kramdown",
"highlighter" => "rouge",

# Serving
"port" => "4000",
"host" => "127.0.0.1",
"base_path" => "/",
"show_dir_listing" => false,

# Output Configuration
"base_path" => "/",
"available_locales" => [:en],
"default_locale" => :en,
"prefix_default_locale" => false,
"permalink" => nil, # default is set according to content engine
"permalink" => "pretty",
"timezone" => nil, # use the local timezone

"quiet" => false,
"verbose" => false,
"defaults" => [],

"liquid" => {
"error_mode" => "warn",
Expand All @@ -107,10 +102,6 @@ def initialize(*)
},
}.each_with_object(Configuration.new) { |(k, v), hsh| hsh[k] = v.freeze }.freeze

# TODO: Deprecated. Remove support for _config as well as toml in the next release.
CONFIG_FILE_PREFIXES = %w(bridgetown.config _config).freeze
CONFIG_FILE_EXTS = %w(yml yaml toml).freeze

# @return [Hash<Symbol, Initializer>]
attr_accessor :initializers

Expand Down Expand Up @@ -208,33 +199,8 @@ def verbose(override = {})
end
alias_method :verbose?, :verbose

def safe_load_file(filename) # rubocop:todo Metrics
case File.extname(filename)
when %r!\.toml!i
Deprecator.deprecation_message(
"TOML configurations will no longer be supported in the next version of Bridgetown." \
"Use initializers or a .yaml config instead."
)
Bridgetown::Utils::RequireGems.require_with_graceful_fail("tomlrb") unless defined?(Tomlrb)
Tomlrb.load_file(filename)
when %r!\.ya?ml!i
if File.basename(filename, ".*") == "_config"
Deprecator.deprecation_message(
"YAML configurations named `_config.y(a)ml' will no longer be supported in the next " \
"version of Bridgetown. Rename to `bridgetown.config.yml' instead."
)
end
if File.extname(filename) == ".yaml"
Deprecator.deprecation_message(
"YAML configurations ending in `.yaml' will no longer be supported in the next " \
"version of Bridgetown. Rename to use `.yml' extension instead."
)
end
YAMLParser.load_file(filename) || {}
else
raise ArgumentError,
"No parser for '#{filename}' is available. Use a .y(a)ml file instead."
end
def safe_load_file(filename)
YAMLParser.load_file(filename) || {}
rescue Psych::DisallowedClass => e
raise "Unable to parse `#{File.basename(filename)}'. #{e.message}"
end
Expand All @@ -256,28 +222,15 @@ def config_files(override)
# By default only the first matching config file will be loaded, but
# multiple configs can be specified via command line.
config_files = override["config"]
if config_files.to_s.empty?
file_lookups = CONFIG_FILE_PREFIXES.map do |prefix|
CONFIG_FILE_EXTS.map do |ext|
Bridgetown.sanitized_path(root_dir(override), "#{prefix}.#{ext}")
end
end.flatten.freeze

found_file = file_lookups.find do |path|
File.exist?(path)
end

config_files = found_file || file_lookups.first
@default_config_file = true
end
config_files = "bridgetown.config.yml" if config_files.to_s.empty?
Array(config_files)
end

# Public: Read in a list of configuration files and merge with this hash
# Read in a list of configuration files and merge with this hash
#
# files - the list of configuration file paths
# @param files [Array<String>]
#
# Returns the full configuration, with the defaults overridden by the values in the
# @return [Hash] configuration with the defaults overridden by the values in the
# configuration files
def read_config_files(files)
config = self
Expand All @@ -298,30 +251,28 @@ def read_config_files(files)
config
end

# Public: Read configuration and return merged Hash
# Read configuration and return merged Hash
#
# file - the path to the YAML file to be read in
# @param file [String] the path to the YAML file to be read in
#
# Returns this configuration, overridden by the values in the file
# @return [Hash]
def read_config_file(file)
default_config_file = file == "bridgetown.config.yml"
file = File.expand_path(file)
next_config = safe_load_file(file)
# We don't care if default config is missing, we can return blank:
return {} if !File.exist?(file) && default_config_file

unless next_config.is_a?(Hash)
file_config = safe_load_file(file)

unless file_config.is_a?(Hash)
raise ArgumentError, "Configuration file: (INVALID) #{file}".yellow
end

Bridgetown.logger.debug "Configuration file:", file
next_config
file_config
rescue SystemCallError
if @default_config_file ||= nil
initializers_file = File.join(root_dir, "config", "initializers.rb")
Bridgetown.logger.warn "Configuration file:", "none" unless File.file?(initializers_file)
{}
else
Bridgetown.logger.error "Fatal:", "The configuration file '#{file}' could not be found."
raise LoadError, "The Configuration file '#{file}' could not be found."
end
Bridgetown.logger.error "Fatal:", "The configuration file '#{file}' could not be found."
raise LoadError, "missing configuration file"
end

# Merge in environment-specific options, if present
Expand Down Expand Up @@ -393,18 +344,12 @@ def add_default_collections # rubocop:todo all
self[:collections][:posts][:output] = true
self[:collections][:posts][:sort_direction] ||= "descending"

self[:permalink] = "pretty" if self[:permalink].blank?
self[:collections][:pages] = {} unless self[:collections][:pages]
self[:collections][:pages][:output] = true
self[:collections][:pages][:permalink] ||= "/:locale/:path/"

self[:collections][:data] = {} unless self[:collections][:data]
self[:collections][:data][:output] = false

unless self[:collections][:posts][:permalink]
self[:collections][:posts][:permalink] = self[:permalink]
end

self
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def roda(&block)
@scope.roda_initializers << block
end

def timezone(tz) # rubocop:disable Naming/MethodParameterName
Bridgetown.set_timezone(tz)
def timezone(new_timezone)
@data[:timezone] = new_timezone
Bridgetown.set_timezone(new_timezone)
end

def method_missing(key, *value, &block) # rubocop:disable Style/MissingRespondToMissing
Expand Down