1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 14:37:50 +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 package main
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -54,11 +53,12 @@ func build() error {
} }
} else { } else {
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath) // fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath)
body, err := page.Render() f, err := os.Create(destPath)
if err != nil { if err != nil {
return err return err
} }
if err = ioutil.WriteFile(destPath, body, 0644); err != nil { defer f.Close()
if err := page.Render(f); err != nil {
return err return err
} }
} }

View File

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

10
page.go
View File

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