Skip to content

Commit 81ed564

Browse files
moorereasonbep
authored andcommitted
tpl: Add urls.Parse function
Add a urls.Parse template function that front-ends url.Parse from the Go stdlib. Fixes #3849
1 parent 19c5910 commit 81ed564

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

‎tpl/urls/urls.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ package urls
1515

1616
import (
1717
"errors"
18+
"fmt"
1819
"html/template"
20+
"net/url"
1921

2022
"github.com/gohugoio/hugo/deps"
2123
"github.com/spf13/cast"
@@ -43,6 +45,17 @@ func (ns *Namespace) AbsURL(a interface{}) (template.HTML, error) {
4345
return template.HTML(ns.deps.PathSpec.AbsURL(s, false)), nil
4446
}
4547

48+
// Parse parses rawurl into a URL structure. The rawurl may be relative or
49+
// absolute.
50+
func (ns *Namespace) Parse(rawurl interface{}) (*url.URL, error) {
51+
s, err := cast.ToStringE(rawurl)
52+
if err != nil {
53+
return nil, fmt.Errorf("Error in Parse: %s", err)
54+
}
55+
56+
return url.Parse(s)
57+
}
58+
4659
// RelURL takes a given string and prepends the relative path according to a
4760
// page's position in the project directory structure.
4861
func (ns *Namespace) RelURL(a interface{}) (template.HTML, error) {

‎tpl/urls/urls_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package urls
15+
16+
import (
17+
"fmt"
18+
"net/url"
19+
"testing"
20+
21+
"github.com/gohugoio/hugo/deps"
22+
"github.com/spf13/viper"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
var ns = New(&deps.Deps{Cfg: viper.New()})
28+
29+
type tstNoStringer struct{}
30+
31+
func TestParse(t *testing.T) {
32+
t.Parallel()
33+
34+
for i, test := range []struct {
35+
rawurl interface{}
36+
expect interface{}
37+
}{
38+
{
39+
"http://www.google.com",
40+
&url.URL{
41+
Scheme: "http",
42+
Host: "www.google.com",
43+
},
44+
},
45+
{
46+
"http://j@ne:password@google.com",
47+
&url.URL{
48+
Scheme: "http",
49+
User: url.UserPassword("j@ne", "password"),
50+
Host: "google.com",
51+
},
52+
},
53+
// errors
54+
{tstNoStringer{}, false},
55+
} {
56+
errMsg := fmt.Sprintf("[%d] %v", i, test)
57+
58+
result, err := ns.Parse(test.rawurl)
59+
60+
if b, ok := test.expect.(bool); ok && !b {
61+
require.Error(t, err, errMsg)
62+
continue
63+
}
64+
65+
require.NoError(t, err, errMsg)
66+
assert.Equal(t, test.expect, result, errMsg)
67+
}
68+
}

0 commit comments

Comments
 (0)