guard-rocco/lib/guard/rocco.rb

50 lines
1.0 KiB
Ruby
Raw Normal View History

2011-06-17 20:01:49 +00:00
require 'rocco'
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 = { :dir => 'doc' }.merge(options)
end
def start
UI.info "Guard::Rocco is waiting to build docs..."
end
def run_all
all_paths.each { |path| build(path) }
end
def run_on_change(paths = [])
paths.each { |path| build(path) }
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-06-18 16:33:23 +00:00
File.join(@options[:dir], path).gsub(%r{\.[^\.]+$}, '.html')
end
2011-10-01 05:04:45 +00:00
def rocco_options
@options[:stylesheet] ? {stylesheet: @options[:stylesheet]} : {}
end
2011-06-17 20:01:49 +00:00
end
end