Skip to content

Commit

Permalink
Merge pull request #1103 from Fjan/feature/add-erubi-support
Browse files Browse the repository at this point in the history
Add a nanoc filter for erubi
  • Loading branch information
denisdefreyne committed Mar 15, 2017
2 parents 62646dd + e0bf7f8 commit f3025e1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Security/Eval:
- 'test/**/*.rb'
- 'spec/**/*.rb'
- 'lib/nanoc/base/entities/code_snippet.rb'

- 'lib/nanoc/filters/erubi.rb'


# ----- DISABLED (metrics) -----
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ group :plugins do
gem 'coderay'
gem 'coffee-script'
gem 'compass'
gem 'erubi'
gem 'erubis'
gem 'fog'
gem 'haml'
Expand Down
1 change: 1 addition & 0 deletions lib/nanoc/cli/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def gems_and_versions
'builder' => 'builder',
'coderay' => 'coderay',
'cri' => 'cri',
'erubi' => 'erubi',
'erubis' => 'erubis',
'escape' => 'escape',
'fog' => 'fog',
Expand Down
1 change: 1 addition & 0 deletions lib/nanoc/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Nanoc::Filters
require_relative 'filters/colorize_syntax'
require_relative 'filters/coffeescript'
require_relative 'filters/erb'
require_relative 'filters/erubi'
require_relative 'filters/erubis'
require_relative 'filters/haml'
require_relative 'filters/handlebars'
Expand Down
27 changes: 27 additions & 0 deletions lib/nanoc/filters/erubi.rb
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
73 changes: 73 additions & 0 deletions test/filters/test_erubi.rb
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

0 comments on commit f3025e1

Please sign in to comment.