Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bfontaine/clygments

:art: Code highlighting using Pygments in Clojure
https://github.com/bfontaine/clygments

clojure library pygments syntax-highlighting

Last synced: 16 days ago
JSON representation

:art: Code highlighting using Pygments in Clojure

Awesome Lists containing this project

README

        

# clygments

[![Build Status](https://img.shields.io/travis/bfontaine/clygments.svg)](https://travis-ci.org/bfontaine/clygments)
[![Coverage Status](https://img.shields.io/coveralls/bfontaine/clygments.svg)](https://coveralls.io/r/bfontaine/clygments)
[![Clojars Project](https://img.shields.io/clojars/v/clygments.svg)](https://clojars.org/clygments)

**clygments** is a Clojure wrapper for [Pygments][].

[Pygments]: http://pygments.org/

## Usage

Add the dependency in your `project.clj`:

```clj
[clygments "2.0.2"]
```

Then:

```clj
(ns your-project.core
(:require [clygments.core :as clygments]))
```

It exposes only one function, `highlight`. Give it your code as a string, its
language and the desired output and it’ll do that for you :)

It’ll return a string or `nil` if there was an error, like an unsupported
language or output.

### Example

```clj
(clygments/highlight "(def x (+ 20 22))" :clojure :html)
;; =>

(def x 42)\n

```

## Support

Clygments 1.0.0+ only supports Java 7+.

### Langages

See [Pygments’ list][lexers] for a list of available languages.

If `nil` is passed as a language we use Pygments’ [`guess_lexer`](http://pygments.org/docs/api/#pygments.lexers.guess_lexer).
Note it takes more time and might not be accurate on small code snippets.

### Output

* `:bbcode`
* `:html`
* `:latex`
* `:null` (no formatting, this leaves the code unchanged)
* `:raw`
* `:rtf`
* `:svg`
* `:terminal256`
* `:terminal`

Images outputs are [not][issue-2] supported. See also
[Pygments docs][formatters] for the full list.

[issue-2]: https://github.com/bfontaine/clygments/issues/2#issuecomment-35169407

### Options

All lexers’ and formatters’ options are supported since version 0.1.1. They are
given as a map to `highlight` and support hyphens for a better readability. See
Pygments’ docs for more info.

#### Example

```clj
;; expand tabs to 4 spaces
(cligments/highlight "def foo():\n\tpass" :python :html {:tab-size 4})

;; generate a full standalone HTML document with a custom title
(cligments/highlight "int i = 2+2;" :C :html {:full true, :title "This is my code"})
```

[lexers]: http://pygments.org/docs/lexers/
[formatters]: http://pygments.org/docs/formatters/

## License

Copyright © 2014-2020 Baptiste Fontaine

Distributed under the Eclipse Public License either version 1.0 or any later
version.