A crude syntax highlighter and formatter written in Golang
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cmd/highlight
lexers
output
.gitignore
LICENSE
README.md
emitter.go
filter.go
filter_test.go
highlight_test.go
lexer.go
rule.go
rule_test.go
stack.go
stack_test.go
states.go
states_test.go
token.go
tokenizer.go
tokenizers.go
tokens.go

README.md

go-highlight

Description

A somewhat crude syntax highlighter loosely based on Pygments.

See chroma for a newer, better, and far more complete syntax highlighter!

Installation

go-highlight can be installed with the regular go get command:

go get github.com/johnsto/go-highlight

Lexers are stored in a separate package, so remember to install those, too:

go get github.com/johnsto/go-highlight/lexers

The CLI app can also be installed with go get:

go get github.com/johnsto/go-highlight/cmd/highlight

Run tests:

go test github.com/johnsto/go-highlight

Usage

Importing for use in your code is as simple as importing both the base highlight package, and registering the default lexers as an anonymous import:

import "github.com/johnsto/go-highlight"
import _ "github.com/johnsto/go-highlight/lexers"

Tokenizers can be retrieved by content type or filename:

tokenizer, err = highlight.GetTokenizerForContentType("application/json")
// or:
tokenizer, err = highlight.GetTokenizerForFilename("futurama.json")

Use Tokenize or TokenizeString to tokenize an io.Reader or string respectively:

err = tokenizer.Tokenize(reader, func(t highlight.Token) error {
	_, err := fmt.Printf(t.Value)
	return err
})

For colourised terminal output, import the "term" package:

import "github.com/johnsto/go-highlight/output/term"

Then instantiate the emitter and tokenize to it:

emitter = output.NewDebugOutputter()
err = tokenizer.Tokenize(reader, emitter.Emit)

Or for formatted (indented) output, use Format instead:

err = tokenizer.Format(reader, emitter.Emit)