/
ace.go
94 lines (74 loc) · 1.68 KB
/
ace.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
package ace
// github.com/yosssi/gcss binding for Gonzo.
import (
"bytes"
html "html/template"
"io/ioutil"
"path/filepath"
"strings"
"github.com/omeid/gonzo/context"
"github.com/omeid/gonzo"
"github.com/yosssi/ace"
)
type (
Options ace.Options
)
func Compile(options Options, data interface{}) gonzo.Stage {
return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error {
options := ace.Options(options)
fs := []*ace.File{}
for {
select {
case file, ok := <-in:
if !ok {
return nil
}
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(file)
file.Close()
if err != nil {
ctx.Error(err)
continue
}
s, err := file.Stat()
if err != nil {
return err
}
name := s.Name() //Probably filepath.Rel(file.Dir, file.Path) ??
f := ace.NewFile(name, buf.Bytes())
source := ace.NewSource(
ace.NewFile("", nil),
f,
fs,
)
fs = append(fs, f)
r, err := ace.ParseSource(source, &options)
if err != nil {
ctx.Error(err)
continue
}
t, err := ace.CompileResultWithTemplate(html.New(name), r, &options)
if err != nil {
ctx.Error(err)
continue
}
if filepath.Base(file.FileInfo().Name())[0] != '_' {
buf := new(bytes.Buffer)
err = t.Execute(buf, data)
if err != nil {
ctx.Error(err)
continue
}
file = gonzo.NewFile(ioutil.NopCloser(buf), file.FileInfo())
file.FileInfo().SetSize(int64(buf.Len()))
name = strings.TrimSuffix(file.FileInfo().Name(), ".ace") + ".html"
file.FileInfo().SetName(name)
}
out <- file
case <-ctx.Done():
return ctx.Err()
}
return nil
}
}
}