From 1dc63a221695eeacf226e301ec4306e8573e1b79 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Thu, 1 Feb 2018 17:51:23 -0800 Subject: list generic templates --- archive/archive.go | 15 ++++++++++----- render/render.go | 21 ++++++++++++++++----- site/site.go | 25 ++++++++++++++++++++----- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/archive/archive.go b/archive/archive.go index 0a6e3c9..60fb89d 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -11,18 +11,18 @@ var archiveTmplName = "archive" var archiveName = "archive.html" /* -ListArchive +ListArchive */ type ListArchive struct { Posts post.Posts Tgt string Template string - + Site interface{} } func NewListArchive(posts post.Posts) *ListArchive { - la := ListArchive{ Posts: posts } + la := ListArchive{Posts: posts} return &la } @@ -40,12 +40,17 @@ func (a ListArchive) Render() []byte { return render.Render(a.Template, a) } - /* NewRssArchive takes posts and returns an archive ready for RSS output */ func NewRssArchive(posts post.Posts) *ListArchive { - ra := ListArchive{ Posts: posts, Template: "rss" } + ra := ListArchive{Posts: posts, Template: "rss"} ra.Tgt = path.Join(config.Config.HtmlDir, "rss.xml") return &ra } + +func NewGenericListArchive(posts post.Posts, templateName string, target string) *ListArchive { + la := ListArchive{Posts: posts, Template: templateName} + la.Tgt = path.Join(config.Config.HtmlDir, target) + return &la +} diff --git a/render/render.go b/render/render.go index 1606305..ba8e52b 100644 --- a/render/render.go +++ b/render/render.go @@ -1,11 +1,11 @@ package render import ( - "bytes" "adammathes.com/snkt/config" + "adammathes.com/snkt/vlog" + "bytes" "io/ioutil" "log" - "adammathes.com/snkt/vlog" "os" "path" "path/filepath" @@ -27,6 +27,18 @@ type Renderable interface { Target() string } +func TemplateNames() []string { + templateNames := make([]string, len(templates)) + + i := 0 + for tName, _ := range templates { + templateNames[i] = tName + i++ + } + + return templateNames +} + func Write(a Renderable) { if config.Config.Verbose { vlog.Printf("Writing to %s\n", a.Target()) @@ -51,11 +63,10 @@ func Init() { tmplFuncs := template.FuncMap{ "ResolveURLs": ResolveURLs, - "SiteTitle": SiteTitle, - "SiteURL": SiteURL, + "SiteTitle": SiteTitle, + "SiteURL": SiteURL, } - base := path.Join(config.Config.TmplDir, BASE_TEMPLATE) for _, t := range ts { tf := filepath.Base(t) diff --git a/site/site.go b/site/site.go index 136c100..b69a4af 100644 --- a/site/site.go +++ b/site/site.go @@ -10,6 +10,7 @@ import ( "log" "path" "sort" + "strings" ) type Site struct { @@ -19,11 +20,12 @@ type Site struct { Posts post.Posts // all archives are optional based on presence of template - Archive *archive.ListArchive - Home *archive.ListArchive - Rss *archive.ListArchive - Paged *archive.PagedArchives - Tagged *archive.TagArchives + Archive *archive.ListArchive + Home *archive.ListArchive + Rss *archive.ListArchive + Paged *archive.PagedArchives + Tagged *archive.TagArchives + ListArchives []*archive.ListArchive } /* @@ -55,6 +57,14 @@ func (s *Site) Read() { s.Home.Template = "home" s.Home.Site = s } + + // generic list templates + for _, t := range render.TemplateNames() { + if strings.HasSuffix(t, ".list") { + la := archive.NewGenericListArchive(s.Posts, t, strings.TrimSuffix(t, ".list")) + s.ListArchives = append(s.ListArchives, la) + } + } } /* @@ -141,6 +151,11 @@ func (s *Site) WriteArchives() { render.Write(t) } } + for _, t := range s.ListArchives { + t.Site = s + render.Write(t) + } + } func (s *Site) WritePosts() { -- cgit v1.2.3