Pearly - A library for syntax highlighting using Sublime Text syntax definitions

Pearly Soames wanted gold and silver, but not, in the way of common thieves, for wealth. He wanted them because they shone and were pure. Strange, afflicted, and deformed, he sought a cure in the abstract relation of colors.
– Mark Helprin, Winter’s Tale

Pearly is an Elixir library for syntax highlighting using Sublime Text syntax definitions.

Pearly.highlight("html", "<h1>Hello, World!</h1>",
  format: :html,
  theme: "Solarized (dark)")
#=> {:ok, "<pre style=\"background-color:#002b36;\">\n<span style=..."}

Pearly currently supports formatting output for either HTML pages or the terminal.

GitHub: https://github.com/mischov/pearly
HexDocs: https://hexdocs.pm/pearly/Pearly.html

About

I needed to highlight code for use in HTML pages. I considered using Makeup, but I needed to support more languages than it currently does. Another option would have been using Pygments via Highlight, but I wanted to avoid depending on Python.

Instead I wrote Pearly, a small library and Rustler NIF for the Syntect library. Syntect is a really nice Rust library for syntax highlighting using Sublime Text syntax definitions, and it supports a lot of languages out of the box.

Future

It is not currently possible to provide additional syntaxes or themes beyond those that are built in, but I plan to add support for this when I get a chance.

7 Likes

This is very cool. This was supposed to be my approach with Makeup (only in pure Elixir), but parsing and compiling SublimeText grammars is very hard and a little confusing (they do funny things with regexs).

1 Like

It would be awesome if Makeup used SublimeText grammars, but I would not have taken on this project had I needed to parse them and I can understand why you didn’t want to climb that particular mountain.

On one hand, it would be pretty cool, because it would give us lots of languages for free. On the other hand, Makeup supports things that SublimeText grammars don’t, like matching delimiters, and corrects some problems in the highlighting of function names that are common to many lexers.

Can always make a ‘SublimeTextGrammer’ language for Makeup that can read those regex’s, once they are figured out in full. That would be a nice fall-back for languages not defined via other packages until they are made. :slight_smile:

Yes, I can, but it would require writing an interpreter or compiler for Sublime grammars, and that’s hard. I don’t want to use this rust library as a fallback because I don’t want to depend on Rust. I want Makeup to be pure Elixir (otherwise I’d use Pygments as a fallback - I’d rather depend on Python than Rust).

2 Likes

Though rust at least has no requirements on anything else installed once deployed as it makes a standalone binary. ^.^

But yep, I’d prefer pure elixir as well.

1 Like

As would I.

Edit: But deadlines, so Pearly!

1 Like

Indeed! It is a great fallback, especially as it does not require anything odd at release time as it makes a standalone binary. :slight_smile:

1 Like

Release v0.1.1

When no language (nil or "") is provided to highlight, Pearly now defaults to highlighting as plain text.

1 Like