Module: Erb2hamlCli

Defined in:
lib/erb2haml_cli.rb,
lib/erb2haml_cli/version.rb

Overview

Taken and modified from github.com/dhl/erb2haml

Constant Summary collapse

RED_FG =
"\033[31m"
GREEN_FG =
"\033[32m"
END_TEXT_STYLE =
"\033[0m"
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.color(text, begin_text_style) ⇒ Object

Helper method to inject ASCII escape sequences for colorized output



12
13
14
# File 'lib/erb2haml_cli.rb', line 12

def self.color(text, begin_text_style)
  begin_text_style + text + END_TEXT_STYLE
end

.run(argv = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/erb2haml_cli.rb', line 16

def self.run(argv=[])
  if !argv.grep('--help').first.nil? or !argv.grep('-h').first.nil?
    return usage
  end

  if `which html2haml`.empty?
    puts "#{color "ERROR: ", RED_FG} Could not find " +
       "#{color "html2haml", GREEN_FG} in your PATH. Aborting."
  end

  dir = argv.first || "."
  unless FileTest.directory?(dir)
    $stderr.puts "#{dir} is not a directory"
    exit 1
  end

  puts "Looking for #{color "ERB", GREEN_FG} files to convert to " +
    "#{color("Haml", RED_FG)}..."

  Find.find(dir) do |path|
    if FileTest.file?(path) and path.downcase.match(/\.erb$/i)
      haml_path = path.gsub(".erb", ".haml")

      unless FileTest.exists?(haml_path)
        print "Converting: #{path}... "

        if system("html2haml", "--erb", "--unix-newlines", path, haml_path)
          puts color("Done!", GREEN_FG)
        else
          puts color("Failed!", RED_FG)
        end
      end
    end
  end
end

.usageObject

self.run



52
53
54
55
56
57
# File 'lib/erb2haml_cli.rb', line 52

def self.usage
  puts <<-DOC
#{$0} [PATH]
Convert all *.erb files in directory PATH. Using current working directory if not set.
DOC
end