| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package main |
| 11 |
|
| 12 |
import ( |
| 13 |
"fmt" |
| 14 |
"io/ioutil" |
| 15 |
"os" |
| 16 |
"path/filepath" |
| 17 |
"text/template" |
| 18 |
) |
| 19 |
|
| 20 |
type info struct { |
| 21 |
Path string |
| 22 |
} |
| 23 |
|
| 24 |
var tfuncs = template.FuncMap{ |
| 25 |
"basename": func(s string) string { |
| 26 |
return filepath.Base(s) |
| 27 |
}, |
| 28 |
"read": func(path string) ([]byte, error) { |
| 29 |
return ioutil.ReadFile(path) |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
var tmpl = template.Must(template.New("").Funcs(tfuncs).Parse(` |
| 34 |
// This file is automatically generated by generate-charset-data. |
| 35 |
// Do not hand-edit. |
| 36 |
|
| 37 |
package data |
| 38 |
import ( |
| 39 |
"github.com/paulrosania/go-charset/charset" |
| 40 |
"io" |
| 41 |
"io/ioutil" |
| 42 |
"strings" |
| 43 |
) |
| 44 |
|
| 45 |
func init() { |
| 46 |
charset.RegisterDataFile({{basename .Path | printf "%q"}}, func() (io.ReadCloser, error) { |
| 47 |
r := strings.NewReader({{read .Path | printf "%q"}}) |
| 48 |
return ioutil.NopCloser(r), nil |
| 49 |
}) |
| 50 |
} |
| 51 |
`)) |
| 52 |
|
| 53 |
var docTmpl = template.Must(template.New("").Funcs(tfuncs).Parse(` |
| 54 |
// This file is automatically generated by generate-charset-data. |
| 55 |
// Do not hand-edit. |
| 56 |
|
| 57 |
// The {{basename .Package}} package embeds all the charset |
| 58 |
// data files as Go data. It registers the data with the charset |
| 59 |
// package as a side effect of its import. To use: |
| 60 |
// |
| 61 |
// import _ "github.com/paulrosania/go-charset" |
| 62 |
package {{basename .Package}} |
| 63 |
`)) |
| 64 |
|
| 65 |
func main() { |
| 66 |
dataDir := filepath.Join("..", "datafiles") |
| 67 |
d, err := os.Open(dataDir) |
| 68 |
if err != nil { |
| 69 |
fatalf("%v", err) |
| 70 |
} |
| 71 |
names, err := d.Readdirnames(0) |
| 72 |
if err != nil { |
| 73 |
fatalf("cannot read datafiles dir: %v", err) |
| 74 |
} |
| 75 |
for _, name := range names { |
| 76 |
writeFile("data_"+name+".go", tmpl, info{ |
| 77 |
Path: filepath.Join(dataDir, name), |
| 78 |
}) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func writeFile(name string, t *template.Template, data interface{}) { |
| 83 |
w, err := os.Create(name) |
| 84 |
if err != nil { |
| 85 |
fatalf("cannot create output file: %v", err) |
| 86 |
} |
| 87 |
defer w.Close() |
| 88 |
err = t.Execute(w, data) |
| 89 |
if err != nil { |
| 90 |
fatalf("template execute %q: %v", name, err) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func fatalf(f string, a ...interface{}) { |
| 95 |
fmt.Fprintf(os.Stderr, "%s\n", fmt.Sprintf(f, a...)) |
| 96 |
os.Exit(2) |
| 97 |
} |