Permalink
Browse files

Fixed instance variables.

  • Loading branch information...
1 parent 524babd commit 53ded07e8d36642d9475bad6aa91fdd4b9c6a55e Florian Aßmann committed Aug 11, 2011
Showing with 47 additions and 13 deletions.
  1. +23 −10 lib/guard/slim.rb
  2. +1 −3 lib/guard/slim/templates/Guardfile
  3. +14 −0 test/Guardfile
  4. +9 −0 test/views/index.slim
View
@@ -1,46 +1,59 @@
require 'guard/guard'
-require 'guard/watchers'
+require 'guard/watcher'
require 'slim'
+require 'fileutils'
module Guard
class Slim < Guard
ALL = File.join '**', '*'
Template = ::Slim::Template
def initialize(watchers = [], options = {})
- @root = options.delete :root
- @locals = options.delete(:locals) || {}
- @options = options.delete(:slim) || {}
+ @output_root = options.delete(:output_root) || Dir.getwd
+ @input_root = options.delete(:input_root) || Dir.getwd
+ @context = options.delete(:context) || Object.new
+ @slim = options.delete(:slim) || {}
super watchers, options
end
+ def start
+ UI.info 'Guard-Slim is waiting for changes...'
+ end
+
def run_all
run_on_change all_paths
end
def run_on_change(paths)
paths.each do |path|
content = render File.read(path)
- open(build_path(path), 'w') { |file| file << content }
+ open(build_path(path), 'w') do |file|
+ @slim[:pretty] ?
+ file.puts(content) :
+ file.write(content)
+ end
+ UI.info "guard-slim: rendered #{ path } to #{ build_path path }"
end
end
protected
def build_path(path)
+ path = File.expand_path(path).sub @input_root, @output_root
dirname = File.dirname path
+
+ FileUtils.mkpath dirname unless File.directory? dirname
+
basename = File.basename path, '.slim'
basename << '.html' if File.extname(basename).empty?
- path = File.join dirname, path
-
- @root ? File.join(@root, dirname, basename) : File.join(dirname, basename)
+ File.join dirname, basename
end
def render(source)
- Template.new( @options ) { source }.render( @locals )
+ Template.new( @slim ) { source }.render( @context )
end
def all_paths
- Watchers.match_files self, Dir[ ALL ]
+ Watcher.match_files self, Dir[ ALL ]
end
end
@@ -1,5 +1,3 @@
-options = { :root => Dir.getwd, :locals => {}, :slim => {:pretty => true} }
-
-guard 'slim', options do
+guard 'slim',:slim => { :pretty => true } do
watch(/^.+\.html\.slim/)
end
View
@@ -0,0 +1,14 @@
+Context = Struct.new :title
+
+options = {
+ :input_root => File.expand_path('../views', __FILE__),
+ :output_root => File.expand_path('../public', __FILE__),
+ :context => Context.new('Guard-Slim'),
+ :slim => { :pretty => true }
+}
+
+require File.expand_path('../../lib/guard/slim', __FILE__)
+
+guard 'slim', options do
+ watch(%r'^views/.+\.slim$')
+end
View
@@ -0,0 +1,9 @@
+doctype html
+
+title= title
+meta charset="utf-8"
+
+h1= title
+
+p.content
+ | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0 comments on commit 53ded07

Please sign in to comment.