1
0
Fork 0
mirror of https://github.com/emojisum/emojisum.git synced 2024-11-30 10:55:39 +00:00
emojisum/emoji/map_json.go
2021-11-01 19:13:49 -07:00

109 lines
2.2 KiB
Go

//go:build ignore
// +build ignore
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"text/template"
"github.com/emojisum/emojisum/emoji"
)
func main() {
flag.Parse()
input, err := os.Open(*flInput)
if err != nil {
log.Fatal(err)
}
defer input.Close()
vm := emoji.VersionedMap{}
dec := json.NewDecoder(input)
if err := dec.Decode(&vm); err != nil {
log.Fatal(err)
}
output, err := os.Create(*flOutput)
if err != nil {
log.Fatal(err)
}
defer output.Close()
//mapGoTemp := template.Must(template.ParseFiles(*flTemplate))
funcMap := template.FuncMap{
"IsColonNotation": emoji.IsColonNotation,
"IsCodepoint": emoji.IsCodepoint,
"CodepointLinkMarkdown": codepointLinkMarkdown,
"Join": join,
}
mapGoTemp, err := template.New("").Funcs(funcMap).Parse(tmpl[*flTemplate])
if err != nil {
log.Fatal(err)
}
if err := mapGoTemp.Execute(output, vm); err != nil {
log.Fatal(err)
}
}
func codepointLinkMarkdown(word string) string {
return fmt.Sprintf(`[%s](%s)`, word, emoji.UnicodeLinkURL(word))
}
func join(sep string, v []string) string {
return strings.Join(v, sep)
}
var (
flInput = flag.String("in", "emojimap.json", "json input")
flOutput = flag.String("out", "map_gen.go", "golang output")
flTemplate = flag.String("template", "map_gen", "template to use (map_gen or markdown_gen)")
)
var tmpl = map[string]string{
"map_gen": `// THIS FILE IS GENERATED. DO NOT EDIT.
package emoji
func init() {
mapGen = VersionedMap{
Description: "{{.Description}}",
Version: "{{.Version}}",
EmojiWords: []Words{
{{- range .EmojiWords }}
{"{{- Join "\", \"" . }}"},
{{- end }}
},
}
}
`,
"markdown_gen": `
## Emoji Map list
_THIS FILE IS GENERATED. DO NOT EDIT._
This is for "pretty" viewing purposes.
To view the functional document, see [emojimap.json](./emojimap.json).
### Description
{{ .Description }}
### Version
{{ .Version }}
### List
{{- range $index, $words := .EmojiWords }}
* ` + "`{{ $index }}`" + ` -- {{ range $words }} {{- if IsColonNotation . -}} {{ . }} ` + "`{{ . }}`" + ` {{- else }} {{ CodepointLinkMarkdown . }} {{- end }}{{- end }}
{{- end }}
`,
}