This is a syntax highlighter for Common Lisp written in Javascript. It is completely themable via CSS (themes included).
The purpose of this is to make it really easy to embed beautiful Common Lisp code into a website with minimal effort.
Usage is simple. You include highlight-lisp.js
, link to one of the CSS themes,
and call one of highlight-lisp's highlighting functions:
<!-- Put these in your document somewhere, probably in the head, although the <script>
tag can probably go anywhere -->
<script type="text/javascript" src="https://github.com/orthecreedence/highlight-lisp/raw/master/js/highlight-lisp/highlight-lisp.js"></script>
<link rel="stylesheet" href="https://github.com/orthecreedence/highlight-lisp/blob/master/js/highlight-lisp/themes/github.css">
...
<!-- By default, the highlighter looks for code blocks with class="lisp" -->
<pre><code class="lisp">(defun test-syntax-highlighter ()
"Docstring explaining what this function does."
(let ((hash (make-hash-table :test #'equal)))
...))</code></pre>
Once the HTML is set up, there are a few ways to initialize highlighting:
// automatically highlight all <code class="lisp">...</code> blocks
HighlightLisp.highlight_auto();
// specify a custom class name (instead of "lisp"):
HighlightLisp.highlight_auto({className: 'common-lisp'});
// highlight *every* code block
HighlightLisp.highlight_auto({className: null});
// manually highlight a code block
var code = document.getElementById('my-code-element');
HighlightLisp.highlight_element(code);
You can now enable paren matching (on mouse hover):
HighlightLisp.paren_match();
This will go through all highlighted blocks of code and add mouseover/mouseout event listeners to all ('s and )'s that highlight the matching paren on hover.
function
(
: (my-function ...)
function known
make-hash-table
, when
,
format
, etcfunction known special
let
, let\*
, lambda
.function symbol
#'my-function
function symbol known
#'equalp
, #'format
keyword
:
like :this-is-a-keyword
keyword known
:hash-keys
, :supersede
, etc.symbol
'
: 'my-symbol
lambda-list
&key
, &body
, etc.number
69
, -82.4
, #xF047
, #b11010
number integer
42
, 867
, etc. (no decimals)number ratio
80/9
, 23/4
number float
+47.82112
, 32.9
3.
.009
number hex
#x8090
, #xc001
number binary
#b01101
variable known
*package*
, *standard-output*
, etcvariable global
\*
: *main-datastore*
, *my-thread-local*
, etcvariable constant
+
: +dt+
, +contant-time+
, etcnil
nil
or t
will get this classcomment
; this is a comment
string
"
: "This is a string."
list
(
or )
characters are classified.On that note, things that don't get highlighted/aren't properly highlighted:
let
bindings or other symols within code that would
be interpreted as variables. Highlighting these would most likely be prohibitive
in terms of time (not the mention the return on investment). Feel free to patch!0.44d0
.#| ... |#
are unsupportedpi
, internal-time-units-per-second
) are classified
as functions, not known variables. This is because I pulled the list out of my
vim highlight script, and couldn't find a list of "Common Lisp standard
variables" to cross reference with. I pulled out the ones I know of and put them
into the known variables list, but there are no doubt more. If you see something
that is a known variable but gets treated as a known function, please open a
github issue.Aren't there a bunch of Javascript syntax highlighters out there already?
Yes, but truth be told, most ignore lisp. You can write custom parsers for some of them, but the APIs they provide didn't work well enough for me. highlight.js has a very nice lisp highlighting mode, along with really nice themes, but I wanted more control over the process.
For instance, highlight-lisp
started as a SyntaxHighlighter
brush, but I quickly realized that because of the limitations of Javascript not
allowing real lookbehind regular expressions,
I needed more direct control over the search/replace process.
What I discovered was that given the proper tools, parsing lisp is easy (in fact, a cake walk after just releasing markdown.cl) and there's no need for a big highlighting framework. You plug in some regexes, slap some tags around certain things, and call it a day.
As always, MIT.