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, :O => 'encoding=utf-8' }
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



73
74
75
76
77
# File 'lib/jekyll/albino.rb', line 73

def colorize(options = {})
  html = execute(@@bin + convert_options(options))
  # Work around an RDiscount bug: http://gist.github.com/97682
  html.to_s.sub(%r{</pre></div>\Z}, "</pre>\n</div>")
end

#convert_options(options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/jekyll/albino.rb', line 80

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
71
# 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
    [stdout, stderr].each { |io| io.close }
  end
  output
end