lib/enhancements.rb in nanoc-1.1.3 vs lib/enhancements.rb in nanoc-1.2

- old
+ new

@@ -1,20 +1,26 @@ def try_require(s) ; begin ; require s ; rescue LoadError ; end ; end try_require 'rubygems' -try_require 'bluecloth' -try_require 'rubypants' require 'erubis' require 'fileutils' require 'yaml' +try_require 'bluecloth' +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' + 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 == true + $stderr.puts "ERROR: expected 1 #{a_noun}, found #{self.size} (#{a_context})" unless $quiet exit end end end @@ -44,95 +50,173 @@ hash[key.to_sym] = value end hash end end + + def stringify_keys + inject({}) do |hash, (key, value)| + hash[key.to_s] = value + hash + end + 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={}) a_filters.inject(self) do |result, filter| case filter + when 'eruby' + result.replace(result.eruby(a_params[:assigns])) + when 'haml' + result.replace(result.haml(a_params[:assigns])) + when 'liquid' + result.replace(result.liquid(a_params[:assigns])) + when 'markaby' + result.replace(result.markaby(a_params[:assigns])) when 'markdown' result.replace(result.markdown) + when 'rdoc' + result.replace(result.rdoc) when 'smartypants' result.replace(result.smartypants) - when 'eruby' - result.replace(result.eruby(a_params[:eruby_context])) end end end - # Converts the string to HTML using Markdown. + # Converts the string using eRuby. + def eruby(a_assigns={}) + Erubis::Eruby.new(self).evaluate(a_assigns) + end + + # Converts the string using Haml + def haml(a_assigns={}) + Haml::Engine.new(self, :locals => a_assigns).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) + 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 + rescue NameError + $stderr.puts 'ERROR: String#markaby failed (Markaby not installed?)' unless $quiet + exit + end + + # Converts the string to HTML using BlueCloth/Markdown. def markdown - BlueCloth::new(self).to_html + BlueCloth.new(self).to_html rescue NameError - $stderr.puts 'ERROR: String#markdown failed: BlueCloth not installed' unless $quiet == true + $stderr.puts 'ERROR: String#markdown failed: BlueCloth not installed' unless $quiet exit end - # Styles the string as HTML by converting quotes, dashes, ... using RubyPants + # Converts the string using RDoc + def rdoc + SM::SimpleMarkup.new.convert(self, SM::ToHtml.new) + end + + # Converts the string using RedCloth/Textile + def textile + RedCloth.new(self).to_html + rescue NameError + $stderr.puts 'ERROR: String#textile failed (RedCloth not installed?)' unless $quiet + exit + end + + # Converts the string using RubyPants/SmartyPants def smartypants - RubyPants::new(self).to_html + RubyPants.new(self).to_html rescue NameError - $stderr.puts 'ERROR: String#smartypants failed: RubyPants not installed' unless $quiet == true + $stderr.puts 'ERROR: String#smartypants failed (RubyPants not installed?)' unless $quiet exit end +end - # Converts the string using eRuby. - def eruby(a_context={}) - Erubis::Eruby.new(self).evaluate(a_context) +class FileLogger + COLORS = { + :reset => "\e[0m", + + :bold => "\e[1m", + + :black => "\e[30m", + :red => "\e[31m", + :green => "\e[32m", + :yellow => "\e[33m", + :blue => "\e[34m", + :magenta => "\e[35m", + :cyan => "\e[36m", + :white => "\e[37m" + } + + ACTION_COLORS = { + :create => COLORS[:bold] + COLORS[:green], + :update => COLORS[:bold] + COLORS[:yellow], + :move => COLORS[:bold] + COLORS[:blue], + :identical => COLORS[:bold] + } + + attr_reader :out + + def initialize(a_out = $stdout) + @out = a_out end + + def log(a_action, a_path) + @out.puts('%s%12s%s %s' % [ACTION_COLORS[a_action.to_sym], a_action, COLORS[:reset], a_path]) unless $quiet + end + + private + + def method_missing(a_method, *a_args) + log(a_method.to_s, a_args.first) + end end class FileManager @@stack = [] + @@logger = FileLogger.new def self.create_dir(a_name) @@stack.push(a_name) path = File.join(@@stack) unless File.directory?(path) FileUtils.mkdir_p(path) - log('create', path) + @@logger.create(path) end yield if block_given? @@stack.pop end def self.create_file(a_name) path = File.join(@@stack + [ a_name ]) FileManager.create_dir(path.sub(/\/[^\/]+$/, '')) if @@stack.empty? content = block_given? ? yield : nil - File.exist?(path) ? ( block_given? and File.read(path) == content ? log('identical', path) : log('update', path) ) : log('create', path) + if File.exist?(path) + if block_given? and File.read(path) == content + @@logger.identical(path) + else + @@logger.update(path) + end + else + @@logger.create(path) + end open(path, 'w') { |io| io.write(content) unless content.nil? } end end -COLORS = { - :reset => "\e[0m", - - :bold => "\e[1m", - - :black => "\e[30m", - :red => "\e[31m", - :green => "\e[32m", - :yellow => "\e[33m", - :blue => "\e[34m", - :magenta => "\e[35m", - :cyan => "\e[36m", - :white => "\e[37m" -} -ACTION_COLORS = { - :create => COLORS[:bold] + COLORS[:green], - :update => COLORS[:bold] + COLORS[:yellow], - :identical => COLORS[:bold] -} - -def log(a_action, a_path) - puts format('%s%12s%s %s', ACTION_COLORS[a_action.to_sym], a_action, COLORS[:reset], a_path) unless $quiet == true -end - def render(a_name, a_context={}) - File.read('layouts/' + a_name + '.erb').eruby(a_context.merge({ :page => @page, :pages => @pages })) + File.read('layouts/' + a_name.to_s + '.erb').eruby(a_context.merge({ :page => @page, :pages => @pages })) end