-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtasks.go
More file actions
200 lines (153 loc) · 4.17 KB
/
Copy pathtasks.go
File metadata and controls
200 lines (153 loc) · 4.17 KB
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
package cutedoc
import (
"bytes"
"errors"
"github.com/nickthedev/cutedoc/template"
"github.com/shurcooL/github_flavored_markdown"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/js"
"io/ioutil"
"log"
"os"
"strings"
tmpl "text/template"
)
var (
// tasks represents all the steps of the generation process.
tasks = []task{
initBuild,
copyBranding,
copyScript,
copyStatic,
copyTemplate,
}
// templates represent the supported themes.
templates = map[string]string{
"minimal": "template/minimal",
}
// packer compresses assets for production.
minifier = minify.New()
)
// tasks represents a step of the generation process.
type task func(doc *Doc) error
// copyImage copies the image to the output minimal directory if it is not null.
func copyImage(doc *Doc, image, output string) error {
if image != "" {
icon, err := ioutil.ReadFile(image)
if err != nil {
return err
}
if err := os.MkdirAll(doc.Build.Dir+"/static/img", os.ModePerm); err != nil {
return err
}
ioutil.WriteFile(doc.Build.Dir+"/static/img/"+output, icon, os.ModePerm)
}
return nil
}
// initBuild checks the template and creates the output directory.
func initBuild(doc *Doc) error {
if _, ok := templates[doc.Theme.Template]; !ok {
return errors.New("unsupported template theme")
}
if err := os.RemoveAll(doc.Build.Dir); err != nil {
return err
}
return os.MkdirAll(doc.Build.Dir, os.ModePerm)
}
// copyBranding copies the logo and icon if they are not null to the minimal output directory.
func copyBranding(doc *Doc) error {
if err := copyImage(doc, doc.Meta.Branding.Icon, "icon.png"); err != nil {
return err
}
if err := copyImage(doc, doc.Meta.Branding.Logo, "logo.png"); err != nil {
return err
}
return nil
}
// copyScript copies the template script output minimal directory.
func copyScript(doc *Doc) error {
if err := os.MkdirAll(doc.Build.Dir+"/static/js", os.ModePerm); err != nil {
return err
}
asset, err := template.Asset(templates[doc.Theme.Template] + "/main.js.tmpl")
if err != nil {
return err
}
if doc.Build.Minify {
packed, err := minifier.Bytes("text/javascript", asset)
if err != nil {
return err
} else {
asset = packed
}
}
return ioutil.WriteFile(doc.Build.Dir+"/static/js/main.js", asset, os.ModePerm)
}
// copyStatic transfers the template stylesheet to the output minimal directory.
func copyStatic(doc *Doc) error {
if err := os.MkdirAll(doc.Build.Dir+"/static/css", os.ModePerm); err != nil {
return err
}
asset, err := template.Asset(templates[doc.Theme.Template] + "/main.css.tmpl")
if err != nil {
return err
}
format, err := tmpl.New("style").Parse(string(asset))
if err != nil {
return err
}
var buffer bytes.Buffer
if err := format.Execute(&buffer, doc.Theme.Colors); err != nil {
return err
}
var data = buffer.Bytes()
if doc.Build.Minify {
data, err = minifier.Bytes("text/css", data)
if err != nil {
return err
}
}
return ioutil.WriteFile(doc.Build.Dir+"/static/css/main.css", data, os.ModePerm)
}
// copyTemplate injects the doc model into the template page and copies it to the output directory.
func copyTemplate(doc *Doc) error {
asset, err := template.Asset(templates[doc.Theme.Template] + "/index.html.tmpl")
if err != nil {
return err
}
format, err := tmpl.New("doc").Funcs(tmpl.FuncMap{
"readContent": func(path string) string {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("An error occured generating documentation: \n\t%v.", err)
}
return string(github_flavored_markdown.Markdown(content))
},
"idFrom": func(name string) string {
return strings.Replace(strings.ToLower(name), " ", "-", -1)
},
}).Parse(string(asset))
if err != nil {
return err
}
dst, err := os.Create(doc.Build.Dir + "/index.html")
if err != nil {
return err
}
defer dst.Close()
return format.Execute(dst, doc)
}
// Run executes the documentation generation tasks.
func Run(doc *Doc) error {
if doc.Build.Minify {
minifier.AddFunc("text/css", css.Minify)
minifier.AddFunc("text/javascript", js.Minify)
}
for _, task := range tasks {
if err := task(doc); err != nil {
return err
}
}
return nil
}