From 7a302eb69bfef1ecd0a17e16085bd4359a0ae717 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 12 Sep 2019 18:49:03 -0400 Subject: Series and tags (#74) * initial support for tags and series * tagging support * oops * Update main.go --- cmd/site/html.go | 41 +++++++++++++++++++++++++++++++++++++++++ cmd/site/main.go | 7 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) (limited to 'cmd') diff --git a/cmd/site/html.go b/cmd/site/html.go index 33bdd0f..a1d5040 100644 --- a/cmd/site/html.go +++ b/cmd/site/html.go @@ -6,6 +6,7 @@ import ( "html/template" "net/http" "path/filepath" + "strings" "time" "christine.website/internal" @@ -69,6 +70,34 @@ var postView = promauto.NewCounterVec(prometheus.CounterOpts{ Help: "The number of views per post or talk", }, []string{"base"}) +func (s *Site) listSeries(w http.ResponseWriter, r *http.Request) { + s.renderTemplatePage("series.html", s.Series).ServeHTTP(w, r) +} + +func (s *Site) showSeries(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == "/blog/series/" { + http.Redirect(w, r, "/blog/series", http.StatusSeeOther) + return + } + + series := filepath.Base(r.URL.Path) + var posts []blog.Post + + for _, p := range s.Posts { + if p.Series == series { + posts = append(posts, p) + } + } + + s.renderTemplatePage("serieslist.html", struct { + Name string + Posts []blog.Post + }{ + Name: series, + Posts: posts, + }).ServeHTTP(w, r) +} + func (s *Site) showTalk(w http.ResponseWriter, r *http.Request) { if r.RequestURI == "/talks/" { http.Redirect(w, r, "/talks", http.StatusSeeOther) @@ -136,17 +165,29 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { return } + var tags string + + if len(p.Tags) != 0 { + for _, t := range p.Tags { + tags = tags + " #" + strings.ReplaceAll(t, "-", "") + } + } + const dateFormat = `2006-01-02` s.renderTemplatePage("blogpost.html", struct { Title string Link string BodyHTML template.HTML Date string + Series string + Tags string }{ Title: p.Title, Link: p.Link, BodyHTML: p.BodyHTML, Date: p.Date.Format(dateFormat), + Series: strings.ReplaceAll(p.Series, "-", ""), + Tags: tags, }).ServeHTTP(w, r) postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc() } diff --git a/cmd/site/main.go b/cmd/site/main.go index 04d8bc4..391b56b 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -58,6 +58,7 @@ type Site struct { Posts blog.Posts Talks blog.Posts Resume template.HTML + Series []string rssFeed *feeds.Feed jsonFeed *jsonfeed.Feed @@ -81,7 +82,7 @@ func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) { middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r) } -var arbDate = time.Date(2019, time.May, 20, 18, 0, 0, 0, time.UTC) +var arbDate = time.Date(2019, time.September, 12, 0, 0, 0, 0, time.UTC) // Build creates a new Site instance or fails. func Build() (*Site, error) { @@ -147,6 +148,8 @@ func Build() (*Site, error) { return nil, err } s.Posts = posts + s.Series = posts.Series() + sort.Strings(s.Series) talks, err := blog.LoadPosts("./talks", "talks") if err != nil { @@ -210,6 +213,8 @@ func Build() (*Site, error) { s.mux.Handle("/blog.atom", middleware.Metrics("blog.atom", http.HandlerFunc(s.createAtom))) s.mux.Handle("/blog.json", middleware.Metrics("blog.json", http.HandlerFunc(s.createJSONFeed))) s.mux.Handle("/blog/", middleware.Metrics("blogpost", http.HandlerFunc(s.showPost))) + s.mux.Handle("/blog/series", http.HandlerFunc(s.listSeries)) + s.mux.Handle("/blog/series/", http.HandlerFunc(s.showSeries)) s.mux.Handle("/talks/", middleware.Metrics("talks", http.HandlerFunc(s.showTalk))) s.mux.Handle("/css/", http.FileServer(http.Dir("."))) s.mux.Handle("/static/", http.FileServer(http.Dir("."))) -- cgit v1.2.3