From 86233c00a0a04e8f0130a5970de8d40e6738ef74 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Tue, 5 Nov 2013 22:28:06 +0000 Subject: [PATCH] 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. --- hugolib/site.go | 10 +------ hugolib/site_test.go | 30 ++++++++++--------- hugolib/site_url_test.go | 2 +- transform/absurl.go | 19 +++++++++--- transform/chain.go | 27 ++++++++++------- transform/chain_test.go | 33 ++++++--------------- transform/nav.go | 12 -------- transform/nav_test.go | 58 ------------------------------------- transform/posttrans_test.go | 14 +++++---- 9 files changed, 68 insertions(+), 137 deletions(-) delete mode 100644 transform/nav.go delete mode 100644 transform/nav_test.go 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", "

title

\n\n

some content

\n"}, - {"sect/doc2.html", "more content"}, - {"sect/doc3.html", "

doc3

\n\n

some content

\n"}, - {"sect/doc4.html", "

doc4

\n\n

some content

\n"}, - {"sect/doc5.html", "body5"}, - {"doc7.html", "doc7 content"}, + {"sect/doc1.html", "

title

\n\n

some content

\n"}, + {"sect/doc2.html", "more content"}, + {"sect/doc3.html", "

doc3

\n\n

some content

\n"}, + {"sect/doc4.html", "

doc4

\n\n

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 = "
content foobar. Follow up
" + +const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "
content foobar. Follow up
" + +var two_chain_tests = []test{ + {H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV}, +} + func TestChainZeroTransformers(t *testing.T) { tr := NewChain() 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 = "
content foobar. Follow up
" - -const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "
content foobar. Follow up
" - -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) { - 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 index 7783b6175..000000000 --- a/transform/nav.go +++ /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 index 372d3f594..000000000 --- a/transform/nav_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package transform - -import ( - "bytes" - "strings" - "testing" -) - -const HTML_WITH_NAV = ` - - - - - - -` -const EXPECTED_HTML_WITH_NAV_1 = ` - - - - -` - -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() - } -} diff --git a/transform/posttrans_test.go b/transform/posttrans_test.go index 12f7df1f6..d7d06fee7 100644 --- a/transform/posttrans_test.go +++ b/transform/posttrans_test.go @@ -6,14 +6,18 @@ import ( "testing" ) -const H5_JS_CONTENT_DOUBLE_QUOTE = "
content foobar. Follow up
" -const H5_JS_CONTENT_SINGLE_QUOTE = "
content foobar. Follow up
" +const H5_JS_CONTENT_DOUBLE_QUOTE = "
content foobar. Follow up
" + +const H5_JS_CONTENT_SINGLE_QUOTE = "
content foobar. Follow up
" + const H5_JS_CONTENT_ABS_URL = "
content foobar. Follow up
" // URL doesn't recognize authorities. BUG? //const H5_JS_CONTENT_ABS_URL = "
content foobar. Follow up
" -const CORRECT_OUTPUT_SRC_HREF = "
content foobar. Follow up
" +const CORRECT_OUTPUT_SRC_HREF_DQ = "
content foobar. Follow up
" + +const CORRECT_OUTPUT_SRC_HREF_SQ = "
content foobar. Follow up
" 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}, }