-
Notifications
You must be signed in to change notification settings - Fork 14
/
write.go
114 lines (107 loc) · 2.49 KB
/
write.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
package site
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
)
// Write cleans the destination and writes files into it.
// It sets TZ from the site config.
func (s *Site) Write() (int, error) {
if err := s.setTimeZone(); err != nil {
return 0, err
}
if err := s.ensureRendered(); err != nil {
return 0, err
}
if err := s.Clean(); err != nil {
return 0, err
}
return s.WriteFiles()
}
// WriteFiles writes output files.
func (s *Site) WriteFiles() (count int, err error) {
errs := make(chan error)
// without this, large sites run out of file descriptors
sem := make(chan bool, 20)
for i, n := 0, cap(sem); i < n; i++ {
sem <- true
}
for _, d := range s.OutputDocs() {
count++
go func(d Document) {
<-sem
errs <- s.WriteDoc(d)
sem <- true
}(d)
}
var errList []error
for i := 0; i < count; i++ {
if e := <-errs; e != nil {
errList = append(errList, e)
}
}
return count, combineErrors(errList)
}
// WriteDoc writes a document to the destination directory.
func (s *Site) WriteDoc(d Document) error {
from := d.Source()
rel := d.URL()
if !d.IsStatic() && filepath.Ext(rel) == "" {
rel = filepath.Join(rel, "index.html")
}
to := filepath.Join(s.DestDir(), rel)
if s.cfg.Verbose {
fmt.Println("create", to, "from", d.Source())
}
if s.cfg.DryRun {
// FIXME render the page, just don't write it
return nil
}
// nolint: gas
if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
return err
}
switch {
case d.IsStatic():
return utils.CopyFileContents(to, from, 0644)
default:
return utils.VisitCreatedFile(to, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
}
}
// WriteDocument writes the rendered document.
func (s *Site) WriteDocument(w io.Writer, d Document) error {
switch p := d.(type) {
case Page:
return s.WritePage(w, p)
default:
return d.Write(w)
}
}
// WritePage writes the rendered page. It is called as part of site.Write,
// but also, in an incremental build, to write a single page – therefore it
// also ensures that all pages have been rendered before writing this one.
func (s *Site) WritePage(w io.Writer, p Page) error {
if err := s.ensureRendered(); err != nil {
return err
}
buf := new(bytes.Buffer)
if err := p.Write(buf); err != nil {
return err
}
b := buf.Bytes()
err := s.runHooks(func(p plugins.Plugin) (err error) {
b, err = p.PostRender(b)
return
})
if err != nil {
return err
}
_, err = w.Write(b)
return err
}