2020-08-09 19:33:47 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-11-14 10:39:18 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2020-08-31 17:52:26 +00:00
|
|
|
"net/http"
|
2020-08-09 19:33:47 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-10-25 15:06:51 +00:00
|
|
|
var (
|
2020-12-08 15:15:32 +00:00
|
|
|
URL string
|
2020-11-13 18:45:42 +00:00
|
|
|
ServerPort string
|
|
|
|
HomePage string
|
2021-01-23 13:45:17 +00:00
|
|
|
SiteNavIcon string
|
|
|
|
SiteName string
|
2020-11-13 18:45:42 +00:00
|
|
|
WikiDir string
|
2021-01-23 16:37:29 +00:00
|
|
|
UserHypha string
|
2021-01-23 19:00:58 +00:00
|
|
|
HeaderLinksHypha string
|
2020-11-13 18:45:42 +00:00
|
|
|
AuthMethod string
|
|
|
|
FixedCredentialsPath string
|
2020-10-25 15:06:51 +00:00
|
|
|
)
|
2020-08-09 19:33:47 +00:00
|
|
|
|
|
|
|
// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir.
|
|
|
|
func ShorterPath(path string) string {
|
|
|
|
if strings.HasPrefix(path, WikiDir) {
|
|
|
|
tmp := strings.TrimPrefix(path, WikiDir)
|
|
|
|
if tmp == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return tmp[1:]
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2020-08-31 17:52:26 +00:00
|
|
|
|
|
|
|
// HTTP200Page wraps some frequently used things for successful 200 responses.
|
|
|
|
func HTTP200Page(w http.ResponseWriter, page string) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(page))
|
|
|
|
}
|
2020-10-03 16:56:56 +00:00
|
|
|
|
|
|
|
// FindSubhyphae finds names of existing hyphae given the `hyphaIterator`.
|
|
|
|
func FindSubhyphae(hyphaName string, hyphaIterator func(func(string))) []string {
|
|
|
|
subhyphae := make([]string, 0)
|
|
|
|
hyphaIterator(func(otherHyphaName string) {
|
|
|
|
if strings.HasPrefix(otherHyphaName, hyphaName+"/") {
|
|
|
|
subhyphae = append(subhyphae, otherHyphaName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return subhyphae
|
|
|
|
}
|
2020-11-14 10:39:18 +00:00
|
|
|
|
|
|
|
func RandomString(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
2021-01-10 11:58:02 +00:00
|
|
|
|
|
|
|
// Strip hypha name from all ancestor names, replace _ with spaces, title case
|
|
|
|
func BeautifulName(uglyName string) string {
|
2021-01-11 16:13:41 +00:00
|
|
|
if uglyName == "" {
|
|
|
|
return uglyName
|
|
|
|
}
|
2021-01-14 11:39:54 +00:00
|
|
|
return strings.Title(strings.ReplaceAll(uglyName, "_", " "))
|
2021-01-10 11:58:02 +00:00
|
|
|
}
|