Buffers instead of pipes

This commit is contained in:
spf13 2013-11-05 00:28:08 -05:00
parent 226bc8f59f
commit 1fbcaf9279

View file

@ -31,6 +31,8 @@ import (
"time" "time"
) )
var _ = transform.AbsURL
var DefaultTimer *nitro.B var DefaultTimer *nitro.B
func MakePermalink(base *url.URL, path *url.URL) *url.URL { func MakePermalink(base *url.URL, path *url.URL) *url.URL {
@ -55,19 +57,19 @@ func MakePermalink(base *url.URL, path *url.URL) *url.URL {
// //
// 5. The entire collection of files is written to disk. // 5. The entire collection of files is written to disk.
type Site struct { type Site struct {
Config Config Config Config
Pages Pages Pages Pages
Tmpl bundle.Template Tmpl bundle.Template
Indexes IndexList Indexes IndexList
Source source.Input Source source.Input
Sections Index Sections Index
Info SiteInfo Info SiteInfo
Shortcodes map[string]ShortcodeFunc Shortcodes map[string]ShortcodeFunc
timer *nitro.B timer *nitro.B
Target target.Output Target target.Output
Alias target.AliasPublisher Alias target.AliasPublisher
Completed chan bool Completed chan bool
RunMode runmode RunMode runmode
} }
type SiteInfo struct { type SiteInfo struct {
@ -167,7 +169,6 @@ func (s *Site) Render() (err error) {
s.timerStep("render and write aliases") s.timerStep("render and write aliases")
s.ProcessShortcodes() s.ProcessShortcodes()
s.timerStep("render shortcodes") s.timerStep("render shortcodes")
s.timerStep("absolute URLify")
if err = s.RenderIndexes(); err != nil { if err = s.RenderIndexes(); err != nil {
return return
} }
@ -580,35 +581,35 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
section, _ = page.RelPermalink() section, _ = page.RelPermalink()
} }
absURL, err := transform.AbsURL(s.Config.BaseUrl) absURL, err := transform.AbsURL(s.Config.BaseUrl)
if err != nil { if err != nil {
return return
} }
transformer := transform.NewChain( transformer := transform.NewChain(
append(absURL, transform.NavActive(section, "hugo-nav")...)... append(absURL, transform.NavActive(section, "hugo-nav")...)...,
) )
renderReader, renderWriter := io.Pipe() var RenderBuffer *bytes.Buffer
go func() {
err = s.renderThing(d, layout, renderWriter) if strings.HasSuffix(out, ".xml") {
if err != nil { RenderBuffer = s.NewXMLBuffer()
// Behavior here should be dependent on if running in server or watch mode. } else {
fmt.Println(fmt.Errorf("Rendering error: %v", err)) RenderBuffer = new(bytes.Buffer)
if !s.Running() { }
os.Exit(-1)
} err = s.renderThing(d, layout, RenderBuffer)
if err != nil {
// Behavior here should be dependent on if running in server or watch mode.
fmt.Println(fmt.Errorf("Rendering error: %v", err))
if !s.Running() {
os.Exit(-1)
} }
}() }
trReader, trWriter := io.Pipe() var outBuffer = new(bytes.Buffer)
go func() { transformer.Apply(outBuffer, RenderBuffer)
transformer.Apply(trWriter, renderReader) return s.WritePublic(out, outBuffer)
trWriter.Close()
}()
return s.WritePublic(out, trReader)
} }
func (s *Site) findFirstLayout(layouts ...string) (layout string) { func (s *Site) findFirstLayout(layouts ...string) (layout string) {
@ -620,16 +621,16 @@ func (s *Site) findFirstLayout(layouts ...string) (layout string) {
return "" return ""
} }
func (s *Site) renderThing(d interface{}, layout string, w io.WriteCloser) error { func (s *Site) renderThing(d interface{}, layout string, w io.Writer) error {
// If the template doesn't exist, then return, but leave the Writer open // If the template doesn't exist, then return, but leave the Writer open
if s.Tmpl.Lookup(layout) == nil { if s.Tmpl.Lookup(layout) == nil {
return fmt.Errorf("Layout not found: %s", layout) return fmt.Errorf("Layout not found: %s", layout)
} }
defer w.Close() //defer w.Close()
return s.Tmpl.ExecuteTemplate(w, layout, d) return s.Tmpl.ExecuteTemplate(w, layout, d)
} }
func (s *Site) whyNewXMLBuffer() *bytes.Buffer { func (s *Site) NewXMLBuffer() *bytes.Buffer {
header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n" header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
return bytes.NewBufferString(header) return bytes.NewBufferString(header)
} }