-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigen.go
120 lines (96 loc) · 2.22 KB
/
apigen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/engine"
_ "github.com/mumax/3/ext"
"io/ioutil"
"os"
"reflect"
"sort"
"strings"
"text/template"
"unicode"
)
var (
api_entries entries
api_ident = make(map[string]entry)
)
func buildAPI() {
cuda.Init(0, "yield") // gpu 0
cuda.LockThread()
ident := engine.World.Identifiers
doc := engine.World.Doc
e := make(entries, 0, len(ident))
for K, v := range doc {
k := strings.ToLower(K)
t := ident[k].Type()
entr := entry{K, t, v}
e = append(e, entr)
api_ident[k] = entr
}
sort.Sort(&e)
api_entries = e
}
func renderAPI() {
e := api_entries
t := template.Must(template.New("api").Parse(templ))
f, err2 := os.OpenFile("api.html", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
check(t.Execute(f, &api{e}))
}
type entry struct {
Name string
Type reflect.Type
Doc string
}
type entries []entry
func (e *entry) Methods() []string {
t := e.Type
// if it's a function, we list the methods on the output type
if t.Kind() == reflect.Func && t.NumOut() == 1 {
t = t.Out(0)
}
nm := t.NumMethod()
m := make([]string, 0, nm)
for i := 0; i < nm; i++ {
n := t.Method(i).Name
if unicode.IsUpper(rune(n[0])) {
m = append(m, n)
}
}
return m
}
func (e *entry) Examples() []int {
return api_examples[strings.ToLower(e.Name)]
}
func (e *entries) Len() int {
return len(*e)
}
func (e *entries) Less(i, j int) bool {
return strings.ToLower((*e)[i].Name) < strings.ToLower((*e)[j].Name)
}
func (e *entries) Swap(i, j int) {
(*e)[i], (*e)[j] = (*e)[j], (*e)[i]
}
type api struct {
Entries entries
}
func (e *api) Include(fname string) string {
b, err := ioutil.ReadFile(fname)
check(err)
return string(b)
}
const templ = `
{{.Include "head.html"}}
<body>
<h1> mx3 API </h1>
<hr/>
{{range .Entries}}
<span style="color:#000088; font-size:1.3em">{{.Name}}</span> <span style="color:gray; font-size:0.9em"> {{.Type}} </span> <br/>
{{with .Doc}} <p> {{.}} </p> {{end}}
{{with .Examples}} <p> <b>examples:</b> {{range .}} <a href="examples.html#example{{.}}">[{{.}}]</a> {{end}} </p> {{end}}
{{with .Methods}} <p> <span style="color:grey"> <b>methods:</b> {{range .}} {{.}} {{end}} </span> </p> {{end}}
<br/><hr/>
{{end}}
</body>
`