tpl: Add urls.Parse function

Add a urls.Parse template function that front-ends url.Parse from the Go
stdlib.

Fixes #3849
This commit is contained in:
Cameron Moore
2017-09-23 19:57:28 -05:00
committed by Bjørn Erik Pedersen
parent 19c5910485
commit 81ed564793
2 changed files with 81 additions and 0 deletions

View File

@@ -15,7 +15,9 @@ package urls
import (
"errors"
"fmt"
"html/template"
"net/url"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/cast"
@@ -43,6 +45,17 @@ func (ns *Namespace) AbsURL(a interface{}) (template.HTML, error) {
return template.HTML(ns.deps.PathSpec.AbsURL(s, false)), nil
}
// Parse parses rawurl into a URL structure. The rawurl may be relative or
// absolute.
func (ns *Namespace) Parse(rawurl interface{}) (*url.URL, error) {
s, err := cast.ToStringE(rawurl)
if err != nil {
return nil, fmt.Errorf("Error in Parse: %s", err)
}
return url.Parse(s)
}
// RelURL takes a given string and prepends the relative path according to a
// page's position in the project directory structure.
func (ns *Namespace) RelURL(a interface{}) (template.HTML, error) {