class EmojiJS::Generate

Public Class Methods

new(project_path = Dir.pwd) click to toggle source
# File lib/emoji-js.rb, line 8
def initialize(project_path = Dir.pwd)
  @project_path = project_path
  @path = File.dirname File.expand_path(__FILE__)
  @vendor_path = "#{@path}/vendor"
end

Public Instance Methods

coffeescript() click to toggle source

takes CoffeeScript from ‘vendor/coffee/emoji.coffee’ and saves the corresponding JS to a variable

# File lib/emoji-js.rb, line 16
def coffeescript
  @coffee_path = "#{@vendor_path}/coffee"

  # get image path (we have to do it here because of coffeescript generation)
  print 'Emoji graphics path: (default: "graphics/") '
  @emoji_path = gets.chomp
  @emoji_path = "/graphics" if @emoji_path.empty?

  # coffee_grounds = modified coffescript
  @coffee_grounds = replace_in_file("#{@coffee_path}/emoji.coffee", %q{Emoji.image_path = "/graphics/" # customize to your liking}, "Emoji.image_path = \"#{@emoji_path}\"")

  @generated_js = CoffeeScript.compile(@coffee_grounds)
  @ugly_js = uglify @generated_js

  # get js dir
  print %q{Your project's JavaScript directory: (default: "js/") }
  js_dir = gets.chomp
  js_dir = "js" if js_dir.empty?
  @orig_js_dir = js_dir
  js_dir = "#{@project_path}/#{js_dir}"

  # write emoji.min.js
  FileUtils.mkdir(js_dir) unless Dir.exists? js_dir
  FileUtils.cd(js_dir)
  write_to_file "emoji.min.js", @ugly_js
  FileUtils.cd("../")
end
emoji_images() click to toggle source

copy emoji images to image path

# File lib/emoji-js.rb, line 50
def emoji_images
  @emoji_path = validate_image_path(@emoji_path)
  FileUtils.mkdir(@emoji_path) unless Dir.exists? @emoji_path
  @emojis = Dir.glob("#{@vendor_path}/graphics/*").map(&File.method(:realpath))

  FileUtils.cp(@emojis, @emoji_path)
end
success() click to toggle source

display when successful

# File lib/emoji-js.rb, line 59
def success
  puts "Emoji.JS successfully added to your project."
  print "Now add: '".green
  print "<script src=\"/#{@orig_js_dir}/emoji.min.js\"></script>".blue
  print "' to the END of your HTML file\n".green
end
uglify(js) click to toggle source

takes raw JS and uglifies it

# File lib/emoji-js.rb, line 45
def uglify(js)
  Uglifier.compile(js)
end

Private Instance Methods

replace_in_file(file, search, replace) click to toggle source

replace something for something else in a string

# File lib/emoji-js.rb, line 68
def replace_in_file(file, search, replace)
  string = File.read(file)
  string.sub(search, replace)
end
validate_image_path(path) click to toggle source

make emoji image path valid

# File lib/emoji-js.rb, line 81
def validate_image_path(path)
  path = "/#{path}" unless path[0] == "/"
  path = @project_path + path
  # raise "Not a real path" if File.directory? path
  return path
end
write_to_file(filename, content) click to toggle source

write content to a file

# File lib/emoji-js.rb, line 74
def write_to_file(filename, content)
  File.open(filename, "w") do |file|
    file.write(content)
  end
end