1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 16:41:14 +01:00

Create a PageEmbed

This commit is contained in:
Oliver Steele 2017-07-25 11:24:37 -04:00
parent 2dc46e00da
commit b001cc7b34
6 changed files with 44 additions and 45 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"path"
"sync"
"time"
@ -30,6 +31,28 @@ type Page interface {
Tags() []string
}
// PageEmbed can be embedded to give defaults for the Page interface.
type PageEmbed struct {
Path string
}
// Permalink is in the pages.Page interface.
func (p *PageEmbed) Permalink() string { return p.Path }
// OutputExt is in the pages.Page interface.
func (p *PageEmbed) OutputExt() string { return path.Ext(p.Path) }
// SourcePath is in the pages.Page interface.
func (p *PageEmbed) SourcePath() string { return "" }
// Published is in the pages.Page interface.
func (p *PageEmbed) Published() bool { return true }
// Static is in the pages.Page interface.
func (p *PageEmbed) Static() bool { return false } // FIXME means different things to different callers
// Reload is in the pages.Page interface.
func (p *PageEmbed) Reload() error { return nil }
type page struct {
file
sync.Mutex

View File

@ -5,6 +5,7 @@ import (
"html"
"io"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/liquid"
"github.com/osteele/liquid/render"
)
@ -41,7 +42,7 @@ func (p *jekyllFeedPlugin) PostRead(s Site) error {
path = "/" + pp
}
}
d := feedDoc{s, p, path}
d := feedDoc{pages.PageEmbed{Path: path}, s, p}
s.AddDocument(&d, true)
return nil
}
@ -55,18 +56,11 @@ func (p *jekyllFeedPlugin) feedMetaTag(ctx render.Context) (string, error) {
}
type feedDoc struct {
pages.PageEmbed
site Site
plugin *jekyllFeedPlugin
path string
}
func (d *feedDoc) Permalink() string { return d.path }
func (d *feedDoc) SourcePath() string { return "" }
func (d *feedDoc) OutputExt() string { return ".xml" }
func (d *feedDoc) Published() bool { return true }
func (d *feedDoc) Static() bool { return false } // FIXME means different things to different callers
func (d *feedDoc) Reload() error { return nil }
func (d *feedDoc) Content() []byte {
bindings := map[string]interface{}{"site": d.site}
b, err := d.plugin.tpl.Render(bindings)

View File

@ -46,7 +46,7 @@ func (p jekyllRedirectFromPlugin) processRedirectFrom(site Site, ps []pages.Page
redirections = []pages.Document{}
)
addRedirectFrom := func(from string, to pages.Page) {
r := redirectionDoc{From: from, To: prefix + to.Permalink()}
r := redirectionDoc{pages.PageEmbed{Path: from}, prefix + to.Permalink()}
redirections = append(redirections, &r)
}
for _, p := range ps {
@ -68,7 +68,7 @@ func (p jekyllRedirectFromPlugin) processRedirectTo(site Site, ps []pages.Page)
return err
}
if len(sources) > 0 {
r := redirectionDoc{From: p.Permalink(), To: sources[0]}
r := redirectionDoc{pages.PageEmbed{Path: p.Permalink()}, sources[0]}
p.SetContent(r.Content())
}
}
@ -95,17 +95,10 @@ func getStringArray(p pages.Page, fieldName string) (out []string, err error) {
}
type redirectionDoc struct {
From string
To string
pages.PageEmbed
To string
}
func (d *redirectionDoc) Permalink() string { return d.From }
func (d *redirectionDoc) SourcePath() string { return "" } // FIXME bad design
func (d *redirectionDoc) OutputExt() string { return ".html" }
func (d *redirectionDoc) Published() bool { return true }
func (d *redirectionDoc) Static() bool { return false } // FIXME means different things to different callers
func (d *redirectionDoc) Reload() error { return nil }
func (d *redirectionDoc) Content() []byte {
buf := new(bytes.Buffer)
if err := redirectTemplate.Execute(buf, d); err != nil {

View File

@ -1,9 +1,5 @@
package plugins
import (
"github.com/osteele/gojekyll/pages"
)
type sitemapPlugin struct{ plugin }
func init() {
@ -16,19 +12,11 @@ func init() {
// }
func (p *sitemapPlugin) PostRead(s Site) error {
s.AddDocument(newTemplateDoc(s, "sitemap.xml", sitemapTemplateSource), true)
s.AddDocument(newTemplateDoc(s, "robots.txt", `Sitemap: {{ "sitemap.xml" | absolute_url }}`), true)
s.AddDocument(newTemplateDoc(s, "/sitemap.xml", sitemapTemplateSource), true)
s.AddDocument(newTemplateDoc(s, "/robots.txt", `Sitemap: {{ "sitemap.xml" | absolute_url }}`), true)
return nil
}
func newTemplateDoc(s Site, path, src string) pages.Document {
tpl, err := s.TemplateEngine().ParseTemplate([]byte(src))
if err != nil {
panic(err)
}
return &templateDoc{s, path, tpl}
}
// Taken verbatim from https://github.com/jekyll/jekyll-sitemap-plugin/
const sitemapTemplateSource = `<?xml version="1.0" encoding="UTF-8"?>
{% if page.xsl %}

View File

@ -2,23 +2,24 @@ package plugins
import (
"io"
"path"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/liquid"
)
type templateDoc struct {
site Site
path string
tpl *liquid.Template
func newTemplateDoc(s Site, path, src string) pages.Document {
tpl, err := s.TemplateEngine().ParseTemplate([]byte(src))
if err != nil {
panic(err)
}
return &templateDoc{pages.PageEmbed{Path: path}, s, tpl}
}
func (d *templateDoc) Permalink() string { return "/" + d.path }
func (d *templateDoc) SourcePath() string { return "" }
func (d *templateDoc) OutputExt() string { return path.Ext(d.path) }
func (d *templateDoc) Published() bool { return true }
func (d *templateDoc) Static() bool { return false } // FIXME means different things to different callers
func (d *templateDoc) Reload() error { return nil }
type templateDoc struct {
pages.PageEmbed
site Site
tpl *liquid.Template
}
func (d *templateDoc) Content() []byte {
bindings := map[string]interface{}{"site": d.site}