1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 21:14:48 +01:00

readFile -> readPage + p.Render

This commit is contained in:
Oliver Steele 2017-06-11 20:30:25 -04:00
parent 3fc20e9e3a
commit 5b258dc0c8
6 changed files with 61 additions and 46 deletions

View File

@ -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

View File

@ -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
}
}

13
main.go
View File

@ -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.")
}

72
page.go
View File

@ -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

View File

@ -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)
}
}

View File

@ -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
}