Git Product home page Git Product logo

highlight's Introduction

Highlight

Build Status

Highlight is a simple syntax highlighting gem for Ruby and Rails. It's basically a wrapper around the popular http://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 http://pygments.simplabs.com/, created by Trevor Turk.

See the API docs at http://rdoc.info/projects/simplabs/highlight.

Usage

Highlight can either be used standalone via

require 'simplabs/highlight'
Simplabs::Highlight.highlight(:ruby, 'class Test; end')

or in Rails where it adds the highlight_code helper:

highlight_code(language, code = nil, &block)

language may be either a Symbol or a String (see supported languages below). The code can be passed either as a string or inside a block, e.g.:

highlight_code(:ruby, 'class Test; end')

or

highlight_code(:ruby) do
  klass = 'class'
  name  = 'Test'
  _end  = 'end'
  "#{klass} #{name}; #{_end}"
end

Since highlighting the code takes a while, all highlighted source code should be cached, e.g.:

<%- code = 'class Test; end' -%>
<%- cache Digest::SHA1.hexdigest(code) do -%>
  <%= highlight_code(:ruby, code) -%>
<%- end -%>

Supported Languages

The following languages are supported (there are probably more that are supported by pygments). All of the paranthesized identifiers may be used as parameters for highlight 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)
  • 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)
  • 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)

Installation

Installation is as easy as

gem install highlight

To use highlight in Rails apps, you have to define the dependency in the Gemfile:

gem 'highlight', :require => 'simplabs/highlight'

Highlight also comes with a default CSS file that defines styles for the highlighted code. This CSS file can be copied to your application's public/stylesheets directory via

./bin/rails generate highlight_styles

If you don't have python and pygments installed, you will need that too. For instructions on installing pygments, refer to http://pygments.org/docs/installation/.

Author

Copyright (c) 2008-2010 Marco Otte-Witte (http://simplabs.com), released under the MIT license

Acknowledgements

The actual highlighting is done by Pygments (http://pygments.org).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.