clygments is a Clojure wrapper for Pygments.
Add the dependency in your project.clj
:
[clygments "2.0.2"]
Then:
(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.
(clygments/highlight "(def x (+ 20 22))" :clojure :html)
;; => <div class=\"highlight\"><pre><span class=\"p\">(</span><span class=\"k\">def </span><span class=\"nv\">x</span> <span class=\"mi\">42</span><span class=\"p\">)</span>\n</pre></div>
Clygments 1.0.0+ only supports Java 7+.
See Pygments’ list for a list of available languages.
If nil
is passed as a language we use Pygments’ guess_lexer
.
Note it takes more time and might not be accurate on small code snippets.
:bbcode
:html
:latex
:null
(no formatting, this leaves the code unchanged):raw
:rtf
:svg
:terminal256
:terminal
Images outputs are not supported. See also Pygments docs for the full list.
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.
;; 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"})
Copyright © 2014-2020 Baptiste Fontaine
Distributed under the Eclipse Public License either version 1.0 or any later version.