-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1103 from Fjan/feature/add-erubi-support
Add a nanoc filter for erubi
- Loading branch information
Showing
6 changed files
with
104 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Nanoc::Filters | ||
# @api private | ||
class Erubi < Nanoc::Filter | ||
identifier :erubi | ||
|
||
requires 'erubi' | ||
|
||
# Runs the content through [Erubi](https://github.com/jeremyevans/erubi). | ||
# To prevent single quote escaping use :escapefunc => 'Nanoc::Helpers::HTMLEscape.html_escape' | ||
# See the Erubi documentation for more options. | ||
# | ||
# @param [String] content The content to filter | ||
# | ||
# @return [String] The filtered content | ||
def run(content, params = {}) | ||
# Create context | ||
context = ::Nanoc::Int::Context.new(assigns) | ||
|
||
# Get binding | ||
proc = assigns[:content] ? -> { assigns[:content] } : nil | ||
assigns_binding = context.get_binding(&proc) | ||
|
||
# Get result | ||
eval(::Erubi::Engine.new(content, { bufvar: '_erbout', filename: filename }.merge(params)).src, assigns_binding) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'helper' | ||
|
||
class Nanoc::Filters::ErubiTest < Nanoc::TestCase | ||
def test_filter_with_instance_variable | ||
if_have 'erubi' do | ||
# Create filter | ||
filter = ::Nanoc::Filters::Erubi.new(location: 'a cheap motel') | ||
|
||
# Run filter | ||
result = filter.setup_and_run('<%= "I was hiding in #{@location}." %>') | ||
assert_equal('I was hiding in a cheap motel.', result) | ||
end | ||
end | ||
|
||
def test_filter_with_instance_method | ||
if_have 'erubi' do | ||
# Create filter | ||
filter = ::Nanoc::Filters::Erubi.new(location: 'a cheap motel') | ||
|
||
# Run filter | ||
result = filter.setup_and_run('<%= "I was hiding in #{location}." %>') | ||
assert_equal('I was hiding in a cheap motel.', result) | ||
end | ||
end | ||
|
||
def test_filter_error | ||
if_have 'erubi' do | ||
# Create filter | ||
filter = ::Nanoc::Filters::Erubi.new | ||
|
||
# Run filter | ||
raised = false | ||
begin | ||
filter.setup_and_run('<%= this isn\'t really ruby so it\'ll break, muahaha %>') | ||
rescue SyntaxError => e | ||
assert_match 'syntax error', e.message | ||
raised = true | ||
end | ||
assert raised | ||
end | ||
end | ||
|
||
def test_filter_with_yield | ||
if_have 'erubi' do | ||
# Create filter | ||
filter = ::Nanoc::Filters::Erubi.new(content: 'a cheap motel') | ||
|
||
# Run filter | ||
result = filter.setup_and_run('<%= "I was hiding in #{yield}." %>') | ||
assert_equal('I was hiding in a cheap motel.', result) | ||
end | ||
end | ||
|
||
def test_filter_with_yield_without_content | ||
if_have 'erubi' do | ||
# Create filter | ||
filter = ::Nanoc::Filters::Erubi.new(location: 'a cheap motel') | ||
|
||
# Run filter | ||
assert_raises LocalJumpError do | ||
filter.setup_and_run('<%= "I was hiding in #{yield}." %>') | ||
end | ||
end | ||
end | ||
|
||
def test_filter_with_erbout | ||
if_have 'erubi' do | ||
filter = ::Nanoc::Filters::Erubi.new | ||
result = filter.setup_and_run('stuff<% _erbout << _erbout %>') | ||
assert_equal 'stuffstuff', result | ||
end | ||
end | ||
end |