1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:14:44 -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

@ -42,6 +42,7 @@ type Config struct {
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.
@ -382,6 +383,7 @@ type rootData struct {
Doc *Doc Doc *Doc
BasePath string BasePath string
GodocURL string GodocURL string
AnalyticsHTML template.HTML
Data interface{} Data interface{}
} }
@ -389,7 +391,11 @@ type rootData struct {
// 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 {