Class: Albino

Inherits:
Object
  • Object
show all
Includes:
POSIX::Spawn
Defined in:
lib/albino.rb,
lib/albino/multi.rb

Overview

Wrapper for the Pygments command line tool, pygmentize.

Pygments: pygments.org/

Assumes pygmentize is in the path. If not, set its location with Albino.bin = ‘/path/to/pygmentize’

Use like so:

@syntaxer = Albino.new('puts "Hello World"', :ruby)
puts @syntaxer.colorize

This’ll print out an HTMLized, Ruby-highlighted version of ‘/some/file.rb’.

To use another formatter, pass it as the third argument:

@syntaxer = Albino.new('puts "Hello World"', :ruby, :bbcode)
puts @syntaxer.colorize

You can also use the #colorize class method:

puts Albino.colorize('puts "Hello World"', :ruby)

To format a file, pass a file stream:

puts Albino.colorize(File.new('/some/file.rb'), :ruby)

Another also: you get a #to_s, for somewhat nicer use in Rails views.

... helper file ...
def highlight(text)
  Albino.new(text, :ruby)
end

... view file ...
<%= highlight text %>

The default lexer is ‘text’. You need to specify a lexer yourself; because we are using STDIN there is no auto-detect.

To see all lexers and formatters available, run ‘pygmentize -L`.

Chris Wanstrath // [email protected]

GitHub // http://github.com

Defined Under Namespace

Classes: Multi, ShellArgumentError

Constant Summary collapse

VERSION =
'1.3.3'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding) ⇒ Albino

Returns a new instance of Albino.



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

def initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding)
  @target  = target
  @options = { :l => lexer, :f => format, :O => "encoding=#{encoding}" }
  @encoding = encoding
end

Class Attribute Details

.binObject

Returns the value of attribute bin.



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

def bin
  @bin
end

.default_encodingObject

Returns the value of attribute default_encoding.



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

def default_encoding
  @default_encoding
end

.timeout_thresholdObject

Returns the value of attribute timeout_threshold.



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

def timeout_threshold
  @timeout_threshold
end

Class Method Details

.colorize(*args) ⇒ Object



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

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

Instance Method Details

#binObject



129
130
131
# File 'lib/albino.rb', line 129

def bin
  self.class.bin
end

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



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/albino.rb', line 90

def colorize(options = {})
  out = execute(options).out

  # markdown requires block elements on their own line
  out.sub!(%r{</pre></div>\Z}, "</pre>\n</div>")

  # covert output to the encoding we told pygmentize to use
  out.force_encoding(@encoding) if out.respond_to?(:force_encoding)

  out
end

#convert_options(options = {}) ⇒ Object



103
104
105
106
107
108
# File 'lib/albino.rb', line 103

def convert_options(options = {})
  @options.merge(options).inject([]) do |memo, (flag, value)|
    validate_shell_args(flag.to_s, value.to_s)
    memo << "-#{flag}" << value.to_s
  end
end

#execute(options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/albino.rb', line 82

def execute(options = {})
  proc_options = {}
  proc_options[:timeout] = options.delete(:timeout) || self.class.timeout_threshold
  command = convert_options(options)
  command.unshift(bin)
  Child.new(*(command + [proc_options.merge(:input => write_target)]))
end

#validate_shell_args(flag, value) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/albino.rb', line 120

def validate_shell_args(flag, value)
  if flag !~ /^[a-z]+$/i
    raise ShellArgumentError, "Flag is invalid: #{flag.inspect}"
  end
  if value !~ /^[a-z0-9\-\_\+\=\#\,\s]+$/i
    raise ShellArgumentError, "Flag value is invalid: -#{flag} #{value.inspect}"
  end
end

#write_targetObject



110
111
112
113
114
115
116
117
118
# File 'lib/albino.rb', line 110

def write_target
  if @target.respond_to?(:read)
    out = @target.read
    @target.close
    out
  else
    @target.to_s
  end
end