diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 18:14:41 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 23:09:01 +0200 |
commit | 142558719324aa1628541d556ef1fa2d123f1e68 (patch) | |
tree | 7df0443f97c11105582af4a31a935d79d13eb752 /tpl | |
parent | a883948c4fa6d6de9ef2912709b42655c4cead83 (diff) |
hugolib: Add optional outputFormat to Ref/RelRef
Fixes #3224
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/tplimpl/template_embedded.go | 4 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 48 |
2 files changed, 20 insertions, 32 deletions
diff --git a/tpl/tplimpl/template_embedded.go b/tpl/tplimpl/template_embedded.go index b1562a0e7..2d4769f78 100644 --- a/tpl/tplimpl/template_embedded.go +++ b/tpl/tplimpl/template_embedded.go @@ -14,8 +14,8 @@ package tplimpl func (t *templateHandler) embedShortcodes() { - t.addInternalShortcode("ref.html", `{{ .Get 0 | ref .Page }}`) - t.addInternalShortcode("relref.html", `{{ .Get 0 | relref .Page }}`) + t.addInternalShortcode("ref.html", `{{ if len .Params | eq 2 }}{{ ref .Page (.Get 0) (.Get 1) }}{{ else }}{{ ref .Page (.Get 0) }}{{ end }}`) + t.addInternalShortcode("relref.html", `{{ if len .Params | eq 2 }}{{ relref .Page (.Get 0) (.Get 1) }}{{ else }}{{ relref .Page (.Get 0) }}{{ end }}`) t.addInternalShortcode("highlight.html", `{{ if len .Params | eq 2 }}{{ highlight .Inner (.Get 0) (.Get 1) }}{{ else }}{{ highlight .Inner (.Get 0) "" }}{{ end }}`) t.addInternalShortcode("test.html", `This is a simple Test`) t.addInternalShortcode("figure.html", `<!-- image --> diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 9703f6cff..11ab4511c 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -46,7 +46,6 @@ import ( "github.com/spf13/afero" "github.com/spf13/cast" "github.com/spf13/hugo/helpers" - jww "github.com/spf13/jwalterweatherman" // Importing image codecs for image.DecodeConfig _ "image/gif" @@ -1432,41 +1431,30 @@ func plainify(in interface{}) (string, error) { return helpers.StripHTML(s), nil } -func refPage(page interface{}, ref, methodName string) template.HTML { - value := reflect.ValueOf(page) - - method := value.MethodByName(methodName) - - if method.IsValid() && method.Type().NumIn() == 1 && method.Type().NumOut() == 2 { - result := method.Call([]reflect.Value{reflect.ValueOf(ref)}) - - url, err := result[0], result[1] - - if !err.IsNil() { - jww.ERROR.Printf("%s", err.Interface()) - return template.HTML(fmt.Sprintf("%s", err.Interface())) - } - - if url.String() == "" { - jww.ERROR.Printf("ref %s could not be found\n", ref) - return template.HTML(ref) - } - - return template.HTML(url.String()) - } - - jww.ERROR.Printf("Can only create references from Page and Node objects.") - return template.HTML(ref) +type reflinker interface { + Ref(refs ...string) (string, error) + RelRef(refs ...string) (string, error) } // ref returns the absolute URL path to a given content item. -func ref(page interface{}, ref string) template.HTML { - return refPage(page, ref, "Ref") +func ref(in interface{}, refs ...string) (template.HTML, error) { + p, ok := in.(reflinker) + if !ok { + return "", errors.New("invalid Page received in ref") + } + s, err := p.Ref(refs...) + return template.HTML(s), err } // relRef returns the relative URL path to a given content item. -func relRef(page interface{}, ref string) template.HTML { - return refPage(page, ref, "RelRef") +func relRef(in interface{}, refs ...string) (template.HTML, error) { + p, ok := in.(reflinker) + if !ok { + return "", errors.New("invalid Page received in relref") + } + + s, err := p.RelRef(refs...) + return template.HTML(s), err } // chomp removes trailing newline characters from a string. |