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

Rearrange; plugin.PostRender returns an error

This commit is contained in:
Oliver Steele 2017-08-10 11:18:36 -04:00
parent bffb3518c6
commit caf5068234
7 changed files with 73 additions and 66 deletions

View File

@ -22,7 +22,7 @@ type Plugin interface {
ConfigureTemplateEngine(*liquid.Engine) error ConfigureTemplateEngine(*liquid.Engine) error
ModifySiteDrop(Site, map[string]interface{}) error ModifySiteDrop(Site, map[string]interface{}) error
PostRead(Site) error PostRead(Site) error
PostRender([]byte) []byte PostRender([]byte) ([]byte, error)
} }
// Site is the site interface that is available to a plugin. // Site is the site interface that is available to a plugin.
@ -61,7 +61,7 @@ func (p plugin) Initialize(Site) error { return nil
func (p plugin) ConfigureTemplateEngine(*liquid.Engine) error { return nil } func (p plugin) ConfigureTemplateEngine(*liquid.Engine) error { return nil }
func (p plugin) ModifySiteDrop(Site, map[string]interface{}) error { return nil } func (p plugin) ModifySiteDrop(Site, map[string]interface{}) error { return nil }
func (p plugin) PostRead(Site) error { return nil } func (p plugin) PostRead(Site) error { return nil }
func (p plugin) PostRender(b []byte) []byte { return b } func (p plugin) PostRender(b []byte) ([]byte, error) { return b, nil }
var directory = map[string]Plugin{} var directory = map[string]Plugin{}
@ -90,11 +90,10 @@ func init() {
// jemojiPlugin emulates the jekyll-jemoji plugin. // jemojiPlugin emulates the jekyll-jemoji plugin.
type jemojiPlugin struct{ plugin } type jemojiPlugin struct{ plugin }
func (p jemojiPlugin) PostRender(b []byte) []byte { func (p jemojiPlugin) PostRender(b []byte) ([]byte, error) {
return utils.ApplyToHTMLText(b, func(s string) string { return utils.ApplyToHTMLText(b, func(s string) string {
s = emoji.Sprint(s) return emoji.Sprint(s)
return s }), nil
})
} }
// jekyllMentionsPlugin emulates the jekyll-mentions plugin. // jekyllMentionsPlugin emulates the jekyll-mentions plugin.
@ -102,10 +101,10 @@ type jekyllMentionsPlugin struct{ plugin }
var mentionPattern = regexp.MustCompile(`@(\w+)`) var mentionPattern = regexp.MustCompile(`@(\w+)`)
func (p jekyllMentionsPlugin) PostRender(b []byte) []byte { func (p jekyllMentionsPlugin) PostRender(b []byte) ([]byte, error) {
return utils.ApplyToHTMLText(b, func(s string) string { return utils.ApplyToHTMLText(b, func(s string) string {
return mentionPattern.ReplaceAllString(s, `<a href="https://github.com/$1" class="user-mention">@$1</a>`) return mentionPattern.ReplaceAllString(s, `<a href="https://github.com/$1" class="user-mention">@$1</a>`)
}) }), nil
} }
// jekyllOptionalFrontMatterPlugin emulates the jekyll-optional-front-matter plugin. // jekyllOptionalFrontMatterPlugin emulates the jekyll-optional-front-matter plugin.

View File

@ -44,7 +44,7 @@ func (s *Site) Build() (int, error) {
if err := s.setTimeZone(); err != nil { if err := s.setTimeZone(); err != nil {
return 0, err return 0, err
} }
if err := s.prepareRendering(); err != nil { if err := s.ensureRendered(); err != nil {
return 0, err return 0, err
} }
if err := s.Clean(); err != nil { if err := s.Clean(); err != nil {

View File

@ -12,7 +12,7 @@ import (
// ToLiquid returns the site variable for template evaluation. // ToLiquid returns the site variable for template evaluation.
func (s *Site) ToLiquid() interface{} { func (s *Site) ToLiquid() interface{} {
s.Do(func() { s.dropOnce.Do(func() {
if err := s.initializeDrop(); err != nil { if err := s.initializeDrop(); err != nil {
log.Fatalf("ToLiquid failed: %s\n", err) log.Fatalf("ToLiquid failed: %s\n", err)
} }
@ -51,16 +51,6 @@ func (s *Site) initializeDrop() error {
}) })
} }
// Render renders the site's pages.
func (s *Site) Render() error {
for _, c := range s.Collections {
if err := c.Render(); err != nil {
return err
}
}
return nil
}
// The following functions are only used in the drop, therefore they're // The following functions are only used in the drop, therefore they're
// non-public and they're listed here. // non-public and they're listed here.
// //

21
site/errors.go Normal file
View File

@ -0,0 +1,21 @@
package site
import (
"fmt"
"strings"
)
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"))
}
}

25
site/render.go Normal file
View File

@ -0,0 +1,25 @@
package site
// Render renders the site's pages.
func (s *Site) Render() error {
for _, c := range s.Collections {
if err := c.Render(); err != nil {
return err
}
}
return nil
}
func (s *Site) ensureRendered() (err error) {
s.renderOnce.Do(func() {
err = s.initializeRenderingPipeline()
if err != nil {
return
}
err = s.Render()
if err != nil {
return
}
})
return
}

View File

@ -23,12 +23,15 @@ type Site struct {
config config.Config config config.Config
data map[string]interface{} data map[string]interface{}
flags config.Flags flags config.Flags
pipeline *pipelines.Pipeline
themeDir string themeDir string
docs []pages.Document // all documents, whether or not they are output docs []pages.Document // all documents, whether or not they are output
preparedToRender bool
pipeline *pipelines.Pipeline
renderOnce sync.Once
drop map[string]interface{} // cached drop value drop map[string]interface{} // cached drop value
sync.Once // for computing the drop dropOnce sync.Once
} }
// SourceDir returns the site source directory. // SourceDir returns the site source directory.

View File

@ -6,7 +6,6 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/osteele/gojekyll/pages" "github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins" "github.com/osteele/gojekyll/plugins"
@ -14,7 +13,6 @@ import (
) )
// WriteFiles writes output files. // WriteFiles writes output files.
// It attends to options.dry_run.
func (s *Site) WriteFiles() (count int, err error) { func (s *Site) WriteFiles() (count int, err error) {
errs := make(chan error) errs := make(chan error)
// without this, large sites run out of file descriptors // without this, large sites run out of file descriptors
@ -40,7 +38,6 @@ func (s *Site) WriteFiles() (count int, err error) {
} }
// WriteDoc writes a document to the destination directory. // WriteDoc writes a document to the destination directory.
// It attends to options.dry_run.
func (s *Site) WriteDoc(d pages.Document) error { func (s *Site) WriteDoc(d pages.Document) error {
from := d.SourcePath() from := d.SourcePath()
rel := d.Permalink() rel := d.Permalink()
@ -69,7 +66,7 @@ func (s *Site) WriteDoc(d pages.Document) error {
} }
} }
// WriteDocument writes the document to a writer. // WriteDocument writes the rendered document.
func (s *Site) WriteDocument(w io.Writer, d pages.Document) error { func (s *Site) WriteDocument(w io.Writer, d pages.Document) error {
switch p := d.(type) { switch p := d.(type) {
case pages.Page: case pages.Page:
@ -79,9 +76,9 @@ func (s *Site) WriteDocument(w io.Writer, d pages.Document) error {
} }
} }
// WritePage writes the page to a writer. // WritePage writes the rendered page.
func (s *Site) WritePage(w io.Writer, p pages.Page) error { func (s *Site) WritePage(w io.Writer, p pages.Page) error {
if err := s.prepareRendering(); err != nil { if err := s.ensureRendered(); err != nil {
return err return err
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@ -89,9 +86,9 @@ func (s *Site) WritePage(w io.Writer, p pages.Page) error {
return err return err
} }
b := buf.Bytes() b := buf.Bytes()
err := s.runHooks(func(p plugins.Plugin) error { err := s.runHooks(func(p plugins.Plugin) (err error) {
b = p.PostRender(b) b, err = p.PostRender(b)
return nil return
}) })
if err != nil { if err != nil {
return err return err
@ -99,31 +96,3 @@ func (s *Site) WritePage(w io.Writer, p pages.Page) error {
_, err = w.Write(b) _, err = w.Write(b)
return err 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.Render(); err != nil {
return err
}
s.preparedToRender = true
}
return nil
}