From aae409d6c46034eec628fc58e0a868b5cb89f970 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Sat, 31 Aug 2013 17:35:17 -0700 Subject: [PATCH] abstract html/template dependency --- hugolib/helpers.go | 7 +++---- hugolib/node.go | 5 ++--- hugolib/page.go | 24 ++++++++++++------------ hugolib/page_test.go | 4 ++-- hugolib/site.go | 26 +++++++++++++------------- hugolib/template.go | 11 +++++++++++ 6 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 hugolib/template.go diff --git a/hugolib/helpers.go b/hugolib/helpers.go index feed341e5..08fafd37f 100644 --- a/hugolib/helpers.go +++ b/hugolib/helpers.go @@ -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 { diff --git a/hugolib/node.go b/hugolib/node.go index 918edeb5e..a8e90b7d8 100644 --- 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 Node struct { type UrlPath struct { Url string - Permalink template.HTML + Permalink HTML Slug string Section string Path string diff --git a/hugolib/page.go b/hugolib/page.go index eb8f142b3..24b1bc860 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -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)) } } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 4ffda4461..0c61a703f 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -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) } } diff --git a/hugolib/site.go b/hugolib/site.go index 68ef6e5ac..8f51026f3 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -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 index 000000000..2d2074c07 --- /dev/null +++ b/hugolib/template.go @@ -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