1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 23:34:47 +01:00
gojekyll/site/write.go

115 lines
2.5 KiB
Go
Raw Permalink Normal View History

2017-07-04 15:09:36 +02:00
package site
2017-06-29 17:00:59 +02:00
import (
"bytes"
2017-06-29 17:00:59 +02:00
"fmt"
"io"
"os"
"path/filepath"
"github.com/danog/gojekyll/plugins"
"github.com/danog/gojekyll/utils"
2017-06-29 17:00:59 +02:00
)
2017-08-16 21:50:31 +02:00
// 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()
}
2017-07-13 03:17:11 +02:00
// WriteFiles writes output files.
func (s *Site) WriteFiles() (count int, err error) {
2017-07-27 23:02:11 +02:00
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() {
2017-06-29 17:00:59 +02:00
count++
2017-09-03 18:18:17 +02:00
go func(d Document) {
2017-07-27 23:02:11 +02:00
<-sem
errs <- s.WriteDoc(d)
2017-07-27 23:02:11 +02:00
sem <- true
}(d)
2017-07-02 00:11:35 +02:00
}
2017-07-10 20:14:42 +02:00
var errList []error
2017-07-02 00:11:35 +02:00
for i := 0; i < count; i++ {
if e := <-errs; e != nil {
2017-07-10 20:14:42 +02:00
errList = append(errList, e)
2017-06-29 17:00:59 +02:00
}
}
2017-07-10 20:14:42 +02:00
return count, combineErrors(errList)
2017-06-29 17:00:59 +02:00
}
2017-07-13 03:17:11 +02:00
// WriteDoc writes a document to the destination directory.
2017-09-03 18:18:17 +02:00
func (s *Site) WriteDoc(d Document) error {
2017-09-02 19:53:50 +02:00
from := d.Source()
rel := d.URL()
if !d.IsStatic() && filepath.Ext(rel) == "" {
2017-08-09 15:08:32 +02:00
rel = filepath.Join(rel, "index.html")
2017-06-29 17:00:59 +02:00
}
2017-08-09 15:08:32 +02:00
to := filepath.Join(s.DestDir(), rel)
2017-08-24 18:25:38 +02:00
if s.cfg.Verbose {
2017-09-02 19:53:50 +02:00
fmt.Println("create", to, "from", d.Source())
2017-06-29 17:00:59 +02:00
}
2017-08-24 18:25:38 +02:00
if s.cfg.DryRun {
2017-06-29 17:00:59 +02:00
// 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 {
2017-09-02 19:53:50 +02:00
case d.IsStatic():
2017-07-09 22:17:20 +02:00
return utils.CopyFileContents(to, from, 0644)
2017-06-29 17:00:59 +02:00
default:
2017-07-13 03:17:11 +02:00
return utils.VisitCreatedFile(to, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
2017-06-29 17:00:59 +02:00
}
}
// WriteDocument writes the rendered document.
2017-09-03 18:18:17 +02:00
func (s *Site) WriteDocument(w io.Writer, d Document) error {
2017-07-13 03:17:11 +02:00
switch p := d.(type) {
2017-09-03 18:18:17 +02:00
case Page:
2017-07-13 03:17:11 +02:00
return s.WritePage(w, p)
default:
return d.Write(w)
}
}
2017-08-16 21:50:31 +02:00
// 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.
2017-09-03 18:18:17 +02:00
func (s *Site) WritePage(w io.Writer, p Page) error {
if err := s.ensureRendered(); err != nil {
2017-07-10 20:14:42 +02:00
return err
}
buf := new(bytes.Buffer)
2017-07-13 03:17:11 +02:00
if err := p.Write(buf); err != nil {
2017-07-10 20:14:42 +02:00
return err
}
b := buf.Bytes()
err := s.runHooks(func(p plugins.Plugin) (err error) {
b, err = p.PostRender(b)
return
2017-07-10 20:14:42 +02:00
})
if err != nil {
return err
}
_, err = w.Write(b)
return err
}