1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-26 12:04:46 +01:00

Invoke pygmentize

This commit is contained in:
Daniil Gentili 2022-01-06 17:53:33 +01:00
parent ba29d94709
commit e99e04f09b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

25
html.go
View File

@ -19,6 +19,8 @@ import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
)
@ -319,15 +321,15 @@ func isSmartypantable(node *Node) bool {
return pt != Link && pt != CodeBlock && pt != Code
}
func appendLanguageAttr(attrs []string, info []byte) []string {
func appendLanguageAttr(attrs []string, info []byte) ([]string, string) {
if len(info) == 0 {
return attrs
return attrs, ""
}
endOfLang := bytes.IndexAny(info, "\t ")
if endOfLang < 0 {
endOfLang = len(info)
}
return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang]))
return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang])), string(info[:endOfLang])
}
func (r *HTMLRenderer) tag(w io.Writer, name []byte, attrs []string) {
@ -764,12 +766,25 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt
r.cr(w)
}
case CodeBlock:
attrs = appendLanguageAttr(attrs, node.Info)
attrs, lang := appendLanguageAttr(attrs, node.Info)
r.cr(w)
r.out(w, divHighlightTag)
r.out(w, preHighlightTag)
r.tag(w, codeTag[:len(codeTag)-1], attrs)
escapeAllHTML(w, node.Literal)
buf := new(bytes.Buffer)
args := []string{"-f", "html"}
if lang != "" {
args = append(args, "-l", lang)
}
cmd := exec.Command("pygmentize", args...) // nolint: gas
cmd.Stdin = strings.NewReader(string(node.Literal))
cmd.Stdout = buf
cmd.Stderr = os.Stderr
if e := cmd.Run(); e != nil {
panic(e)
}
r.out(w, buf.Bytes())
r.out(w, codeCloseTag)
r.out(w, preCloseTag)
r.out(w, divCloseTag)