Remove hugolib.HTML and hugolib.URL types

These types were not be rendered correctly by the html/template package.
Removing them gets the correct behavior.

Fixes #74
This commit is contained in:
Noah Campbell 2013-09-03 12:41:13 -07:00
parent a591a10626
commit 3ecc698f5e
7 changed files with 34 additions and 30 deletions

View file

@ -15,6 +15,7 @@ package hugolib
import ( import (
"bytes" "bytes"
"html/template"
"errors" "errors"
"fmt" "fmt"
"github.com/kr/pretty" "github.com/kr/pretty"
@ -164,11 +165,11 @@ func Urlize(url string) string {
return Sanitize(strings.ToLower(strings.Replace(strings.TrimSpace(url), " ", "-", -1))) return Sanitize(strings.ToLower(strings.Replace(strings.TrimSpace(url), " ", "-", -1)))
} }
func AbsUrl(url string, base string) HTML { func AbsUrl(url string, base string) template.HTML {
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return HTML(url) return template.HTML(url)
} }
return HTML(MakePermalink(base, url)) return template.HTML(MakePermalink(base, url))
} }
func Gt(a interface{}, b interface{}) bool { func Gt(a interface{}, b interface{}) bool {

View file

@ -15,10 +15,11 @@ package hugolib
import ( import (
"time" "time"
"html/template"
) )
type Node struct { type Node struct {
RSSlink HTML RSSlink template.HTML
Site SiteInfo Site SiteInfo
layout string layout string
Data map[string]interface{} Data map[string]interface{}
@ -31,7 +32,7 @@ type Node struct {
type UrlPath struct { type UrlPath struct {
Url string Url string
Permalink HTML Permalink template.HTML
Slug string Slug string
Section string Section string
Path string Path string

View file

@ -22,6 +22,7 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/theplant/blackfriday" "github.com/theplant/blackfriday"
"io" "io"
"html/template"
"io/ioutil" "io/ioutil"
"launchpad.net/goyaml" "launchpad.net/goyaml"
"os" "os"
@ -37,8 +38,8 @@ var _ = filepath.Base("")
type Page struct { type Page struct {
Status string Status string
Images []string Images []string
Content HTML Content template.HTML
Summary HTML Summary template.HTML
RawMarkdown string // TODO should be []byte RawMarkdown string // TODO should be []byte
Params map[string]interface{} Params map[string]interface{}
RenderedContent *bytes.Buffer RenderedContent *bytes.Buffer
@ -184,7 +185,7 @@ func splitPageContent(data []byte, start string, end string) ([]string, []string
return datum, lines return datum, lines
} }
func (p *Page) Permalink() HTML { func (p *Page) Permalink() template.HTML {
baseUrl := string(p.Site.BaseUrl) baseUrl := string(p.Site.BaseUrl)
section := strings.TrimSpace(p.Section) section := strings.TrimSpace(p.Section)
pSlug := strings.TrimSpace(p.Slug) pSlug := strings.TrimSpace(p.Slug)
@ -208,7 +209,7 @@ func (p *Page) Permalink() HTML {
path = section + "/" + file path = section + "/" + file
} }
} }
return HTML(MakePermalink(baseUrl, path)) return template.HTML(MakePermalink(baseUrl, path))
} }
func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) { func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
@ -426,14 +427,14 @@ func chompWhitespace(data *bufio.Reader) (r rune, err error) {
} }
} }
func (p *Page) Render(layout ...string) HTML { func (p *Page) Render(layout ...string) template.HTML {
curLayout := "" curLayout := ""
if len(layout) > 0 { if len(layout) > 0 {
curLayout = layout[0] curLayout = layout[0]
} }
return HTML(string(p.ExecuteTemplate(curLayout).Bytes())) return template.HTML(string(p.ExecuteTemplate(curLayout).Bytes()))
} }
func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer { func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
@ -480,12 +481,12 @@ func (page *Page) convertMarkdown(lines io.Reader) {
b := new(bytes.Buffer) b := new(bytes.Buffer)
b.ReadFrom(lines) b.ReadFrom(lines)
content := b.Bytes() content := b.Bytes()
page.Content = HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content)))) page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
summary, plain := getSummaryString(content) summary, plain := getSummaryString(content)
if plain { if plain {
page.Summary = HTML(string(summary)) page.Summary = template.HTML(string(summary))
} else { } else {
page.Summary = HTML(string(blackfriday.MarkdownCommon(summary))) page.Summary = template.HTML(string(blackfriday.MarkdownCommon(summary)))
} }
} }
@ -493,11 +494,11 @@ func (page *Page) convertRestructuredText(lines io.Reader) {
b := new(bytes.Buffer) b := new(bytes.Buffer)
b.ReadFrom(lines) b.ReadFrom(lines)
content := b.Bytes() content := b.Bytes()
page.Content = HTML(getRstContent(content)) page.Content = template.HTML(getRstContent(content))
summary, plain := getSummaryString(content) summary, plain := getSummaryString(content)
if plain { if plain {
page.Summary = HTML(string(summary)) page.Summary = template.HTML(string(summary))
} else { } else {
page.Summary = HTML(getRstContent(summary)) page.Summary = template.HTML(getRstContent(summary))
} }
} }

View file

@ -4,6 +4,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"html/template"
) )
var EMPTY_PAGE = "" var EMPTY_PAGE = ""
@ -141,13 +142,13 @@ func checkPageTitle(t *testing.T, page *Page, title string) {
} }
func checkPageContent(t *testing.T, page *Page, content string) { func checkPageContent(t *testing.T, page *Page, content string) {
if page.Content != HTML(content) { if page.Content != template.HTML(content) {
t.Fatalf("Page content is: %s. Expected %s", page.Content, content) t.Fatalf("Page content is: %s. Expected %s", page.Content, content)
} }
} }
func checkPageSummary(t *testing.T, page *Page, summary string) { func checkPageSummary(t *testing.T, page *Page, summary string) {
if page.Summary != HTML(summary) { if page.Summary != template.HTML(summary) {
t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary) t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary)
} }
} }

View file

@ -15,6 +15,7 @@ package hugolib
import ( import (
"bitbucket.org/pkg/inflect" "bitbucket.org/pkg/inflect"
"html/template"
"bytes" "bytes"
"fmt" "fmt"
"github.com/spf13/hugo/target" "github.com/spf13/hugo/target"
@ -41,7 +42,7 @@ type Site struct {
} }
type SiteInfo struct { type SiteInfo struct {
BaseUrl URL BaseUrl template.URL
Indexes OrderedIndexList Indexes OrderedIndexList
Recent *Pages Recent *Pages
LastChange time.Time LastChange time.Time
@ -169,7 +170,7 @@ func (s *Site) initialize() {
filepath.Walk(s.absContentDir(), walker) filepath.Walk(s.absContentDir(), walker)
s.Info = SiteInfo{ s.Info = SiteInfo{
BaseUrl: URL(s.Config.BaseUrl), BaseUrl: template.URL(s.Config.BaseUrl),
Title: s.Config.Title, Title: s.Config.Title,
Recent: &s.Pages, Recent: &s.Pages,
Config: &s.Config, Config: &s.Config,
@ -206,7 +207,7 @@ func (s *Site) checkDirectories() {
func (s *Site) ProcessShortcodes() { func (s *Site) ProcessShortcodes() {
for _, page := range s.Pages { for _, page := range s.Pages {
page.Content = HTML(ShortcodesHandle(string(page.Content), page, s.Tmpl)) page.Content = template.HTML(ShortcodesHandle(string(page.Content), page, s.Tmpl))
} }
} }
@ -220,7 +221,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, " href=\"/", " href=\""+baseWithSlash, -1) content = strings.Replace(content, " href=\"/", " href=\""+baseWithSlash, -1)
content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithSlash, -1) content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithSlash, -1)
page.Content = HTML(content) page.Content = template.HTML(content)
} }
} }
@ -525,7 +526,7 @@ func (s *Site) RenderLists() error {
} else { } else {
n.Url = Urlize(section + "/" + "index.xml") n.Url = Urlize(section + "/" + "index.xml")
} }
n.Permalink = HTML(string(n.Site.BaseUrl) + n.Url) n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
y := s.NewXMLBuffer() y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n) s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
err = s.WritePublic(section+"/index.xml", y.Bytes()) err = s.WritePublic(section+"/index.xml", y.Bytes())
@ -591,8 +592,8 @@ func (s *Site) Stats() {
} }
} }
func permalink(s *Site, plink string) HTML { func permalink(s *Site, plink string) template.HTML {
return HTML(MakePermalink(string(s.Info.BaseUrl), plink)) return template.HTML(MakePermalink(string(s.Info.BaseUrl), plink))
} }
func (s *Site) NewNode() *Node { func (s *Site) NewNode() *Node {

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
"html/template"
) )
var TEMPLATE_TITLE = "{{ .Title }}" var TEMPLATE_TITLE = "{{ .Title }}"
@ -104,7 +105,7 @@ func TestRenderThing(t *testing.T) {
}{ }{
{PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"}, {PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"},
{PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"}, {PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"},
{PAGE_WITH_MD, TEMPLATE_CONTENT, "<h1>heading 1</h1>\n<p>text</p>\n<h2>heading 2</h2>\n<p>more text</p>\n"}, {PAGE_WITH_MD, TEMPLATE_CONTENT, "<h1>heading 1</h1>\n\n<p>text</p>\n\n<h2>heading 2</h2>\n\n<p>more text</p>\n"},
} }
s := new(Site) s := new(Site)
@ -121,6 +122,7 @@ func TestRenderThing(t *testing.T) {
t.Fatalf("Unable to add template") t.Fatalf("Unable to add template")
} }
p.Content = template.HTML(p.Content)
html, err2 := s.RenderThing(p, templateName) html, err2 := s.RenderThing(p, templateName)
if err2 != nil { if err2 != nil {
t.Errorf("Unable to render html: %s", err) t.Errorf("Unable to render html: %s", err)

View file

@ -14,7 +14,6 @@ import (
// It should not be used for HTML from a third-party, or HTML with // 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 // unclosed tags or comments. The outputs of a sound HTML sanitizer
// and a template escaped by this package are fine for use with HTML. // and a template escaped by this package are fine for use with HTML.
type HTML template.HTML
type Template interface { type Template interface {
ExecuteTemplate(wr io.Writer, name string, data interface{}) error ExecuteTemplate(wr io.Writer, name string, data interface{}) error
@ -25,8 +24,6 @@ type Template interface {
AddTemplate(name, tpl string) error AddTemplate(name, tpl string) error
} }
type URL template.URL
type templateErr struct { type templateErr struct {
name string name string
err error err error