mirror of
https://github.com/rwinkhart/go-highlite.git
synced 2026-08-28 04:46:30 -04:00
update readme and add examples
This commit is contained in:
@@ -6,6 +6,8 @@ languages currently supported, see the [`syntax_files`](./syntax_files) director
|
||||
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
|
||||
|
||||
```
|
||||
@@ -14,7 +16,8 @@ go get github.com/zyedidia/highlight
|
||||
|
||||
# Usage
|
||||
|
||||
Here is how to use this package to highlight a string:
|
||||
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.
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -29,7 +32,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Here is going to be the go code we highlight
|
||||
// Here is the go code we will highlight
|
||||
inputString := `
|
||||
package main
|
||||
|
||||
@@ -108,7 +111,7 @@ func main() {
|
||||
```
|
||||
|
||||
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`:
|
||||
you can use the `DetectFiletype` function:
|
||||
|
||||
```go
|
||||
// Name of the file
|
||||
@@ -123,3 +126,5 @@ var defs []*highlight.Def
|
||||
def := highlight.DetectFiletype(defs, filename, firstLine)
|
||||
fmt.Println("Filetype is", def.ft)
|
||||
```
|
||||
|
||||
For a full example, see the [`syncat`](./examples) example which acts like cat but will syntax highlight the output (if highlight recognizes the filetype).
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Examples
|
||||
|
||||
These examples will find the `syntax_files` directory by using your `GOPATH` environment variable.
|
||||
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/zyedidia/highlight"
|
||||
)
|
||||
|
||||
var defs []*highlight.Def
|
||||
|
||||
func main() {
|
||||
gopath := os.Getenv("GOPATH")
|
||||
files, _ := ioutil.ReadDir(gopath + "/src/github.com/zyedidia/highlight/syntax_files")
|
||||
|
||||
for _, f := range files {
|
||||
if strings.HasSuffix(f.Name(), ".yaml") {
|
||||
input, _ := ioutil.ReadFile(gopath + "/src/github.com/zyedidia/highlight/syntax_files")
|
||||
d, err := highlight.ParseDef(input)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
defs = append(defs, d)
|
||||
}
|
||||
}
|
||||
|
||||
fileSrc, _ := ioutil.ReadFile(os.Args[1])
|
||||
def := highlight.DetectFiletype(defs, os.Args[1], bytes.Split(fileSrc, []byte("\n"))[0])
|
||||
|
||||
if def == nil {
|
||||
fmt.Println(string(fileSrc))
|
||||
return
|
||||
}
|
||||
|
||||
h := highlight.NewHighlighter(def)
|
||||
|
||||
matches := h.Highlight(string(fileSrc))
|
||||
|
||||
lines := strings.Split(string(fileSrc), "\n")
|
||||
for lineN, l := range lines {
|
||||
colN := 0
|
||||
for _, c := range l {
|
||||
if group, ok := matches[lineN][colN]; ok {
|
||||
if group == "statement" {
|
||||
color.Set(color.FgGreen)
|
||||
} else if group == "identifier" {
|
||||
color.Set(color.FgBlue)
|
||||
} else if group == "preproc" {
|
||||
color.Set(color.FgHiRed)
|
||||
} else if group == "special" {
|
||||
color.Set(color.FgRed)
|
||||
} else if group == "constant.string" {
|
||||
color.Set(color.FgCyan)
|
||||
} else if group == "constant" {
|
||||
color.Set(color.FgCyan)
|
||||
} else if group == "constant.specialChar" {
|
||||
color.Set(color.FgHiMagenta)
|
||||
} else if group == "type" {
|
||||
color.Set(color.FgYellow)
|
||||
} else if group == "constant.number" {
|
||||
color.Set(color.FgCyan)
|
||||
} else if group == "comment" {
|
||||
color.Set(color.FgHiGreen)
|
||||
} else {
|
||||
color.Unset()
|
||||
}
|
||||
}
|
||||
fmt.Print(string(c))
|
||||
colN++
|
||||
}
|
||||
if group, ok := matches[lineN][colN]; ok {
|
||||
if group == "" {
|
||||
color.Unset()
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print("\n")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user