lib/enhancements.rb in nanoc-1.3.1 vs lib/enhancements.rb in nanoc-1.4

- old
+ new

@@ -1,22 +1,32 @@ def try_require(s) ; begin ; require s ; rescue LoadError ; end ; end try_require 'rubygems' -require 'erubis' +require 'erb' require 'fileutils' require 'yaml' try_require 'bluecloth' +try_require 'erubis' try_require 'redcloth' try_require 'rubypants' try_require 'markaby' try_require 'liquid' try_require 'haml' try_require 'rdoc/markup/simple_markup' try_require 'rdoc/markup/simple_markup/to_html' +def handle_exception(exception, text) + unless $quiet or exception.class == SystemExit + $stderr.puts "ERROR: Exception occured while #{text}:\n" + $stderr.puts exception + $stderr.puts exception.backtrace.join("\n") + end + exit +end + class Array # Ensures that the array contains only one element def ensure_single(a_noun, a_context) if self.size != 1 $stderr.puts "ERROR: expected 1 #{a_noun}, found #{self.size} (#{a_context})" unless $quiet @@ -52,32 +62,52 @@ end hash end end + def symbolize_keys + inject({}) do |hash, (key, value)| + hash[key.intern] = value + hash + end + end + def stringify_keys inject({}) do |hash, (key, value)| hash[key.to_s] = value hash end end end +class ERBContext + def initialize(hash) + hash.each_pair do |key, value| + instance_variable_set('@' + key.to_s, value) + end + end + + def get_binding + binding + end +end + class String # Runs the string through the filters as given by the array of # filter names. Available filters include 'markdown', 'smartypants' and 'eruby'. def filter(a_filters, a_params={}) + assigns = a_params[:assigns] a_filters.inject(self) do |result, filter| case filter when 'eruby' - result.replace(result.eruby(a_params[:assigns])) + result.replace(result.eruby(:assigns => assigns)) when 'haml' - result.replace(result.haml(a_params[:assigns])) + result.replace(result.haml(:assigns => assigns)) when 'liquid' - result.replace(result.liquid(a_params[:assigns])) + result.replace(result.liquid(:assigns => assigns)) when 'markaby' - result.replace(result.markaby(a_params[:assigns])) + result.replace(result.markaby(:assigns => assigns)) when 'markdown', 'bluecloth' result.replace(result.markdown) when 'rdoc' result.replace(result.rdoc) when 'sass' @@ -88,35 +118,49 @@ result.replace(result.textile) end end end - # Converts the string using eRuby. - def eruby(a_assigns={}) - Erubis::Eruby.new(self).evaluate(a_assigns) + # Converts the string using eRuby + def eruby(a_params={}) + erubis(a_params) + rescue NameError + erb(a_params) end + # Converts the string using Erubis + def erubis(a_params={}) + Erubis::Eruby.new(self).evaluate(a_params[:assigns] || {}) + end + + # Converts the string using ERB + def erb(a_params={}) + ERB.new(self).result(ERBContext.new(a_params[:assigns] || {}).get_binding) + end + # Converts the string using Haml - def haml(a_assigns={}) - Haml::Engine.new(self, :locals => a_assigns).to_html + def haml(a_params={}) + options = (a_params[:haml_options] || {}) + options[:locals] = a_params[:assigns] unless a_params[:assigns].nil? + Haml::Engine.new(self, options).to_html rescue NameError $stderr.puts 'ERROR: String#haml failed (Haml not installed?)' unless $quiet exit end # Converts the string using Liquid - def liquid(a_assigns={}) - Liquid::Template.parse(self).render(a_assigns.stringify_keys) + def liquid(a_params={}) + Liquid::Template.parse(self).render((a_params[:assigns] || {}).stringify_keys) rescue NameError $stderr.puts 'ERROR: String#liquid failed (Liquid not installed?)' unless $quiet exit end # Converts the string using Markaby # TODO perhaps add support for helpers - def markaby(a_assigns={}) - Markaby::Builder.new(a_assigns).instance_eval(self).to_s + def markaby(a_params={}) + Markaby::Builder.new((a_params[:assigns] || {})).instance_eval(self).to_s rescue NameError $stderr.puts 'ERROR: String#markaby failed (Markaby not installed?)' unless $quiet exit end @@ -226,7 +270,8 @@ open(path, 'w') { |io| io.write(content) unless content.nil? } end end def render(a_name, a_context={}) - File.read('layouts/' + a_name.to_s + '.erb').eruby(a_context.merge({ :page => @page, :pages => @pages })) + assigns = a_context.merge({ :page => @page, :pages => @pages }) + File.read('layouts/' + a_name.to_s + '.erb').eruby(:assigns => assigns) end