Highlight.js for rails 4.0

NB: Apologies to those who downloaded versions < 7.4.0.3; third times a charm as they say.

This is my first gem and I am learning as I go along. I am reasonably certain this version works, however you may want to wait a day or so since my next job is to add some tests.

This is a minor update to chloerei/highlight_js-rails which I have forked because :

  1. I've nevered forked a repository before.
  2. it hadn't been updated in a year and was 3 versions behind highlight.js.
  3. I've yet to create a gem. Its about time I learned.
  4. It doesn't mention compatability with rails 4.0.

Description

Rails pipeline wraper for highlight.js http://highlightjs.org/

Install

In your Gemfile

gem 'highlight_js-rails4'

# or

gem 'highlight_js-rails4', :git => 'git://github.com/os6sense/highlight_js-rails4.git'

Then bundle install

Usage

1. Edit the applications javascript file

If you want to only select a few languages you can include just highlight and the languages you want/need. Edit your applications js file (or appropriate layouts .js file if you are using layouts): app/assets/javascrpt/application.js

and include something like the following but specifying the languages you want to be highlighted:

//= require highlight_js/highlight
//= require highlight_js/languages/ruby
//= require highlight_js/languages/python
//= require highlight_js/languages/xml

hljs.initHighlightingOnLoad();

ALTERNATIVELY: The source for highlight.js itself also comes with a pack which enables autodetection. I have included 2 packs from highlight.js:

highlight.pack.js - this is the download pack as supplied at http://highlightjs.org/download/ and includes: Bash, C#, C++, CSS, Diff, HTML, XML, HTTP, Ini, JSON, Java, JavaScript, PHP, Perl, Python, Ruby, SQL

highlight.pack.all.js - this is the complete shebang and is rather large at 111K.

The great thing about these is that they autodetect and hence don't require you to specify which language to include. To use them all you will need to add to the application.js file is:

//= require highlight_js/highlight.pack
// CHOOSE **ONE** OF THESE REQUIRES, NOT BOTH
//= require highlight_js/highlight.pack.all

hljs.initHighlightingOnLoad();

2. Edit applications stylesheet file

highlight.js comes with several different styles which can be applied to the highlighted code block. There is a very useful test where you can see the effects of these at: http://highlightjs.org/static/test.html

In order to add a given style edit the application stylesheet file: app/assets/stylesheets/application.css

If you are using a different layout the name of this file will correspond with that layout. Add the name of css file in the style that you want. e.g.

*= require highlight_js/github