Remove the hugo-nav function
authorNoah Campbell <noahcampbell@gmail.com>
Tue, 5 Nov 2013 22:28:06 +0000 (22:28 +0000)
committerNoah Campbell <noahcampbell@gmail.com>
Tue, 5 Nov 2013 22:28:06 +0000 (22:28 +0000)
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.

hugolib/site.go
hugolib/site_test.go
hugolib/site_url_test.go
transform/absurl.go
transform/chain.go
transform/chain_test.go
transform/nav.go [deleted file]
transform/nav_test.go [deleted file]
transform/posttrans_test.go

index fbebed94c4a666bd0a22c05bc20421a16084b386..5e22fd96968d5bb42d5ee2fd26c28978daf430e7 100644 (file)
@@ -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
 
index 3a47b7ff7559819e7b3a6fbff745d004151f2f1e..2ef18441550acb94eb6a9cb940e4c10b42cd9eb7 100644 (file)
@@ -23,7 +23,8 @@ content`
        TEMPLATE_CONTENT             = "{{ .Content }}"
        TEMPLATE_DATE                = "{{ .Date }}"
        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           = `---
 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("<html><head></head><body>%s</body></html>", 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("<!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>"), ""},
        }
 
        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", "<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 {
                t.Fatalf("Unable to create pages: %s", err)
@@ -254,12 +257,13 @@ func TestSkipRender(t *testing.T) {
                doc      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/doc2.html", "<!DOCTYPE html><html><head></head><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/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"},
-               {"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
-               {"doc7.html", "<html><head></head><body>doc7 content</body></html>"},
+               {"sect/doc1.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"},
+               {"sect/doc2.html", "<!doctype html><html><body>more content</body></html>"},
+               {"sect/doc3.html", "<h1>doc3</h1>\n\n<p><em>some</em> content</p>\n"},
+               {"sect/doc4.html", "<h1>doc4</h1>\n\n<p><em>some content</em></p>\n"},
+               {"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</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 {
@@ -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", "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"},
-               {"sect/doc1.html", "<!DOCTYPE html><html><head></head><body><a href=\"#frag1\">link</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>"},
        }
 
        for _, test := range tests {
index 27be0721f9fdda223622a125aee2edac4828ce70..9d41c204686222be905637bbf381b5660099a67c 100644 (file)
@@ -81,7 +81,7 @@ func TestPageCount(t *testing.T) {
                t.Errorf("No indexed rendered. %v", target.Files)
        }
 
-       expected := "<html><head></head><body>..</body></html>"
+       expected := ".."
        if string(blueIndex) != expected {
                t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex))
        }
index f66edab9d55198b8654aef4f4003ad2063c2e5c7..5967e597e86b7dc49324c795f3c490d85a89d836 100644 (file)
@@ -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
 }
 
index a4929b70d8d0da697d040a5b3da3945f9e562f2b..fb3c2985c5300a42a68a9a83cb7eb664a6282480 100644 (file)
@@ -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
 }
index 594b5a5d41cb2a13a29b03012a72f9f620387b35..94135f6d7232b5830d1bdfa5593a7c4724643aa1 100644 (file)
@@ -5,41 +5,26 @@ import (
        "testing"
 )
 
-func TestChainZeroTransformers(t *testing.T) {
-       tr := NewChain()
-       in := new(bytes.Buffer)
-       out := new(bytes.Buffer)
-       if err := tr.Apply(in, out); err != nil {
-               t.Errorf("A zero transformer chain returned an error.")
-       }
-}
-
-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>"
+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 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 TestChainZeroTransformers(t *testing.T) {
+       tr := NewChain()
+       in := new(bytes.Buffer)
+       out := new(bytes.Buffer)
+       if err := tr.Apply(in, out); err != nil {
+               t.Errorf("A zero transformer chain returned an error.")
+       }
 }
 
 func BenchmarkChain(b *testing.B) {
-
        absURL, _ := AbsURL("http://two")
-       nav := NavActive("section_1", "hugo-nav")
-       tr := NewChain(append(absURL, nav...)...)
+       tr := NewChain(absURL...)
 
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
diff --git a/transform/nav.go b/transform/nav.go
deleted file mode 100644 (file)
index 7783b61..0000000
+++ /dev/null
@@ -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
-}
diff --git a/transform/nav_test.go b/transform/nav_test.go
deleted file mode 100644 (file)
index 372d3f5..0000000
+++ /dev/null
@@ -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()
-       }
-}
index 12f7df1f69194cc33555737042e4b8228f705778..d7d06fee7894d462265c981549900ac5dbf6fdfd 100644 (file)
@@ -6,14 +6,18 @@ import (
        "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_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_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><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>"
 
 // 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 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) {
        tr, _ := AbsURL("http://base")
@@ -27,8 +31,8 @@ type test struct {
 }
 
 var abs_url_tests = []test{
-       {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
-       {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
+       {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ},
+       {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ},
        {H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
 }