Module: Simplabs::Highlight

Defined in:
lib/simplabs/highlight.rb,
lib/simplabs/highlight/railtie.rb,
lib/simplabs/highlight/pygments_wrapper.rb

Overview

Highlight is a simple syntax highlighting plugin for Ruby on Rails. It’s basically a wrapper around the popular pygments.org highlighter that’s written in Python and supports an impressive number of languages. If pygments is installed on the machine and in the PATH, that binary is used, otherwise the plugin falls back to the web API at (pygments.simplabs.com/), created by Trevor Turk.

Supported Languages

The following languages are supported. All of the paranthesized identifiers may be used as parameters for the highlight method to denote the language the source code to highlight is written in (use either Symbols or Strings).

  • Actionscript (as, as3, actionscript)

  • Applescript (applescript)

  • bash (bash, sh)

  • C (c, h)

  • Clojure (clojure)

  • CoffeeScript (coffee)

  • C++ (+c+++, cpp, hpp)

  • C# (c#, csharp, cs)

  • CSS (css)

  • diff (diff)

  • Dylan (dylan)

  • Erlang (erlang, erl, er)

  • HTML (html, htm)

  • Java (java)

  • JavaScript (javascript, js, jscript)

  • JSP (jsp)

  • Make (make, basemake, makefile)

  • NASM (nasm, asm)

  • Objective-C (objective-c)

  • OCaml (ocaml)

  • Perl (perl, pl)

  • PHP (php)

  • Python (python, py)

  • RHTML (erb, rhtml)

  • Ruby (ruby, rb)

  • Scala (scala)

  • Scheme (scheme)

  • Smalltalk (smalltalk)

  • Smarty (smarty)

  • SQL (sql)

  • XML (xml, xsd)

  • XSLT (xslt)

  • YAML (yaml, yml)

Defined Under Namespace

Modules: ViewMethods Classes: PygmentsWrapper, Railtie

Constant Summary collapse

SUPPORTED_LANGUAGES =
{
  :as            => ['as', 'as3', 'actionscript'],
  :applescript   => ['applescript'],
  :bash          => ['bash', 'sh'],
  :c             => ['c', 'h'],
  :clojure       => ['clojure'],
  :coffeescript  => ['coffeescript', 'coffee'],
  :cpp           => ['c++', 'cpp', 'hpp'],
  :csharp        => ['c#', 'csharp', 'cs'],
  :css           => ['css'],
  :diff          => ['diff'],
  :dylan         => ['dylan'],
  :erlang        => ['erlang'],
  :html          => ['html', 'htm'],
  :java          => ['java'],
  :js            => ['javascript', 'js', 'jscript'],
  :jsp           => ['jsp'],
  :make          => ['make', 'basemake', 'makefile'],
  :nasm          => ['nasm', 'asm'],
  :'objective-c' => ['objective-c'],
  :ocaml         => ['ocaml'],
  :perl          => ['perl', 'pl'],
  :php           => ['php'],
  :python        => ['python', 'py'],
  :rhtml         => ['erb', 'rhtml'],
  :ruby          => ['ruby', 'rb'],
  :scala         => ['scala'],
  :scheme        => ['scheme'],
  :smallralk     => ['smalltalk'],
  :smarty        => ['smarty'],
  :sql           => ['sql', 'mysql'],
  :xml           => ['xml', 'xsd'],
  :xslt          => ['xslt'],
  :yaml          => ['yaml', 'yml']
}
WEB_API_URL =
'http://pygments.simplabs.com/'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.use_web_apiObject

Returns the value of attribute use_web_api.



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

def use_web_api
  @use_web_api
end

Class Method Details

.highlight(language, code) ⇒ String

Highlights the passed code with the appropriate rules according to the specified language.

Parameters:

  • language (Symbol, String)

    the language the code is in

  • code (String)

    the actual code to highlight

Returns:

  • (String)

    the highlighted code or simply the HTML-escaped code if language is not supported.



115
116
117
118
119
120
121
122
123
# File 'lib/simplabs/highlight.rb', line 115

def self.highlight(language, code)
  language = get_language_sym(language)
  return CGI.escapeHTML(code) unless language
  if Simplabs::Highlight.use_web_api
    highlight_with_web_api(language, code)
  else
    Simplabs::Highlight::PygmentsWrapper.new(code, language).highlight
  end
end