docshelper: List Chroma lexers
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 2 Apr 2018 06:49:54 +0000 (08:49 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 2 Apr 2018 06:50:42 +0000 (08:50 +0200)
Fixes #4554

helpers/docshelper.go [new file with mode: 0644]

diff --git a/helpers/docshelper.go b/helpers/docshelper.go
new file mode 100644 (file)
index 0000000..742b7bc
--- /dev/null
@@ -0,0 +1,60 @@
+package helpers
+
+import (
+       "fmt"
+       "path/filepath"
+       "sort"
+       "strings"
+
+       "github.com/alecthomas/chroma/lexers"
+       "github.com/gohugoio/hugo/docshelper"
+)
+
+// This is is just some helpers used to create some JSON used in the Hugo docs.
+func init() {
+
+       docsProvider := func() map[string]interface{} {
+               docs := make(map[string]interface{})
+
+               var chromaLexers []interface{}
+
+               sort.Sort(lexers.Registry.Lexers)
+
+               for _, l := range lexers.Registry.Lexers {
+
+                       config := l.Config()
+
+                       var filenames []string
+                       filenames = append(filenames, config.Filenames...)
+                       filenames = append(filenames, config.AliasFilenames...)
+
+                       aliases := config.Aliases
+
+                       for _, filename := range filenames {
+                               alias := strings.TrimSpace(strings.TrimPrefix(filepath.Ext(filename), "."))
+                               if alias != "" {
+                                       aliases = append(aliases, alias)
+                               }
+                       }
+
+                       sort.Strings(aliases)
+                       aliases = UniqueStrings(aliases)
+
+                       lexerEntry := struct {
+                               Name    string
+                               Aliases []string
+                       }{
+                               config.Name,
+                               aliases,
+                       }
+
+                       chromaLexers = append(chromaLexers, lexerEntry)
+
+                       docs["lexers"] = chromaLexers
+               }
+               return docs
+
+       }
+
+       docshelper.AddDocProvider("chroma", docsProvider)
+}