From 3965b59893a5eaa7eb8b44267d42e01e728e0d89 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 23 Jul 2023 15:00:31 -0700 Subject: [PATCH] Add search highlighting --- dict/main.go | 25 ++++++++++++++++++++++--- dict/templates/partials/definition.html | 2 +- httputils/templates.go | 13 ++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dict/main.go b/dict/main.go index c358b2f..67f278f 100644 --- a/dict/main.go +++ b/dict/main.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "encoding/json" "fmt" + "html/template" "log" "net/http" "os" @@ -213,6 +214,13 @@ func ParseEntry(entry *jmdict.JmdictEntry, furiganaInfo *string) Entry { } } +func highlight(input, substring string) template.HTML { + // Replace all occurrences of substring with the highlighted version + replacement := fmt.Sprintf("%s", substring) + result := strings.ReplaceAll(input, substring, replacement) + return template.HTML(result) +} + func Search(query string) queryResult { query = strings.TrimSpace(query) exactResults := make([]Entry, 0) @@ -286,6 +294,12 @@ func main() { return } fmt.Println("JMdict loaded!") + httputils.DefaultTemplateFuncs = template.FuncMap{ + "highlight": func(input string) string { + return input + }, + } + httputils.TemplateFuncs = httputils.DefaultTemplateFuncs r := mux.NewRouter() r.HandleFunc("/", httputils.GenerateHandler( func(w http.ResponseWriter, r *http.Request) bool { return true }, @@ -326,15 +340,20 @@ func main() { }, httputils.NewTemplateSet("index.html", "search.html"), // template data - func(w http.ResponseWriter, r *http.Request) (template string, data any) { + func(w http.ResponseWriter, r *http.Request) (templateName string, data any) { if r.Header.Get("HX-Request") == "" { - template = "search.html" + templateName = "search.html" } else { - template = "search" + templateName = "search" } // Only runs if handler returns true query := mux.Vars(r)["query"] data = Search(query) + httputils.TemplateFuncs = template.FuncMap{ + "highlight": func(input string) template.HTML { + return highlight(input, strings.TrimSpace(query)) + }, + } return }, []string{http.MethodGet}, diff --git a/dict/templates/partials/definition.html b/dict/templates/partials/definition.html index 86595c7..cc826d4 100644 --- a/dict/templates/partials/definition.html +++ b/dict/templates/partials/definition.html @@ -1,3 +1,3 @@ {{- define "definition" -}} -{{ if .PartOfSpeech }}{{ .PartOfSpeech }}
{{ end }}{{ .Definition -}} +{{ if .PartOfSpeech }}{{ .PartOfSpeech }}
{{ end }}{{ highlight .Definition -}} {{ end }} \ No newline at end of file diff --git a/httputils/templates.go b/httputils/templates.go index 7c48e1e..2e61d06 100644 --- a/httputils/templates.go +++ b/httputils/templates.go @@ -29,9 +29,11 @@ func newTemplateSet(partials *TemplateSet, paths ...string) TemplateSet { fileInfo, _ := os.Stat(path) modTimes[path] = fileInfo.ModTime() } - templates := template.Must(template.ParseFiles(allPaths...)) + templates := template.Template{} + templates.Funcs(DefaultTemplateFuncs) + templates.ParseFiles(allPaths...) return TemplateSet{ - templates: templates, + templates: &templates, paths: allPaths, loadTimes: modTimes, } @@ -46,7 +48,10 @@ func NewTemplateSet(paths ...string) TemplateSet { func (templateSet *TemplateSet) ExecuteTemplate(wr io.Writer, name string, data any) error { templateSet.reloadTemplatesIfModified() - return templateSet.templates.ExecuteTemplate(wr, name, data) + templateSet.templates.Funcs(TemplateFuncs) + err := templateSet.templates.ExecuteTemplate(wr, name, data) + TemplateFuncs = DefaultTemplateFuncs + return err } func (templateSet *TemplateSet) reloadTemplatesIfModified() { @@ -86,3 +91,5 @@ const partialsFolder = templateFolder + "/partials" var paths, _ = getTemplatePathsInDirectory(partialsFolder) var partials = newTemplateSet(nil, paths...) +var DefaultTemplateFuncs template.FuncMap +var TemplateFuncs template.FuncMap