-
Notifications
You must be signed in to change notification settings - Fork 186
/
glamour.go
268 lines (235 loc) Β· 6.78 KB
/
glamour.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package glamour
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/muesli/termenv"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
"golang.org/x/term"
"github.com/charmbracelet/glamour/ansi"
styles "github.com/charmbracelet/glamour/styles"
)
const (
defaultWidth = 80
highPriority = 1000
)
// A TermRendererOption sets an option on a TermRenderer.
type TermRendererOption func(*TermRenderer) error
// TermRenderer can be used to render markdown content, posing a depth of
// customization and styles to fit your needs.
type TermRenderer struct {
md goldmark.Markdown
ansiOptions ansi.Options
buf bytes.Buffer
renderBuf bytes.Buffer
}
// Render initializes a new TermRenderer and renders a markdown with a specific
// style.
func Render(in string, stylePath string) (string, error) {
b, err := RenderBytes([]byte(in), stylePath)
return string(b), err
}
// RenderWithEnvironmentConfig initializes a new TermRenderer and renders a
// markdown with a specific style defined by the GLAMOUR_STYLE environment variable.
func RenderWithEnvironmentConfig(in string) (string, error) {
b, err := RenderBytes([]byte(in), getEnvironmentStyle())
return string(b), err
}
// RenderBytes initializes a new TermRenderer and renders a markdown with a
// specific style.
func RenderBytes(in []byte, stylePath string) ([]byte, error) {
r, err := NewTermRenderer(
WithStylePath(stylePath),
)
if err != nil {
return nil, err
}
return r.RenderBytes(in)
}
// NewTermRenderer returns a new TermRenderer the given options.
func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) {
tr := &TermRenderer{
md: goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
),
ansiOptions: ansi.Options{
WordWrap: defaultWidth,
ColorProfile: termenv.TrueColor,
},
}
for _, o := range options {
if err := o(tr); err != nil {
return nil, err
}
}
ar := ansi.NewRenderer(tr.ansiOptions)
tr.md.SetRenderer(
renderer.NewRenderer(
renderer.WithNodeRenderers(
util.Prioritized(ar, highPriority),
),
),
)
return tr, nil
}
// WithBaseURL sets a TermRenderer's base URL.
func WithBaseURL(baseURL string) TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.BaseURL = baseURL
return nil
}
}
// WithColorProfile sets the TermRenderer's color profile
// (TrueColor / ANSI256 / ANSI).
func WithColorProfile(profile termenv.Profile) TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.ColorProfile = profile
return nil
}
}
// WithStandardStyle sets a TermRenderer's styles with a standard (builtin)
// style.
func WithStandardStyle(style string) TermRendererOption {
return func(tr *TermRenderer) error {
styles, err := getDefaultStyle(style)
if err != nil {
return err
}
tr.ansiOptions.Styles = *styles
return nil
}
}
// WithAutoStyle sets a TermRenderer's styles with either the standard dark
// or light style, depending on the terminal's background color at run-time.
func WithAutoStyle() TermRendererOption {
return WithStandardStyle(styles.AutoStyle)
}
// WithEnvironmentConfig sets a TermRenderer's styles based on the
// GLAMOUR_STYLE environment variable.
func WithEnvironmentConfig() TermRendererOption {
return WithStylePath(getEnvironmentStyle())
}
// WithStylePath sets a TermRenderer's style from stylePath. stylePath is first
// interpreted as a filename. If no such file exists, it is re-interpreted as a
// standard style.
func WithStylePath(stylePath string) TermRendererOption {
return func(tr *TermRenderer) error {
styles, err := getDefaultStyle(stylePath)
if err != nil {
jsonBytes, err := os.ReadFile(stylePath)
if err != nil {
return err
}
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
}
tr.ansiOptions.Styles = *styles
return nil
}
}
// WithStyles sets a TermRenderer's styles.
func WithStyles(styles ansi.StyleConfig) TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.Styles = styles
return nil
}
}
// WithStylesFromJSONBytes sets a TermRenderer's styles by parsing styles from
// jsonBytes.
func WithStylesFromJSONBytes(jsonBytes []byte) TermRendererOption {
return func(tr *TermRenderer) error {
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
}
}
// WithStylesFromJSONFile sets a TermRenderer's styles from a JSON file.
func WithStylesFromJSONFile(filename string) TermRendererOption {
return func(tr *TermRenderer) error {
jsonBytes, err := os.ReadFile(filename)
if err != nil {
return err
}
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
}
}
// WithWordWrap sets a TermRenderer's word wrap.
func WithWordWrap(wordWrap int) TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.WordWrap = wordWrap
return nil
}
}
// WithPreservedNewlines preserves newlines from being replaced.
func WithPreservedNewLines() TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.PreserveNewLines = true
return nil
}
}
// WithEmoji sets a TermRenderer's emoji rendering.
func WithEmoji() TermRendererOption {
return func(tr *TermRenderer) error {
emoji.New().Extend(tr.md)
return nil
}
}
func (tr *TermRenderer) Read(b []byte) (int, error) {
return tr.renderBuf.Read(b)
}
func (tr *TermRenderer) Write(b []byte) (int, error) {
return tr.buf.Write(b)
}
// Close must be called after writing to TermRenderer. You can then retrieve
// the rendered markdown by calling Read.
func (tr *TermRenderer) Close() error {
err := tr.md.Convert(tr.buf.Bytes(), &tr.renderBuf)
if err != nil {
return err
}
tr.buf.Reset()
return nil
}
// Render returns the markdown rendered into a string.
func (tr *TermRenderer) Render(in string) (string, error) {
b, err := tr.RenderBytes([]byte(in))
return string(b), err
}
// RenderBytes returns the markdown rendered into a byte slice.
func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) {
var buf bytes.Buffer
err := tr.md.Convert(in, &buf)
return buf.Bytes(), err
}
func getEnvironmentStyle() string {
glamourStyle := os.Getenv("GLAMOUR_STYLE")
if len(glamourStyle) == 0 {
glamourStyle = styles.AutoStyle
}
return glamourStyle
}
func getDefaultStyle(style string) (*ansi.StyleConfig, error) {
if style == styles.AutoStyle {
if !term.IsTerminal(int(os.Stdout.Fd())) {
return &styles.NoTTYStyleConfig, nil
}
if termenv.HasDarkBackground() {
return &styles.DarkStyleConfig, nil
}
return &styles.LightStyleConfig, nil
}
styles, ok := styles.DefaultStyles[style]
if !ok {
return nil, fmt.Errorf("%s: style not found", style)
}
return styles, nil
}