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"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Site) prepareRendering() error {
|
|
|
|
if !s.preparedToRender {
|
|
|
|
if err := s.initializeRenderingPipeline(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-03 16:39:55 +02:00
|
|
|
if err := s.setPageContent(); err != nil {
|
2017-06-29 17:00:59 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.preparedToRender = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:37:23 +02:00
|
|
|
// WriteDocument writes the document to a writer.
|
|
|
|
func (s *Site) WriteDocument(w io.Writer, p pages.Document) error {
|
2017-06-29 17:00:59 +02:00
|
|
|
if err := s.prepareRendering(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-09 15:37:23 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2017-07-10 19:23:51 +02:00
|
|
|
if err := p.Write(buf); err != nil {
|
2017-07-09 15:37:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c := buf.Bytes()
|
|
|
|
err := s.runHooks(func(p plugins.Plugin) error {
|
|
|
|
c = p.PostRender(c)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-09 19:12:30 +02:00
|
|
|
_, err = w.Write(c)
|
|
|
|
return err
|
2017-06-29 17:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WritePages writes output files.
|
|
|
|
// It attends to options.dry_run.
|
2017-07-04 23:13:47 +02:00
|
|
|
func (s *Site) WritePages(options BuildOptions) (count int, err error) {
|
2017-07-02 00:11:35 +02:00
|
|
|
errs := make(chan error)
|
2017-06-29 17:00:59 +02:00
|
|
|
for _, p := range s.OutputPages() {
|
|
|
|
count++
|
2017-07-02 00:11:35 +02:00
|
|
|
go func(p pages.Document) {
|
2017-07-09 15:37:23 +02:00
|
|
|
errs <- s.SavePage(p, options)
|
2017-07-02 00:11:35 +02:00
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
// might as well report the last error as the first
|
2017-07-04 23:13:47 +02:00
|
|
|
// TODO return an aggregate
|
2017-07-02 00:11:35 +02:00
|
|
|
if e := <-errs; e != nil {
|
|
|
|
err = e
|
2017-06-29 17:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 00:11:35 +02:00
|
|
|
return count, err
|
2017-06-29 17:00:59 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 15:37:23 +02:00
|
|
|
// SavePage writes a document to the destination directory.
|
2017-06-29 17:00:59 +02:00
|
|
|
// It attends to options.dry_run.
|
2017-07-09 15:37:23 +02:00
|
|
|
func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
|
2017-07-09 01:57:41 +02:00
|
|
|
from := filepath.Join(s.SourceDir(), filepath.ToSlash(p.SourcePath()))
|
2017-07-01 02:00:38 +02:00
|
|
|
to := filepath.Join(s.DestDir(), p.Permalink())
|
2017-06-29 17:00:59 +02:00
|
|
|
if !p.Static() && filepath.Ext(to) == "" {
|
|
|
|
to = filepath.Join(to, "index.html")
|
|
|
|
}
|
|
|
|
if options.Verbose {
|
2017-07-09 01:57:41 +02:00
|
|
|
fmt.Println("create", to, "from", p.SourcePath())
|
2017-06-29 17:00:59 +02:00
|
|
|
}
|
|
|
|
if options.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 p.Static() && options.UseHardLinks:
|
|
|
|
return os.Link(from, to)
|
|
|
|
case p.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-09 15:37:23 +02:00
|
|
|
return s.SaveDocumentToFile(p, to)
|
2017-06-29 17:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-09 06:21:02 +02:00
|
|
|
|
2017-07-09 15:37:23 +02:00
|
|
|
// SaveDocumentToFile writes a page to filename.
|
|
|
|
func (s *Site) SaveDocumentToFile(d pages.Document, filename string) error {
|
2017-07-09 22:17:20 +02:00
|
|
|
return utils.VisitCreatedFile(filename, func(w io.Writer) error {
|
2017-07-09 15:37:23 +02:00
|
|
|
return s.WriteDocument(w, d)
|
|
|
|
})
|
|
|
|
}
|