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

84 lines
1.9 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 (
"fmt"
"io"
"os"
"path/filepath"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
)
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
}
// WriteDocument writes the document to w.
func (s *Site) WriteDocument(p pages.Document, w io.Writer) error {
if err := s.prepareRendering(); err != nil {
return err
}
2017-07-04 23:36:06 +02:00
return p.Write(w, s)
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) {
errs <- s.WritePage(p, options)
}(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
}
// WritePage writes a page to the destination directory.
// It attends to options.dry_run.
func (s *Site) WritePage(p pages.Document, options BuildOptions) error {
from := filepath.Join(s.SourceDir(), filepath.ToSlash(p.SiteRelPath()))
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 {
fmt.Println("create", to, "from", p.SiteRelPath())
}
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():
return helpers.CopyFileContents(to, from, 0644)
default:
2017-07-04 23:36:06 +02:00
return helpers.VisitCreatedFile(to, func(w io.Writer) error {
2017-07-06 01:23:05 +02:00
return p.Write(w, s)
2017-07-04 23:36:06 +02:00
})
2017-06-29 17:00:59 +02:00
}
}