From 3769738f410bfa407254646cfd352f867d0281f6 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Tue, 1 Oct 2019 18:52:16 -0400 Subject: [PATCH] blog: add support for optional analytics HTML This change makes it possible for users of the blog package to provide analytics HTML, which can then be inserted on pages. Previously, this was possible by making ad-hoc modifications to the template files. It's easier to have a dedicated field. Change-Id: Id7c24dc9c7b5b9e23d18f3987f2f1af27c709681 Reviewed-on: https://go-review.googlesource.com/c/tools/+/198323 Reviewed-by: Brad Fitzpatrick --- blog/blog.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/blog/blog.go b/blog/blog.go index 73c184d4b4..11b95c9f55 100644 --- a/blog/blog.go +++ b/blog/blog.go @@ -38,10 +38,11 @@ type Config struct { ContentPath string // Relative or absolute location of article files and related content. TemplatePath string // Relative or absolute location of template files. - BaseURL string // Absolute base URL (for permalinks; no trailing slash). - BasePath string // Base URL path relative to server root (no trailing slash). - GodocURL string // The base URL of godoc (for menu bar; no trailing slash). - Hostname string // Server host name, used for rendering ATOM feeds. + BaseURL string // Absolute base URL (for permalinks; no trailing slash). + BasePath string // Base URL path relative to server root (no trailing slash). + GodocURL string // The base URL of godoc (for menu bar; no trailing slash). + Hostname string // Server host name, used for rendering ATOM feeds. + AnalyticsHTML template.HTML // Optional analytics HTML to insert at the beginning of . HomeArticles int // Articles to display on the home page. FeedArticles int // Articles to include in Atom and JSON feeds. @@ -379,17 +380,22 @@ func summary(d *Doc) string { // rootData encapsulates data destined for the root template. type rootData struct { - Doc *Doc - BasePath string - GodocURL string - Data interface{} + Doc *Doc + BasePath string + GodocURL string + AnalyticsHTML template.HTML + Data interface{} } // ServeHTTP serves the front, index, and article pages // as well as the ATOM and JSON feeds. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { var ( - d = rootData{BasePath: s.cfg.BasePath, GodocURL: s.cfg.GodocURL} + d = rootData{ + BasePath: s.cfg.BasePath, + GodocURL: s.cfg.GodocURL, + AnalyticsHTML: s.cfg.AnalyticsHTML, + } t *template.Template ) switch p := strings.TrimPrefix(r.URL.Path, s.cfg.BasePath); p {