-
Notifications
You must be signed in to change notification settings - Fork 8
/
slateficate.go
137 lines (131 loc) · 3.24 KB
/
slateficate.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
package slate
import (
"os"
"io"
"github.com/growler/go-slate/slate/internal/slate"
"sort"
"strings"
"fmt"
"github.com/spf13/afero"
"io/ioutil"
)
func makeTargetDirs(fs *afero.Afero, dirs ...string) error {
for _, d := range dirs {
if err := fs.MkdirAll(d, 0755); err != nil {
return err
}
}
return nil
}
// Configuration
type Params struct {
MinifyHTML bool // produce compact HTML
MinifyJS bool // minify Javascript
MinifyCSS bool // produce compact CSS
StyleFile string // load SCSS overrides
LogoFile string // use this logo (which should be located in images/ directory)
Search *bool // if nil, use the default from index.html.md preamble
RTL *bool // Right-to-Left CSS, if nil, use the default from index.html.md preamble
}
// Go Slate!
func Slateficate(src string, target *afero.Afero, params Params) error {
var err error
fs, err := slate.NewUnionFS(src)
if err != nil {
return err
}
err = makeTargetDirs(target, "javascripts", "stylesheets", "fonts", "images")
if err != nil {
return err
}
input, err := load(fs, params)
if err != nil {
return err
}
if err = input.produce(target, params.MinifyHTML); err != nil {
return err
}
if err = copyJavaScripts(fs, target, input.Params.Search, params.MinifyJS); err != nil {
return err
}
var styles []string
if input.Params.Style != "" {
styles = append(styles, input.Params.Style)
}
if params.StyleFile != "" {
file, err := os.OpenFile(params.StyleFile, os.O_RDONLY, 0)
if err != nil {
return err
}
data, err := ioutil.ReadAll(file)
file.Close()
if err != nil {
return err
}
styles = append(styles, string(data))
}
if err = copyStylesheetsAndFonts(fs, target, styles, input.Params.RTLEnabled, params.MinifyCSS); err != nil {
return err
}
if err = copyImages(fs, target, input.Params.Logo); err != nil {
return err
}
return nil
}
// Extract embedded slate components to target
func Extract(target string, overwrite bool, components ...string) error {
var srcFiles []string
var dstFiles = make(map[string]bool)
var dst []string
slate.FS().Walk("", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
srcFiles = append(srcFiles, path)
}
return nil
})
sort.Strings(srcFiles)
for _, c := range components {
switch c {
case "all":
dst = srcFiles
goto Copy
case "contents":
dstFiles["index.html.md"] = true
dstFiles["includes/_errors.md"] = true
case "fonts", "images", "layouts", "javascripts", "stylesheets":
for _, s := range srcFiles {
if strings.HasPrefix(s, c + "/") {
dstFiles[s] = true
}
}
default:
if n := sort.SearchStrings(srcFiles, c); n < len(srcFiles) && srcFiles[n] == c {
dstFiles[c] = true
} else {
return fmt.Errorf("unknown slate component or file %s", c)
}
}
}
for f := range dstFiles {
dst = append(dst, f)
}
Copy:
if err := slate.CopyTo(target, 0640, overwrite, dst...); err != nil {
return err
}
return nil
}
func ListBundled(w io.Writer) {
var srcFiles []string
slate.FS().Walk("", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
srcFiles = append(srcFiles, path)
}
return nil
})
sort.Strings(srcFiles)
for _, s := range srcFiles {
w.Write([]byte(s))
w.Write([]byte{'\n'})
}
}