Skip to content

Commit

Permalink
Use Chroma as new default syntax highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Sep 22, 2017
1 parent 0b34af2 commit a0b3e95
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
56 changes: 56 additions & 0 deletions helpers/pygments.go
Expand Up @@ -24,11 +24,25 @@ import (
"sort"
"strings"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"

"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugofs"
jww "github.com/spf13/jwalterweatherman"
)

func init() {
// TODO(bep) highlight
options := []html.Option{
html.TabWidth(4)}

formatters.Register("html", html.New(options...))
}

const pygmentsBin = "pygmentize"

// HasPygments checks to see if Pygments is installed and available
Expand All @@ -42,6 +56,19 @@ func HasPygments() bool {

// Highlight takes some code and returns highlighted code.
func Highlight(cfg config.Provider, code, lang, optsStr string) string {
if true {
// TODO(bep) highlight
var buff bytes.Buffer
err := chromaHighlight(&buff, code, lang, "html", "trac")
if err != nil {
jww.ERROR.Print(err.Error())
return code
}

return buff.String()

}

if !HasPygments() {
jww.WARN.Println("Highlighting requires Pygments to be installed and in the path")
return code
Expand Down Expand Up @@ -131,6 +158,35 @@ func Highlight(cfg config.Provider, code, lang, optsStr string) string {
return str
}

func chromaHighlight(w io.Writer, source, lexer, formatter, style string) error {

l := lexers.Get(lexer)
if l == nil {
l = lexers.Analyse(source)
}
if l == nil {
l = lexers.Fallback
}
l = chroma.Coalesce(l)

f := formatters.Get(formatter)
if f == nil {
f = formatters.Fallback
}

s := styles.Get(style)
if s == nil {
s = styles.Fallback
}

writer, err := f.Format(w, s)
if err != nil {
return err
}

return l.Tokenise(nil, source, writer)
}

var pygmentsKeywords = make(map[string]bool)

func init() {
Expand Down
30 changes: 30 additions & 0 deletions vendor/vendor.json
Expand Up @@ -20,6 +20,36 @@
"revision": "bbf7a2afc14f93e1e0a5c06df524fbd75e5031e5",
"revisionTime": "2017-03-24T14:02:28Z"
},
{
"checksumSHA1": "IW5IOamDS/QQOTJS55NPGeTkpok=",
"path": "github.com/alecthomas/chroma",
"revision": "b57f8a4b4b3e1acca1e97895d7ff9f1a105e5321",
"revisionTime": "2017-09-19T04:37:00Z"
},
{
"checksumSHA1": "hIBbm6T96GE6uexfQwDP7Xnop1g=",
"path": "github.com/alecthomas/chroma/formatters",
"revision": "b57f8a4b4b3e1acca1e97895d7ff9f1a105e5321",
"revisionTime": "2017-09-19T04:37:00Z"
},
{
"checksumSHA1": "LqBFWwE77eJUaIOMf9HBzpYj4No=",
"path": "github.com/alecthomas/chroma/formatters/html",
"revision": "b57f8a4b4b3e1acca1e97895d7ff9f1a105e5321",
"revisionTime": "2017-09-19T04:37:00Z"
},
{
"checksumSHA1": "k4iHIk4IrOcJwosB1UfXGKrLXrk=",
"path": "github.com/alecthomas/chroma/lexers",
"revision": "b57f8a4b4b3e1acca1e97895d7ff9f1a105e5321",
"revisionTime": "2017-09-19T04:37:00Z"
},
{
"checksumSHA1": "V+rAjRnac9zAcLTAuYBVQ1/UMMc=",
"path": "github.com/alecthomas/chroma/styles",
"revision": "b57f8a4b4b3e1acca1e97895d7ff9f1a105e5321",
"revisionTime": "2017-09-19T04:37:00Z"
},
{
"checksumSHA1": "7yrV1Gzr1ajco1xJ1gsyqRDTY2U=",
"path": "github.com/bep/gitmap",
Expand Down

0 comments on commit a0b3e95

Please sign in to comment.