class Albino

Wrapper for the Pygments command line tool, pygmentize.

Pygments: pygments.org/

Assumes pygmentize is in the path. If not, set its location with #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 // chris@ozmm.org

GitHub // http://github.com

Constants

VERSION

Attributes

bin[RW]
default_encoding[R]
timeout_threshold[RW]

Public Class Methods

colorize(*args) click to toggle source
# File lib/albino.rb, line 72
def self.colorize(*args)
  new(*args).colorize
end
default_encoding=(encoding) click to toggle source
# File lib/albino.rb, line 60
def default_encoding=(encoding)
  # make sure the encoding is valid
  Encoding.find(encoding) if defined?(Encoding)

  @default_encoding = encoding
end
new(target, lexer = :text, format = :html, encoding = self.class.default_encoding) click to toggle source
# 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

Public Instance Methods

bin() click to toggle source
# File lib/albino.rb, line 129
def bin
  self.class.bin
end
colorize(options = {}) click to toggle source
# 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
Also aliased as: to_s
convert_options(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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
to_s(options = {})
Alias for: colorize
validate_shell_args(flag, value) click to toggle source
# 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_target() click to toggle source
# 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