diff --git a/hugolib/site.go b/hugolib/site.go index fbebed94c..5e22fd969 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -576,19 +576,11 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error) return } - - section := "" - if page, ok := d.(*Page); ok { - section, _ = page.RelPermalink() - } - absURL, err := transform.AbsURL(s.Config.BaseUrl) if err != nil { return } - transformer := transform.NewChain( - append(absURL, transform.NavActive(section, "hugo-nav")...)..., - ) + transformer := transform.NewChain(absURL...) var renderBuffer *bytes.Buffer diff --git a/hugolib/site_test.go b/hugolib/site_test.go index 3a47b7ff7..2ef184415 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -23,7 +23,8 @@ content` TEMPLATE_CONTENT = "{{ .Content }}" TEMPLATE_DATE = "{{ .Date }}" INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}" - TEMPLATE_WITH_URL = "Going" + TEMPLATE_WITH_URL_REL = "Going" + TEMPLATE_WITH_URL_ABS = "Going" PAGE_URL_SPECIFIED = `--- title: simple template url: "mycategory/my-whatever-content/" @@ -128,7 +129,7 @@ func TestRenderThing(t *testing.T) { } func HTML(in string) string { - return fmt.Sprintf("
%s", in) + return in } func TestRenderThingOrDefault(t *testing.T) { @@ -224,12 +225,13 @@ func TestSkipRender(t *testing.T) { {"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"}, {"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"}, {"sect/doc5.html", []byte("{{ template \"head\" }}body5"), "sect"}, + {"sect/doc6.html", []byte("{{ template \"head_abs\" }}body5"), "sect"}, {"doc7.html", []byte("doc7 content"), ""}, } s := &Site{ Target: target, - Config: Config{Verbose: true, BaseUrl: "http://auth/bub/"}, + Config: Config{Verbose: true, BaseUrl: "http://auth/bub"}, Source: &source.InMemorySource{sources}, } s.initializeSiteInfo() @@ -237,6 +239,7 @@ func TestSkipRender(t *testing.T) { must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("head", "")) + must(s.addTemplate("head_abs", "")) if err := s.CreatePages(); err != nil { t.Fatalf("Unable to create pages: %s", err) @@ -254,12 +257,13 @@ func TestSkipRender(t *testing.T) { doc string expected string }{ - {"sect/doc1.html", "some content
\n"}, - {"sect/doc2.html", "more content"}, - {"sect/doc3.html", "some content
\n"}, - {"sect/doc4.html", "some content
\n"}, - {"sect/doc5.html", "body5"}, - {"doc7.html", "doc7 content"}, + {"sect/doc1.html", "some content
\n"}, + {"sect/doc2.html", "more content"}, + {"sect/doc3.html", "some content
\n"}, + {"sect/doc4.html", "some content
\n"}, + {"sect/doc5.html", "body5"}, + {"sect/doc6.html", "body5"}, + {"doc7.html", "doc7 content"}, } for _, test := range tests { @@ -283,12 +287,12 @@ func TestAbsUrlify(t *testing.T) { } s := &Site{ Target: target, - Config: Config{BaseUrl: "http://auth/bub/"}, + Config: Config{BaseUrl: "http://auth/bub"}, Source: &source.InMemorySource{sources}, } s.initializeSiteInfo() s.prepTemplates() - must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL)) + must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS)) if err := s.CreatePages(); err != nil { t.Fatalf("Unable to create pages: %s", err) @@ -305,8 +309,8 @@ func TestAbsUrlify(t *testing.T) { tests := []struct { file, expected string }{ - {"content/blue/doc2.html", "Going"}, - {"sect/doc1.html", "link"}, + {"content/blue/doc2.html", "Going"}, + {"sect/doc1.html", "link"}, } for _, test := range tests { diff --git a/hugolib/site_url_test.go b/hugolib/site_url_test.go index 27be0721f..9d41c2046 100644 --- a/hugolib/site_url_test.go +++ b/hugolib/site_url_test.go @@ -81,7 +81,7 @@ func TestPageCount(t *testing.T) { t.Errorf("No indexed rendered. %v", target.Files) } - expected := ".." + expected := ".." if string(blueIndex) != expected { t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex)) } diff --git a/transform/absurl.go b/transform/absurl.go index f66edab9d..5967e597e 100644 --- a/transform/absurl.go +++ b/transform/absurl.go @@ -3,18 +3,29 @@ package transform import ( htmltran "code.google.com/p/go-html-transform/html/transform" "net/url" + "bytes" ) -func AbsURL(absURL string) (trs []*htmltran.Transform, err error) { +func AbsURL(absURL string) (trs []link, err error) { var baseURL *url.URL if baseURL, err = url.Parse(absURL); err != nil { return } - if trs, err = absUrlify(baseURL, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { - return - } + var ( + srcdq = []byte(" src=\""+baseURL.String()+"/") + hrefdq = []byte(" href=\""+baseURL.String()+"/") + srcsq = []byte(" src='"+baseURL.String()+"/") + hrefsq = []byte(" href='"+baseURL.String()+"/") + ) + trs = append(trs, func(content []byte) []byte { + content = bytes.Replace(content, []byte(" src=\"/"), srcdq, -1) + content = bytes.Replace(content, []byte(" src='/"), srcsq, -1) + content = bytes.Replace(content, []byte(" href=\"/"), hrefdq, -1) + content = bytes.Replace(content, []byte(" href='/"), hrefsq, -1) + return content + }) return } diff --git a/transform/chain.go b/transform/chain.go index a4929b70d..fb3c2985c 100644 --- a/transform/chain.go +++ b/transform/chain.go @@ -1,25 +1,30 @@ package transform import ( - htmltran "code.google.com/p/go-html-transform/html/transform" + "bytes" "io" ) -type chain []*htmltran.Transform +type trans func([]byte) []byte -func NewChain(trs ...*htmltran.Transform) chain { +type link trans + +type chain []link + +func NewChain(trs ...link) chain { return trs } func (c *chain) Apply(w io.Writer, r io.Reader) (err error) { - var tr *htmltran.Transformer - - if tr, err = htmltran.NewFromReader(r); err != nil { - return + buffer := new(bytes.Buffer) + buffer.ReadFrom(r) + b := buffer.Bytes() + for _, tr := range *c { + b = tr(b) } - - tr.ApplyAll(*c...) - - return tr.Render(w) + buffer.Reset() + buffer.Write(b) + buffer.WriteTo(w) + return } diff --git a/transform/chain_test.go b/transform/chain_test.go index 594b5a5d4..94135f6d7 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -5,6 +5,14 @@ import ( "testing" ) +const H5_JS_CONTENT_ABS_URL_WITH_NAV = "