mirror of
https://github.com/rwinkhart/go-highlite.git
synced 2026-09-06 00:47:15 -04:00
Merge branch 'master' into master
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
Highlight is licensed under the MIT "Expat" License:
|
||||||
|
|
||||||
|
Copyright (c) 2016: Zachary Yedidia.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# Highlight
|
||||||
|
|
||||||
|
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`](./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.
|
||||||
|
|
||||||
|
```go
|
||||||
|
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("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.Highlight(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 == "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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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 == "" {
|
||||||
|
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:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 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.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")
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-5
@@ -1,10 +1,6 @@
|
|||||||
package highlight
|
package highlight
|
||||||
|
|
||||||
import "bytes"
|
func DetectFiletype(defs []*Def, filename string, firstLine []byte) *Def {
|
||||||
|
|
||||||
func DetectFiletype(defs []*Def, filename string, fileSrc []byte) *Def {
|
|
||||||
firstLine := bytes.Split(fileSrc, []byte("\n"))[0]
|
|
||||||
|
|
||||||
for _, d := range defs {
|
for _, d := range defs {
|
||||||
if d.ftdetect[0].Match([]byte(filename)) {
|
if d.ftdetect[0].Match([]byte(filename)) {
|
||||||
return d
|
return d
|
||||||
|
|||||||
+4
-33
@@ -1,34 +1,5 @@
|
|||||||
# Micro Syntax Highlighting Files v2.0
|
# Syntax Files
|
||||||
These are the new and improved syntax highlighting files for micro. They no longer follow a format similar to nano.
|
|
||||||
These files should be written in yaml, and can easily be converted from .nanorc or .micro to yaml.
|
|
||||||
|
|
||||||
# Using with colorschemes
|
Here are highlight's syntax files. If you would like to make a new syntax file, you should first check it
|
||||||
|
with the `syntax_checker.go` program. Just place it in this directory and run the program (`go run syntax_checker.go`)
|
||||||
Not all of these files have been converted to use micro's colorscheme feature. Most of them just hardcode the colors, which
|
and it will let you know if there are issues with any of the files in the directory.
|
||||||
can be problematic depending on the colorscheme you use.
|
|
||||||
|
|
||||||
Here is a list of the files that have been converted to properly use colorschemes:
|
|
||||||
|
|
||||||
* vi
|
|
||||||
* go
|
|
||||||
* c
|
|
||||||
* d
|
|
||||||
* markdown
|
|
||||||
* html
|
|
||||||
* lua
|
|
||||||
* swift
|
|
||||||
* rust
|
|
||||||
* java
|
|
||||||
* javascript
|
|
||||||
* pascal
|
|
||||||
* python
|
|
||||||
* ruby
|
|
||||||
* sh
|
|
||||||
* git
|
|
||||||
* tex
|
|
||||||
* solidity
|
|
||||||
|
|
||||||
# License
|
|
||||||
|
|
||||||
Because the nano syntax files I have modified are distributed under the GNU GPLv3 license, these files are also distributed
|
|
||||||
under that license. See [LICENSE](LICENSE).
|
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
filetype: c
|
filetype: c
|
||||||
|
|
||||||
detect:
|
detect:
|
||||||
filename: "\\.(c|C)$"
|
filename: "(\\.(c|C)$|\\.(h|H)$|\\.ii?$|\\.(def)$)"
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
- identifier: "\\b[A-Z_][0-9A-Z_]+\\b"
|
- identifier: "\\b[A-Z_][0-9A-Z_]+\\b"
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
filetype: yaml
|
||||||
|
|
||||||
|
detect:
|
||||||
|
filename: "\\.ya?ml$"
|
||||||
|
header: "%YAML"
|
||||||
|
|
||||||
|
rules:
|
||||||
|
- type: "(^| )!!(binary|bool|float|int|map|null|omap|seq|set|str) "
|
||||||
|
- constant: "\\b(YES|yes|Y|y|ON|on|NO|no|N|n|OFF|off)\\b"
|
||||||
|
- constant: "\\b(true|false)\\b"
|
||||||
|
- statement: "(:[[:space:]]|\\[|\\]|:[[:space:]]+[|>]|^[[:space:]]*- )"
|
||||||
|
- identifier: "[[:space:]][\\*&][A-Za-z0-9]+"
|
||||||
|
- type: "([-\\w]+:\\s+)|([-\\w]+:$)"
|
||||||
|
- special: "(^---|^\\.\\.\\.|^%YAML|^%TAG)"
|
||||||
|
|
||||||
|
- constant.string:
|
||||||
|
start: "\""
|
||||||
|
end: "\""
|
||||||
|
rules:
|
||||||
|
- constant.specialChar: "\\\\."
|
||||||
|
|
||||||
|
- constant.string:
|
||||||
|
start: "'"
|
||||||
|
end: "'"
|
||||||
|
rules:
|
||||||
|
- constant.specialChar: "\\\\."
|
||||||
|
|
||||||
|
- comment:
|
||||||
|
start: "#"
|
||||||
|
end: "$"
|
||||||
|
rules: []
|
||||||
|
|
||||||
Reference in New Issue
Block a user