diff --git a/go.mod b/go.mod index d490566..065a5a5 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,7 @@ module github.com/danog/blackfriday/v2 + +go 1.17 + +require github.com/alecthomas/chroma v0.9.4 + +require github.com/dlclark/regexp2 v1.4.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..70739ce --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/alecthomas/chroma v0.9.4 h1:YL7sOAE3p8HS96T9km7RgvmsZIctqbK1qJ0b7hzed44= +github.com/alecthomas/chroma v0.9.4/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/html.go b/html.go index 3f1ab17..2d679fa 100644 --- a/html.go +++ b/html.go @@ -19,10 +19,13 @@ import ( "bytes" "fmt" "io" - "os" - "os/exec" "regexp" "strings" + + "github.com/alecthomas/chroma" + "github.com/alecthomas/chroma/formatters/html" + "github.com/alecthomas/chroma/lexers" + "github.com/alecthomas/chroma/styles" ) // HTMLFlags control optional behavior of HTML renderer. @@ -769,25 +772,37 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt r.cr(w) r.tag(w, divTag[:len(divTag)-1], attrs) r.tag(w, divTag[:len(divTag)-1], []string{`class="highlight"`}) - r.tag(w, preTag[:len(preTag)-1], []string{`class="highlight"`}) - r.out(w, codeTag) - buf := new(bytes.Buffer) - args := []string{"highlight", "-f", "html"} - if lang != "" { - args = append(args, "-l", lang) - } - cmd := exec.Command("rougify", 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()) + source := string(node.Literal) + + // Determine lexer. + l := lexers.Get(lang) + if l == nil { + l = lexers.Analyse(source) + } + if l == nil { + l = lexers.Fallback + } + l = chroma.Coalesce(l) + + // Determine formatter. + f := html.New(html.WithClasses(true)) + + // Determine style. + s := styles.Get("") + if s == nil { + s = styles.Fallback + } + + it, err := l.Tokenise(nil, source) + if err != nil { + panic(err) + } + + if err = f.Format(w, s, it); err != nil { + panic(err) + } - r.out(w, codeCloseTag) - r.out(w, preCloseTag) r.out(w, divCloseTag) r.out(w, divCloseTag)