generated from ElnuDev/go-project
Add search highlighting
This commit is contained in:
parent
b20cdaf819
commit
3965b59893
3 changed files with 33 additions and 7 deletions
25
dict/main.go
25
dict/main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"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("<mark>%s</mark>", substring)
|
||||||
|
result := strings.ReplaceAll(input, substring, replacement)
|
||||||
|
return template.HTML(result)
|
||||||
|
}
|
||||||
|
|
||||||
func Search(query string) queryResult {
|
func Search(query string) queryResult {
|
||||||
query = strings.TrimSpace(query)
|
query = strings.TrimSpace(query)
|
||||||
exactResults := make([]Entry, 0)
|
exactResults := make([]Entry, 0)
|
||||||
|
@ -286,6 +294,12 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("JMdict loaded!")
|
fmt.Println("JMdict loaded!")
|
||||||
|
httputils.DefaultTemplateFuncs = template.FuncMap{
|
||||||
|
"highlight": func(input string) string {
|
||||||
|
return input
|
||||||
|
},
|
||||||
|
}
|
||||||
|
httputils.TemplateFuncs = httputils.DefaultTemplateFuncs
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", httputils.GenerateHandler(
|
r.HandleFunc("/", httputils.GenerateHandler(
|
||||||
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
||||||
|
@ -326,15 +340,20 @@ func main() {
|
||||||
},
|
},
|
||||||
httputils.NewTemplateSet("index.html", "search.html"),
|
httputils.NewTemplateSet("index.html", "search.html"),
|
||||||
// template data
|
// 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") == "" {
|
if r.Header.Get("HX-Request") == "" {
|
||||||
template = "search.html"
|
templateName = "search.html"
|
||||||
} else {
|
} else {
|
||||||
template = "search"
|
templateName = "search"
|
||||||
}
|
}
|
||||||
// Only runs if handler returns true
|
// Only runs if handler returns true
|
||||||
query := mux.Vars(r)["query"]
|
query := mux.Vars(r)["query"]
|
||||||
data = Search(query)
|
data = Search(query)
|
||||||
|
httputils.TemplateFuncs = template.FuncMap{
|
||||||
|
"highlight": func(input string) template.HTML {
|
||||||
|
return highlight(input, strings.TrimSpace(query))
|
||||||
|
},
|
||||||
|
}
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
[]string{http.MethodGet},
|
[]string{http.MethodGet},
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{{- define "definition" -}}
|
{{- define "definition" -}}
|
||||||
{{ if .PartOfSpeech }}<small><chip>{{ .PartOfSpeech }}</chip></small><br>{{ end }}{{ .Definition -}}
|
{{ if .PartOfSpeech }}<small><chip>{{ .PartOfSpeech }}</chip></small><br>{{ end }}{{ highlight .Definition -}}
|
||||||
{{ end }}
|
{{ end }}
|
|
@ -29,9 +29,11 @@ func newTemplateSet(partials *TemplateSet, paths ...string) TemplateSet {
|
||||||
fileInfo, _ := os.Stat(path)
|
fileInfo, _ := os.Stat(path)
|
||||||
modTimes[path] = fileInfo.ModTime()
|
modTimes[path] = fileInfo.ModTime()
|
||||||
}
|
}
|
||||||
templates := template.Must(template.ParseFiles(allPaths...))
|
templates := template.Template{}
|
||||||
|
templates.Funcs(DefaultTemplateFuncs)
|
||||||
|
templates.ParseFiles(allPaths...)
|
||||||
return TemplateSet{
|
return TemplateSet{
|
||||||
templates: templates,
|
templates: &templates,
|
||||||
paths: allPaths,
|
paths: allPaths,
|
||||||
loadTimes: modTimes,
|
loadTimes: modTimes,
|
||||||
}
|
}
|
||||||
|
@ -46,7 +48,10 @@ func NewTemplateSet(paths ...string) TemplateSet {
|
||||||
|
|
||||||
func (templateSet *TemplateSet) ExecuteTemplate(wr io.Writer, name string, data any) error {
|
func (templateSet *TemplateSet) ExecuteTemplate(wr io.Writer, name string, data any) error {
|
||||||
templateSet.reloadTemplatesIfModified()
|
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() {
|
func (templateSet *TemplateSet) reloadTemplatesIfModified() {
|
||||||
|
@ -86,3 +91,5 @@ const partialsFolder = templateFolder + "/partials"
|
||||||
|
|
||||||
var paths, _ = getTemplatePathsInDirectory(partialsFolder)
|
var paths, _ = getTemplatePathsInDirectory(partialsFolder)
|
||||||
var partials = newTemplateSet(nil, paths...)
|
var partials = newTemplateSet(nil, paths...)
|
||||||
|
var DefaultTemplateFuncs template.FuncMap
|
||||||
|
var TemplateFuncs template.FuncMap
|
||||||
|
|
Reference in a new issue