-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
76 lines (62 loc) · 1.63 KB
/
config.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
package svgsaurus
import (
"bytes"
"regexp"
"strings"
)
var whitespace = regexp.MustCompile("\\s+")
// Config contains the data rendered by the template.
type Config struct {
Text string
Size string
Width string
Height string
X string
Y string
Font string
Color string
Background string
Bold bool
Italic bool
Underline bool
}
// FromQuery populates the Config's values from query params.
func (c *Config) FromQuery(q map[string]string) *Config {
fallback := func(key string, def string) string {
val, ok := q[key]
if !ok {
return def
}
return val
}
c.Text = fallback("t", "svgsaurus")
c.Size = fallback("s", "55")
c.Width = fallback("w", "265")
c.Height = fallback("h", "60")
c.X = fallback("x", "5")
c.Y = fallback("y", "46")
c.Font = fallback("f", "arial")
c.Color = fallback("c", "000000")
c.Background = fallback("b", "")
options := fallback("o", "")
c.Bold = strings.Index(options, "b") >= 0
c.Italic = strings.Index(options, "i") >= 0
c.Underline = strings.Index(options, "u") >= 0
// Use the replacement pattern to insert spaces.
replace := fallback("r", "_")
c.Text = strings.Replace(c.Text, replace, " ", -1)
c.Font = strings.Replace(c.Font, replace, " ", -1)
return c
}
// Render generates the output string from a Config.
func (c *Config) Render() ([]byte, error) {
buf := new(bytes.Buffer)
err := svgTemplate.Execute(buf, c)
if err != nil {
return nil, err
}
// Remove extra whitespace without modifying the text.
b := whitespace.ReplaceAllLiteral(buf.Bytes(), []byte{' '})
b = bytes.Replace(b, []byte("[[Text]]"), []byte(c.Text), -1)
return b, nil
}