Remove the hugo-nav function

Remove the hugo-nav since it relied on a slow library.  The current
build reimplements the absurl functionality based on string replace.
Discovered that my prior implementation missed the requirement for
making absolute paths (/path) absolute with the host, whereas a relative
path is left untouched.  Updated the test cases to support this if this
is reimplemented.
This commit is contained in:
Noah Campbell 2013-11-05 22:28:06 +00:00
parent 1cebce12ad
commit 86233c00a0
9 changed files with 68 additions and 137 deletions

View file

@ -576,19 +576,11 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
return return
} }
section := ""
if page, ok := d.(*Page); ok {
section, _ = page.RelPermalink()
}
absURL, err := transform.AbsURL(s.Config.BaseUrl) absURL, err := transform.AbsURL(s.Config.BaseUrl)
if err != nil { if err != nil {
return return
} }
transformer := transform.NewChain( transformer := transform.NewChain(absURL...)
append(absURL, transform.NavActive(section, "hugo-nav")...)...,
)
var renderBuffer *bytes.Buffer var renderBuffer *bytes.Buffer

View file

@ -23,7 +23,8 @@ content`
TEMPLATE_CONTENT = "{{ .Content }}" TEMPLATE_CONTENT = "{{ .Content }}"
TEMPLATE_DATE = "{{ .Date }}" TEMPLATE_DATE = "{{ .Date }}"
INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}" INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
TEMPLATE_WITH_URL = "<a href=\"foobar.jpg\">Going</a>" TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>"
TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>"
PAGE_URL_SPECIFIED = `--- PAGE_URL_SPECIFIED = `---
title: simple template title: simple template
url: "mycategory/my-whatever-content/" url: "mycategory/my-whatever-content/"
@ -128,7 +129,7 @@ func TestRenderThing(t *testing.T) {
} }
func HTML(in string) string { func HTML(in string) string {
return fmt.Sprintf("<html><head></head><body>%s</body></html>", in) return in
} }
func TestRenderThingOrDefault(t *testing.T) { func TestRenderThingOrDefault(t *testing.T) {
@ -224,12 +225,13 @@ func TestSkipRender(t *testing.T) {
{"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"}, {"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"},
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"}, {"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"},
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"}, {"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
{"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"},
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""}, {"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
} }
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{Verbose: true, BaseUrl: "http://auth/bub/"}, Config: Config{Verbose: true, BaseUrl: "http://auth/bub"},
Source: &source.InMemorySource{sources}, Source: &source.InMemorySource{sources},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -237,6 +239,7 @@ func TestSkipRender(t *testing.T) {
must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("_default/single.html", "{{.Content}}"))
must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>")) must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>"))
must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>"))
if err := s.CreatePages(); err != nil { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
@ -254,12 +257,13 @@ func TestSkipRender(t *testing.T) {
doc string doc string
expected string expected string
}{ }{
{"sect/doc1.html", "<html><head></head><body><h1>title</h1>\n\n<p>some <em>content</em></p>\n</body></html>"}, {"sect/doc1.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"},
{"sect/doc2.html", "<!DOCTYPE html><html><head></head><body>more content</body></html>"}, {"sect/doc2.html", "<!doctype html><html><body>more content</body></html>"},
{"sect/doc3.html", "<html><head></head><body><h1>doc3</h1>\n\n<p><em>some</em> content</p>\n</body></html>"}, {"sect/doc3.html", "<h1>doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{"sect/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"}, {"sect/doc4.html", "<h1>doc4</h1>\n\n<p><em>some content</em></p>\n"},
{"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, {"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
{"doc7.html", "<html><head></head><body>doc7 content</body></html>"}, {"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
{"doc7.html", "<html><body>doc7 content</body></html>"},
} }
for _, test := range tests { for _, test := range tests {
@ -283,12 +287,12 @@ func TestAbsUrlify(t *testing.T) {
} }
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{BaseUrl: "http://auth/bub/"}, Config: Config{BaseUrl: "http://auth/bub"},
Source: &source.InMemorySource{sources}, Source: &source.InMemorySource{sources},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
s.prepTemplates() 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 { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
@ -305,8 +309,8 @@ func TestAbsUrlify(t *testing.T) {
tests := []struct { tests := []struct {
file, expected string file, expected string
}{ }{
{"content/blue/doc2.html", "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"}, {"content/blue/doc2.html", "<a href=\"http://auth/bub/foobar.jpg\">Going</a>"},
{"sect/doc1.html", "<!DOCTYPE html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, {"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
} }
for _, test := range tests { for _, test := range tests {

View file

@ -81,7 +81,7 @@ func TestPageCount(t *testing.T) {
t.Errorf("No indexed rendered. %v", target.Files) t.Errorf("No indexed rendered. %v", target.Files)
} }
expected := "<html><head></head><body>..</body></html>" expected := ".."
if string(blueIndex) != expected { if string(blueIndex) != expected {
t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex)) t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex))
} }

View file

@ -3,18 +3,29 @@ package transform
import ( import (
htmltran "code.google.com/p/go-html-transform/html/transform" htmltran "code.google.com/p/go-html-transform/html/transform"
"net/url" "net/url"
"bytes"
) )
func AbsURL(absURL string) (trs []*htmltran.Transform, err error) { func AbsURL(absURL string) (trs []link, err error) {
var baseURL *url.URL var baseURL *url.URL
if baseURL, err = url.Parse(absURL); err != nil { if baseURL, err = url.Parse(absURL); err != nil {
return return
} }
if trs, err = absUrlify(baseURL, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { var (
return 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 return
} }

View file

@ -1,25 +1,30 @@
package transform package transform
import ( import (
htmltran "code.google.com/p/go-html-transform/html/transform" "bytes"
"io" "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 return trs
} }
func (c *chain) Apply(w io.Writer, r io.Reader) (err error) { func (c *chain) Apply(w io.Writer, r io.Reader) (err error) {
var tr *htmltran.Transformer buffer := new(bytes.Buffer)
buffer.ReadFrom(r)
if tr, err = htmltran.NewFromReader(r); err != nil { b := buffer.Bytes()
return for _, tr := range *c {
b = tr(b)
} }
buffer.Reset()
tr.ApplyAll(*c...) buffer.Write(b)
buffer.WriteTo(w)
return tr.Render(w) return
} }

View file

@ -5,6 +5,14 @@ import (
"testing" "testing"
) )
const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"/foobar\">foobar</a>. Follow up</article></body></html>"
const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article></body></html>"
var two_chain_tests = []test{
{H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV},
}
func TestChainZeroTransformers(t *testing.T) { func TestChainZeroTransformers(t *testing.T) {
tr := NewChain() tr := NewChain()
in := new(bytes.Buffer) in := new(bytes.Buffer)
@ -14,32 +22,9 @@ func TestChainZeroTransformers(t *testing.T) {
} }
} }
func TestChainOneTransformer(t *testing.T) {
absURL, _ := AbsURL("http://base")
tr := NewChain(absURL...)
apply(t.Errorf, tr, abs_url_tests)
}
const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"/foobar\">foobar</a>. Follow up</article></body></html>"
const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\" class=\"active\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article></body></html>"
var two_chain_tests = []test{
{H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV},
}
func TestChainTwoTransformer(t *testing.T) {
absURL, _ := AbsURL("http://two")
nav := NavActive("section_1", "hugo-nav")
tr := NewChain(append(absURL, nav...)...)
apply(t.Errorf, tr, two_chain_tests)
}
func BenchmarkChain(b *testing.B) { func BenchmarkChain(b *testing.B) {
absURL, _ := AbsURL("http://two") absURL, _ := AbsURL("http://two")
nav := NavActive("section_1", "hugo-nav") tr := NewChain(absURL...)
tr := NewChain(append(absURL, nav...)...)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

View file

@ -1,12 +0,0 @@
package transform
import (
htmltran "code.google.com/p/go-html-transform/html/transform"
"fmt"
)
func NavActive(section, attrName string) (tr []*htmltran.Transform) {
ma := htmltran.MustTrans(htmltran.ModifyAttrib("class", "active"), fmt.Sprintf("li[%s=%s]", attrName, section))
tr = append(tr, ma)
return
}

View file

@ -1,58 +0,0 @@
package transform
import (
"bytes"
"strings"
"testing"
)
const HTML_WITH_NAV = `<!DOCTYPE html>
<html>
<head></head>
<body>
<nav>
<ul class="nav navbar-nav">
<li hugo-nav="section_1"><a href="#">Section 1</a></li>
<li hugo-nav="section_2"><a href="#">Section 2</a></li>
</ul>
</nav>
</body>
</html>
`
const EXPECTED_HTML_WITH_NAV_1 = `<!DOCTYPE html><html><head></head>
<body>
<nav>
<ul class="nav navbar-nav">
<li hugo-nav="section_1"><a href="#">Section 1</a></li>
<li hugo-nav="section_2" class="active"><a href="#">Section 2</a></li>
</ul>
</nav>
</body></html>`
func TestSetNav(t *testing.T) {
trs := NavActive("section_2", "hugo-nav")
chain := NewChain(trs...)
out := new(bytes.Buffer)
if err := chain.Apply(out, strings.NewReader(HTML_WITH_NAV)); err != nil {
t.Errorf("Unexpected error in Apply() for NavActive: %s", err)
}
expected := EXPECTED_HTML_WITH_NAV_1
if out.String() != expected {
t.Errorf("NavActive.Apply output expected and got:\n%q\n%q", expected, out.String())
}
}
func BenchmarkTransform(b *testing.B) {
tr := NavActive("section_2", "hugo-nav")
chain := NewChain(tr...)
out := new(bytes.Buffer)
for i := 0; i < b.N; i++ {
if err := chain.Apply(out, strings.NewReader(HTML_WITH_NAV)); err != nil {
b.Errorf("Unexpected error in Apply() for NavActive: %s", err)
}
out.Reset()
}
}

View file

@ -6,14 +6,18 @@ import (
"testing" "testing"
) )
const H5_JS_CONTENT_DOUBLE_QUOTE = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</article></body></html>" const H5_JS_CONTENT_DOUBLE_QUOTE = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"/foobar\">Follow up</a></article></body></html>"
const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foobar.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</article></body></html>"
const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='/foobar'>Follow up</a></article></body></html>"
const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>"
// URL doesn't recognize authorities. BUG? // URL doesn't recognize authorities. BUG?
//const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" //const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>"
const CORRECT_OUTPUT_SRC_HREF = "<!DOCTYPE html><html><head><script src=\"http://base/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"http://base/foobar\">foobar</a>. Follow up</article></body></html>" const CORRECT_OUTPUT_SRC_HREF_DQ = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"http://base/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"http://base/foobar\">Follow up</a></article></body></html>"
const CORRECT_OUTPUT_SRC_HREF_SQ = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='http://base/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='http://base/foobar'>Follow up</a></article></body></html>"
func TestAbsUrlify(t *testing.T) { func TestAbsUrlify(t *testing.T) {
tr, _ := AbsURL("http://base") tr, _ := AbsURL("http://base")
@ -27,8 +31,8 @@ type test struct {
} }
var abs_url_tests = []test{ var abs_url_tests = []test{
{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF}, {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ},
{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF}, {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ},
{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL}, {H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
} }