1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 08:08:59 +01:00
gojekyll/site/write.go

116 lines
2.6 KiB
Go
Raw 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/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
2017-07-09 22:17:20 +02:00
"github.com/osteele/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-07-13 03:17:11 +02:00
go func(d pages.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.
func (s *Site) WriteDoc(d pages.Document) error {
2017-07-13 03:17:11 +02:00
from := d.SourcePath()
2017-08-09 15:08:32 +02:00
rel := d.Permalink()
if !d.Static() && filepath.Ext(rel) == "" {
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-07-13 03:17:11 +02:00
fmt.Println("create", to, "from", d.SourcePath())
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-07-13 03:17:11 +02:00
case d.Static():
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-07-10 20:14:42 +02:00
func (s *Site) WriteDocument(w io.Writer, d pages.Document) error {
2017-07-13 03:17:11 +02:00
switch p := d.(type) {
case pages.Page:
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-07-13 03:17:11 +02:00
func (s *Site) WritePage(w io.Writer, p pages.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
}