package hugolib
import (
+ "io"
"bitbucket.org/pkg/inflect"
"bytes"
"fmt"
"github.com/spf13/hugo/target"
helpers "github.com/spf13/hugo/template"
"github.com/spf13/hugo/template/bundle"
+ "github.com/spf13/hugo/transform"
"github.com/spf13/nitro"
"html/template"
"os"
Info SiteInfo
Shortcodes map[string]ShortcodeFunc
timer *nitro.B
+ Transformer *transform.Transformer
Target target.Output
Alias target.AliasPublisher
}
s.timerStep("render and write aliases")
s.ProcessShortcodes()
s.timerStep("render shortcodes")
- s.AbsUrlify()
s.timerStep("absolute URLify")
if err = s.RenderIndexes(); err != nil {
return
}
}
-func (s *Site) AbsUrlify() {
- baseWithoutTrailingSlash := strings.TrimRight(s.Config.BaseUrl, "/")
- baseWithSlash := baseWithoutTrailingSlash + "/"
- for _, page := range s.Pages {
- content := string(page.Content)
- content = strings.Replace(content, " src=\"/", " src=\""+baseWithSlash, -1)
- content = strings.Replace(content, " src='/", " src='"+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)
- page.Content = template.HTML(content)
- }
-}
-
func (s *Site) CreatePages() (err error) {
for _, file := range s.Source.Files() {
page, err := ReadFrom(file.Contents, file.Name)
if err != nil {
return err
}
- err = s.WritePublic(p.OutFile, content.Bytes())
+ err = s.WritePublic(p.OutFile, content)
if err != nil {
return err
}
var base string
base = plural + "/" + k
- err = s.WritePublic(base+".html", x.Bytes())
+ err = s.WritePublic(base+".html", x)
if err != nil {
return err
}
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
n.Permalink = permalink(s, n.Url)
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
- err = s.WritePublic(base+".xml", y.Bytes())
+ err = s.WritePublic(base+".xml", y)
if err != nil {
return err
}
return err
}
- err = s.WritePublic(plural+"/index.html", x.Bytes())
+ err = s.WritePublic(plural+"/index.html", x)
if err != nil {
return err
}
if err != nil {
return err
}
- err = s.WritePublic(section, content.Bytes())
+ err = s.WritePublic(section, content)
if err != nil {
return err
}
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
- err = s.WritePublic(section+"/index.xml", y.Bytes())
+ err = s.WritePublic(section+"/index.xml", y)
return err
}
}
if err != nil {
return err
}
- err = s.WritePublic("/", x.Bytes())
+ err = s.WritePublic("/", x)
if err != nil {
return err
}
n.Permalink = permalink(s, "index.xml")
y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
- err = s.WritePublic("index.xml", y.Bytes())
+ err = s.WritePublic("index.xml", y)
return err
}
if err != nil {
return err
}
- err = s.WritePublic("404.html", x.Bytes())
+ err = s.WritePublic("404.html", x)
return err
}
}
}
-func (s *Site) WritePublic(path string, content []byte) (err error) {
+func (s *Site) WritePublic(path string, content io.Reader) (err error) {
s.initTarget()
if s.Config.Verbose {
fmt.Println(path)
}
- return s.Target.Publish(path, bytes.NewReader(content))
+ if s.Transformer == nil {
+ s.Transformer = &transform.Transformer{BaseURL: s.Config.BaseUrl}
+ }
+ final := new(bytes.Buffer)
+ s.Transformer.Apply(content, final)
+ return s.Target.Publish(path, final)
}
func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
var TEMPLATE_CONTENT = "{{ .Content }}"
var TEMPLATE_DATE = "{{ .Date }}"
var INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
+var TEMPLATE_WITH_URL = "<a href=\"foobar.jpg\">Going</a>"
var PAGE_URL_SPECIFIED = `---
title: simple template
t.Errorf("Outfile does not match. Expected '%s', got '%s'", expected, p.OutFile)
}
}
+
+func TestAbsUrlify(t *testing.T) {
+ files := make(map[string][]byte)
+ target := &InMemoryTarget{files: files}
+ s := &Site{
+ Target: target,
+ Config: Config{BaseUrl: "http://auth/bub/"},
+ Source: &inMemorySource{urlFakeSource},
+ }
+ s.initializeSiteInfo()
+ s.prepTemplates()
+ must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL))
+
+ if err := s.CreatePages(); err != nil {
+ t.Fatalf("Unable to create pages: %s", err)
+ }
+
+ if err := s.BuildSiteMeta(); err != nil {
+ t.Fatalf("Unable to build site metadata: %s", err)
+ }
+
+ if err := s.RenderPages(); err != nil {
+ t.Fatalf("Unable to render pages. %s", err)
+ }
+
+ content, ok := target.files["content/blue/slug-doc-1.html"]
+ if !ok {
+ t.Fatalf("Unable to locate rendered content")
+ }
+
+ expected := "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"
+ if string(content) != expected {
+ t.Errorf("Expected: %q, got: %q", expected, string(content))
+ }
+}
return
}
- if err = t.absUrlify(tr); err != nil {
+ if err = t.absUrlify(tr, elattr{"a", "href"}, elattr{"script", "src"}); err != nil {
return
}
return tr.Render(w)
}
-func (t *Transformer) absUrlify(tr *htmltran.Transformer) (err error) {
+type elattr struct {
+ tag, attr string
+}
+
+func (t *Transformer) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err error) {
var baseURL, inURL *url.URL
if baseURL, err = url.Parse(t.BaseURL); err != nil {
return baseURL.ResolveReference(inURL).String()
}
- if err = tr.Apply(htmltran.TransformAttrib("src", replace), "script"); err != nil {
- return
+ for _, el := range selectors {
+ if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil {
+ return
+ }
}
- return tr.Apply(htmltran.TransformAttrib("href", replace), "a")
+ return
}