43fd3e8956
* create JS domain variable * use domain JS variable when creating cookies * Create getDomainName() function in Go * Update template_functions_test.go * Update template_functions_test.go * recreate cookie when resetCookies() is called * Update main.css * Update classic.css * show "unknown" instead of "0 0 0" if stats are unknown * don't show top ad if there's an announcement * remove BBCode support that broke description's line breaks * remove useless identifier * hide error instead of fixing it * Update markdown_test.go * Update markdown_test.go * css for tags in classic * remove wrong explanation for alternating color * fix RSS URL in header * remove eu cookie <div> from base * fix cookie warning never showing up * Update main.js * nyaa.se-like styling for upload form on classic.css * Smaller markdown toolbar on classic upload form * Fix indent, non-round borders for markdown & language list on classic * Update view.jet.html * add fillZero function * ditto but in test * Update view.jet.html * add RJ prefix to dlsite link * Update template_functions_test.go * Update listing.jet.html * prevent responsiveness of upload form in classic.css * remove now useless css rule * fix tag input padding on classic * Update template_functions_test.go * rollback workaround * rollback workaround * rollback workaround * remove now unused function * ditto * Update main.css
397 lignes
11 Kio
Go
397 lignes
11 Kio
Go
package templates
|
|
|
|
import (
|
|
"html/template"
|
|
"math"
|
|
"math/rand"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
"os"
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"github.com/CloudyKit/jet"
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/models"
|
|
"github.com/NyaaPantsu/nyaa/models/torrents"
|
|
"github.com/NyaaPantsu/nyaa/utils/categories"
|
|
"github.com/NyaaPantsu/nyaa/utils/filelist"
|
|
"github.com/NyaaPantsu/nyaa/utils/format"
|
|
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
|
|
"github.com/NyaaPantsu/nyaa/utils/torrentLanguages"
|
|
)
|
|
|
|
// FuncMap : Functions accessible in templates by {{ $.Function }}
|
|
func templateFunctions(vars jet.VarMap) jet.VarMap {
|
|
vars.Set("getRawQuery", getRawQuery)
|
|
vars.Set("genSearchWithOrdering", genSearchWithOrdering)
|
|
vars.Set("genSearchWithCategory", genSearchWithCategory)
|
|
vars.Set("genSortArrows", genSortArrows)
|
|
vars.Set("genNav", genNav)
|
|
vars.Set("Sukebei", config.IsSukebei)
|
|
vars.Set("getDefaultLanguage", publicSettings.GetDefaultLanguage)
|
|
vars.Set("FlagCode", flagCode)
|
|
vars.Set("getAvatar", getAvatar)
|
|
vars.Set("torrentFileExists", torrentFileExists)
|
|
vars.Set("formatDateRFC", formatDateRFC)
|
|
vars.Set("GetHostname", format.GetHostname)
|
|
vars.Set("GetCategories", categories.GetSelect)
|
|
vars.Set("GetCategory", getCategory)
|
|
vars.Set("CategoryName", categoryName)
|
|
vars.Set("GetTorrentLanguages", torrentLanguages.GetTorrentLanguages)
|
|
vars.Set("LanguageName", languageName)
|
|
vars.Set("LanguageNameFromCode", languageNameFromCode)
|
|
vars.Set("fileSize", fileSize)
|
|
vars.Set("DefaultUserSettings", defaultUserSettings)
|
|
vars.Set("makeTreeViewData", makeTreeViewData)
|
|
vars.Set("lastID", lastID)
|
|
vars.Set("getReportDescription", getReportDescription)
|
|
vars.Set("genUploaderLink", genUploaderLink)
|
|
vars.Set("genActivityContent", genActivityContent)
|
|
vars.Set("contains", contains)
|
|
vars.Set("toString", toString)
|
|
vars.Set("kilo_strcmp", kilo_strcmp)
|
|
vars.Set("kilo_strfind", kilo_strfind)
|
|
vars.Set("kilo_rand", kilo_rand)
|
|
vars.Set("getDomainName", getDomainName)
|
|
return vars
|
|
}
|
|
func getRawQuery(currentURL *url.URL) string {
|
|
return currentURL.RawQuery
|
|
}
|
|
func genSearchWithOrdering(currentURL *url.URL, sortBy string) string {
|
|
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))
|
|
|
|
u, _ := url.Parse("/search")
|
|
u.RawQuery = values.Encode()
|
|
|
|
return u.String()
|
|
}
|
|
|
|
func genSearchWithCategory(currentURL *url.URL, category string) string {
|
|
values := currentURL.Query()
|
|
cat := "_" //Default
|
|
|
|
if _, ok := values["c"]; ok {
|
|
cat = values["c"][0]
|
|
}
|
|
|
|
cat = category
|
|
|
|
values.Set("c", cat)
|
|
|
|
u, _ := url.Parse("/search")
|
|
u.RawQuery = values.Encode()
|
|
|
|
return u.String()
|
|
}
|
|
|
|
func genSortArrows(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)
|
|
}
|
|
|
|
|
|
func genNav(nav Navigation, currentURL *url.URL, pagesSelectable int) template.HTML {
|
|
var ret = ""
|
|
if nav.TotalItem > 0 {
|
|
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
|
|
|
|
href := ""
|
|
display := " style=\"display:none;\""
|
|
if nav.CurrentPage-1 > 0 {
|
|
display = ""
|
|
href = " href=\"" + "/" + nav.Route + "/1" + "?" + currentURL.RawQuery + "\""
|
|
}
|
|
ret = ret + "<a id=\"page-prev\"" + display + href + " aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a>"
|
|
|
|
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 := "/" + nav.Route + "/" + pageNum
|
|
ret = ret + "<a aria-label=\"Page " + strconv.Itoa(i) + "\" href=\"" + url + "?" + currentURL.RawQuery + "\">" + "<span"
|
|
if i == nav.CurrentPage {
|
|
ret = ret + " class=\"active\""
|
|
}
|
|
ret = ret + ">" + strconv.Itoa(i) + "</span></a>"
|
|
}
|
|
|
|
href = ""
|
|
display = " style=\"display:none;\""
|
|
if nav.CurrentPage < int(maxPages) {
|
|
display = ""
|
|
href = " href=\"" + "/" + nav.Route + "/" + strconv.Itoa(nav.CurrentPage+1) + "?" + currentURL.RawQuery + "\""
|
|
}
|
|
ret = ret + "<a id=\"page-next\"" + display + href +" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a>"
|
|
|
|
itemsThisPageStart := nav.MaxItemPerPage*(nav.CurrentPage-1) + 1
|
|
itemsThisPageEnd := nav.MaxItemPerPage * nav.CurrentPage
|
|
if nav.TotalItem < itemsThisPageEnd {
|
|
itemsThisPageEnd = nav.TotalItem
|
|
}
|
|
ret = ret + "<p>" + strconv.Itoa(itemsThisPageStart) + "-" + strconv.Itoa(itemsThisPageEnd) + "/" + strconv.Itoa(nav.TotalItem) + "</p>"
|
|
}
|
|
return template.HTML(ret)
|
|
}
|
|
|
|
func flagCode(languageCode string) string {
|
|
return publicSettings.Flag(languageCode, true)
|
|
}
|
|
|
|
func getAvatar(hash string, size int) string {
|
|
return "https://www.gravatar.com/avatar/" + hash + "?s=" + strconv.Itoa(size)
|
|
}
|
|
|
|
func formatDateRFC(t time.Time) string {
|
|
// because time.* isn't available in templates...
|
|
return t.Format(time.RFC3339)
|
|
}
|
|
func getCategory(category string, keepParent bool) categories.Categories {
|
|
cats := categories.GetSelect(true, true)
|
|
found := false
|
|
categoryRet := categories.Categories{}
|
|
for _, v := range cats {
|
|
if v.ID == category+"_" {
|
|
found = true
|
|
if keepParent {
|
|
categoryRet = append(categoryRet, v)
|
|
}
|
|
} else if len(v.ID) <= 2 && len(categoryRet) > 0 {
|
|
break
|
|
} else if found {
|
|
categoryRet = append(categoryRet, v)
|
|
}
|
|
}
|
|
return categoryRet
|
|
}
|
|
func categoryName(category string, subCategory string) string {
|
|
s := category + "_" + subCategory
|
|
|
|
if category, ok := categories.GetByID(s); ok {
|
|
return category.Name
|
|
}
|
|
return ""
|
|
}
|
|
func languageName(lang publicSettings.Language, T publicSettings.TemplateTfunc) string {
|
|
if strings.Contains(lang.Name, ",") {
|
|
langs := strings.Split(lang.Name, ", ")
|
|
tags := strings.Split(lang.Tag, ", ")
|
|
for k := range langs {
|
|
langs[k] = strings.Title(publicSettings.Translate(tags[k], string(T("language_code"))))
|
|
}
|
|
return strings.Join(langs, ", ")
|
|
}
|
|
return strings.Title(lang.Translate(T("language_code")))
|
|
}
|
|
func languageNameFromCode(languageCode string, T publicSettings.TemplateTfunc) string {
|
|
if strings.Contains(languageCode, ",") {
|
|
tags := strings.Split(languageCode, ", ")
|
|
var langs []string
|
|
for k := range tags {
|
|
langs = append(langs, strings.Title(publicSettings.Translate(tags[k], string(T("language_code")))))
|
|
}
|
|
return strings.Join(langs, ", ")
|
|
}
|
|
return strings.Title(publicSettings.Translate(languageCode, string(T("language_code"))))
|
|
}
|
|
func fileSize(filesize int64, T publicSettings.TemplateTfunc) template.HTML {
|
|
if filesize == 0 {
|
|
return T("unknown")
|
|
}
|
|
return template.HTML(format.FileSize(filesize))
|
|
}
|
|
|
|
func defaultUserSettings(s string) bool {
|
|
return config.Get().Users.DefaultUserSettings[s]
|
|
}
|
|
|
|
func makeTreeViewData(f *filelist.FileListFolder, nestLevel int, identifierChain string) interface{} {
|
|
return struct {
|
|
Folder *filelist.FileListFolder
|
|
NestLevel int
|
|
IdentifierChain string
|
|
}{f, nestLevel, identifierChain}
|
|
}
|
|
func lastID(currentURL *url.URL, torrents []models.TorrentJSON) int {
|
|
if len(torrents) == 0 {
|
|
return 0
|
|
}
|
|
values := currentURL.Query()
|
|
|
|
order := false
|
|
sort := "2"
|
|
|
|
if _, ok := values["order"]; ok {
|
|
order, _ = strconv.ParseBool(values["order"][0])
|
|
}
|
|
if _, ok := values["sort"]; ok {
|
|
sort = values["sort"][0]
|
|
}
|
|
lastID := 0
|
|
if sort == "2" || sort == "" {
|
|
if order {
|
|
lastID = int(torrents[len(torrents)-1].ID)
|
|
} else if len(torrents) > 0 {
|
|
lastID = int(torrents[0].ID)
|
|
}
|
|
}
|
|
return lastID
|
|
}
|
|
func getReportDescription(d string, T publicSettings.TemplateTfunc) string {
|
|
if d == "illegal" {
|
|
return "Illegal content"
|
|
} else if d == "spam" {
|
|
return "Spam / Garbage"
|
|
} else if d == "wrongcat" {
|
|
return "Wrong category"
|
|
} else if d == "dup" {
|
|
return "Duplicate / Deprecated"
|
|
}
|
|
return string(T(d))
|
|
}
|
|
func genUploaderLink(uploaderID uint, uploaderName template.HTML, torrentHidden bool) template.HTML {
|
|
uploaderID, username := torrents.HideUser(uploaderID, string(uploaderName), torrentHidden)
|
|
if uploaderID == 0 {
|
|
return template.HTML(username)
|
|
}
|
|
url := "/user/" + strconv.Itoa(int(uploaderID)) + "/" + username
|
|
|
|
return template.HTML("<a href=\"" + url + "\">" + username + "</a>")
|
|
}
|
|
func genActivityContent(a models.Activity, T publicSettings.TemplateTfunc) template.HTML {
|
|
return a.ToLocale(T)
|
|
}
|
|
func contains(arr interface{}, comp string) bool {
|
|
switch str := arr.(type) {
|
|
case string:
|
|
if str == comp {
|
|
return true
|
|
}
|
|
case publicSettings.Language:
|
|
if str.Code == comp {
|
|
return true
|
|
}
|
|
default:
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
func torrentFileExists(hash string, TorrentLink string) bool {
|
|
if(len(TorrentLink) > 30 && !kilo_strfind(TorrentLink, ".pantsu.cat", 8)) {
|
|
return true
|
|
}
|
|
Openfile, err := os.Open(fmt.Sprintf("%s%c%s.torrent", config.Get().Torrents.FileStorage, os.PathSeparator, hash))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer Openfile.Close()
|
|
return true
|
|
}
|
|
|
|
func toString(number int) string {
|
|
return strconv.Itoa(number)
|
|
}
|
|
|
|
func kilo_strcmp(str1 string, str2 string, end int, start int) bool {
|
|
//Compare two strings but has length arguments
|
|
|
|
len1 := len(str1)
|
|
len2 := len(str2)
|
|
|
|
if end == -1 && len1 != len2 {
|
|
return false
|
|
}
|
|
|
|
if end == -1 || end > len1 {
|
|
end = len1
|
|
}
|
|
if end > len2 {
|
|
end = len2
|
|
}
|
|
|
|
if start >= end {
|
|
return false
|
|
}
|
|
|
|
return strings.Compare(str1[start:end], str2[start:end]) == 0
|
|
}
|
|
|
|
func kilo_strfind(str1 string, searchfor string, start int) bool {
|
|
//Search a string inside another with start parameter
|
|
//start parameter indicates where we start searching
|
|
|
|
len1 := len(str1)
|
|
|
|
if start >= len1 {
|
|
return false
|
|
}
|
|
|
|
|
|
return strings.Contains(str1[start:len1], searchfor)
|
|
}
|
|
|
|
func kilo_rand(min int, max int) int {
|
|
return min + rand.Intn(max - min)
|
|
}
|
|
|
|
func getDomainName() string {
|
|
domain := config.Get().Cookies.DomainName
|
|
if config.Get().Environment == "DEVELOPMENT" {
|
|
domain = ""
|
|
}
|
|
return domain
|
|
}
|