-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79af7fc
commit 7702e9b
Showing
3 changed files
with
29 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
# inspired by https://github.com/haml/haml/blob/main/lib/haml/plugin.rb | ||
module Serbea | ||
# This module makes Serbea work with Rails using the template handler API. | ||
class Plugin | ||
def handles_encoding?; true; end | ||
|
||
def compile(template, source) | ||
"self.class.include(Serbea::Helpers);" + Tilt::SerbeaTemplate.new { source }.precompiled_template([]) | ||
module Rails | ||
module FrontmatterHelpers | ||
def set_page_frontmatter=(data) | ||
@frontmatter ||= HashWithDotAccess::Hash.new | ||
@frontmatter.update(data) | ||
end | ||
end | ||
|
||
def self.call(template, source = nil) | ||
source ||= template.source | ||
module FrontmatterControllerActions | ||
extend ActiveSupport::Concern | ||
|
||
new.compile(template, source) | ||
included do | ||
Serbea::TemplateEngine.front_matter_preamble = "self.set_page_frontmatter = local_frontmatter = YAML.load" | ||
|
||
before_action { @frontmatter ||= HashWithDotAccess::Hash.new } | ||
|
||
helper Serbea::Rails::FrontmatterHelpers | ||
end | ||
end | ||
|
||
def self.initialize_frontmatter | ||
if defined?(ApplicationHelper) | ||
Serbea::TemplateEngine.front_matter_preamble = "self.set_page_frontmatter = local_frontmatter = YAML.load" | ||
|
||
ApplicationHelper.define_method(:set_page_frontmatter=) do |data| | ||
@frontmatter ||= HashWithDotAccess::Hash.new | ||
@frontmatter.update(data) | ||
end | ||
|
||
ApplicationController.before_action { @frontmatter ||= HashWithDotAccess::Hash.new } | ||
class TemplateHandler | ||
def handles_encoding?; true; end | ||
|
||
def compile(template, source) | ||
"self.class.include(Serbea::Helpers);" + Tilt::SerbeaTemplate.new { source }.precompiled_template([]) | ||
end | ||
|
||
def self.call(template, source = nil) | ||
source ||= template.source | ||
|
||
new.compile(template, source) | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionView::Template.register_template_handler(:serb, Serbea::Plugin) | ||
ActionView::Template.register_template_handler(:serb, Serbea::Rails::TemplateHandler) |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Serbea | ||
VERSION = "0.8.0" | ||
VERSION = "0.8.1" | ||
end |