-
Notifications
You must be signed in to change notification settings - Fork 15
/
option.go
128 lines (108 loc) · 2.8 KB
/
option.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
package pdf
import (
"context"
"image/color"
"io"
"net/http"
"github.com/alecthomas/chroma/v2"
"github.com/go-swiss/fonts"
"github.com/yuin/goldmark/util"
)
// An Option interface is a functional option type for the Renderer.
type Option interface {
SetConfig(*Config)
}
// A function that implements the Option interface
type OptionFunc func(*Config)
// To implement the SetConfig method of the Option interface
func (o OptionFunc) SetConfig(c *Config) {
o(c)
}
// Pass a completely new config
func WithConfig(config *Config) Option {
return OptionFunc(func(c *Config) {
*c = *config
})
}
// Add a context that will be used for operations like downloading fonts
func WithContext(ctx context.Context) Option {
return OptionFunc(func(c *Config) {
c.Context = ctx
})
}
// Set the image filesystem
func WithImageFS(images http.FileSystem) Option {
return OptionFunc(func(c *Config) {
c.ImageFS = images
})
}
// Set a cache for fonts
func WithFontsCache(fc fonts.Cache) Option {
return OptionFunc(func(c *Config) {
c.FontsCache = fc
})
}
// Set a color for links
func WithLinkColor(val color.Color) Option {
return OptionFunc(func(c *Config) {
c.Styles.LinkColor = val
})
}
// Provide an io.Write where debug information will be written to
func WithTraceWriter(val io.Writer) Option {
return OptionFunc(func(c *Config) {
c.TraceWriter = val
})
}
// Set the font for every heading style
func WithHeadingFont(f Font) Option {
return OptionFunc(func(c *Config) {
c.Styles.H1.Font = f
c.Styles.H2.Font = f
c.Styles.H3.Font = f
c.Styles.H4.Font = f
c.Styles.H5.Font = f
c.Styles.H6.Font = f
c.Styles.THeader.Font = f
})
}
// Set the font for every body element
func WithBodyFont(f Font) Option {
return OptionFunc(func(c *Config) {
c.Styles.Normal.Font = f
c.Styles.Blockquote.Font = f
c.Styles.TBody.Font = f
})
}
// Set a font for code spans and code blocks
func WithCodeFont(f Font) Option {
return OptionFunc(func(c *Config) {
c.Styles.CodeFont = f
})
}
// Set the code block chroma theme
func WithCodeBlockTheme(theme *chroma.Style) Option {
return OptionFunc(func(c *Config) {
c.Styles.CodeBlockTheme = theme
})
}
type withNodeRenderers struct {
value []util.PrioritizedValue
}
func (o *withNodeRenderers) SetConfig(c *Config) {
c.NodeRenderers = append(c.NodeRenderers, o.value...)
}
// Extend the NodeRenderers to support or overwrite how nodes are rendered.
func WithNodeRenderers(ps ...util.PrioritizedValue) Option {
return &withNodeRenderers{ps}
}
// Pass your own PDF object that satisfies the PDF interface
func WithPDF(pdf PDF) Option {
return OptionFunc(func(c *Config) {
c.PDF = pdf
})
}
// Easily configure a PDF writer to use based on https://github.com/phpdave11/gofpdf
func WithFpdf(ctx context.Context, c FpdfConfig) Option {
return WithPDF(NewFpdf(ctx, c, nil))
}