forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygmentize.go
53 lines (43 loc) · 1021 Bytes
/
pygmentize.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
package pygmentize
import (
"bytes"
"encoding/xml"
"os"
"os/exec"
"github.com/vito/booklit"
)
type highlightedHTML struct {
Pre struct {
Code string `xml:",innerxml"`
} `xml:"pre"`
}
func Inline(language string, content string) (booklit.Content, error) {
if os.Getenv("SKIP_PYGMENTS") != "" {
return booklit.String(content), nil
}
html := new(bytes.Buffer)
pygmentize := exec.Command("pygmentize", "-l", language, "-f", "html", "-O", "encoding=utf-8")
pygmentize.Stdin = bytes.NewBufferString(content)
pygmentize.Stdout = html
pygmentize.Stderr = os.Stderr
err := pygmentize.Run()
if err != nil {
return nil, err
}
var hl highlightedHTML
err = xml.Unmarshal(html.Bytes(), &hl)
if err != nil {
return nil, err
}
return booklit.String(hl.Pre.Code), nil
}
func Block(language string, content string) (booklit.Content, error) {
hl, err := Inline(language, content)
if err != nil {
return nil, err
}
return booklit.Styled{
Style: "highlighted-block",
Content: hl,
}, nil
}