2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-11-29 22:32:53 -05:00
|
|
|
// Package urls provides template functions to deal with URLs.
|
2017-03-13 18:55:02 -04:00
|
|
|
package urls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-09-23 20:57:28 -04:00
|
|
|
"fmt"
|
2017-03-13 18:55:02 -04:00
|
|
|
"html/template"
|
2017-09-23 20:57:28 -04:00
|
|
|
"net/url"
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
"github.com/gohugoio/hugo/common/urls"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new instance of the urls-namespaced template functions.
|
|
|
|
func New(deps *deps.Deps) *Namespace {
|
|
|
|
return &Namespace{
|
2017-11-12 04:03:56 -05:00
|
|
|
deps: deps,
|
2023-01-04 12:24:36 -05:00
|
|
|
multihost: deps.Conf.IsMultihost(),
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "urls" namespace.
|
|
|
|
type Namespace struct {
|
2017-11-12 04:03:56 -05:00
|
|
|
deps *deps.Deps
|
|
|
|
multihost bool
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// AbsURL takes the string s and converts it to an absolute URL.
|
|
|
|
func (ns *Namespace) AbsURL(s any) (template.HTML, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
return template.HTML(ns.deps.PathSpec.AbsURL(ss, false)), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2017-09-23 20:57:28 -04:00
|
|
|
// Parse parses rawurl into a URL structure. The rawurl may be relative or
|
|
|
|
// absolute.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
|
2017-09-23 20:57:28 -04:00
|
|
|
s, err := cast.ToStringE(rawurl)
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("Error in Parse: %w", err)
|
2017-09-23 20:57:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return url.Parse(s)
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// RelURL takes the string s and prepends the relative path according to a
|
2017-03-13 18:55:02 -04:00
|
|
|
// page's position in the project directory structure.
|
2022-04-30 12:12:08 -04:00
|
|
|
func (ns *Namespace) RelURL(s any) (template.HTML, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
return template.HTML(ns.deps.PathSpec.RelURL(ss, false)), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// URLize returns the the strings s formatted as an URL.
|
|
|
|
func (ns *Namespace) URLize(s any) (string, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-05-01 03:06:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-04-30 12:12:08 -04:00
|
|
|
return ns.deps.PathSpec.URLize(ss), nil
|
2017-05-01 03:06:42 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// Anchorize creates sanitized anchor name version of the string s that is compatible
|
|
|
|
// with how your configured markdown renderer does it.
|
|
|
|
func (ns *Namespace) Anchorize(s any) (string, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2018-04-15 17:17:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-04-30 12:12:08 -04:00
|
|
|
return ns.deps.ContentSpec.SanitizeAnchorName(ss), nil
|
2018-04-15 17:17:50 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// Ref returns the absolute URL path to a given content item from Page p.
|
|
|
|
func (ns *Namespace) Ref(p any, args any) (template.HTML, error) {
|
|
|
|
pp, ok := p.(urls.RefLinker)
|
2017-03-13 18:55:02 -04:00
|
|
|
if !ok {
|
|
|
|
return "", errors.New("invalid Page received in Ref")
|
|
|
|
}
|
2018-07-17 15:44:08 -04:00
|
|
|
argsm, err := ns.refArgsToMap(args)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-04-30 12:12:08 -04:00
|
|
|
s, err := pp.Ref(argsm)
|
2017-03-13 18:55:02 -04:00
|
|
|
return template.HTML(s), err
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// RelRef returns the relative URL path to a given content item from Page p.
|
|
|
|
func (ns *Namespace) RelRef(p any, args any) (template.HTML, error) {
|
|
|
|
pp, ok := p.(urls.RefLinker)
|
2017-03-13 18:55:02 -04:00
|
|
|
if !ok {
|
|
|
|
return "", errors.New("invalid Page received in RelRef")
|
|
|
|
}
|
2018-07-17 15:44:08 -04:00
|
|
|
argsm, err := ns.refArgsToMap(args)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
s, err := pp.RelRef(argsm)
|
2017-03-13 18:55:02 -04:00
|
|
|
return template.HTML(s), err
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) {
|
2018-07-17 15:44:08 -04:00
|
|
|
var (
|
|
|
|
s string
|
|
|
|
of string
|
|
|
|
)
|
2019-09-29 08:51:51 -04:00
|
|
|
|
|
|
|
v := args
|
2022-03-17 17:03:27 -04:00
|
|
|
if _, ok := v.([]any); ok {
|
2019-09-29 08:51:51 -04:00
|
|
|
v = cast.ToStringSlice(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any:
|
2018-07-17 15:44:08 -04:00
|
|
|
return v, nil
|
|
|
|
case map[string]string:
|
2022-03-17 17:03:27 -04:00
|
|
|
m := make(map[string]any)
|
2018-07-17 15:44:08 -04:00
|
|
|
for k, v := range v {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case []string:
|
|
|
|
if len(v) == 0 || len(v) > 2 {
|
2020-12-16 06:11:32 -05:00
|
|
|
return nil, fmt.Errorf("invalid number of arguments to ref")
|
2018-07-17 15:44:08 -04:00
|
|
|
}
|
|
|
|
// These where the options before we introduced the map type:
|
|
|
|
s = v[0]
|
|
|
|
if len(v) == 2 {
|
|
|
|
of = v[1]
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
s, err = cast.ToStringE(args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-29 08:51:51 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
return map[string]any{
|
2018-07-17 15:44:08 -04:00
|
|
|
"path": s,
|
|
|
|
"outputFormat": of,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// RelLangURL takes the string s and prepends the relative path according to a
|
2017-03-13 18:55:02 -04:00
|
|
|
// page's position in the project directory structure and the current language.
|
2022-04-30 12:12:08 -04:00
|
|
|
func (ns *Namespace) RelLangURL(s any) (template.HTML, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
return template.HTML(ns.deps.PathSpec.RelURL(ss, !ns.multihost)), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// AbsLangURL the string s and converts it to an absolute URL according
|
2017-03-13 18:55:02 -04:00
|
|
|
// to a page's position in the project directory structure and the current
|
|
|
|
// language.
|
2022-04-30 12:12:08 -04:00
|
|
|
func (ns *Namespace) AbsLangURL(s any) (template.HTML, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
return template.HTML(ns.deps.PathSpec.AbsURL(ss, !ns.multihost)), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2023-04-14 16:27:16 -04:00
|
|
|
|
|
|
|
// JoinPath joins the provided elements into a URL string and cleans the result
|
2023-05-19 11:29:30 -04:00
|
|
|
// of any ./ or ../ elements. If the argument list is empty, JoinPath returns
|
|
|
|
// an empty string.
|
2023-04-14 16:27:16 -04:00
|
|
|
func (ns *Namespace) JoinPath(elements ...any) (string, error) {
|
|
|
|
|
2023-05-19 11:29:30 -04:00
|
|
|
if len(elements) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:27:16 -04:00
|
|
|
var selements []string
|
|
|
|
for _, e := range elements {
|
|
|
|
switch v := e.(type) {
|
|
|
|
case []string:
|
2023-05-19 11:29:30 -04:00
|
|
|
selements = append(selements, v...)
|
2023-04-14 16:27:16 -04:00
|
|
|
case []any:
|
|
|
|
for _, e := range v {
|
|
|
|
se, err := cast.ToStringE(e)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
selements = append(selements, se)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
se, err := cast.ToStringE(e)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
selements = append(selements, se)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := url.JoinPath(selements[0], selements[1:]...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|