Sha256: 592bf89b27478ba87d8c29a3d5b147b904a7d13ca5add3923405d22dcc6a2674

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

require "sdoc"

module SDoc::Templatable
	### Load and render the erb template in the given +templatefile+ within the
	### specified +context+ (a Binding object) and return output
	### Both +templatefile+ and +outfile+ should be Pathname-like objects.
  def eval_template(templatefile, context)
		template_src = templatefile.read
		template = ERB.new( template_src, nil, '<>' )
		template.filename = templatefile.to_s

    begin
      template.result( context )
    rescue NoMethodError => err
      raise RDoc::Error, "Error while evaluating %s: %s (at %p)" % [
        templatefile.to_s,
        err.message,
        eval( "_erbout[-50,50]", context )
        ], err.backtrace
      end
  end
  
  ### Load and render the erb template with the given +template_name+ within
  ### current context. Adds all +local_assigns+ to context
  def include_template(template_name, local_assigns = {})
    source = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join
    eval("#{source};templatefile = @template_dir + template_name;eval_template(templatefile, binding)")
  end
  
	### Load and render the erb template in the given +templatefile+ within the
	### specified +context+ (a Binding object) and write it out to +outfile+.
	### Both +templatefile+ and +outfile+ should be Pathname-like objects.
	def render_template( templatefile, context, outfile )
    output = eval_template(templatefile, context)
		unless $dryrun
			outfile.dirname.mkpath
			outfile.open( 'w', 0644 ) do |file|
				file.print( output )
			end
		else
			debug_msg "  would have written %d bytes to %s" %
			[ output.length, outfile ]
		end
	end  
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
voloko-sdoc-0.2.0 lib/sdoc/templatable.rb
voloko-sdoc-0.2.1 lib/sdoc/templatable.rb
voloko-sdoc-0.2.2 lib/sdoc/templatable.rb
voloko-sdoc-0.2.3 lib/sdoc/templatable.rb
voloko-sdoc-0.2.4 lib/sdoc/templatable.rb
voloko-sdoc-0.2.5 lib/sdoc/templatable.rb
voloko-sdoc-0.2.6 lib/sdoc/templatable.rb
voloko-sdoc-0.2.7 lib/sdoc/templatable.rb
voloko-sdoc-0.2.8 lib/sdoc/templatable.rb
voloko-sdoc-0.2.9 lib/sdoc/templatable.rb