Initial favicon add
This commit is contained in:
parent
6445be87a9
commit
bc89f5b819
8 changed files with 755 additions and 21 deletions
51
common.go
51
common.go
|
@ -8,6 +8,7 @@ import (
|
|||
"html/template"
|
||||
mathrand "math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -36,6 +37,11 @@ type SearchEngine struct {
|
|||
Func func(string, string, string, int) ([]SearchResult, time.Duration, error)
|
||||
}
|
||||
|
||||
type LinkParts struct {
|
||||
Domain template.HTML
|
||||
Path template.HTML
|
||||
}
|
||||
|
||||
// Helper function to render templates without elapsed time measurement
|
||||
func renderTemplate(w http.ResponseWriter, tmplName string, data map[string]interface{}) {
|
||||
// Generate icon paths for SVG and PNG, including a 1/10 chance for an alternate icon
|
||||
|
@ -125,3 +131,48 @@ func FormatElapsedTime(elapsed time.Duration) string {
|
|||
}
|
||||
return fmt.Sprintf("%.2f %s", elapsed.Seconds(), Translate("seconds"))
|
||||
}
|
||||
|
||||
func FormatURLParts(rawURL string) (domain, path string) {
|
||||
parsed, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return rawURL, ""
|
||||
}
|
||||
|
||||
domain = parsed.Host
|
||||
if strings.HasPrefix(domain, "www.") {
|
||||
domain = domain[4:]
|
||||
}
|
||||
|
||||
// Clean up the path - remove empty segments and trailing slashes
|
||||
path = strings.Trim(parsed.Path, "/")
|
||||
pathSegments := strings.Split(path, "/")
|
||||
|
||||
// Filter out empty segments
|
||||
var cleanSegments []string
|
||||
for _, seg := range pathSegments {
|
||||
if seg != "" {
|
||||
cleanSegments = append(cleanSegments, seg)
|
||||
}
|
||||
}
|
||||
|
||||
path = strings.Join(cleanSegments, "/")
|
||||
return domain, path
|
||||
}
|
||||
|
||||
func FormatLinkHTML(rawURL string) LinkParts {
|
||||
domain, path := FormatURLParts(rawURL)
|
||||
|
||||
if path == "" {
|
||||
return LinkParts{
|
||||
Domain: template.HTML(fmt.Sprintf(`<span class="result-domain">%s</span>`, template.HTMLEscapeString(domain))),
|
||||
}
|
||||
}
|
||||
|
||||
// Only add separators between non-empty path segments
|
||||
pathDisplay := strings.ReplaceAll(path, "/", " › ")
|
||||
|
||||
return LinkParts{
|
||||
Domain: template.HTML(fmt.Sprintf(`<span class="result-domain">%s</span>`, template.HTMLEscapeString(domain))),
|
||||
Path: template.HTML(fmt.Sprintf(`<span class="result-path"> › %s</span>`, template.HTMLEscapeString(pathDisplay))),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue