Class: Albino

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/albino.rb

Constant Summary collapse

@@bin =
Rails.development? ? 'pygmentize' : '/usr/bin/pygmentize' rescue 'pygmentize'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, lexer = :text, format = :html) ⇒ Albino

Returns a new instance of Albino.



57
58
59
60
# File 'lib/jekyll/albino.rb', line 57

def initialize(target, lexer = :text, format = :html)
  @target  = File.exists?(target) ? File.read(target) : target rescue target
  @options = { :l => lexer, :f => format }
end

Class Method Details

.bin=(path) ⇒ Object



49
50
51
# File 'lib/jekyll/albino.rb', line 49

def self.bin=(path)
  @@bin = path
end

.colorize(*args) ⇒ Object



53
54
55
# File 'lib/jekyll/albino.rb', line 53

def self.colorize(*args)
  new(*args).colorize
end

Instance Method Details

#colorize(options = {}) ⇒ Object Also known as: to_s



72
73
74
# File 'lib/jekyll/albino.rb', line 72

def colorize(options = {})
  execute @@bin + convert_options(options)
end

#convert_options(options = {}) ⇒ Object



77
78
79
80
81
# File 'lib/jekyll/albino.rb', line 77

def convert_options(options = {})
  @options.merge(options).inject('') do |string, (flag, value)|
    string + " -#{flag} #{value}"
  end
end

#execute(command) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/jekyll/albino.rb', line 62

def execute(command)
  output = ''
  Open4.popen4(command) do |pid, stdin, stdout, stderr|
    stdin.puts @target
    stdin.close
    output = stdout.read.strip
  end
  output
end