commit 78e8ba25799969fab446cc1f27634ea902a18049 Author: Darko Luketic Date: Fri Feb 17 22:09:37 2017 +0100 initial diff --git a/gotemplate b/gotemplate new file mode 100644 index 0000000..e8c1588 --- /dev/null +++ b/gotemplate @@ -0,0 +1,14 @@ +package lang + +type language struct { + Code string + Description string +} + +var Languages []language + +func init() { + {{range .Items}} + Languages = append(Languages, language{Code: "{{.Lang}}", Description: "{{.Description}}"}) + {{end}} +} diff --git a/json/.notempty b/json/.notempty new file mode 100644 index 0000000..e69de29 diff --git a/lang/.notempty b/lang/.notempty new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..56bd02d --- /dev/null +++ b/main.go @@ -0,0 +1,90 @@ +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()) + } + +}