mirror of
https://github.com/danog/blackfriday.git
synced 2024-11-26 20:14:43 +01:00
Switch to chroma
This commit is contained in:
parent
2c98907303
commit
46f3fff6e1
6
go.mod
6
go.mod
@ -1 +1,7 @@
|
|||||||
module github.com/danog/blackfriday/v2
|
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
|
||||||
|
15
go.sum
Normal file
15
go.sum
Normal file
@ -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=
|
53
html.go
53
html.go
@ -19,10 +19,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"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.
|
// 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.cr(w)
|
||||||
r.tag(w, divTag[:len(divTag)-1], attrs)
|
r.tag(w, divTag[:len(divTag)-1], attrs)
|
||||||
r.tag(w, divTag[:len(divTag)-1], []string{`class="highlight"`})
|
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)
|
source := string(node.Literal)
|
||||||
args := []string{"highlight", "-f", "html"}
|
|
||||||
if lang != "" {
|
// Determine lexer.
|
||||||
args = append(args, "-l", lang)
|
l := lexers.Get(lang)
|
||||||
}
|
if l == nil {
|
||||||
cmd := exec.Command("rougify", args...) // nolint: gas
|
l = lexers.Analyse(source)
|
||||||
cmd.Stdin = strings.NewReader(string(node.Literal))
|
}
|
||||||
cmd.Stdout = buf
|
if l == nil {
|
||||||
cmd.Stderr = os.Stderr
|
l = lexers.Fallback
|
||||||
if e := cmd.Run(); e != nil {
|
}
|
||||||
panic(e)
|
l = chroma.Coalesce(l)
|
||||||
}
|
|
||||||
r.out(w, buf.Bytes())
|
// 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)
|
||||||
r.out(w, divCloseTag)
|
r.out(w, divCloseTag)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user