Module: Ink::Highlight

Defined in:
lib/ink/highlight.rb

Overview

Pygments is a nice syntax highlighter for Python. This wrapper will call the pygmentize bin and return a highlighted source code.

If the pygmentize bin is not available in your $PATH, you can set it:

Ink::Highlight.bin = "/other/path/pygmentize"

To use it with Ruby on Rails, you can include it on ApplicationHelper.

module ApplicationHelper
  include Ink::Highlight::Helper
end

Then you can use it on views:

<%= highlight @code, :ruby %>

Constant Summary collapse

FORMATTERS =

Store all available formatters.

[:bbcode, :html, :latex, :rtf, :terminal, :text]
FILENAME_MATCHERS =
{
  :rb => [ "Rakefile", "Capfile", "Gemfile", /\.(rb|rbx|rxml|builder|rjs|rake|rbw|gemspec)$/i ],
  :rhtml => [ /\.rhtml$/i ],
  :erb => [ /\.erb$/i ],
  :js => [ /\.js$/i ],
  "html+php" => [ /\.(phtml|php)$/i ],
  :yaml => [ /\.ya?ml$/i ],
  :xml => [ /\.(xml|xsl|rss|atom|xslt|xsd|wsdl)$/i ],
  :erlang => [ /\.(erl|hrl)$/i ],
  :css => [ /\.css$/i ],
  :cucumber => [ /\.feature$/i ],
  :c => [ /\.(c|h)$/i ],
  :coffeescript => [ /\.coffee$/i ],
  :haml => [ /\.haml$/i ],
  :html => [ /\.x?html?$/i ],
  :sass => [ /\.sass$/i ],
  "objective-c" => [ /\.m$/i ],
  :python => [ /\.(pyw?|sc|SConstruct|SConscript|tac)$/i ],
  :scala => [ /\.scala$/i ],
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.binObject

Set the bin that should be ran. It assumes that the pygmentize bin is available on $PATH.



25
26
27
# File 'lib/ink/highlight.rb', line 25

def bin
  @bin
end

Class Method Details

.execute(command) ⇒ Object

:nodoc:



110
111
112
113
114
# File 'lib/ink/highlight.rb', line 110

def self.execute(command) # :nodoc:
  stdin, stdout, stderr = Open3.popen3(command)
  stdin.close
  stdout.read.chomp
end

.guess(file) ⇒ Object

Try to guess what’s the file’s language by checking its content. If no language is detected, returns :text.

Ink::Highlight.guess("Rakefile")
#=> "rb"


62
63
64
# File 'lib/ink/highlight.rb', line 62

def self.guess(file)
  execute("#{bin} -N #{file.inspect}").to_sym
end

.guess_by_filename(file) ⇒ Object

Try to guess what’s the file’s language by checking its filename. If no language is detected, returns :text.

Ink::Highlight.guess_by_filename("Rakefile")
#=> "rb"


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ink/highlight.rb', line 72

def self.guess_by_filename(file)
  basename = File.basename(file)

  FILENAME_MATCHERS.each do |lexer, matchers|
    matchers.each do |matcher|
      case matcher
      when String
        return lexer.to_s if matcher == basename
      when Regexp
        return lexer.to_s if basename =~ matcher
      end
    end
  end

  "text"
end

.highlight(file, options = {}) ⇒ Object

Return the highlighted code for the specified file. You can provide the language (try to guess by default) and format (defaults to :html). The available formatters are stored in Highlight::FORMATTERS. The available languages can be found by running pygmentize -L lexers.

Ink::Highlight.highlight("Rakefile")
Ink::Highlight.highlight("Rakefile", :format => :terminal)
Ink::Highlight.highlight("Rakefile", :language => "rb")
Ink::Highlight.highlight("Rakefile", :guess => :filename)
Ink::Highlight.highlight("Rakefile", :guess => :content)
Ink::Highlight.highlight("Rakefile", :theme => :colorful)


101
102
103
104
105
106
107
108
# File 'lib/ink/highlight.rb', line 101

def self.highlight(file, options = {})
  options = {
    :language => (options[:guess] == :filename ? guess_by_filename(file) : guess(file)),
    :format => :html
  }.merge(options)

  execute("#{bin} -f #{options[:format]} -O nowrap=true -l #{options[:language]} #{file.inspect}")
end