baseURL.go - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
baseURL.go (3076B)
---
1 // Copyright 2024 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 "strconv"
20 "strings"
21 )
22
23 // A BaseURL in Hugo is normally on the form scheme://path, but the
24 // form scheme: is also valid (mailto:hugo@rules.com).
25 type BaseURL struct {
26 url *url.URL
27 WithPath string
28 WithPathNoTrailingSlash string
29 WithoutPath string
30 BasePath string
31 BasePathNoTrailingSlash string
32 }
33
34 func (b BaseURL) String() string {
35 return b.WithPath
36 }
37
38 func (b BaseURL) Path() string {
39 return b.url.Path
40 }
41
42 func (b BaseURL) Port() int {
43 p, _ := strconv.Atoi(b.url.Port())
44 return p
45 }
46
47 // HostURL returns the URL to the host root without any path elements.
48 func (b BaseURL) HostURL() string {
49 return strings.TrimSuffix(b.String(), b.Path())
50 }
51
52 // WithProtocol returns the BaseURL prefixed with the given protocol.
53 // The Protocol is normally of the form "scheme://", i.e. "webcal://".
54 func (b BaseURL) WithProtocol(protocol string) (BaseURL, error) {
55 u := b.URL()
56
57 scheme := protocol
58 isFullProtocol := strings.HasSuffix(scheme, "://")
59 isOpaqueProtocol := strings.HasSuffix(scheme, ":")
60
61 if isFullProtocol {
62 scheme = strings.TrimSuffix(scheme, "://")
63 } else if isOpaqueProtocol {
64 scheme = strings.TrimSuffix(scheme, ":")
65 }
66
67 u.Scheme = scheme
68
69 if isFullProtocol && u.Opaque != "" {
70 u.Opaque = "//" + u.Opaque
71 } else if isOpaqueProtocol && u.Opaque == "" {
72 return BaseURL{}, fmt.Errorf("cannot determine BaseURL for protocol %q", protocol)
73 }
74
75 return newBaseURLFromURL(u)
76 }
77
78 func (b BaseURL) WithPort(port int) (BaseURL, error) {
79 u := b.URL()
80 u.Host = u.Hostname() + ":" + strconv.Itoa(port)
81 return newBaseURLFromURL(u)
82 }
83
84 // URL returns a copy of the internal URL.
85 // The copy can be safely used and modified.
86 func (b BaseURL) URL() *url.URL {
87 c := *b.url
88 return &c
89 }
90
91 func NewBaseURLFromString(b string) (BaseURL, error) {
92 u, err := url.Parse(b)
93 if err != nil {
94 return BaseURL{}, err
95 }
96 return newBaseURLFromURL(u)
97 }
98
99 func newBaseURLFromURL(u *url.URL) (BaseURL, error) {
100 // A baseURL should always have a trailing slash, see #11669.
101 if !strings.HasSuffix(u.Path, "/") {
102 u.Path += "/"
103 }
104 baseURL := BaseURL{url: u, WithPath: u.String(), WithPathNoTrailingSlash: strings.TrimSuffix(u.String(), "/")}
105 baseURLNoPath := baseURL.URL()
106 baseURLNoPath.Path = ""
107 baseURL.WithoutPath = baseURLNoPath.String()
108 baseURL.BasePath = u.Path
109 baseURL.BasePathNoTrailingSlash = strings.TrimSuffix(u.Path, "/")
110
111 return baseURL, nil
112 }