/
make.go
165 lines (137 loc) · 3.15 KB
/
make.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"regexp"
"sort"
"strings"
"text/template"
)
var flag_vet = flag.Bool("vet", false, "only vet source files, don't run them")
var flag_api = flag.Bool("api", false, "only generate API")
func main() {
flag.Parse()
buildAPI()
// read template
b, err := ioutil.ReadFile("template.html")
check(err)
replaceInRaw(b, '\n', '@') // hack to allow raw strings spanning multi lines
templ := template.Must(template.New("guide").Parse(string(b)))
// output file
f, err2 := os.OpenFile("examples.html", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
// execute!
if !*flag_api {
state := &State{}
check(templ.Execute(f, state))
}
renderAPI()
}
type State struct {
count int
}
func (s *State) Example(in string) string {
s.count++
// extract example source
in = strings.Replace(in, "@", "\n", -1) // undo raw string hack
in = strings.Trim(in, "\n")
// exec input file
check(ioutil.WriteFile(s.infile(), []byte(in), 0666))
arg := "-v"
if *flag_vet {
arg = "-vet"
}
cmd("mumax3", "-cache", "/tmp", arg, s.infile())
recordExamples(in, s.count)
return `<a id=example` + fmt.Sprint(s.count) + `></a><pre>` + template.HTMLEscapeString(in) + `</pre>`
}
var api_examples = make(map[string][]int)
func recordExamples(input string, num int) {
in := strings.ToLower(input)
for k, _ := range api_ident {
if ok, _ := regexp.MatchString(k, in); ok {
api_examples[k] = append(api_examples[k], num)
}
}
}
func (s *State) Img(fname string) string {
cmd("mumax3-convert", "-png", "-arrows", "16", s.outfile()+"/"+fname+".ovf")
pngfile := s.outfile() + "/" + fname + ".png"
return fmt.Sprintf(`
<figure style="float:left">
<img src="%v"/>
<figcaption> %v </figcaption>
</figure>`, pngfile, fname)
}
func (s *State) Include(fname string) string {
b, err := ioutil.ReadFile(fname)
check(err)
return string(b)
}
func (s *State) Output() string {
out := `<h3>output</h3> `
dir, err := os.Open(s.outfile())
check(err)
files, err2 := dir.Readdirnames(-1)
check(err2)
sort.Strings(files)
for _, f := range files {
if path.Ext(f) == ".ovf" {
out += s.Img(f[:len(f)-len(".ovf")])
}
}
out += `<br style="clear:both"/> `
for _, f := range files {
if f == "table.txt" {
cmd("mumax3-plot", s.outfile()+"/"+f)
}
}
dir, err = os.Open(s.outfile())
check(err)
files, err2 = dir.Readdirnames(-1)
check(err2)
sort.Strings(files)
for _, f := range files {
if path.Ext(f) == ".svg" {
src := s.outfile() + "/" + f
out += fmt.Sprintf(`
<figure>
<img src="%v"/>
<figcaption> %v </figcaption>
</figure>`, src, f)
}
}
return out
}
func (s *State) infile() string {
return fmt.Sprintf("example%v.mx3", s.count)
}
func (s *State) outfile() string {
return fmt.Sprintf("example%v.out", s.count)
}
func cmd(cmd string, args ...string) {
out, err := exec.Command(cmd, args...).CombinedOutput()
os.Stdout.Write(out)
check(err)
}
func replaceInRaw(bytes []byte, old, new byte) {
inraw := false
for i, b := range bytes {
if b == '`' {
inraw = !inraw
}
if inraw && b == old {
bytes[i] = new
}
}
}
func check(err error) {
if err != nil {
log.Panic(err)
}
}