1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:24:39 -07:00

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 <bradfitz@golang.org>
This commit is contained in:
Dmitri Shuralyov 2019-10-01 18:52:16 -04:00
parent 329c8d646e
commit 3769738f41

View File

@ -38,10 +38,11 @@ type Config struct {
ContentPath string // Relative or absolute location of article files and related content. ContentPath string // Relative or absolute location of article files and related content.
TemplatePath string // Relative or absolute location of template files. TemplatePath string // Relative or absolute location of template files.
BaseURL string // Absolute base URL (for permalinks; no trailing slash). BaseURL string // Absolute base URL (for permalinks; no trailing slash).
BasePath string // Base URL path relative to server root (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). GodocURL string // The base URL of godoc (for menu bar; no trailing slash).
Hostname string // Server host name, used for rendering ATOM feeds. Hostname string // Server host name, used for rendering ATOM feeds.
AnalyticsHTML template.HTML // Optional analytics HTML to insert at the beginning of <head>.
HomeArticles int // Articles to display on the home page. HomeArticles int // Articles to display on the home page.
FeedArticles int // Articles to include in Atom and JSON feeds. 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. // rootData encapsulates data destined for the root template.
type rootData struct { type rootData struct {
Doc *Doc Doc *Doc
BasePath string BasePath string
GodocURL string GodocURL string
Data interface{} AnalyticsHTML template.HTML
Data interface{}
} }
// ServeHTTP serves the front, index, and article pages // ServeHTTP serves the front, index, and article pages
// as well as the ATOM and JSON feeds. // as well as the ATOM and JSON feeds.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var ( 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 t *template.Template
) )
switch p := strings.TrimPrefix(r.URL.Path, s.cfg.BasePath); p { switch p := strings.TrimPrefix(r.URL.Path, s.cfg.BasePath); p {