lib/enhancements.rb in nanoc-1.0.1 vs lib/enhancements.rb in nanoc-1.1

- old
+ new

@@ -9,86 +9,76 @@ require 'yaml' class Array # Ensures that the array contains only one element def ensure_single(a_noun, a_context) - raise "ERROR: expected 1 #{a_noun}, found #{self.size} (#{a_context})" if self.size != 1 + if self.size != 1 + $stderr.puts "ERROR: expected 1 #{a_noun}, found #{self.size} (#{a_context})" unless $quiet == true + exit + end end end -class File - # Reads the contents of the entire file - def self.read_file(a_filename) - content = '' - File.open(a_filename) { |io| content = io.read } - content - end - - # Returns the contents of an entire file interpreted as YAML - def self.read_yaml(a_filename) - YAML::load(self.read_file(a_filename)) || {} - end - +module YAML # Returns the contents of an entire file interpreted as YAML and cleaned - def self.read_clean_yaml(a_filename) - self.read_yaml(a_filename).clean + def self.load_file_and_clean(a_filename) + (YAML.load_file(a_filename) || {}).clean end end class Hash # Converts all keys to symbols, and converts *_at and *_on # keys to Times and Dates, respectively def clean - hash = {} - - self.each_pair do |key, value| + inject({}) do |hash, (key, value)| if key =~ /_on$/ hash[key.to_sym] = Date.parse(value) elsif key =~ /_at$/ hash[key.to_sym] = Time.parse(value) elsif value == 'true' hash[key.to_sym] = true elsif value == 'false' hash[key.to_sym] = false + elsif value == 'none' + hash[key.to_sym] = nil else hash[key.to_sym] = value end + hash end - - hash 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.each do |filter| + def filter(a_filters, a_params={}) + a_filters.inject(self) do |result, filter| case filter when 'markdown' - self.replace(self.markdown) + result.replace(result.markdown) when 'smartypants' - self.replace(self.smartypants) + result.replace(result.smartypants) when 'eruby' - self.replace(self.eruby(a_params[:eruby_context])) + result.replace(result.eruby(a_params[:eruby_context])) end end end # Converts the string to HTML using Markdown. def markdown BlueCloth::new(self).to_html rescue NameError - $stderr.puts 'ERROR: String#markdown failed: BlueCloth not installed' + $stderr.puts 'ERROR: String#markdown failed: BlueCloth not installed' unless $quiet == true exit end # Styles the string as HTML by converting quotes, dashes, ... using RubyPants def smartypants RubyPants::new(self).to_html rescue NameError - $stderr.puts 'ERROR: String#smartypants failed: RubyPants not installed' + $stderr.puts 'ERROR: String#smartypants failed: RubyPants not installed' unless $quiet == true exit end # Converts the string using eRuby. def eruby(a_context={}) @@ -97,27 +87,52 @@ end class FileManager @@stack = [] + 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 self.create_dir(a_name) @@stack.push(a_name) - unless File.directory?(File.join(@@stack)) - puts ' create ' + @@stack.join('/') - FileUtils.mkdir_p(@@stack.join('/')) + path = File.join(@@stack) + unless File.directory?(path) + FileUtils.mkdir_p(path) + log('create', path) end yield if block_given? @@stack.pop end def self.create_file(a_name) - path = @@stack.empty? ? a_name : @@stack.join('/') + '/' + a_name + path = @@stack.empty? ? a_name : File.join(@@stack + [ a_name ]) FileManager.create_dir(path.sub(/\/[^\/]+$/, '')) if @@stack.empty? - puts " #{File.exist?(a_name) ? 'update' : 'create'} " + path - if block_given? - open(path, 'w') { |io| io.write(yield) } - else - open(path, 'w') { |io| } - end + content = block_given? ? yield : nil + File.exist?(path) ? ( block_given? and File.read(path) == content ? log('identical', path) : log('update', path) ) : log('create', path) + open(path, 'w') { |io| io.write(content) unless content.nil? } end + def self.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 +end + +def render(a_name, a_context={}) + File.read('layouts/' + a_name + '.erb').eruby(a_context.merge({ :page => @page, :pages => @pages })) end