Sha256: a18e0d76201fd92f76945b1576da2007cb83d930f816d588101af756ee3bf010
Contents?: true
Size: 1.87 KB
Versions: 12
Compression:
Stored size: 1.87 KB
Contents
## # Wrapper for the Pygments command line tool, pygmentize. # # Pygments: http://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('/some/file.rb', :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('/some/file.rb', :ruby, :bbcode) # puts @syntaxer.colorize # # You can also use the #colorize class method: # # puts Albino.colorize('/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 # require 'open4' class Albino @@bin = '/usr/local/bin/pygmentize' def self.bin=(path) @@bin = path end def self.colorize(*args) new(*args).colorize end def initialize(target, lexer = :ruby, format = :html) @target = File.exists?(target) ? File.read(target) : target rescue target @options = { :l => lexer, :f => format } end def execute(command) pid, stdin, stdout, stderr = Open4.popen4(command) stdin.puts @target stdin.close stdout.read.strip end def colorize(options = {}) execute @@bin + convert_options(options) end alias_method :to_s, :colorize def convert_options(options = {}) @options.merge(options).inject('') do |string, (flag, value)| string + " -#{flag} #{value}" if value string end end end
Version data entries
12 entries across 12 versions & 4 rubygems