91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"text/template"
|
||
|
)
|
||
|
|
||
|
// wget http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
|
||
|
|
||
|
const (
|
||
|
inFilename = "language-subtag-registry"
|
||
|
outJSONFilename = "json/language.json"
|
||
|
outGoFilename = "lang/language.go"
|
||
|
)
|
||
|
|
||
|
type Item struct {
|
||
|
Lang string `json:"lang"`
|
||
|
Description string `json:"description"`
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
f, e := os.Open(inFilename)
|
||
|
if e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
scanner := bufio.NewScanner(f)
|
||
|
items := []*Item{}
|
||
|
for scanner.Scan() {
|
||
|
if scanner.Text() == "%%" {
|
||
|
item := new(Item)
|
||
|
scanner.Scan()
|
||
|
if scanner.Text() == "Type: language" {
|
||
|
for !strings.HasPrefix(scanner.Text(), "Subtag:") {
|
||
|
scanner.Scan()
|
||
|
}
|
||
|
if strings.HasPrefix(scanner.Text(), "Subtag:") {
|
||
|
ss := strings.Split(scanner.Text(), ": ")
|
||
|
item.Lang = strings.Trim(ss[1], " ")
|
||
|
if len(item.Lang) > 2 {
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
for !strings.HasPrefix(scanner.Text(), "Description:") {
|
||
|
scanner.Scan()
|
||
|
}
|
||
|
if strings.HasPrefix(scanner.Text(), "Description:") {
|
||
|
ss := strings.Split(scanner.Text(), ": ")
|
||
|
item.Description = strings.Trim(ss[1], " ")
|
||
|
scanner.Scan()
|
||
|
if strings.HasPrefix(scanner.Text(), " ") {
|
||
|
item.Description += strings.TrimPrefix(scanner.Text(), " ")
|
||
|
}
|
||
|
}
|
||
|
items = append(items, item)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
if e := f.Close(); e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
jw, e := os.Create(outJSONFilename)
|
||
|
if e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
if e := json.NewEncoder(jw).Encode(items); e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
if e := jw.Close(); e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
gw, e := os.Create(outGoFilename)
|
||
|
if e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
gotmpl := template.Must(template.ParseFiles("gotemplate"))
|
||
|
ctx := make(map[string]interface{})
|
||
|
ctx["Items"] = items
|
||
|
if e := gotmpl.Execute(gw, ctx); e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
if e := gw.Close(); e != nil {
|
||
|
log.Fatal(e.Error())
|
||
|
}
|
||
|
|
||
|
}
|