diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 82f95db3..98d06516 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -60,6 +60,65 @@ module Haml buffer.tabulation -= i end + # Surrounds the given block of Haml code with the given characters, + # with no whitespace in between. + # For example, + # + # = surround '(', ')' do + # %a{:href => "food"} chicken + # + # makes + # + # (chicken) + # + # and + # + # = surround '*' do + # %strong angry + # + # makes + # + # *angry* + # + def surround(front, back = nil, &block) + back ||= front + output = capture_haml(&block) + + "#{front}#{output.chomp}#{back}\n" + end + + # Prepends the given character to the beginning of the Haml block, + # with no whitespace between. + # For example: + # + # = precede '*' do + # %span.small Not really + # + # compiles to + # + # *Not really + # + def precede(char, &block) + "#{char}#{capture_haml(&block).chomp}\n" + end + + # Appends the given character to the end of the Haml block, + # with no whitespace between. + # For example: + # + # click + # = succeed '.' do + # %a{:href=>"thing"} here + # + # compiles to + # + # click + # here. + # + def succeed(char, &block) + "#{capture_haml(&block).chomp}#{char}\n" + end + # Captures the result of the given block of Haml code, # gets rid of the excess indentation, # and returns it as a string. diff --git a/test/results/helpers.xhtml b/test/results/helpers.xhtml index b40e3627..4e2d8633 100644 --- a/test/results/helpers.xhtml +++ b/test/results/helpers.xhtml @@ -35,7 +35,11 @@ + (parentheses!) +*Not really +click +here.

baz

boom

foo diff --git a/test/templates/helpers.haml b/test/templates/helpers.haml index 3d66acaa..52810206 100644 --- a/test/templates/helpers.haml +++ b/test/templates/helpers.haml @@ -19,6 +19,13 @@ %h1 Big! %p Small / Invisible + = surround '(', ')' do + %strong parentheses! += precede '*' do + %span.small Not really +click += succeed '.' do + %a{:href=>"thing"} here %p baz - buffer.tabulation = 10 %p boom