-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragments.go
308 lines (271 loc) · 6.56 KB
/
fragments.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package website
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
. "github.com/gregoryv/web"
"github.com/gregoryv/web/files"
)
func versionField() *Element {
el := Span()
v := Version()
if v == "unreleased" {
el.With(Class("unreleased"), v)
} else {
el.With("v", v)
}
return el
}
func linkToPage(page *Page) *Element {
h1 := MustQueryOne(page.Element, "h1")
return Li(A(Href(page.Filename), h1.Text()))
}
func MustQueryOne(root *Element, expr string) *Element {
got := Query(root, expr)
if len(got) != 1 {
panic(fmt.Sprintf("expr %q: %v matches", expr, len(got)))
}
return got[0]
}
func stripTags(in string) string {
var buf bytes.Buffer
var inside bool
for _, r := range in {
switch r {
case '<':
inside = true
case '>':
inside = false
default:
if inside {
continue
}
buf.WriteRune(r)
}
}
return buf.String()
}
func filenameFrom(in string) string {
tidy := bytes.NewBufferString("")
var inside bool
var last rune
for _, c := range in {
switch c {
case '(', ')', '-':
continue
case ' ':
if last == '_' {
continue // skip two consecutive spaces
}
last = '_'
tidy.WriteRune(last)
case '<':
inside = true
case '>':
inside = false
default:
if inside {
continue
}
last = rune(strings.ToLower(string(c))[0])
tidy.WriteRune(last)
}
}
return tidy.String()
}
// stylesheet returns a link web element
func stylesheet(href string) *Element {
return Link(Rel("stylesheet"), Type("text/css"), Href(href))
}
// Boxnote returns a small box aligned to the left with given top
// margin in cm.
func sidenote(el interface{}, cm float64) *Element {
return Div(Class("sidenote"),
&Attribute{
Name: "style",
Val: fmt.Sprintf("margin-top: %vcm", cm),
},
Div(Class("inner"), el),
)
}
// loadFullFile returns a wrapped element with label and file contents.
// If label is empty string the filename last part is used.
func loadFullFile(label, filename string) *Element {
if label == "" {
dir := filepath.Base(filepath.Dir(filename))
label = path.Join(dir, filepath.Base(filename))
}
return Wrap(
Div(Class("filename"), label),
loadFile(filename, 0, -1),
)
}
// loadFile returns a pre web element wrapping the contents from the
// given file. If to == -1 all lines to the end of file are returned.
func loadFile(filename string, span ...int) *Element {
from, to := 0, -1
if len(span) == 2 {
from, to = span[0], span[1]
}
v := files.MustLoadLines(filename, from, to)
class := "srcfile"
if from == 0 && to == -1 {
class += " complete"
}
ext := filepath.Ext(filename)
return Pre(Class(class), Code(Class(ext[1:]), v))
}
func gregoryv(name, txt string) *Element {
return Li(
fmt.Sprintf(
`<a href="https://github.com/gregoryv/%s">%s</a> - %s`,
name, name, txt,
),
)
}
func example(args string, files ...string) *Element {
res, err := runExample(args, files...)
if err != nil {
log.Println(err, files)
}
return shellCommand(string(res))
}
func loadExample(filename string) *Element {
src := loadAs(filename, "init", "main")
i := strings.Index(src, "\n") // first line
fn := strings.Index(src, "\npackage")
var block string
if fn > i {
block = src[i+1 : fn]
block = strings.ReplaceAll(block, "//", "")
}
e := Wrap(
H1(src[3:i]),
P(block),
Div(Class("filename"), filename),
Pre(Class("srcfile complete"),
Code(Class("go"), src[fn+1:]),
),
)
return e
}
func skipFirstLine(in string) string {
i := strings.Index(in, "\n") // skip first line, which should be comment
return in[i+1:]
}
func loadAs(filename, fn, as string) string {
data := files.MustLoad(filename)
return strings.ReplaceAll(data, fn, as)
}
// shellCommand returns a web Element wrapping shell commands
func shellCommand(v string) *Element {
return Pre(Class("command"), Code(v))
}
func linkDrill(filename string) *Element {
title := drillTitle(filename)
return Li(A(Href(toHtmlFile(filename)), title))
}
func drillTitle(filename string) string {
line := firstLine(filename)
title := line
parts := strings.Split(line, ";")
if len(parts) > 1 {
title = parts[1]
}
return title
}
func toHtmlFile(filename string) string {
return strings.Replace(filename, ".go", ".html", 1)
}
func firstLine(filename string) string {
fh := openFile(filename)
defer fh.Close()
line := readLine(fh)
if line[:2] != "//" {
log.Fatal("missing file comment: ", filename)
}
return line[3:] // skip first comment '// '
}
func openFile(filename string) io.ReadCloser {
fh, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
return fh
}
func readLine(r io.Reader) string {
s := bufio.NewScanner(r)
s.Scan()
return s.Text()
}
// runExample first file contains init() that is renamed to main()
func runExample(args string, files ...string) ([]byte, error) {
first := files[0]
// Use name of first file as command name, so we can have many
// files in same directory, but speed up builds
name := filepath.Base(first)
i := strings.Index(name, ".")
dir := filepath.Join("./build", name[:i])
scriptFile := filepath.Join(dir, name)
outfile := filepath.Join(dir, "output.txt")
// remove so we have less noise during grep
defer os.RemoveAll(scriptFile)
if changed(first, outfile) {
data, err := ioutil.ReadFile(first)
if err != nil {
return nil, err
}
// modify drill to contain a main func
data = bytes.ReplaceAll(data, []byte("func init("), []byte("func main("))
data = bytes.ReplaceAll(data, []byte("package drill"), []byte("package main"))
if err := os.MkdirAll(dir, 0722); err != nil {
return nil, err
}
if err := ioutil.WriteFile(scriptFile, data, 0644); err != nil {
return nil, err
}
log.Println(scriptFile)
parts := strings.Split(args, " ")
fullArgs := append(
[]string{"run", filepath.Base(scriptFile)}, parts...,
)
// run the drill
cmd := exec.Command("go", fullArgs...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
// combine command line and output
var buf bytes.Buffer
buf.WriteString("$ ")
cmdline := cmd.String()
cmdline = strings.Replace(cmdline, "/home/gregory/dl/go1/go/bin/", "", 1)
buf.WriteString(cmdline)
buf.WriteString("\n")
buf.Write(out)
if err := os.WriteFile(outfile, buf.Bytes(), 0644); err != nil {
return nil, err
}
return buf.Bytes(), err
}
return os.ReadFile(outfile)
}
// changed returns true if the src has been changed after the dst file
// Returns false on stat errors
func changed(src, dst string) bool {
s, err := os.Stat(src)
if err != nil {
return true
}
d, err := os.Stat(dst)
if err != nil {
return true
}
return s.ModTime().After(d.ModTime())
}