require 'redirect-to-filter' require 'version' module NanocRedirector module RedirectFrom def self.process(item, dest, config = {}) return if item[:redirect_from].nil? return if dest.nil? redirect_hash = {} output_dir = config[:output_dir] || 'output' index_filenames = config[:index_filenames] || ['index.html'] key = item.identifier.without_ext value = item[:redirect_from].is_a?(String) ? [item[:redirect_from]] : item[:redirect_from] redirect_hash[key] = value redirect_hash.values.each do |redirects| redirects.each do |redirect| content = NanocRedirector.redirect_template(dest) dir = File.join(output_dir, redirect) index_filenames.each do |index_filename| redirect_path = File.join(dir, index_filename) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.write(redirect_path, content) unless File.exist? redirect_path end end end end end def self.redirect_template(item_url) <<-EOF