shithub: hugo

Download patch

ref: ee5865f239786f354c6d3f14bf14bcb5890a6c22
parent: 0a9dc705f315ea2344ee4edc7f2394973c931711
author: Fabrizio (Misto) Milo <[email protected]>
date: Sat Aug 31 13:35:17 EDT 2013

Abstract html/template dependency

Signed-off-by: Noah Campbell <[email protected]>

--- a/hugolib/helpers.go
+++ b/hugolib/helpers.go
@@ -18,7 +18,6 @@
 	"errors"
 	"fmt"
 	"github.com/kr/pretty"
-	"html/template"
 	"os"
 	"os/exec"
 	"reflect"
@@ -165,11 +164,11 @@
 	return Sanitize(strings.ToLower(strings.Replace(strings.TrimSpace(url), " ", "-", -1)))
 }
 
-func AbsUrl(url string, base string) template.HTML {
+func AbsUrl(url string, base string) HTML {
 	if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
-		return template.HTML(url)
+		return HTML(url)
 	}
-	return template.HTML(MakePermalink(base, url))
+	return HTML(MakePermalink(base, url))
 }
 
 func Gt(a interface{}, b interface{}) bool {
--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -14,12 +14,11 @@
 package hugolib
 
 import (
-	"html/template"
 	"time"
 )
 
 type Node struct {
-	RSSlink     template.HTML
+	RSSlink     HTML
 	Site        SiteInfo
 	layout      string
 	Data        map[string]interface{}
@@ -32,7 +31,7 @@
 
 type UrlPath struct {
 	Url       string
-	Permalink template.HTML
+	Permalink HTML
 	Slug      string
 	Section   string
 	Path      string
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -38,8 +38,8 @@
 type Page struct {
 	Status          string
 	Images          []string
-	Content         template.HTML
-	Summary         template.HTML
+	Content         HTML
+	Summary         HTML
 	RawMarkdown     string // TODO should be []byte
 	Params          map[string]interface{}
 	RenderedContent *bytes.Buffer
@@ -185,7 +185,7 @@
 	return datum, lines
 }
 
-func (p *Page) Permalink() template.HTML {
+func (p *Page) Permalink() HTML {
 	baseUrl := string(p.Site.BaseUrl)
 	section := strings.TrimSpace(p.Section)
 	pSlug := strings.TrimSpace(p.Slug)
@@ -209,7 +209,7 @@
 			path = section + "/" + file
 		}
 	}
-	return template.HTML(MakePermalink(baseUrl, path))
+	return HTML(MakePermalink(baseUrl, path))
 }
 
 func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
@@ -427,7 +427,7 @@
 	}
 }
 
-func (p *Page) Render(layout ...string) template.HTML {
+func (p *Page) Render(layout ...string) HTML {
 	curLayout := ""
 
 	if len(layout) > 0 {
@@ -434,7 +434,7 @@
 		curLayout = layout[0]
 	}
 
-	return template.HTML(string(p.ExecuteTemplate(curLayout).Bytes()))
+	return HTML(string(p.ExecuteTemplate(curLayout).Bytes()))
 }
 
 func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
@@ -481,12 +481,12 @@
 	b := new(bytes.Buffer)
 	b.ReadFrom(lines)
 	content := b.Bytes()
-	page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
+	page.Content = HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
 	summary, plain := getSummaryString(content)
 	if plain {
-		page.Summary = template.HTML(string(summary))
+		page.Summary = HTML(string(summary))
 	} else {
-		page.Summary = template.HTML(string(blackfriday.MarkdownCommon(summary)))
+		page.Summary = HTML(string(blackfriday.MarkdownCommon(summary)))
 	}
 }
 
@@ -494,11 +494,11 @@
 	b := new(bytes.Buffer)
 	b.ReadFrom(lines)
 	content := b.Bytes()
-	page.Content = template.HTML(getRstContent(content))
+	page.Content = HTML(getRstContent(content))
 	summary, plain := getSummaryString(content)
 	if plain {
-		page.Summary = template.HTML(string(summary))
+		page.Summary = HTML(string(summary))
 	} else {
-		page.Summary = template.HTML(getRstContent(summary))
+		page.Summary = HTML(getRstContent(summary))
 	}
 }
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -142,13 +142,13 @@
 }
 
 func checkPageContent(t *testing.T, page *Page, content string) {
-	if page.Content != template.HTML(content) {
+	if page.Content != HTML(content) {
 		t.Fatalf("Page content is: %s.  Expected %s", page.Content, content)
 	}
 }
 
 func checkPageSummary(t *testing.T, page *Page, summary string) {
-	if page.Summary != template.HTML(summary) {
+	if page.Summary != HTML(summary) {
 		t.Fatalf("Page summary is: `%s`.  Expected `%s`", page.Summary, summary)
 	}
 }
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -259,7 +259,7 @@
 
 func (s *Site) ProcessShortcodes() {
 	for i, _ := range s.Pages {
-		s.Pages[i].Content = template.HTML(ShortcodesHandle(string(s.Pages[i].Content), s.Pages[i], s.Tmpl))
+		s.Pages[i].Content = HTML(ShortcodesHandle(string(s.Pages[i].Content), s.Pages[i], s.Tmpl))
 	}
 }
 
@@ -273,7 +273,7 @@
 		content = strings.Replace(content, " href='/", " href='"+baseWithSlash, -1)
 		content = strings.Replace(content, " href=\"/", " href=\""+baseWithSlash, -1)
 		content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithSlash, -1)
-		s.Pages[i].Content = template.HTML(content)
+		s.Pages[i].Content = HTML(content)
 	}
 }
 
@@ -480,8 +480,8 @@
 			} else {
 				n.Url = url + "/index.html"
 			}
-			n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(plink)))
-			n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(url+".xml")))
+			n.Permalink = HTML(MakePermalink(string(n.Site.BaseUrl), string(plink)))
+			n.RSSlink = HTML(MakePermalink(string(n.Site.BaseUrl), string(url+".xml")))
 			n.Date = o[0].Date
 			n.Data[singular] = o
 			n.Data["Pages"] = o
@@ -511,7 +511,7 @@
 				} else {
 					n.Url = Urlize(plural + "/" + k + "/" + "index.xml")
 				}
-				n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
+				n.Permalink = HTML(string(n.Site.BaseUrl) + n.Url)
 				s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
 				err = s.WritePublic(base+".xml", y.Bytes())
 				if err != nil {
@@ -531,7 +531,7 @@
 			n.Title = strings.Title(plural)
 			url := Urlize(plural)
 			n.Url = url + "/index.html"
-			n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
+			n.Permalink = HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
 			n.Data["Singular"] = singular
 			n.Data["Plural"] = plural
 			n.Data["Index"] = s.Indexes[plural]
@@ -556,8 +556,8 @@
 		n := s.NewNode()
 		n.Title = strings.Title(inflect.Pluralize(section))
 		n.Url = Urlize(section + "/" + "index.html")
-		n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
-		n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(section+".xml")))
+		n.Permalink = HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
+		n.RSSlink = HTML(MakePermalink(string(n.Site.BaseUrl), string(section+".xml")))
 		n.Date = data[0].Date
 		n.Data["Pages"] = data
 		layout := "indexes/" + section + ".html"
@@ -578,7 +578,7 @@
 			} else {
 				n.Url = Urlize(section + "/" + "index.xml")
 			}
-			n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
+			n.Permalink = HTML(string(n.Site.BaseUrl) + n.Url)
 			y := s.NewXMLBuffer()
 			s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
 			err = s.WritePublic(section+"/index.xml", y.Bytes())
@@ -592,8 +592,8 @@
 	n := s.NewNode()
 	n.Title = n.Site.Title
 	n.Url = Urlize(string(n.Site.BaseUrl))
-	n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string("index.xml")))
-	n.Permalink = template.HTML(string(n.Site.BaseUrl))
+	n.RSSlink = HTML(MakePermalink(string(n.Site.BaseUrl), string("index.xml")))
+	n.Permalink = HTML(string(n.Site.BaseUrl))
 	if len(s.Pages) > 0 {
 		n.Date = s.Pages[0].Date
 		if len(s.Pages) < 9 {
@@ -615,7 +615,7 @@
 		// XML Feed
 		n.Url = Urlize("index.xml")
 		n.Title = "Recent Content"
-		n.Permalink = template.HTML(string(n.Site.BaseUrl) + "index.xml")
+		n.Permalink = HTML(string(n.Site.BaseUrl) + "index.xml")
 		y := s.NewXMLBuffer()
 		s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
 		err = s.WritePublic("index.xml", y.Bytes())
@@ -625,7 +625,7 @@
 	if a := s.Tmpl.Lookup("404.html"); a != nil {
 		n.Url = Urlize("404.html")
 		n.Title = "404 Page not found"
-		n.Permalink = template.HTML(string(n.Site.BaseUrl) + "404.html")
+		n.Permalink = HTML(string(n.Site.BaseUrl) + "404.html")
 		x, err := s.RenderThing(n, "404.html")
 		if err != nil {
 			return err
--- /dev/null
+++ b/hugolib/template.go
@@ -1,0 +1,11 @@
+package hugolib
+
+import (
+	"html/template"
+)
+
+// HTML encapsulates a known safe HTML document fragment.
+// It should not be used for HTML from a third-party, or HTML with
+// unclosed tags or comments. The outputs of a sound HTML sanitizer
+// and a template escaped by this package are fine for use with HTML.
+type HTML template.HTML