forked from g3n/engine
/
main.go
176 lines (151 loc) · 3.22 KB
/
main.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/format"
"io"
"log"
"os"
"strings"
"text/template"
"unicode"
)
// Command line options
var (
oPackage = flag.String("pkg", "icon", "Package name")
)
// Program name and version
const (
PROGNAME = "g3nicodes"
VMAJOR = 0
VMINOR = 1
)
type constInfo struct {
Name string
Value string
}
type templateData struct {
Packname string
Consts []constInfo
}
func main() {
// Parse command line parameters
flag.Usage = usage
flag.Parse()
// Opens input file
if len(flag.Args()) == 0 {
log.Fatal("Input file not supplied")
return
}
finput, err := os.Open(flag.Args()[0])
if err != nil {
log.Fatal(err)
return
}
// Creates optional output file
fout := os.Stdout
if len(flag.Args()) > 1 {
fout, err = os.Create(flag.Args()[1])
if err != nil {
log.Fatal(err)
return
}
defer fout.Close()
}
// Parse input file
var td templateData
td.Packname = *oPackage
err = parse(finput, &td)
if err != nil {
log.Fatal(err)
return
}
// Parses the template
tmpl := template.New("templ")
tmpl, err = tmpl.Parse(templText)
if err != nil {
log.Fatal(err)
return
}
// Expands template to buffer
var buf bytes.Buffer
err = tmpl.Execute(&buf, &td)
if err != nil {
log.Fatal(err)
return
}
// Formats buffer as Go source
p, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
return
}
// Writes formatted source to output file
fout.Write(p)
}
func parse(fin io.Reader, td *templateData) error {
// Read words from input reader and builds words map
scanner := bufio.NewScanner(fin)
for scanner.Scan() {
// Read next line
line := scanner.Text()
if err := scanner.Err(); err != nil {
return err
}
// Remove line terminator, spaces and ignore empty lines
line = strings.Trim(line, "\n ")
if len(line) == 0 {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 2 {
continue
}
name := parts[0]
code := parts[1]
nameParts := strings.Split(name, "_")
for i := 0; i < len(nameParts); i++ {
nameParts[i] = strings.Title(nameParts[i])
}
finalName := strings.Join(nameParts, "")
// If name starts with number adds prefix
runes := []rune(finalName)
if unicode.IsDigit(runes[0]) {
finalName = "N" + finalName
}
td.Consts = append(td.Consts, constInfo{Name: finalName, Value: "0x" + code})
}
return nil
}
// Shows application usage
func usage() {
fmt.Fprintf(os.Stderr, "%s v%d.%d\n", PROGNAME, VMAJOR, VMINOR)
fmt.Fprintf(os.Stderr, "usage: %s [options] <input file> <output file>\n", strings.ToLower(PROGNAME))
flag.PrintDefaults()
os.Exit(0)
}
const templText = `// File generated by g3nicodes. Do not edit.
// This file is based on the original 'codepoints' file
// from the material design icon fonts:
// https://github.com/google/material-design-icons
package {{.Packname}}
// Icon constants.
const (
{{range .Consts}}
{{.Name}} = string({{.Value}})
{{- end}}
)
// Codepoint returns the codepoint for the specified icon name.
// Returns 0 if the name not found
func Codepoint(name string) string {
return name2Codepoint[name]
}
// Maps icon name to codepoint
var name2Codepoint = map[string]string{
{{range .Consts}}
"{{.Name}}": string({{.Value}}),
{{- end}}
}
`