diff --git a/README.md b/README.md index 390630f..b39a395 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,13 @@ git pull -f osteele `--source DIR` is optional. -`build` needn't be run before `server`. Tbe latter serves from memory. +`build` needn't be run before `server`. The latter serves from memory. `server` only rebuilds individual changed pages, doesn't rebuild collections, and doesn't detect new pages. `render` renders a single file, identified by permalink if it starts with `/`, and by pathname (relative to the source directory) if it doesn't. -`./scripts/gojekyll` invokes `go run` each time. Alternatives to `./scripts/gojekyll` are: `go build && ./gojekyll ...`; or `go install && gojekyll ...` (if `$GOPATH/bin` is on your `$PATH`). These would be nicer for actual use (where the gojekyll sources don't change between invocations), but they aren't as handy during development. +`./scripts/gojekyll` uses `go run` each time it's invoked. Alternatives to it are: `go build && ./gojekyll ...`; or `go install && gojekyll ...` (if `$GOPATH/bin` is on your `$PATH`). These would be nicer for actual use (where the **gojekyll** sources don't change between invocations), but they aren't as handy during development. ## Credits @@ -52,6 +52,8 @@ For rendering Liquid templates: the [acstech/liquid](https://github.com/acstech/ [Hugo](https://gohugo.io) isn't Jekyll-compatible (-), but actually works (+++). +[Jekyll](https://jekyllrb.com), of course. + ## License MIT diff --git a/build.go b/build.go index c4a049f..330391f 100644 --- a/build.go +++ b/build.go @@ -34,7 +34,7 @@ func build() error { } for path, page := range siteMap { if !page.Static { - p, err := readFile(page.Path, siteData, true) + p, err := readPage(page.Path, siteData) if err != nil { return err } @@ -54,7 +54,11 @@ func build() error { } } else { // fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath) - if err := ioutil.WriteFile(destPath, page.Body, 0644); err != nil { + body, err := page.Render() + if err != nil { + return err + } + if err = ioutil.WriteFile(destPath, body, 0644); err != nil { return err } } diff --git a/main.go b/main.go index 8f6e604..354f396 100644 --- a/main.go +++ b/main.go @@ -103,14 +103,19 @@ func main() { return } } - page, e := readFile(path, siteData, true) - if e != nil { - fmt.Println(e) + page, err := readPage(path, siteData) + if err != nil { + fmt.Println(err) + break + } + body, err := page.Render() + if err != nil { + fmt.Println(err) break } printPathSetting("Render:", filepath.Join(siteConfig.SourceDir, path)) printSetting("URL:", page.Permalink) - fmt.Println(string(page.Body)) + fmt.Println(string(body)) default: fmt.Println("A subcommand is required.") } diff --git a/page.go b/page.go index 3d35d79..18c2b8a 100644 --- a/page.go +++ b/page.go @@ -36,10 +36,9 @@ type Page struct { Path string Permalink string Static bool - Expanded bool Published bool FrontMatter *map[interface{}]interface{} - Body []byte + Content []byte } func (p Page) String() string { @@ -60,7 +59,7 @@ func (p Page) CollectionItemData() map[interface{}]interface{} { return data } -func readFile(path string, defaults map[interface{}]interface{}, expand bool) (*Page, error) { +func readPage(path string, defaults map[interface{}]interface{}) (*Page, error) { var ( frontMatter *map[interface{}]interface{} static = true @@ -77,12 +76,10 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* if match := frontmatterMatcher.FindSubmatchIndex(source); match != nil { static = false - if expand { - // TODO only prepend newlines if it's markdown - body = append( - regexp.MustCompile(`[^\n\r]+`).ReplaceAllLiteral(source[:match[1]], []byte{}), - source[match[1]:]...) - } + // TODO only prepend newlines if it's markdown + body = append( + regexp.MustCompile(`[^\n\r]+`).ReplaceAllLiteral(source[:match[1]], []byte{}), + source[match[1]:]...) frontMatter = &map[interface{}]interface{}{} err = yaml.Unmarshal(source[match[2]:match[3]], &frontMatter) if err != nil { @@ -91,10 +88,10 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* } data = mergeMaps(data, *frontMatter) + } else { + body = []byte{} } - ext := filepath.Ext(path) - permalink := path if val, ok := data["permalink"]; ok { pattern, ok := val.(string) @@ -106,38 +103,45 @@ func readFile(path string, defaults map[interface{}]interface{}, expand bool) (* permalink = expandPermalinkPattern(pattern, data, path) } - if expand { - template, err := liquid.Parse(body, nil) - if err != nil { - fmt.Println(data) - err := &os.PathError{Op: "Liquid Error", Path: path, Err: err} - return nil, err - } - writer := new(bytes.Buffer) - if printFrontmatter { - b, _ := yaml.Marshal(stringMap(data)) - println(string(b)) - } - template.Render(writer, stringMap(data)) - body = writer.Bytes() - if ext == ".md" { - body = blackfriday.MarkdownBasic(body) - } - } else { - body = []byte{} - } - return &Page{ Path: path, Permalink: permalink, - Expanded: expand, Static: static, Published: getBool(data, "published", true), FrontMatter: frontMatter, - Body: body, + Content: body, }, nil } +func (p Page) Render() ([]byte, error) { + var ( + path = p.Path + ext = filepath.Ext(path) + data = *p.FrontMatter + ) + + if printFrontmatter { + b, _ := yaml.Marshal(stringMap(data)) + println(string(b)) + } + + template, err := liquid.Parse(p.Content, nil) + if err != nil { + err := &os.PathError{Op: "Liquid Error", Path: path, Err: err} + return nil, err + } + + writer := new(bytes.Buffer) + template.Render(writer, stringMap(data)) + body := writer.Bytes() + + if ext == ".md" { + body = blackfriday.MarkdownBasic(body) + } + + return []byte(body), nil +} + func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, path string) string { if p, found := permalinkStyles[pattern]; found { pattern = p diff --git a/server.go b/server.go index ec527b9..5a23b6f 100755 --- a/server.go +++ b/server.go @@ -28,12 +28,12 @@ func handler(w http.ResponseWriter, r *http.Request) { return } - p, err := readFile(p.Path, siteData, true) + body, err := p.Render() if err != nil { fmt.Printf("Error rendering %s: %s", path, err) http.Error(w, err.Error(), http.StatusInternalServerError) } - if _, err := w.Write(p.Body); err != nil { + if _, err := w.Write(body); err != nil { fmt.Printf("Error writing %s: %s", path, err) } } diff --git a/site.go b/site.go index ab057b7..3846d17 100644 --- a/site.go +++ b/site.go @@ -78,7 +78,7 @@ func buildSiteMap() (map[string]*Page, error) { if info.IsDir() { return nil } - p, err := readFile(relPath, siteData, false) + p, err := readPage(relPath, siteData) if err != nil { return err } @@ -130,7 +130,7 @@ func addCollectionFiles(fileMap map[string]*Page, name string, data map[interfac if info.IsDir() { return nil } - p, err := readFile(relPath, collData, false) + p, err := readPage(relPath, collData) if err != nil { return err }