Skip to content

Instantly share code, notes, and snippets.

@siddontang
Created June 27, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siddontang/581495374e1abbca49c4 to your computer and use it in GitHub Desktop.
Save siddontang/581495374e1abbca49c4 to your computer and use it in GitHub Desktop.
generate redis commands from official website
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"github.com/PuerkitoBio/goquery"
)
var file = flag.String("f", "./commands.xml", "redis command from official website")
var output = flag.String("o", "output.list", "commands markdown output list")
type item struct {
Name string
Summary string
Group string
Args string
URL string
}
type items []*item
func (s items) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s items) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func (s items) Len() int {
return len(s)
}
func printErr(err error) {
if err != nil {
println(err.Error())
os.Exit(1)
}
}
func main() {
flag.Parse()
f, err := os.Open(*file)
printErr(err)
defer f.Close()
o, err := os.OpenFile(*output, os.O_CREATE|os.O_WRONLY, 0644)
printErr(err)
defer o.Close()
doc, err := goquery.NewDocumentFromReader(f)
printErr(err)
groups := make(map[string]items)
groupNames := make([]string, 0)
t := func(i int, s *goquery.Selection) {
cmd, _ := s.Attr("data-name")
group, _ := s.Attr("data-group")
args := s.Find(".args").Text()
summary := s.Find(".summary").Text()
href, _ := s.Find("a").Attr("href")
seps := strings.Fields(args)
args = strings.Join(seps, " ")
item := new(item)
item.Name = strings.ToUpper(cmd)
item.Group = strings.ToLower(group)
item.Args = args
item.Summary = summary
item.URL = fmt.Sprintf("http://redis.io%s", href)
g, ok := groups[group]
if !ok {
g = make(items, 0)
groupNames = append(groupNames, group)
}
g = append(g, item)
groups[group] = g
}
n := doc.Find("li")
n.Each(t)
sort.Strings(groupNames)
for _, name := range groupNames {
g := groups[name]
fmt.Fprintf(o, "## %s\n\n", strings.Title(name))
sort.Sort(g)
for _, i := range g {
fmt.Fprintf(o, "- [ ] [%s](%s)\n", strings.TrimSpace(fmt.Sprintf("%s %s", i.Name, i.Args)), i.URL)
}
fmt.Fprintf(o, "\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment