Simple library for code syntax highlighting
It was designed to be lightweight (~2.5kb), easy to use, and extendable.
It is completely themable via CSS.
You can see rainbow in action at http://rainbowco.de.
You can also build/download custom packages from there.
Include some markup for code you want to be highlighted:
def openFile(path):file = open(path, "r")content = file.read()file.close()return content
Include a CSS theme file in the
Include rainbow.js and whatever languages you want before the closing
dist/rainbow.min.js comes with some popular languages bundled together with it.
Rainbow 2.0 introduced support for node.js. All of the existing API methods should work, but there is also a new
Rainbow.colorSync method for synchronous highlighting.
npm install --save rainbow-code
var rainbow = ;var highlighted = rainbow;console;
Rainbow 2.0 should work in the following browsers:
For older browsers you can download the legacy 1.2.0 release.
Currently supported languages are:
In your markup the
data-language attribute is used to specify what language to use for highlighting. For example:
var testing = true;
Rainbow also supports the HTML5 style for specifying languages:
var testing = true;
And the Google prettify style:
var testing = true;
Themes are located in the
themes/sass directory. They are written using sass so that common logic can be shared among all themes without having to duplicate it in each theme file. You should not edit the css files directly.
_base.sass includes some default styles shared by almost every theme.
_init.sass contains mixins and initialization logic that is shared by every theme.
As of version 2.0 the themes use a clever trick to display the highlighted code. All code blocks default to
opacity: 0, but an animation is triggered on page load to happen after a 2 second delay.
0s. That ensures that as soon as the code is done being highlighted it will be able to show up instantly. This is used to prevent a flash of unstyled text on page load and ensure that the code blocks only show up after they have been highlighted.
There is also a preload animation that will show up for any code block that takes longer than 300ms to load.
A SASS mixin was added to simplify defining styles that should only apply for a specific language. Using it looks like this:
You can pass a single language or a list of languages.
Rainbow has four public methods:
Rainbow.color is used to highlight blocks of code.
For convenience, this method is called automatically to highlight all code blocks on the page when
DOMContentLoaded fires. If you would like to highlight stuff that is not in the DOM you can use it on its own. There are three ways to use it.
The first option is calling the color method on its own:
Each time this is called, Rainbow will look for matching
pre blocks on the page that have not yet been highlighted and highlight them.
You can optionally pass a callback function that will fire when all the blocks have been highlighted.
The second option is passing a specific element to the color method.
In this example we are creating a code block, highlighting it, then inserting it into the DOM:
The final option is passing in your code as a string to
If you want to prevent code on the page from being highlighted when the page loads you can set the
defer property to
Rainbowdefer = true;
Note that you have to set this before
DOMContentLoaded fires or else it will not do anything.
As of right now there is one extra option for color.
This option allows you to have an extra class added to every span that Rainbow renders. This can be useful if you want to remove the classes in order to trigger a special effect of some sort.
To apply a global class you can add it in your markup:
var hello = true;
Or you can pass it into a
Rainbow.color call like this:
Rainbow.extend is used to define language grammars which are used to highlight the code you pass in. It can be used to define new languages or to extend existing languages.
A very simple language grammer looks something like this:
Any pattern used with extend will take precedence over an existing pattern that matches the same block. It will also take precedence over any pattern that is included as part of the generic patterns.
For example if you were to call
This would mean that function will be highlighted as
<span class="keyword magic">function</span>, but
break will still be highlighted as just
<span class="keyword">return</span>, etc.
By default languages are considered to be standalone, but if you specify an optional third parameter you can have your language inherit from another one.
For example the python language grammars inherit from the generic ones:
If you wanted to remove the default boolean values you should be able to do something like this:
name value determines what classes will be added to a span that matches the pattern you specify. For example, if you name a pattern
constant.hex-color the code that matches that pattern will be wrapped in
<span class="constant hex-color></span>. This allows you to share common base styles but add more customization in your themes for more specific matches.
The simplest way to define a grammar is to define a regular expression and a name to go along with that.
This allows you to take a more complicated regular expression and map certain parts of it to specific scopes.
For more complicated matches you may want to process code within another match group.
You should be able to nest sub-patterns as many levels deep as you would like.
Let's say for example you want to highlight PHP's apc functions. You can include the php language then in the markup on your page add:
In general the best practice is to make your patterns as specific as possible (for example targetting specific keywords).
When you create a new language it gets pushed to the front of whatever language it is inheriting from. This means whatever rules are added last will be checked against first.
Rainbow chooses the first match it finds for a block. If another pattern overlaps with a pattern that has already been chosen then it is ignored.
There is one exception to this. If a match that comes later is more specific (the start AND end points stretch beyond another pattern already matched) then the new match will take precedence and the old one will be discarded.
That means if you have a pattern that matches
function test() and you add a pattern that matches
public function test() the new one will be used instead.
You cannot match part of a subgroup directly to a scope. For example if you have:
matches:1: 'name.attribute'2: 'value'pattern: //g
This will result in code
name="value" being highlighted as:
You see the value class never gets applied.
To achieve what you really want you would have to use a subpattern like this:
name: 'name.attribute'matches:matches:1: 'value'pattern: /\"\"/gpattern: //g
This means the entire block is wrapped with
name.attribute scope and then within that any part in double quotes will be wrapped as
That means the entire block will behighlighted as
In this example you could avoid subpatterns completely by using a regex like this to begin with:
The addAlias function allows you to map a different name to a language. For example:
js instead of
This method notifies you as soon as a block of code has been highlighted.
The first parameter returns a reference to that code block in the DOM. The second parameter returns a string of the language being highlighted.
This method allows you to remove syntax rules for a specific language. It is only really useful for development if you want to be able to reload language grammars on the fly without having to refresh the page.
Gulp is used for all build related tasks.
git clone email@example.com:ccampbell/rainbow.gitcd rainbownpm install
The build command is used to build a custom version of rainbow.js. If you run
A file will be created at
dist/rainbow-custom.min.js containing rainbow.js as well as popular languages. If you want to specify specific languages you can use:
If you want a minimized version of rainbow without any languages you can pass
gulp build --languages=none
If you want a minimized version with all languages you can pass
gulp build --languages=all
The pack command will run a buble + rollup build and save the resulting file to
The sass command will compile all the rainbow themes
The test command will run the unit tests. You can pass the
--watch flag to keep the tests running and have them rerun when you make changes. That is a little buggy though which has something to do with karma + rollup.
You can also use the
--browsers flag to specify a browser to run the tests in. Currently only
Chrome are supported.
The watch command will look at sass files and src js files and build the css or js (using
gulp sass and
gulp pack) every time you make a change.
If you are looking for line number support you can try one of the following:
You can check out demos and build custom packages at rainbowco.de.