From b001cc7b34091b70e85996c7fe385214cc5a6c31 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Tue, 25 Jul 2017 11:24:37 -0400 Subject: [PATCH] Create a PageEmbed --- pages/{interfaces.go => document.go} | 0 pages/page.go | 23 +++++++++++++++++++++++ plugins/feed.go | 12 +++--------- plugins/redirect_from.go | 15 ++++----------- plugins/sitemap.go | 16 ++-------------- plugins/template_doc.go | 23 ++++++++++++----------- 6 files changed, 44 insertions(+), 45 deletions(-) rename pages/{interfaces.go => document.go} (100%) diff --git a/pages/interfaces.go b/pages/document.go similarity index 100% rename from pages/interfaces.go rename to pages/document.go diff --git a/pages/page.go b/pages/page.go index a37d21b..e7fb2fa 100644 --- a/pages/page.go +++ b/pages/page.go @@ -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 diff --git a/plugins/feed.go b/plugins/feed.go index 9e8ce76..9f598a7 100644 --- a/plugins/feed.go +++ b/plugins/feed.go @@ -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) diff --git a/plugins/redirect_from.go b/plugins/redirect_from.go index 0054656..7d7d89b 100644 --- a/plugins/redirect_from.go +++ b/plugins/redirect_from.go @@ -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 { diff --git a/plugins/sitemap.go b/plugins/sitemap.go index a2d4255..0a9a48d 100644 --- a/plugins/sitemap.go +++ b/plugins/sitemap.go @@ -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 = ` {% if page.xsl %} diff --git a/plugins/template_doc.go b/plugins/template_doc.go index 0aa601b..217aa0b 100644 --- a/plugins/template_doc.go +++ b/plugins/template_doc.go @@ -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}