Class: Termtter::Plugins::Haml

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/haml.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = Termtter::Config.instance, logger = Termtter::Client.logger) ⇒ Haml

Returns a new instance of Haml.



8
9
10
11
12
# 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

Instance Method Details

#editor(extname) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



32
33
34
35
36
37
# 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_configObject



14
15
16
# File 'lib/plugins/haml.rb', line 14

def plugin_config
  @config.plugins.haml
end

#run(arg) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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