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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
package router
import (
"html/template"
"math"
"net/url"
"strconv"
"time"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/service/user/permission"
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/languages"
)
type captchaData struct {
CaptchaID string
T languages.TemplateTfunc
}
var FuncMap = template.FuncMap{
"inc": func(i int) int {
return i + 1
},
"min": math.Min,
"genRoute": func(name string, params ...string) string {
url, err := Router.Get(name).URL(params...)
if err == nil {
return url.String()
}
return "error"
},
"genRouteWithQuery": func(name string, currentUrl *url.URL, params ...string) template.URL {
url, err := Router.Get(name).URL(params...)
if err == nil {
return template.URL(url.String() + "?" + currentUrl.RawQuery)
}
return "error"
},
"genViewTorrentRoute": func(torrent_id uint) string {
// Helper for when you have an uint while genRoute("view_torrent", ...) takes a string
// FIXME better solution?
s := strconv.FormatUint(uint64(torrent_id), 10)
url, err := Router.Get("view_torrent").URL("id", s)
if err == nil {
return url.String()
}
return "error"
},
"genSearchWithOrdering": func(currentUrl url.URL, sortBy string) template.URL {
values := currentUrl.Query()
order := false //Default is DESC
sort := "2" //Default is Date (Actually ID, but Date is the same thing)
if _, ok := values["order"]; ok {
order, _ = strconv.ParseBool(values["order"][0])
}
if _, ok := values["sort"]; ok {
sort = values["sort"][0]
}
if sort == sortBy {
order = !order //Flip order by repeat-clicking
} else {
order = false //Default to descending when sorting by something new
}
values.Set("sort", sortBy)
values.Set("order", strconv.FormatBool(order))
url, _ := Router.Get("search").URL()
url.RawQuery = values.Encode()
return template.URL(url.String())
},
"genSortArrows": func(currentUrl url.URL, sortBy string) template.HTML {
values := currentUrl.Query()
leftclass := "sortarrowdim"
rightclass := "sortarrowdim"
order := false
sort := "2"
if _, ok := values["order"]; ok {
order, _ = strconv.ParseBool(values["order"][0])
}
if _, ok := values["sort"]; ok {
sort = values["sort"][0]
}
if sort == sortBy {
if order {
rightclass = ""
} else {
leftclass = ""
}
}
arrows := "<span class=\"sortarrowleft " + leftclass + "\">▼</span><span class=\"" + rightclass + "\">▲</span>"
return template.HTML(arrows)
},
"genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
var ret = ""
if nav.TotalItem > 0 {
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
if nav.CurrentPage-1 > 0 {
url, _ := Router.Get(nav.Route).URL("page", "1")
ret = ret + "<li><a id=\"page-prev\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
}
startValue := 1
if nav.CurrentPage > pagesSelectable/2 {
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
}
if startValue < 1 {
startValue = 1
}
endValue := (startValue + pagesSelectable - 1)
if endValue > int(maxPages) {
endValue = int(maxPages)
}
for i := startValue; i <= endValue; i++ {
pageNum := strconv.Itoa(i)
url, _ := Router.Get(nav.Route).URL("page", pageNum)
ret = ret + "<li"
if i == nav.CurrentPage {
ret = ret + " class=\"active\""
}
ret = ret + "><a href=\"" + url.String() + "?" + currentUrl.RawQuery + "\">" + strconv.Itoa(i) + "</a></li>"
}
if nav.CurrentPage < int(maxPages) {
url, _ := Router.Get(nav.Route).URL("page", strconv.Itoa(nav.CurrentPage+1))
ret = ret + "<li><a id=\"page-next\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
}
}
return template.HTML(ret)
},
"Sukebei": config.IsSukebei,
"getDefaultLanguage": languages.GetDefaultLanguage,
"getAvatar": func(hash string, size int) string {
return "https://www.gravatar.com/avatar/" + hash + "?s=" + strconv.Itoa(size)
},
"CurrentOrAdmin": userPermission.CurrentOrAdmin,
"CurrentUserIdentical": userPermission.CurrentUserIdentical,
"HasAdmin": userPermission.HasAdmin,
"NeedsCaptcha": userPermission.NeedsCaptcha,
"GetRole": userPermission.GetRole,
"IsFollower": userPermission.IsFollower,
"NoEncode": func(str string) template.HTML {
return template.HTML(str)
},
"calcWidthSeed": func(seed uint32, leech uint32) float64 {
return float64(float64(seed)/(float64(seed)+float64(leech))) * 100
},
"calcWidthLeech": func(seed uint32, leech uint32) float64 {
return float64(float64(leech)/(float64(seed)+float64(leech))) * 100
},
"formatDateRFC": func(t time.Time) string {
// because time.* isn't available in templates...
return t.Format(time.RFC3339)
},
"Category_Sukebei": func(category string, sub_category string) string {
s := category + "_" + sub_category; e := ""
switch s {
default: e = ""
case "1_": e = "art"
case "1_1": e = "art_anime"
case "1_2": e = "art_doujinshi"
case "1_3": e = "art_games"
case "1_4": e = "art_manga"
case "1_5": e = "art_pictures"
case "2_": e = "real_life"
case "2_1": e = "real_life_photobooks_and_pictures"
case "2_2": e = "real_life_videos"
}
return e
},
"Category_Nyaa": func(category string, sub_category string) string {
s := category + "_" + sub_category; e := ""
switch s {
default: e = ""
case "3_": e = "anime"
case "3_12": e = "anime_amv"
case "3_5": e = "anime_english_translated"
case "3_13": e = "anime_non_english_translated"
case "3_6": e = "anime_raw"
case "2_": e = "audio"
case "2_3": e = "audio_lossless"
case "2_4": e = "audio_lossy"
case "4_": e = "literature"
case "4_7": e = "literature_english_translated"
case "4_8": e = "literature_raw"
case "4_14": e = "literature_non_english_translated"
case "5_": e = "live_action"
case "5_9": e = "live_action_english_translated"
case "5_10": e = "live_action_idol_pv"
case "5_18": e = "live_action_non_english_translated"
case "5_11": e = "live_action_raw"
case "6_": e = "pictures"
case "6_15": e = "pictures_graphics"
case "6_16": e = "pictures_photos"
case "1_": e = "software"
case "1_1": e = "software_applications"
case "1_2": e = "software_games"
}
return e
},
"fileSize": func(filesize int64, T languages.TemplateTfunc) template.HTML {
if (filesize == 0) {
return T("unknown")
}
return template.HTML(util.FormatFilesize(filesize))
},
"makeCaptchaData": func(captchaID string, T languages.TemplateTfunc) captchaData {
return captchaData{captchaID, T}
},
"DefaultUserSettings": func(s string) bool{
return config.DefaultUserSettings[s]
},
}
|