Provide (relative) reference funcs & shortcodes.

-   `.Ref` and `.RelRef` take a reference (the logical filename for a
    page, including extension and/or a document fragment ID) and return
    a permalink (or relative permalink) to the referenced document.

    -   If the reference is a page name (such as `about.md`), the page
        will be discovered and the permalink will be returned: `/about/`
    -   If the reference is a page name with a fragment (such as
        `about.md#who`), the page will be discovered and used to add the
        `page.UniqueID()` to the resulting fragment and permalink:
        `/about/#who:deadbeef`.
    -   If the reference is a fragment and `.*Ref` has been called from
        a `Node` or `SiteInfo`, it will be returned as is: `#who`.
    -   If the reference is a fragment and `.*Ref` has been called from
        a `Page`, it will be returned with the page’s unique ID:
        `#who:deadbeef`.

-   `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
    `Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
    templates.

-   `.*Ref` cannot be used in content, so two shortcodes have been
    created to provide the functionality to content: `ref` and `relref`.
    These are intended to be used within markup, like `[Who]({{% ref
    about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.

-   There are also `ref` and `relref` template functions (used to create
    the shortcodes) that expect a `Page` or `Node` object and the
    reference string (e.g., `{{ relref . "about.md" }}` or `{{
    "about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
    `Node` or `Page` objects.

-   Shortcode handling had to use a *differently unique* wrapper in
    `createShortcodePlaceholder` because of the way that the `ref` and
    `relref` are intended to be used in content.
This commit is contained in:
Austin Ziegler
2014-11-24 01:15:34 -05:00
committed by spf13
parent 14bce119b6
commit 112c3c5c04
7 changed files with 183 additions and 35 deletions

View File

@@ -16,6 +16,7 @@ package tpl
import (
"bytes"
"errors"
"fmt"
"github.com/eknkc/amber"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
@@ -110,6 +111,8 @@ func New() Template {
"upper": func(a string) string { return strings.ToUpper(a) },
"title": func(a string) string { return strings.Title(a) },
"partial": Partial,
"ref": Ref,
"relref": RelRef,
}
templates.Funcs(funcMap)
@@ -427,6 +430,41 @@ func Markdownify(text string) template.HTML {
return template.HTML(helpers.RenderBytes([]byte(text), "markdown", ""))
}
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)
}
func Ref(page interface{}, ref string) template.HTML {
return refPage(page, ref, "Ref")
}
func RelRef(page interface{}, ref string) template.HTML {
return refPage(page, ref, "RelRef")
}
func SafeHtml(text string) template.HTML {
return template.HTML(text)
}