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:
parent
c997ca7c42
commit
9959bb41ae
6
build.go
6
build.go
@ -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
|
||||
}
|
||||
}
|
||||
|
8
main.go
8
main.go
@ -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
10
page.go
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user