|
|
@ -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},
|
|
|
|