summaryrefslogtreecommitdiff
path: root/src/render/url.go
blob: eb9756bd99316a971ae34e56955980b0bc16c5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package render

import (
	"html/template"
	"net/url"
	"path"
	"strconv"
	"strings"
)

const (
	urlBasePublic = iota
	urlBaseHTTP
	urlBaseGemini
)

// URLBuilder is used to construct a URL for the site. The API of URLBuilder is
// designed to be convenient to used from a go template.
type URLBuilder struct {
	publicURL, httpURL, geminiURL *url.URL

	abs          bool
	base         int
	path, method string
	page         int
}

func NewURLBuilder(publicURL, httpURL, geminiURL *url.URL) URLBuilder {
	return URLBuilder{
		publicURL: publicURL,
		httpURL:   httpURL,
		geminiURL: geminiURL,
	}
}

func (b URLBuilder) String() string {
	var u *url.URL
	switch b.base {
	case urlBasePublic:
		u = b.publicURL
	case urlBaseHTTP:
		u = b.httpURL
	case urlBaseGemini:
		u = b.geminiURL
	}

	u = u.JoinPath(b.path)

	if u.Scheme == "gemini" &&
		u.Host == b.geminiURL.Host &&
		!strings.HasSuffix(u.Path, "/") &&
		path.Ext(u.Path) == "" {
		u.Path += ".gmi"
	}

	{
		query := url.Values{}
		if b.method != "" {
			query.Set("method", b.method)
		}
		if b.page > 0 {
			query.Set("page", strconv.Itoa(b.page))
		}
		if len(query) > 0 {
			u.RawQuery = query.Encode()
		}
	}

	if abs := b.abs || u.Scheme != b.publicURL.Scheme; !abs {
		u.Scheme = ""
		u.Host = ""
	}

	return u.String()
}

func (b URLBuilder) HTMLSafe() template.URL {
	return template.URL(b.String())
}

// Absolute returns a URLBuilder which will always construct a URL containing
// scheme and host.
func (b URLBuilder) Absolute() URLBuilder {
	b.abs = true
	return b
}

func (b URLBuilder) Gemini() URLBuilder {
	b.base = urlBaseGemini
	return b
}

func (b URLBuilder) HTTP() URLBuilder {
	b.base = urlBaseHTTP
	return b
}

func (b URLBuilder) Assets() URLBuilder {
	b.path = "assets/"
	return b
}

func (b URLBuilder) Asset(id string) URLBuilder {
	b.path = "assets/" + id
	return b
}

func (b URLBuilder) Posts() URLBuilder {
	b.path = "posts/"
	return b
}

func (b URLBuilder) Post(id string) URLBuilder {
	b.path = "posts/" + id
	return b
}

func (b URLBuilder) Drafts() URLBuilder {
	b.base = urlBaseHTTP
	b.path = "drafts/"
	return b
}

func (b URLBuilder) Draft(id string) URLBuilder {
	b.base = urlBaseHTTP
	b.path = "drafts/" + id
	return b
}

func (b URLBuilder) Static(staticPath string) URLBuilder {
	b.base = urlBaseHTTP
	b.path = path.Join("static", staticPath)
	return b
}

func (b URLBuilder) Path(p string) URLBuilder {
	b.path = p
	return b
}

func (b URLBuilder) Page(page int) URLBuilder {
	b.page = page
	return b
}

func (b URLBuilder) MethodEdit() URLBuilder {
	b.base = urlBaseHTTP
	b.method = "edit"
	return b
}

func (b URLBuilder) MethodManage() URLBuilder {
	b.base = urlBaseHTTP
	b.method = "manage"
	return b
}

func (b URLBuilder) MethodDelete() URLBuilder {
	b.base = urlBaseHTTP
	b.method = "delete"
	return b
}

func (b URLBuilder) MethodPreview() URLBuilder {
	b.base = urlBaseHTTP
	b.method = "preview"
	return b
}