Debian Codegraph @

helpers/url.go

raw
1 // Copyright 2015 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 helpers
15
16 import (
17 "net/url"
18 "path"
19 "path/filepath"
20 "strings"
21
22 "github.com/gohugoio/hugo/common/paths"
23 )
24
25 // URLize is similar to MakePath, but with Unicode handling
26 // Example:
27 //
28 // uri: Vim (text editor)
29 // urlize: vim-text-editor
30 func (p *PathSpec) URLize(uri string) string {
31 return p.URLEscape(p.MakePathSanitized(uri))
32 }
33
34 // URLEscape escapes unicode letters.
35 func (p *PathSpec) URLEscape(uri string) string {
36 // escape unicode letters
37 parsedURI, err := url.Parse(uri)
38 if err != nil {
39 // if net/url can not parse URL it means Sanitize works incorrectly
40 panic(err)
41 }
42 x := parsedURI.String()
43 return x
44 }
45
46 // AbsURL creates an absolute URL from the relative path given and the BaseURL set in config.
47 func (p *PathSpec) AbsURL(in string, addLanguage bool) string {
48 isAbs, err := p.IsAbsURL(in)
49 if err != nil {
50 return in
51 }
52 if isAbs || strings.HasPrefix(in, "//") {
53 // It is already absolute, return it as is.
54 return in
55 }
56
57 baseURL := p.getBaseURLRoot(in)
58
59 if addLanguage {
60 prefix := p.GetLanguagePrefix()
61 if prefix != "" {
62 hasPrefix := false
63 // avoid adding language prefix if already present
64 in2 := in
65 if strings.HasPrefix(in, "/") {
66 in2 = in[1:]
67 }
68 if in2 == prefix {
69 hasPrefix = true
70 } else {
71 hasPrefix = strings.HasPrefix(in2, prefix+"/")
72 }
73
74 if !hasPrefix {
75 addSlash := in == "" || strings.HasSuffix(in, "/")
76 in = path.Join(prefix, in)
77
78 if addSlash {
79 in += "/"
80 }
81 }
82 }
83 }
84
85 return paths.MakePermalink(baseURL, in).String()
86 }
87
88 func (p *PathSpec) getBaseURLRoot(path string) string {
89 if strings.HasPrefix(path, "/") {
90 // Treat it as relative to the server root.
91 return p.Cfg.BaseURL().WithoutPath
92 } else {
93 // Treat it as relative to the baseURL.
94 return p.Cfg.BaseURL().WithPath
95 }
96 }
97
98 func (p *PathSpec) IsAbsURL(in string) (bool, error) {
99 // Fast path.
100 if strings.HasPrefix(in, "http://") || strings.HasPrefix(in, "https://") {
101 return true, nil
102 }
103 u, err := url.Parse(in)
104 if err != nil {
105 return false, err
106 }
107 return u.IsAbs(), nil
108 }
109
110 func (p *PathSpec) RelURL(in string, addLanguage bool) string {
111 isAbs, err := p.IsAbsURL(in)
112 if err != nil {
113 return in
114 }
115 baseURL := p.getBaseURLRoot(in)
116 canonifyURLs := p.Cfg.CanonifyURLs()
117
118 if (!strings.HasPrefix(in, baseURL) && isAbs) || strings.HasPrefix(in, "//") {
119 return in
120 }
121
122 u := in
123
124 if strings.HasPrefix(in, baseURL) {
125 u = strings.TrimPrefix(u, baseURL)
126 }
127
128 if addLanguage {
129 prefix := p.GetLanguagePrefix()
130 if prefix != "" {
131 hasPrefix := false
132 // avoid adding language prefix if already present
133 in2 := in
134 if strings.HasPrefix(in, "/") {
135 in2 = in[1:]
136 }
137 if in2 == prefix {
138 hasPrefix = true
139 } else {
140 hasPrefix = strings.HasPrefix(in2, prefix+"/")
141 }
142
143 if !hasPrefix {
144 hadSlash := strings.HasSuffix(u, "/")
145
146 u = path.Join(prefix, u)
147
148 if hadSlash {
149 u += "/"
150 }
151 }
152 }
153 }
154
155 if !canonifyURLs {
156 u = paths.AddContextRoot(baseURL, u)
157 }
158
159 if in == "" && !strings.HasSuffix(u, "/") && strings.HasSuffix(baseURL, "/") {
160 u += "/"
161 }
162
163 if !strings.HasPrefix(u, "/") {
164 u = "/" + u
165 }
166
167 return u
168 }
169
170 // PrependBasePath prepends any baseURL sub-folder to the given resource
171 func (p *PathSpec) PrependBasePath(rel string, isAbs bool) string {
172 basePath := p.GetBasePath(!isAbs)
173 if basePath != "" {
174 rel = filepath.ToSlash(rel)
175 // Need to prepend any path from the baseURL
176 hadSlash := strings.HasSuffix(rel, "/")
177 rel = path.Join(basePath, rel)
178 if hadSlash {
179 rel += "/"
180 }
181 }
182 return rel
183 }