abstract html/template dependency

This commit is contained in:
Fabrizio (Misto) Milo 2013-08-31 17:35:17 -07:00
parent c14fe9e505
commit aae409d6c4
6 changed files with 43 additions and 34 deletions

View 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 {

View file

@ -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

View 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))
}
}

View 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)
}
}

View 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

11
hugolib/template.go Normal file
View file

@ -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