hanayo/scripts/generate_mappings.go

84 lines
2.0 KiB
Go
Raw Permalink Normal View History

2019-02-23 13:29:15 +00:00
// +build ignore
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
func main() {
var toGoMap bool
flag.BoolVar(&toGoMap, "g", false, "Set if you want to export data to mappings.go")
flag.Parse()
semantic, err := getMappings("https://raw.githubusercontent.com/Semantic-Org/Semantic-UI/master/src/themes/default/elements/icon.overrides", semanticRegex)
if err != nil {
panic(err)
}
fontawesome, err := getMappings("https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.css", fontAwesomeRegex)
if err != nil {
panic(err)
}
classMappings := make(map[string]string, len(semantic))
for k, v := range semantic {
if equivalent, ok := fontawesome[k]; ok {
classMappings[equivalent] = v
}
}
b, err := json.MarshalIndent(classMappings, "", "\t")
if err != nil {
panic(err)
}
if toGoMap {
f, err := os.Create("modules/fa-semantic-mappings/mappings.go")
defer f.Close()
if err != nil {
panic(err)
}
f.Write([]byte(fileHeader))
fmt.Fprintf(f, "var Mappings = %#v\n", classMappings)
fmt.Println("generate: mappings.go")
} else {
fmt.Println(string(b))
}
}
const fileHeader = `// THIS FILE WAS AUTOMATICALLY GENERATED BY A TOOL
// Use ` + "`go generate`" + ` to generate this.
package fasuimappings
// Mappings is a map containing the Semantic UI icon equivalent of FontAwesome
// icons.
`
var semanticRegex = regexp.MustCompile(`i\.([\.a-zA-Z0-9-]+):before { content: "(.{5})"; }`)
var fontAwesomeRegex = regexp.MustCompile(`.([a-zA-Z0-9-]+):before {
content: "(.{5})";
}`)
func getMappings(url string, regex *regexp.Regexp) (map[string]string, error) {
ov, err := http.Get(url)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(ov.Body)
if err != nil {
return nil, err
}
strs := regex.FindAllStringSubmatch(string(b), -1)
m := make(map[string]string, len(strs))
for _, strs := range strs {
m[strs[2]] = strings.Replace(strs[1], ".", " ", -1)
}
return m, nil
}