A simple syntax highlighting library for Inko, with Pygments compatible HTML output.
This library focuses solely on syntax highlighting, and doesn't provide any means to detect a language/lexer to use according to a file extension, filename, MIME type, or the file contents.
- Inko 0.15.0 or newer
inko pkg add github.com/yorickpeterse/inko-syntax 0.10.0
inko pkg sync
Language | Name | Aliases |
---|---|---|
C | c |
cpp |
Fish | fish |
|
Inko | inko |
|
Make | make |
|
Ruby | ruby |
|
Rust | rust |
|
Shell | shell |
bash , sh , zsh , ksh |
TOML | toml |
Format | Formatter |
---|---|
Pygments compatible HTML | syntax.format.Html |
A basic example:
import std.stdio (STDOUT)
import syntax (Languages)
import syntax.format (Html)
class async Main {
fn async main {
# The `Languages` type is a registry of the available languages. This type
# makes it easy to create a lexer for a language, without having to
# explicitly import the underlying types into your code.
let langs = Languages.new
# This gets a language for the given name, returning a `None` if the name
# isn't recognized.
let lang = langs.get('inko').unwrap
# Now we can create a lexer and format it. Lexers take an immutable
# reference to a `ByteArray`, so we need to keep it around until we've
# produced the token stream.
let bytes = '# This is a test'.to_byte_array
let lexer = lang.lexer(bytes)
# The `Html` formatter takes a lexer and turns it into Pygments compatible
# HTML document fragment.
let html = Html.new.format(lexer)
STDOUT.new.print(html.to_string)
}
}
The output of this is the following HTML (formatted manually to increase readability):
<div class="highlight">
<pre class="highlight"><code><span class="c"># This is a test</span></code></pre>
</div>
All source code in this repository is licensed under the Mozilla Public License version 2.0, unless stated otherwise. A copy of this license can be found in the file "LICENSE".