highlight

package module
v0.0.0-...-201131c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2017 License: MIT Imports: 5 Imported by: 0

README

Highlight

Go Report Card GoDoc MIT License

This is a package for syntax highlighting a large number of different languages. To see the list of languages currently supported, see the syntax_files directory.

Highlight allows you to pass in a string and get back all the information you need to syntax highlight that string well.

This project is still a work in progress and more features and documentation will be coming later.

Installation

go get github.com/zyedidia/highlight

Usage

Here is how to use this package to highlight a string. We will also be using github.com/fatih/color to actually colorize the output to the console.

package main

import (
    "fmt"
    "io/ioutil"
    "strings"

    "github.com/fatih/color"
    "github.com/zyedidia/highlight"
)

func main() {
    // Here is the go code we will highlight
    inputString := `package main

import "fmt"

// A hello world program
func main() {
    fmt.Println("Hello world")
}`

    // Load the go syntax file
    // Make sure that the syntax_files directory is in the current directory
    syntaxFile, _ := ioutil.ReadFile("highlight/syntax_files/go.yaml")

    // Parse it into a `*highlight.Def`
    syntaxDef, err := highlight.ParseDef(syntaxFile)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Make a new highlighter from the definition
    h := highlight.NewHighlighter(syntaxDef)
    // Highlight the string
    // Matches is an array of maps which point to groups
    // matches[lineNum][colNum] will give you the change in group at that line and column number
    // Note that there is only a group at a line and column number if the syntax highlighting changed at that position
    matches := h.HighlightString(inputString)

    // We split the string into a bunch of lines
    // Now we will print the string
    lines := strings.Split(inputString, "\n")
    for lineN, l := range lines {
        for colN, c := range l {
            // Check if the group changed at the current position
            if group, ok := matches[lineN][colN]; ok {
                // Check the group name and set the color accordingly (the colors chosen are arbitrary)
                if group == highlight.Groups["statement"] {
                    color.Set(color.FgGreen)
                } else if group == highlight.Groups["preproc"] {
                    color.Set(color.FgHiRed)
                } else if group == highlight.Groups["special"] {
                    color.Set(color.FgBlue)
                } else if group == highlight.Groups["constant.string"] {
                    color.Set(color.FgCyan)
                } else if group == highlight.Groups["constant.specialChar"] {
                    color.Set(color.FgHiMagenta)
                } else if group == highlight.Groups["type"] {
                    color.Set(color.FgYellow)
                } else if group == highlight.Groups["constant.number"] {
                    color.Set(color.FgCyan)
                } else if group == highlight.Groups["comment"] {
                    color.Set(color.FgHiGreen)
                } else {
                    color.Unset()
                }
            }
            // Print the character
            fmt.Print(string(c))
        }
        // This is at a newline, but highlighting might have been turned off at the very end of the line so we should check that.
        if group, ok := matches[lineN][len(l)]; ok {
            if group == highlight.Groups["default"] || group == highlight.Groups[""] {
                color.Unset()
            }
        }

        fmt.Print("\n")
    }
}

If you would like to automatically detect the filetype of a file based on the filename, and have the appropriate definition returned, you can use the DetectFiletype function:

// Name of the file
filename := ...
// The first line of the file (needed to check the filetype by header: e.g. `#!/bin/bash` means shell)
firstLine := ...

// Parse all the syntax files in an array with type []*highlight.Def
var defs []*highlight.Def
...

def := highlight.DetectFiletype(defs, filename, firstLine)
fmt.Println("Filetype is", def.FileType)

For a full example, see the syncat example which acts like cat but will syntax highlight the output (if highlight recognizes the filetype).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Groups map[string]Group

Groups contains all of the groups that are defined You can access them in the map via their string name

Functions

func ResolveIncludes

func ResolveIncludes(defs []*Def)

ResolveIncludes will sort out the rules for including other filetypes You should call this after parsing all the Defs

Types

type Def

type Def struct {
	FileType string
	// contains filtered or unexported fields
}

A Def is a full syntax definition for a language It has a filetype, information about how to detect the filetype based on filename or header (the first line of the file) Then it has the rules which define how to highlight the file

func DetectFiletype

func DetectFiletype(defs []*Def, filename string, firstLine []byte) *Def

DetectFiletype will use the list of syntax definitions provided and the filename and first line of the file to determine the filetype of the file It will return the corresponding syntax definition for the filetype

func ParseDef

func ParseDef(input []byte) (s *Def, err error)

ParseDef parses an input syntax file into a highlight Def

type Group

type Group uint8

A Group represents a syntax group

func (Group) String

func (g Group) String() string

String returns the group name attached to the specific group

type Highlighter

type Highlighter struct {
	Def *Def
	// contains filtered or unexported fields
}

A Highlighter contains the information needed to highlight a string

func NewHighlighter

func NewHighlighter(def *Def) *Highlighter

NewHighlighter returns a new highlighter from the given syntax definition

func (*Highlighter) HighlightMatches

func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int)

HighlightMatches sets the matches for each line in between startline and endline It sets all other matches in the buffer to nil to conserve memory This assumes that all the states are set correctly

func (*Highlighter) HighlightStates

func (h *Highlighter) HighlightStates(input LineStates)

HighlightStates correctly sets all states for the buffer

func (*Highlighter) HighlightString

func (h *Highlighter) HighlightString(input string) []LineMatch

HighlightString syntax highlights a string Use this function for simple syntax highlighting and use the other functions for more advanced syntax highlighting. They are optimized for quick rehighlighting of the same text with minor changes made

func (*Highlighter) ReHighlightLine

func (h *Highlighter) ReHighlightLine(input LineStates, lineN int)

ReHighlightLine will rehighlight the state and match for a single line

func (*Highlighter) ReHighlightStates

func (h *Highlighter) ReHighlightStates(input LineStates, startline int)

ReHighlightStates will scan down from `startline` and set the appropriate end of line state for each line until it comes across the same state in two consecutive lines

type LineMatch

type LineMatch map[int]Group

LineMatch represents the syntax highlighting matches for one line. Each index where the coloring is changed is marked with that color's group (represented as one byte)

type LineStates

type LineStates interface {
	Line(n int) string
	LinesNum() int
	State(lineN int) State
	SetState(lineN int, s State)
	SetMatch(lineN int, m LineMatch)
}

LineStates is an interface for a buffer-like object which can also store the states and matches for every line

type State

type State *region

A State represents the region at the end of a line

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL