-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
End-to-end Ruby front matter, templates, and data files (#285)
* Major feature addition for Ruby Front Matter and raw templates Also additional work to normalize Liquid & ERB template APIs * Improve docs * improve error messages, support Ruby data files * Add to_json support for Resources * Improve code quality and test rbfm * Refactor front matter importing and add rbfm to layouts * Revert and use regex capture * Remove logging around rbfm * Lots of Ruby files and rbfm documentation, couple of fixes * Better to_json compat * Fix Liquid error on website build
- Loading branch information
1 parent
12ed5fb
commit 99576f3
Showing
32 changed files
with
480 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
bridgetown-core/lib/bridgetown-core/concerns/front_matter_importer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
SafeYAML.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..-1].join("\n"), file_path, 2) | ||
elsif is_a?(Layout) | ||
self.content = file_contents | ||
{} | ||
else | ||
yaml_data = SafeYAML.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
bridgetown-core/lib/bridgetown-core/converters/ruby_templates.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown | ||
module Converters | ||
class RubyTemplates < Converter | ||
priority :highest | ||
input :rb | ||
|
||
def convert(content, convertible) | ||
erb_view = Bridgetown::ERBView.new(convertible) | ||
erb_view.instance_eval( | ||
content, convertible.relative_path.to_s, line_start(convertible) | ||
).to_s | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.