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'
|
2011-06-18 22:20:49 +00:00
|
|
|
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
|
|
|
|
|
2011-11-13 13:07:19 +00:00
|
|
|
@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
|
2011-11-13 13:07:19 +00:00
|
|
|
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
|
2011-11-13 13:07:19 +00:00
|
|
|
all_paths.each { |path| build(path) } if run_for? :all
|
2011-06-17 20:01:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_change(paths = [])
|
2011-11-13 13:07:19 +00:00
|
|
|
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)
|
2011-11-13 13:07:19 +00:00
|
|
|
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)
|
2011-11-13 13:07:19 +00:00
|
|
|
opts.delete(:run_on)
|
2011-10-02 13:47:34 +00:00
|
|
|
opts
|
2011-10-01 05:04:45 +00:00
|
|
|
end
|
2011-11-13 13:07:19 +00:00
|
|
|
|
|
|
|
def run_for? command
|
|
|
|
@run_on.include?(command)
|
|
|
|
end
|
2011-06-17 20:01:49 +00:00
|
|
|
end
|
|
|
|
end
|