Skip to content

Commit

Permalink
Fixed instance variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Aßmann committed Aug 11, 2011
1 parent 524babd commit 53ded07
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
33 changes: 23 additions & 10 deletions lib/guard/slim.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/guard/slim/templates/Guardfile
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/Guardfile
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions test/views/index.slim
Original file line number Diff line number Diff line change
@@ -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.