forked from Depado/bfchroma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
172 lines (150 loc) · 4.67 KB
/
renderer.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Package bfchroma provides an easy and extensible blackfriday renderer that
// uses the chroma syntax highlighter to render code blocks.
package bfchroma
import (
"io"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
bf "github.com/russross/blackfriday/v2"
)
// Analyser defines a function that returns the correct chroma.Lexer for src.
type Analyser func(src string) chroma.Lexer
// Option defines the functional option type
type Option func(r *Renderer)
// Style is a function option allowing to set the style used by chroma
// Default : "monokai"
func Style(s string) Option {
return func(r *Renderer) {
r.Style = styles.Get(s)
}
}
// ChromaStyle is an option to directly set the style of the renderer using a
// chroma style instead of a string
func ChromaStyle(s *chroma.Style) Option {
return func(r *Renderer) {
r.Style = s
}
}
// WithoutAutodetect disables chroma's language detection when no codeblock
// extra information is given. It will fallback to a sane default instead of
// trying to detect the language.
func WithoutAutodetect() Option {
return func(r *Renderer) {
r.Autodetect = false
}
}
// DetectAs causes autodetection to always "detect" lang, using chroma's
// lexers.Get function. If lang is unknown then it will use lexers.Fallback.
func DetectAs(lang string) Option {
lex := lexers.Get(lang)
if lex == nil {
lex = lexers.Fallback
}
return func(r *Renderer) {
r.Analyser = func(s string) chroma.Lexer { return lex }
}
}
// DetectLexer causes autodetection to always return lex.
func DetectLexer(lex chroma.Lexer) Option {
return func(r *Renderer) {
r.Analyser = func(s string) chroma.Lexer { return lex }
}
}
// EmbedCSS will embed CSS needed for html.WithClasses() in beginning of the document
func EmbedCSS() Option {
return func(r *Renderer) {
r.embedCSS = true
}
}
// ChromaOptions allows to pass Chroma html.Option such as Standalone()
// WithClasses(), ClassPrefix(prefix)...
func ChromaOptions(options ...html.Option) Option {
return func(r *Renderer) {
r.ChromaOptions = options
}
}
// Extend allows to specify the blackfriday renderer which is extended
func Extend(br bf.Renderer) Option {
return func(r *Renderer) {
r.Base = br
}
}
// NewRenderer will return a new bfchroma renderer with sane defaults
func NewRenderer(options ...Option) *Renderer {
r := &Renderer{
Base: bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
}),
Style: styles.Get("monokai"),
Autodetect: true,
Analyser: lexers.Analyse,
}
for _, option := range options {
option(r)
}
r.Formatter = html.New(r.ChromaOptions...)
return r
}
// RenderWithChroma will render the given text to the w io.Writer
func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error {
var lexer chroma.Lexer
// Determining the lexer to use
if len(data.Info) > 0 {
lexer = lexers.Get(string(data.Info)) // e.g. ```python
} else if r.Autodetect {
lexer = r.Analyser(string(text))
}
if lexer == nil {
lexer = lexers.Fallback
}
// Tokenize the code
iterator, err := lexer.Tokenise(nil, string(text))
if err != nil {
return err
}
return r.Formatter.Format(w, r.Style, iterator)
}
// Renderer is a custom Blackfriday renderer that uses the capabilities of
// chroma to highlight code with triple backtick notation
type Renderer struct {
Base bf.Renderer
Autodetect bool
Analyser Analyser
ChromaOptions []html.Option
Style *chroma.Style
Formatter *html.Formatter
embedCSS bool
}
// RenderNode satisfies the Renderer interface
func (r *Renderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
switch node.Type {
case bf.Document:
if entering && r.embedCSS {
w.Write([]byte("<style>")) // nolint: errcheck
r.Formatter.WriteCSS(w, r.Style) // nolint: errcheck
w.Write([]byte("</style>")) // nolint: errcheck
}
return r.Base.RenderNode(w, node, entering)
case bf.CodeBlock:
if err := r.RenderWithChroma(w, node.Literal, node.CodeBlockData); err != nil {
return r.Base.RenderNode(w, node, entering)
}
return bf.SkipChildren
default:
return r.Base.RenderNode(w, node, entering)
}
}
// RenderHeader satisfies the Renderer interface
func (r *Renderer) RenderHeader(w io.Writer, ast *bf.Node) {
r.Base.RenderHeader(w, ast)
}
// RenderFooter satisfies the Renderer interface
func (r *Renderer) RenderFooter(w io.Writer, ast *bf.Node) {
r.Base.RenderFooter(w, ast)
}
// ChromaCSS returns CSS used with chroma's html.WithClasses() option
func (r *Renderer) ChromaCSS(w io.Writer) error {
return r.Formatter.WriteCSS(w, r.Style)
}