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

Page.Render takes an io.Writer

This commit is contained in:
Oliver Steele 2017-06-11 20:51:01 -04:00
parent c997ca7c42
commit 9959bb41ae
4 changed files with 13 additions and 17 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -54,11 +53,12 @@ func build() error {
}
} else {
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath)
body, err := page.Render()
f, err := os.Create(destPath)
if err != nil {
return err
}
if err = ioutil.WriteFile(destPath, body, 0644); err != nil {
defer f.Close()
if err := page.Render(f); err != nil {
return err
}
}

View File

@ -108,14 +108,12 @@ func main() {
fmt.Println(err)
break
}
body, err := page.Render()
if err != nil {
printPathSetting("Render:", filepath.Join(siteConfig.SourceDir, path))
printSetting("URL:", page.Permalink)
if err := page.Render(os.Stdout); err != nil {
fmt.Println(err)
break
}
printPathSetting("Render:", filepath.Join(siteConfig.SourceDir, path))
printSetting("URL:", page.Permalink)
fmt.Println(string(body))
default:
fmt.Println("A subcommand is required.")
}

10
page.go
View File

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
@ -114,7 +115,7 @@ func readPage(path string, defaults map[interface{}]interface{}) (*Page, error)
}
// Render applies Liquid and Markdown, as appropriate.
func (p Page) Render() ([]byte, error) {
func (p Page) Render(w io.Writer) error {
var (
path = p.Path
ext = filepath.Ext(path)
@ -129,7 +130,7 @@ func (p Page) Render() ([]byte, error) {
template, err := liquid.Parse(p.Content, nil)
if err != nil {
err := &os.PathError{Op: "Liquid Error", Path: path, Err: err}
return nil, err
return err
}
writer := new(bytes.Buffer)
@ -137,10 +138,11 @@ func (p Page) Render() ([]byte, error) {
body := writer.Bytes()
if ext == ".md" {
body = blackfriday.MarkdownBasic(body)
body = blackfriday.MarkdownCommon(body)
}
return body, nil
_, err = w.Write(body)
return err
}
func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, path string) string {

View File

@ -28,12 +28,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
return
}
body, err := p.Render()
err := p.Render(w)
if err != nil {
fmt.Printf("Error rendering %s: %s", path, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
if _, err := w.Write(body); err != nil {
fmt.Printf("Error writing %s: %s", path, err)
}
}