guard-rocco/lib/guard/rocco.rb

65 lines
1.4 KiB
Ruby
Raw Normal View History

2011-06-17 20:01:49 +00:00
require 'rocco'
2011-10-02 14:07:15 +00:00
require 'guard'
2011-06-17 20:01:49 +00:00
require 'guard/guard'
require 'guard/watcher'
2011-06-18 19:33:21 +00:00
require 'fileutils'
2011-06-17 20:01:49 +00:00
module Guard
class Rocco < Guard
def initialize(watchers = [], options = {})
super
@options = options || {}
@dir = @options[:dir] || 'doc'
@run_on = @options[:run_on] || [:start, :change]
@run_on = [@run_on] unless @run_on.respond_to?(:include?)
2011-06-17 20:01:49 +00:00
end
def start
all_paths.each { |path| build(path) } if run_for? :start
end
def reload
all_paths.each { |path| build(path) } if run_for? :reload
2011-06-17 20:01:49 +00:00
end
def run_all
all_paths.each { |path| build(path) } if run_for? :all
2011-06-17 20:01:49 +00:00
end
def run_on_change(paths = [])
paths.each { |path| build(path) } if run_for? :change
2011-06-17 20:01:49 +00:00
end
private
2011-06-18 16:33:23 +00:00
def build(path, target = nil)
2011-06-18 19:33:21 +00:00
target ||= filename_to_target(path)
2011-06-17 20:01:49 +00:00
puts "rocco: #{path} -> #{target}"
2011-06-18 19:33:21 +00:00
FileUtils.mkdir_p File.split(target).first
2011-10-01 05:04:45 +00:00
File.open(target, 'wb') do |fh|
fh.print ::Rocco.new(path, all_paths, rocco_options).to_html
end
2011-06-17 20:01:49 +00:00
end
def all_paths
Watcher.match_files(self, Dir['**/*'])
end
2011-06-18 16:33:23 +00:00
2011-06-18 19:33:21 +00:00
def filename_to_target(path)
File.join(@dir, path).gsub(%r{\.[^\.]+$}, '.html')
2011-06-18 16:33:23 +00:00
end
2011-10-01 05:04:45 +00:00
def rocco_options
2011-10-02 13:47:34 +00:00
opts = @options.dup
opts.delete(:dir)
opts.delete(:run_on)
2011-10-02 13:47:34 +00:00
opts
2011-10-01 05:04:45 +00:00
end
def run_for? command
@run_on.include?(command)
end
2011-06-17 20:01:49 +00:00
end
end