Class: Guard::Haml2Erb

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/haml2erb.rb

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Haml2Erb

Returns a new instance of Haml2Erb.



9
10
11
12
13
14
# File 'lib/guard/haml2erb.rb', line 9

def initialize(watchers = [], options = {})
  super(watchers, {
    :notifications => true      
  }.merge(options))
  @watchers, @options = watchers, options
end

Instance Method Details

#convert_haml(file) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/guard/haml2erb.rb', line 16

def convert_haml file
  content = File.new(file).read
  begin
    ::Haml2Erb.convert(content)
  rescue StandardError => error
    ::Guard::UI.info "HAML2ERB Error: " + error.message
  end
end

#get_output(file) ⇒ String

Get the file path to output the html based on the file being built. The output path is relative to where guard is being run.

Parameters:

  • file (String)

    path to file being built

Returns:

  • (String)

    path to file where output should be written



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/guard/haml2erb.rb', line 31

def get_output(file)
  file_dir = File.dirname(file)
  file_name = (File.basename(file).split('.')[0..-2] << 'erb').join('.')
  
  file_dir = file_dir.gsub(Regexp.new("#{@options[:input]}(\/){0,1}"), '') if @options[:input]
  file_dir = File.join(@options[:output], file_dir) if @options[:output]

  if file_dir == ''
    file_name
  else
    File.join(file_dir, file_name)
  end
end

#notify(changed_files) ⇒ Object



60
61
62
63
64
65
# File 'lib/guard/haml2erb.rb', line 60

def notify(changed_files)
  ::Guard.guards.reject{ |guard| guard == self }.each do |guard|
    paths = Watcher.match_files(guard, changed_files)
    guard.run_on_change paths unless paths.empty?
  end
end

#run_allObject



45
46
47
# File 'lib/guard/haml2erb.rb', line 45

def run_all
  run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.*'))))
end

#run_on_change(paths) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/guard/haml2erb.rb', line 49

def run_on_change(paths)
  paths.each do |file|
    output_file = get_output(file)
    FileUtils.mkdir_p File.dirname(output_file)
    File.open(output_file, 'w') { |f| f.write(convert_haml(file)) }
    ::Guard::UI.info "# converted haml in '#{file}' to erb in '#{output_file}'"
    ::Guard::Notifier.notify("# converted haml in #{file}", :title => "Guard::Haml", :image => :success) if @options[:notifications]
  end
  notify paths
end