Skip to content

Commit

Permalink
Do not raise when encountering an unsupported block
Browse files Browse the repository at this point in the history
Output a warning, and simply ignore the block.
  • Loading branch information
Jason Webster committed Oct 10, 2013
1 parent 67e6e52 commit 372d2ed
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master

- Don't raise an exception on missing blocks, merely log a warning
- Implement `{TagsAsClasses}` tag

## 0.2.1
Expand Down
13 changes: 8 additions & 5 deletions lib/tumblargh/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def self.factory(node, context, options = {})
args << n_post[1].to_i
end

base = "Blocks::#{block_name}"
base = "Blocks::#{ block_name }"
end

klass_name = "Tumblargh::Renderer::#{base}"
klass = klass_name.constantize

klass.new(node, context, options, *args)
klass_name = "Tumblargh::Renderer::#{ base }"
begin
klass = klass_name.constantize
klass.new(node, context, options, *args)
rescue NameError => e
puts "WARNING: Unsupported block `#{ klass_name }`"
end
end

end
Expand Down
1 change: 0 additions & 1 deletion lib/tumblargh/renderer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def render
alias_method :to_s, :render

def method_missing(method, *arguments)
# puts "mm #{method} - #{self.class.name} -- context is: #{context.class.name}"
raise "Can't find anything to do with '#{method}'" if context.nil?
context.send(method, *arguments)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/tumblargh/renderer/blocks/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def render
_, type, options, *nodes = node

res = nodes.map do |n|
Renderer.factory(n, self, options).render
renderer = Renderer.factory(n, self, options)
renderer.render unless renderer.nil?
end

" #{res.join('')} "
" #{ res.join('') } "
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/tumblargh/renderer/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def custom_value_for_type(type, key)

def render
node.map do |n|
Renderer.factory(n, self).render
renderer = Renderer.factory(n, self)
renderer.render unless renderer.nil?
end.flatten.join('')
end
end
Expand Down
71 changes: 50 additions & 21 deletions spec/renderer/blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@

describe Tumblargh::Renderer::Blocks do

before do
@parser = Tumblargh::Parser.new
def capture_stdout(&block)
original_stdout = $stdout
$stdout = fake = StringIO.new
begin
yield
ensure
$stdout = original_stdout
end
fake.string
end

let(:parser) { Tumblargh::Parser.new }

describe "{block:PermalinkPage}" do
before do
@parser.html = <<-eos
parser.html = <<-eos
{block:PermalinkPage}
ERMAGERD PERMERLINK
{/block:PermalinkPage}
eos
end

it "should render its content when it is a permalink page" do
@document = Tumblargh::Renderer::Document.new(@parser.tree, nil, :permalink => true)
@output = @document.render
@output.should match(/ERMAGERD PERMERLINK/)
document = Tumblargh::Renderer::Document.new(parser.tree, nil, :permalink => true)
output = document.render
output.should match(/ERMAGERD PERMERLINK/)
end

it "should NOT render its content when it is a permalink page" do
@document = Tumblargh::Renderer::Document.new(@parser.tree, nil, :permalink => false)
@output = @document.render
@output.should_not match(/ERMAGERD PERMERLINK/)
document = Tumblargh::Renderer::Document.new(parser.tree, nil, :permalink => false)
output = document.render
output.should_not match(/ERMAGERD PERMERLINK/)
end

end

describe "boolean blocks" do

before do
@parser.html = <<-eos
parser.html = <<-eos
{block:IfSomethingOnByDefault}
PASS_ON
{/block:IfSomethingOnByDefault}
Expand All @@ -50,29 +57,51 @@
PASS_INVERSE_OFF
{/block:IfNotSomethingOffByDefault}
eos
end

options = {
let(:options) do
{
"if" => {
"somethingonbydefault" => true,
"somethingoffbydefault" => false
}
}

@document = Tumblargh::Renderer::Document.new(@parser.tree, nil, options)
@output = @document.render
end

let(:output) { Tumblargh::Renderer::Document.new(parser.tree, nil, options).render }

it "should respect the default settings" do
@output.should match(/PASS_ON/)
@output.should_not match(/FAIL_OFF/)
output.should match(/PASS_ON/)
output.should_not match(/FAIL_OFF/)
end


it "inverse (IfNot) blocks should work" do
@output.should_not match(/FAIL_INVERSE_ON/)
@output.should match(/PASS_INVERSE_OFF/)
output.should_not match(/FAIL_INVERSE_ON/)
output.should match(/PASS_INVERSE_OFF/)
end

end

describe "using unsupported blocks" do
before do
parser.html = <<-eos
{block:ThisIsDefinitelyNotABlock}{/block:ThisIsDefinitelyNotABlock}
eos
end

it "should not raise an error" do
expect {
Tumblargh::Renderer::Document.new(parser.tree, nil, {}).render
}.not_to raise_error
end

it "should output a warning" do
out = capture_stdout {
Tumblargh::Renderer::Document.new(parser.tree, nil, {}).render
}

out.should =~ /WARNING: Unsupported block `Tumblargh::Renderer::Blocks::ThisIsDefinitelyNotABlock`/
end

end
end

0 comments on commit 372d2ed

Please sign in to comment.