class Termtter::Plugins::Haml

Public Class Methods

new(config = Termtter::Config.instance, logger = Termtter::Client.logger) click to toggle source
# File lib/plugins/haml.rb, line 8
def initialize(config = Termtter::Config.instance, logger = Termtter::Client.logger)
  @config, @logger = config, logger

  plugin_config.set_default :options, {}
end

Public Instance Methods

editor(extname) click to toggle source
# File lib/plugins/haml.rb, line 39
def editor(extname)
  unless cmd = ENV['VISUAL'] || ENV['EDITOR']
    raise 'Please set VISUAL or EDITOR variable.'
  end

  # XXX: works only in Ruby 1.8.7 or later
  Tempfile.open(['tmp', ".#{extname}"]) do |f|
    system cmd, f.path
    return f.read
  end
end
haml(format) click to toggle source
# File lib/plugins/haml.rb, line 32
def haml(format)
  return unless input = editor(:haml)

  opts = plugin_config.options.merge(format.empty? ? {} : {:format => format.to_sym})
  ::Haml::Engine.new(input, opts).render.chomp
end
plugin_config() click to toggle source
# File lib/plugins/haml.rb, line 14
def plugin_config
  @config.plugins.haml
end
run(arg) click to toggle source
# File lib/plugins/haml.rb, line 18
def run(arg)
  begin
    hamlified = haml(arg)
  rescue => e
    @logger.error e
    return
  end

  return if hamlified.nil? || hamlified.empty?

  Termtter::API.twitter.update(hamlified)
  puts "=> #{hamlified}"
end