Abstract html/template dependency
authorFabrizio (Misto) Milo <mistobaan@gmail.com>
Sun, 1 Sep 2013 00:35:17 +0000 (17:35 -0700)
committerNoah Campbell <noahcampbell@gmail.com>
Mon, 2 Sep 2013 02:51:27 +0000 (19:51 -0700)
Signed-off-by: Noah Campbell <noahcampbell@gmail.com>
hugolib/helpers.go
hugolib/node.go
hugolib/page.go
hugolib/page_test.go
hugolib/site.go
hugolib/template.go [new file with mode: 0644]

index feed341e59e92d8c50b4df41237050e302897405..08fafd37f306449f1039d8132cbc82f6b9d55600 100644 (file)
@@ -18,7 +18,6 @@ import (
        "errors"
        "fmt"
        "github.com/kr/pretty"
-       "html/template"
        "os"
        "os/exec"
        "reflect"
@@ -165,11 +164,11 @@ func Urlize(url string) string {
        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 {
index 918edeb5e562e55f60b3a992e137beed450cfc23..a8e90b7d87206aabcf8ce332b999dc41dc01f732 100644 (file)
 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 Node struct {
 
 type UrlPath struct {
        Url       string
-       Permalink template.HTML
+       Permalink HTML
        Slug      string
        Section   string
        Path      string
index eb8f142b3829579437b47bba6aceae66d64acea4..24b1bc860539e4b3a24e0257496040ca9f247c05 100644 (file)
@@ -38,8 +38,8 @@ var _ = filepath.Base("")
 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 @@ func splitPageContent(data []byte, start string, end string) ([]string, []string
        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 @@ func (p *Page) Permalink() template.HTML {
                        path = section + "/" + file
                }
        }
-       return template.HTML(MakePermalink(baseUrl, path))
+       return HTML(MakePermalink(baseUrl, path))
 }
 
 func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
@@ -427,14 +427,14 @@ func chompWhitespace(data *bufio.Reader) (r rune, err error) {
        }
 }
 
-func (p *Page) Render(layout ...string) template.HTML {
+func (p *Page) Render(layout ...string) HTML {
        curLayout := ""
 
        if len(layout) > 0 {
                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 @@ func (page *Page) convertMarkdown(lines io.Reader) {
        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 @@ func (page *Page) convertRestructuredText(lines io.Reader) {
        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))
        }
 }
index 4ffda4461d9ac4e1192a43c349ddfeb8c595ed2e..0c61a703f00cb7086876b908f087924040fbef1b 100644 (file)
@@ -142,13 +142,13 @@ func checkPageTitle(t *testing.T, page *Page, title string) {
 }
 
 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)
        }
 }
index 68ef6e5accbbdd620380ab83cd516182a3ee87d4..8f51026f3b44ef0d72eff40f53063db7fd81c308 100644 (file)
@@ -259,7 +259,7 @@ func (s *Site) checkDirectories() {
 
 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 @@ func (s *Site) AbsUrlify() {
                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 @@ func (s *Site) RenderIndexes() error {
                        } 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 @@ func (s *Site) RenderIndexes() error {
                                } 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 @@ func (s *Site) RenderIndexesIndexes() (err error) {
                        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 @@ func (s *Site) RenderLists() error {
                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 @@ func (s *Site) RenderLists() error {
                        } 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 @@ func (s *Site) RenderHomePage() error {
        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 @@ func (s *Site) RenderHomePage() error {
                // 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 @@ func (s *Site) RenderHomePage() error {
        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
diff --git a/hugolib/template.go b/hugolib/template.go
new file mode 100644 (file)
index 0000000..2d2074c
--- /dev/null
@@ -0,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