Skip to content
Browse files

Pulled in some of the ERB capture code from Merb. This provides a cle…

…aner way

for helper methods to extract text from an ERB template, modify that text, and
then insert the results in place of the original text.
  • Loading branch information...
1 parent c09e853 commit 8b0d76de0d5245a51d578264c665a20566f45b72 @TwP TwP committed May 19, 2008
View
89 lib/webby/helpers/capture_helper.rb
@@ -1,29 +1,76 @@
+
+module Webby::Helpers
+
+# Based on code from Rails and Merb.
+#
module CaptureHelper
- # Based on code from Rails
- def content_for(name, content = nil, &block)
- existing_content_for = instance_variable_get("@content_for_#{name}").to_s
- new_content_for = existing_content_for + (block_given? ? capture(&block) : content)
- instance_variable_set("@content_for_#{name}", new_content_for)
+ def content_for( name, content = nil, &block )
+ cur = instance_variable_get("@content_for_#{name}").to_s
+ new = if block.nil? then content
+ else capture_erb(&block) end
+
+ new = cur + new.to_s
+ instance_variable_set("@content_for_#{name}", new)
+ end
+
+ # Provides direct acccess to the buffer for this view context
+ #
+ # ==== Parameters
+ # the_binding<Binding>:: The binding to pass to the buffer.
+ #
+ # ==== Returns
+ # DOC
+ def _erb_buffer( the_binding )
+ eval("_erbout", the_binding, __FILE__, __LINE__)
+ end
+
+ # ==== Parameters
+ # *args:: Arguments to pass to the block.
+ # &block:: The template block to call.
+ #
+ # ==== Returns
+ # String:: The output of the block.
+ #
+ # ==== Examples
+ # Capture being used in a .html.erb page:
+ #
+ # <% @foo = capture do %>
+ # <p>Some Foo content!</p>
+ # <% end %>
+ #
+ def capture_erb( *args, &block )
+ # get the buffer from the block's binding
+ buffer = _erb_buffer(block.binding) rescue nil
+
+ # If there is no buffer, just call the block and get the contents
+ if buffer.nil?
+ block.call(*args)
+ # If there is a buffer, execute the block, then extract its contents
+ else
+ pos = buffer.length
+ block.call(*args)
+
+ # extract the block
+ data = buffer[pos..-1]
+
+ # replace it in the original with empty string
+ buffer[pos..-1] = ''
+
+ data
+ end
end
- def capture_erb(*args, &block)
- buffer = eval('_erbout', block.binding)
- capture_erb_with_buffer(buffer, *args, &block)
+ # DOC
+ def concat_erb(string, the_binding)
+ _erb_buffer(the_binding) << string
end
- alias_method :capture, :capture_erb
- alias_method :capture_block, :capture_erb
-
- def capture_erb_with_buffer(buffer, *args, &block)
- pos = buffer.length
- block.call(*args)
- # extract the block
- data = buffer[pos..-1]
- # replace it in the original with empty string
- buffer[pos..-1] = ''
- data
- end
+
end # module CaptureHelper
-Webby::Helpers.register(CaptureHelper)
+register(CaptureHelper)
+
+end # module Webby::Helpers
+
+# EOF
View
13 lib/webby/helpers/coderay_helper.rb
@@ -31,15 +31,8 @@ module CodeRayHelper
def coderay( *args, &block )
opts = args.last.instance_of?(Hash) ? args.pop : {}
- buffer = eval('_erbout', block.binding)
- pos = buffer.length
- block.call(*args)
-
- text = buffer[pos..-1]
- if text.empty?
- buffer[pos..-1] = ''
- return
- end
+ text = capture_erb(&block)
+ return if text.empty?
defaults = ::Webby.site.coderay
lang = opts.getopt(:lang, defaults[:lang]).to_sym
@@ -65,7 +58,7 @@ def coderay( *args, &block )
out << "\n</notextile>"
end
- buffer[pos..-1] = out
+ concat_erb(out, block.binding)
return
end
end # module CodeRayHelper
View
14 lib/webby/helpers/graphviz_helper.rb
@@ -62,24 +62,18 @@ def self.error_check( file )
def graphviz( *args, &block )
opts = args.last.instance_of?(Hash) ? args.pop : {}
+ text = capture_erb(&block)
+ return if text.empty?
+
# create a temporary file for holding any error messages
# from the graphviz program
err = Tempfile.new('graphviz_err')
err.close
- buffer = eval('_erbout', block.binding)
- pos = buffer.length
- block.call(*args)
-
defaults = ::Webby.site.graphviz
path = opts.getopt(:path, defaults[:path])
cmd = opts.getopt(:cmd, defaults[:cmd])
type = opts.getopt(:type, defaults[:type])
- text = buffer[pos..-1].strip
- if text.empty?
- buffer[pos..-1] = ''
- return
- end
# pull the name of the graph|digraph out of the DOT script
name = text.match(%r/\A\s*(?:strict\s+)?(?:di)?graph\s+([A-Za-z_][A-Za-z0-9_]*)\s+\{/o)[1]
@@ -132,7 +126,7 @@ def graphviz( *args, &block )
out << "\n</notextile>"
end
- buffer[pos..-1] = out
+ concat_erb(out, block.binding)
return
end
end # module GraphvizHelper
View
13 lib/webby/helpers/tex_img_helper.rb
@@ -41,15 +41,8 @@ def tex2img( *args, &block )
name = args.first
raise 'TeX graphics must have a name' if name.nil?
- buffer = eval('_erbout', block.binding)
- pos = buffer.length
- block.call(*args)
-
- text = buffer[pos..-1].strip
- if text.empty?
- buffer[pos..-1] = ''
- return
- end
+ text = capture_erb(&block)
+ return if text.empty?
defaults = ::Webby.site.tex2img
path = opts.getopt(:path, defaults[:path])
@@ -128,7 +121,7 @@ def tex2img( *args, &block )
out << "\n</notextile>"
end
- buffer[pos..-1] = out
+ concat_erb(out, block.binding)
return
end
end # module TexImgHelper
View
13 lib/webby/helpers/ultraviolet_helper.rb
@@ -35,15 +35,8 @@ module UltraVioletHelper
def uv( *args, &block )
opts = args.last.instance_of?(Hash) ? args.pop : {}
- buffer = eval('_erbout', block.binding)
- pos = buffer.length
- block.call(*args)
-
- text = buffer[pos..-1]
- if text.empty?
- buffer[pos..-1] = ''
- return
- end
+ text = capture_erb(&block)
+ return if text.empty?
defaults = ::Webby.site.uv
lang = opts.getopt(:lang, defaults[:lang])
@@ -59,7 +52,7 @@ def uv( *args, &block )
out << "\n</notextile>"
end
- buffer[pos..-1] = out
+ concat_erb(out, block.binding)
return
end
end # module UltraVioletHelper
View
2 spec/webby/helpers/capture_helper_spec.rb
@@ -2,7 +2,7 @@
require ::File.join(::File.dirname(__FILE__), %w[.. .. spec_helper])
# ---------------------------------------------------------------------------
-describe CaptureHelper do
+describe Webby::Helpers::CaptureHelper do
CFN = ::File.expand_path(::File.join(::File.dirname(__FILE__), '..', '..', 'capture_for_yaml.txt'))
CLINES = [
"--- ",

0 comments on commit 8b0d76d

Please sign in to comment.
Something went wrong with that request. Please try again.