# peacock [![build status](https://secure.travis-ci.org/thlorenz/peacock.svg?branch=master)](http://travis-ci.org/thlorenz/peacock)

<a href="https://www.patreon.com/bePatron?u=8663953"><img alt="become a patron" src="https://c5.patreon.com/external/logo/become_a_patron_button.png" height="35px"></a>

JavaScript syntax highlighter that generates [pygments](http://pygments.org/) compatible html and therefore supports
[pygments styles](https://github.com/thlorenz/peacock/tree/master/styles).

Peacock _(Pavo cristasus)_

## Installation

    npm install peacock

### Included demo

`npm exlore peacock`
`npm run demo`

## Features and limitations

-   works server side via [nodejs](http://nodejs.org)
-   works in the browser via browserify, AMD, if available, or as global window object
-   **only highlights JavaScript** - if that is a dealbreaker look into some [alternatives](#alternatives)

## [API](https://thlorenz.github.io/peacock)

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### peacock.defaultTheme

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the default peacock theme used to highlight code

### peacock.spans

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the default peacock spans used to wrap code tokens

### peacock.classes

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the default peacock classes used to highlight code

### peacock.highlight

Highlights the proviced code or throws an error if it was not able to parse it.

**Parameters**

-   `code` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** to highlight
-   `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options
    -   `$0.theme` **([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))?** to use when highlighting [empty sample](https://github.com/thlorenz/peacock/blob/master/themes/empty.js) (optional, default `peacock.defaultTheme`)
    -   `$0.linenos` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** if `true` line numbers will be included (optional, default `false`)
    -   `$0.jsx` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** if `true` peacock will support `jsx` syntax (which makes highlighting a tad bit slower) (optional, default `true`)

Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the HTML with containing the highlighted code

## Browser support

### AMD

Ensure to include [redeyed](https://github.com/thlorenz/redeyed) and [esprima](https://github.com/ariya/esprima) as your
dependencies

```js
define(['peacock'], function (peacock) {
 [ .. ]
});
```

### Attached to global window object

peacock will be exposed as `window.peacock` which allows calling `peacock.highlight` from anywhere.

```html
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/esprima/2.7.2/esprima.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/thlorenz/redeyed/master/redeyed.js"></script>
<script type="text/javascript" src="https://rawgit.com/thlorenz/peacock/master/peacock-browser.js"></script>
```

## Examples ([_browse_](https://github.com/thlorenz/peacock/tree/master/examples))

-   [pageofself](https://github.com/thlorenz/peacock/blob/master/examples/pageofself.js) highlights itself using
    the [tango style](https://github.com/thlorenz/peacock/blob/master/styles/tango.css)
-   [pageofself-hide-semicolons](https://github.com/thlorenz/peacock/blob/master/examples/pageofself-hide-semicolons.js) highlights itself using
    the [fruity style](https://github.com/thlorenz/peacock/blob/master/styles/fruity.css) and hides all semicolons
-   [amd browser example](https://github.com/thlorenz/peacock/tree/master/examples/browser-amd) which uses
    [requirejs](http://requirejs.org) to resolve peacock
-   [simple browser example](https://github.com/thlorenz/peacock/tree/master/examples/browser-simple) which uses the
    global window object to access peacock

## Alternatives

-   for client side highlighting give [SyntaxHighlighter](http://alexgorbatchev.com/SyntaxHighlighter/) a try
-   for server side highlighting driven by nodejs, try
    [node-syntaxhighlighter](https://github.com/thlorenz/node-syntaxhighlighter)
-   [highlightjs](http://softwaremaniacs.org/soft/highlight/en/) is another client side option
-   [pygments](http://pygments.org/) is a great server side highlighter, granted you are running python
