forked from ghetzel/diecast
/
functions_defs_lang.go
54 lines (47 loc) · 1.47 KB
/
functions_defs_lang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package diecast
import (
"bytes"
"html/template"
"github.com/PerformLine/go-stockutil/typeutil"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/quick"
)
var HighlightTheme = `monokai`
func loadStandardFunctionsLangHighlighting(funcs FuncMap, server *Server) funcGroup {
var group = funcGroup{
Name: `Language Highlighting`,
Description: `Utilities for performing syntax highlighting of source code.`,
Functions: []funcDef{
{
Name: `highlight`,
Summary: `Take source code in the given language and return marked up HTML that highlights language keywords and syntax.`,
Arguments: []funcArg{
{
Name: `language`,
Type: `string`,
Description: `The name of the language expected as input. If empty or the string "*", a best effort will be made to detect the language.`,
}, {
Name: `src`,
Type: `string`,
Description: `The source code to highlight.`,
},
},
Function: func(language string, in interface{}) (template.HTML, error) {
var out bytes.Buffer
var src = typeutil.String(in)
if language == `` || language == `*` {
if c := lexers.Analyse(src).Config(); c != nil {
language = c.Name
}
}
if err := quick.Highlight(&out, typeutil.String(in), language, `html`, HighlightTheme); err == nil {
return template.HTML(out.String()), nil
} else {
return ``, err
}
},
},
},
}
return group
}