added icons to website

This commit is contained in:
partisan 2024-10-28 10:52:39 +01:00
parent dbc5a2b4b1
commit 5157414fce
18 changed files with 185 additions and 36 deletions

View file

@ -4,7 +4,9 @@ import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
mathrand "math/rand"
"net/http"
"strings"
"time"
@ -37,7 +39,17 @@ type SearchEngine struct {
// Helper function to render templates without elapsed time measurement
func renderTemplate(w http.ResponseWriter, tmplName string, data map[string]interface{}) {
// Parse the template with common functions (including translate)
// Generate icon paths for SVG and PNG, including a 1/10 chance for an alternate icon
iconPathSVG, iconPathPNG := GetIconPath()
// Add icon paths to data map so they are available in all templates
if data == nil {
data = make(map[string]interface{})
}
data["IconPathSVG"] = iconPathSVG
data["IconPathPNG"] = iconPathPNG
// Parse and execute the template with shared functions
tmpl, err := template.New(tmplName).Funcs(funcs).ParseFiles("templates/" + tmplName)
if err != nil {
printErr("Error parsing template: %v", err)
@ -82,3 +94,16 @@ func addProtocol(domain string) string {
}
return "https://" + domain
}
// GetIconPath returns both SVG and PNG icon paths, with a 1/10 chance for a randomly generated "alt" icon.
func GetIconPath() (string, string) {
// 1 in 10 chance to select an alt icon
if mathrand.Intn(10) == 0 {
// Generate a random number between 2 and 4
altIconNumber := 2 + mathrand.Intn(3) // mathrand.Intn(3) generates 0, 1, or 2
selectedAltIcon := "icon-alt" + fmt.Sprint(altIconNumber)
return "/static/images/" + selectedAltIcon + ".svg", "/static/images/" + selectedAltIcon + ".png"
}
// Default paths
return "/static/images/icon.svg", "/static/images/icon.png"
}