2017-07-04 15:09:36 +02:00
|
|
|
package site
|
2017-06-29 17:00:59 +02:00
|
|
|
|
|
|
|
import (
|
2017-07-09 06:21:02 +02:00
|
|
|
"bytes"
|
2017-06-29 17:00:59 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-18 15:33:02 +02:00
|
|
|
"runtime"
|
2017-07-10 20:14:42 +02:00
|
|
|
"strings"
|
2017-06-29 17:00:59 +02:00
|
|
|
|
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-07-09 06:21:02 +02:00
|
|
|
"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-07-13 03:17:11 +02:00
|
|
|
// WriteFiles writes output files.
|
2017-06-29 17:00:59 +02:00
|
|
|
// It attends to options.dry_run.
|
2017-07-17 14:00:13 +02:00
|
|
|
func (s *Site) WriteFiles() (count int, err error) {
|
2017-07-18 15:33:02 +02:00
|
|
|
errs := make(chan error, runtime.NumCPU())
|
2017-07-13 03:17:11 +02:00
|
|
|
for _, p := 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-17 14:00:13 +02:00
|
|
|
errs <- s.WriteDoc(d)
|
2017-07-02 00:11:35 +02:00
|
|
|
}(p)
|
|
|
|
}
|
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-06-29 17:00:59 +02:00
|
|
|
// It attends to options.dry_run.
|
2017-07-17 14:00:13 +02:00
|
|
|
func (s *Site) WriteDoc(d pages.Document) error {
|
2017-07-13 03:17:11 +02:00
|
|
|
from := d.SourcePath()
|
|
|
|
to := filepath.Join(s.DestDir(), d.Permalink())
|
|
|
|
if !d.Static() && filepath.Ext(to) == "" {
|
2017-06-29 17:00:59 +02:00
|
|
|
to = filepath.Join(to, "index.html")
|
|
|
|
}
|
2017-07-17 14:00:13 +02:00
|
|
|
if s.config.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-07-17 14:00:13 +02:00
|
|
|
if s.config.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
|
|
|
}
|
|
|
|
}
|
2017-07-09 06:21:02 +02:00
|
|
|
|
2017-07-10 20:14:42 +02:00
|
|
|
// WriteDocument writes the document to a writer.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePage writes the page to a writer.
|
|
|
|
func (s *Site) WritePage(w io.Writer, p pages.Page) error {
|
2017-07-10 20:14:42 +02:00
|
|
|
if err := s.prepareRendering(); err != nil {
|
|
|
|
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) error {
|
|
|
|
b = p.PostRender(b)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func combineErrors(errs []error) error {
|
|
|
|
switch len(errs) {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return errs[0]
|
|
|
|
default:
|
|
|
|
messages := make([]string, len(errs))
|
|
|
|
for i, e := range errs {
|
|
|
|
messages[i] = e.Error()
|
|
|
|
}
|
|
|
|
return fmt.Errorf(strings.Join(messages, "\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) prepareRendering() error {
|
|
|
|
if !s.preparedToRender {
|
|
|
|
if err := s.initializeRenderingPipeline(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.setPageContent(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.preparedToRender = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|