generated from ElnuDev/go-project
Compare commits
No commits in common. "e70916c6c1ebb7a8e90f7d98ae0df8137f6d6473" and "61c1ce55028d238bba4a91cb9b454218ea73ca62" have entirely different histories.
e70916c6c1
...
61c1ce5502
11 changed files with 119 additions and 278 deletions
134
dict/main.go
134
dict/main.go
|
@ -69,11 +69,10 @@ func ParseEntry(entry jmdict.JmdictEntry) Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search(query string) queryResult {
|
func Search(query string) (exactResults []Entry, otherResults []Entry, truncated bool) {
|
||||||
query = strings.TrimSpace(query)
|
query = strings.TrimSpace(query)
|
||||||
exactResults := make([]Entry, 0)
|
exactResults = make([]Entry, 0)
|
||||||
otherResults := make([]Entry, 0)
|
otherResults = make([]Entry, 0)
|
||||||
truncated := false
|
|
||||||
count := 0
|
count := 0
|
||||||
for _, jmdictEntry := range dict.Entries {
|
for _, jmdictEntry := range dict.Entries {
|
||||||
exactMatch := false
|
exactMatch := false
|
||||||
|
@ -106,8 +105,18 @@ func Search(query string) queryResult {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return queryResult{
|
return
|
||||||
Query: query,
|
}
|
||||||
|
|
||||||
|
type searchTemplateData struct {
|
||||||
|
ExactResults []Entry
|
||||||
|
OtherResults []Entry
|
||||||
|
Truncated bool
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func initSearchTemplateData(exactResults []Entry, otherResults []Entry, truncated bool) searchTemplateData {
|
||||||
|
return searchTemplateData{
|
||||||
ExactResults: exactResults,
|
ExactResults: exactResults,
|
||||||
OtherResults: otherResults,
|
OtherResults: otherResults,
|
||||||
Truncated: truncated,
|
Truncated: truncated,
|
||||||
|
@ -115,26 +124,6 @@ func Search(query string) queryResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lookup(word string) *Entry {
|
|
||||||
for _, jmdictEntry := range dict.Entries {
|
|
||||||
entry := ParseEntry(jmdictEntry)
|
|
||||||
if entry.Kanji == word {
|
|
||||||
return &entry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type queryResult struct {
|
|
||||||
// Fields must be capitalized
|
|
||||||
// to be accessible in templates
|
|
||||||
Query string
|
|
||||||
ExactResults []Entry
|
|
||||||
OtherResults []Entry
|
|
||||||
Truncated bool
|
|
||||||
Count int
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := LoadDict()
|
err := LoadDict()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -144,80 +133,51 @@ func main() {
|
||||||
fmt.Println("JMdict loaded!")
|
fmt.Println("JMdict loaded!")
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", httputils.GenerateHandler(
|
r.HandleFunc("/", httputils.GenerateHandler(
|
||||||
|
"index.html",
|
||||||
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
||||||
httputils.NewTemplateSet("index.html"),
|
func(w http.ResponseWriter, r *http.Request) any { return nil },
|
||||||
func(w http.ResponseWriter, r *http.Request) (string, any) { return "index.html", nil },
|
|
||||||
[]string{http.MethodGet},
|
[]string{http.MethodGet},
|
||||||
))
|
))
|
||||||
rawSearchHandler := func(w http.ResponseWriter, r *http.Request) {
|
redirectToHome := func(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseMultipartForm(0)
|
http.Redirect(w, r, "/", http.StatusPermanentRedirect)
|
||||||
q := r.FormValue("q")
|
|
||||||
var redirect string
|
|
||||||
if q == "" {
|
|
||||||
redirect = "/"
|
|
||||||
} else {
|
|
||||||
redirect = "/search/" + q
|
|
||||||
}
|
|
||||||
http.Redirect(w, r, redirect, http.StatusMovedPermanently)
|
|
||||||
}
|
}
|
||||||
r.HandleFunc("/search", rawSearchHandler)
|
r.HandleFunc("/search", redirectToHome)
|
||||||
r.HandleFunc("/search/", rawSearchHandler)
|
r.HandleFunc("/search/", redirectToHome)
|
||||||
r.HandleFunc("/search/{query}", httputils.GenerateHandler(
|
r.HandleFunc("/search/{query}", httputils.GenerateHandler(
|
||||||
// handler whether or not to use template
|
"index.html",
|
||||||
|
func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
func(w http.ResponseWriter, r *http.Request) any {
|
||||||
|
query := mux.Vars(r)["query"]
|
||||||
|
return struct {
|
||||||
|
Query string
|
||||||
|
Results searchTemplateData
|
||||||
|
}{
|
||||||
|
Query: query,
|
||||||
|
Results: initSearchTemplateData(Search(query)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[]string{http.MethodGet},
|
||||||
|
))
|
||||||
|
r.HandleFunc("/api/search", httputils.GenerateHandler(
|
||||||
|
"search.html",
|
||||||
func(w http.ResponseWriter, r *http.Request) bool {
|
func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
// If Accept: applicaiton/json we'll use the template
|
|
||||||
if r.Header.Get("Accept") != "application/json" {
|
if r.Header.Get("Accept") != "application/json" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, let's send JSON
|
|
||||||
query := mux.Vars(r)["query"]
|
|
||||||
result := Search(query)
|
|
||||||
jsonBytes, _ := json.Marshal(append(result.ExactResults, result.OtherResults...))
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
r.ParseMultipartForm(0)
|
||||||
|
query := r.FormValue("q")
|
||||||
|
exactResults, otherResults, _ := Search(query)
|
||||||
|
jsonBytes, _ := json.Marshal(append(exactResults, otherResults...))
|
||||||
fmt.Fprint(w, string(jsonBytes))
|
fmt.Fprint(w, string(jsonBytes))
|
||||||
|
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
httputils.NewTemplateSet("index.html", "search.html"),
|
func(w http.ResponseWriter, r *http.Request) any {
|
||||||
// template data
|
r.ParseMultipartForm(0)
|
||||||
func(w http.ResponseWriter, r *http.Request) (template string, data any) {
|
query := r.FormValue("q")
|
||||||
if r.Header.Get("HX-Request") == "" {
|
return initSearchTemplateData(Search(query))
|
||||||
template = "search.html"
|
|
||||||
} else {
|
|
||||||
template = "search"
|
|
||||||
}
|
|
||||||
// Only runs if handler returns true
|
|
||||||
query := mux.Vars(r)["query"]
|
|
||||||
data = Search(query)
|
|
||||||
return
|
|
||||||
},
|
|
||||||
[]string{http.MethodGet},
|
|
||||||
))
|
|
||||||
rawWordHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println("Redirecting raw word handler")
|
|
||||||
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
|
||||||
}
|
|
||||||
r.HandleFunc("/word", rawWordHandler)
|
|
||||||
r.HandleFunc("/word/", rawWordHandler)
|
|
||||||
r.HandleFunc("/word/{word}", httputils.GenerateHandler(
|
|
||||||
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
|
||||||
// Order matters
|
|
||||||
// word.html overrided the results block in index.html
|
|
||||||
// so should be loaded second
|
|
||||||
httputils.NewTemplateSet("index.html", "word.html"),
|
|
||||||
func(w http.ResponseWriter, r *http.Request) (template string, data any) {
|
|
||||||
template = "word.html"
|
|
||||||
query := mux.Vars(r)["word"]
|
|
||||||
data = struct {
|
|
||||||
Query any
|
|
||||||
Entry *Entry
|
|
||||||
}{
|
|
||||||
Query: nil,
|
|
||||||
Entry: Lookup(query),
|
|
||||||
}
|
|
||||||
return
|
|
||||||
},
|
},
|
||||||
[]string{http.MethodGet},
|
[]string{http.MethodGet},
|
||||||
))
|
))
|
||||||
|
|
|
@ -1,42 +1,33 @@
|
||||||
{{- define "index" -}}
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>{{ block "title" . }}{{ template "sitetitle" . }}{{ end }}</title>
|
<title>jidict</title>
|
||||||
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
|
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
|
||||||
<style>
|
<style>
|
||||||
li {
|
li {
|
||||||
margin-top: 0.75em;
|
margin-top: 0.75em;
|
||||||
}
|
}
|
||||||
#results .box h3 a {
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
#results .box h3 a:not(:hover) {
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
<script src="https://unpkg.com/htmx.org@1.9.3"></script>
|
<script src="https://unpkg.com/htmx.org@1.9.3"></script>
|
||||||
</head>
|
</head>
|
||||||
<body hx-boost="true">
|
<body>
|
||||||
<main>
|
<main>
|
||||||
<a href="/">
|
<a href="/">
|
||||||
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
|
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
|
||||||
</a>
|
</a>
|
||||||
<form
|
<form
|
||||||
hx-get="/search"
|
hx-get="/api/search"
|
||||||
hx-replace-url="true"
|
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
|
||||||
hx-target="#results"
|
hx-target="#results"
|
||||||
hx-on::before-request="document.title = `${this.querySelector('input').value} search - jidict`">
|
hx-swap="innerHTML">
|
||||||
<input type="text" name="q" value="{{ block "value" . }}{{ end }}" placeholder="辞書をサーチする" class="width:100%" autocomplete="false" required>
|
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
|
||||||
</form>
|
</form>
|
||||||
<div id="results">
|
<div id="results">
|
||||||
{{ block "results" . }}{{ if .Query }}{{ template "search" . }}{{ end }}{{ end }}
|
{{ with .Results }}{{ template "search" . }}{{ end }}
|
||||||
</div>
|
</div>
|
||||||
<br>
|
<br>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{- end -}}
|
|
||||||
{{- template "index" . -}}
|
|
|
@ -1,23 +0,0 @@
|
||||||
{{ define "entry" }}
|
|
||||||
<div class="box">
|
|
||||||
<h3>
|
|
||||||
{{- if .Kanji -}}
|
|
||||||
<a href="/word/{{ .Kanji }}"><ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby></a>
|
|
||||||
{{- else -}}
|
|
||||||
{{- .Reading -}}
|
|
||||||
{{- end -}}
|
|
||||||
</h3>
|
|
||||||
{{- $count := len .Definitions -}}
|
|
||||||
{{ if eq $count 1 -}}
|
|
||||||
<p>{{- template "definition" (index .Definitions 0) -}}</p>
|
|
||||||
{{- else if ne $count 0 -}}
|
|
||||||
<ol>
|
|
||||||
{{- range .Definitions }}
|
|
||||||
<li>
|
|
||||||
{{ template "definition" . }}
|
|
||||||
</li>
|
|
||||||
{{- end }}
|
|
||||||
</ol>
|
|
||||||
{{- end }}
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
|
@ -1,23 +0,0 @@
|
||||||
{{ define "entryfull" }}
|
|
||||||
<div class="box">
|
|
||||||
<h3>
|
|
||||||
{{- if .Kanji -}}
|
|
||||||
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby>
|
|
||||||
{{- else -}}
|
|
||||||
{{- .Reading -}}
|
|
||||||
{{- end -}}
|
|
||||||
</h3>
|
|
||||||
{{- $count := len .Definitions -}}
|
|
||||||
{{ if eq $count 1 -}}
|
|
||||||
<p>{{- template "definition" (index .Definitions 0) -}}</p>
|
|
||||||
{{- else if ne $count 0 -}}
|
|
||||||
<ol>
|
|
||||||
{{- range .Definitions }}
|
|
||||||
<li>
|
|
||||||
{{ template "definition" . }}
|
|
||||||
</li>
|
|
||||||
{{- end }}
|
|
||||||
</ol>
|
|
||||||
{{- end }}
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
|
@ -1,11 +0,0 @@
|
||||||
{{- define "search" -}}
|
|
||||||
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
|
|
||||||
{{ range .ExactResults -}}
|
|
||||||
{{- template "entry" . -}}
|
|
||||||
{{- end }}
|
|
||||||
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
|
|
||||||
{{ range .OtherResults -}}
|
|
||||||
{{ template "entry" . }}
|
|
||||||
{{- end -}}
|
|
||||||
{{- end -}}
|
|
||||||
{{- template "search" . -}}
|
|
|
@ -1 +0,0 @@
|
||||||
{{ define "sitetitle" }}jidict{{ end }}
|
|
|
@ -1,9 +1,11 @@
|
||||||
{{- define "title" }}{{ .Query }} search - {{ template "sitetitle" . }}{{- end -}}
|
{{- define "search" -}}
|
||||||
|
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
|
||||||
{{- define "value" }}{{ .Query }}{{- end -}}
|
{{- range .ExactResults -}}
|
||||||
|
{{- template "word" . -}}
|
||||||
{{- define "results" -}}
|
{{- end }}
|
||||||
{{- template "entryfull" .Entry -}}
|
<hr>
|
||||||
|
{{ range .OtherResults -}}
|
||||||
|
{{ template "word" . }}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
{{- template "index" . -}}
|
{{- template "search" . -}}
|
|
@ -1,5 +1,22 @@
|
||||||
{{- define "title" }}{{ .Entry.Kanji }} - {{ template "sitetitle" . }}{{- end -}}
|
{{ define "word" }}
|
||||||
{{- define "results" -}}
|
<div class="box">
|
||||||
{{- template "entryfull" .Entry -}}
|
<h3>
|
||||||
{{- end -}}
|
{{- if .Kanji -}}
|
||||||
{{- template "index" . -}}
|
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby>
|
||||||
|
{{- else -}}
|
||||||
|
{{- .Reading -}}
|
||||||
|
{{- end -}}
|
||||||
|
</h3>
|
||||||
|
{{ if le (len .Definitions) 2 -}}
|
||||||
|
<p>{{- template "definition" (index .Definitions 0) -}}</p>
|
||||||
|
{{- else -}}
|
||||||
|
<ol>
|
||||||
|
{{- range .Definitions }}
|
||||||
|
<li>
|
||||||
|
{{ template "definition" . }}
|
||||||
|
</li>
|
||||||
|
{{- end }}
|
||||||
|
</ol>
|
||||||
|
{{- end }}
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
|
@ -1,21 +1,25 @@
|
||||||
package httputils
|
package httputils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler = func(http.ResponseWriter, *http.Request)
|
type Handler = func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
func getPartials() ([]string, map[string]time.Time, error) {
|
const templateFolder = "templates"
|
||||||
|
|
||||||
|
var templatePaths, templateModTimes, _ = getTemplates()
|
||||||
|
var templates *template.Template = template.Must(template.ParseFiles(templatePaths...))
|
||||||
|
|
||||||
|
func getTemplates() ([]string, map[string]time.Time, error) {
|
||||||
var modTimes map[string]time.Time = make(map[string]time.Time)
|
var modTimes map[string]time.Time = make(map[string]time.Time)
|
||||||
err := filepath.Walk(partialsFolder, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(templateFolder, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -33,15 +37,35 @@ func getPartials() ([]string, map[string]time.Time, error) {
|
||||||
return paths, modTimes, err
|
return paths, modTimes, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reloadTemplateIfModified(path string) {
|
||||||
|
fileInfo, _ := os.Stat(path)
|
||||||
|
modTime := fileInfo.ModTime()
|
||||||
|
if modTime.After(templateModTimes[path]) {
|
||||||
|
fmt.Printf("Reloading template %s...\n", path)
|
||||||
|
templates.ParseFiles(path)
|
||||||
|
templateModTimes[path] = modTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reloadTemplatesIfModified() {
|
||||||
|
for _, path := range templatePaths {
|
||||||
|
reloadTemplateIfModified(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const reloadTemplates = true
|
const reloadTemplates = true
|
||||||
|
|
||||||
func GenerateHandler(
|
func GenerateHandler(
|
||||||
|
file string,
|
||||||
handler func(http.ResponseWriter, *http.Request) bool,
|
handler func(http.ResponseWriter, *http.Request) bool,
|
||||||
templateSet TemplateSet,
|
data func(http.ResponseWriter, *http.Request) any,
|
||||||
template func(http.ResponseWriter, *http.Request) (template string, data any),
|
|
||||||
methods []string,
|
methods []string,
|
||||||
) Handler {
|
) Handler {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// All templates must be reloaded in case of dependencies
|
||||||
|
if reloadTemplates {
|
||||||
|
reloadTemplatesIfModified()
|
||||||
|
}
|
||||||
for _, method := range methods {
|
for _, method := range methods {
|
||||||
if method == r.Method {
|
if method == r.Method {
|
||||||
goto ok
|
goto ok
|
||||||
|
@ -51,18 +75,9 @@ func GenerateHandler(
|
||||||
return
|
return
|
||||||
ok:
|
ok:
|
||||||
renderTemplate := handler(w, r)
|
renderTemplate := handler(w, r)
|
||||||
if renderTemplate {
|
if renderTemplate && file != "" {
|
||||||
file_path, data := template(w, r)
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
err := templateSet.ExecuteTemplate(buf, file_path, reflect.ValueOf(data))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Fprint(w, "500 Internal Server Error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
fmt.Fprint(w, buf.String())
|
templates.ExecuteTemplate(w, file, data(w, r))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,86 +0,0 @@
|
||||||
package httputils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TemplateSet struct {
|
|
||||||
templates *template.Template
|
|
||||||
paths []string
|
|
||||||
modTimes map[string]time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTemplateSet(partials *TemplateSet, paths ...string) TemplateSet {
|
|
||||||
var partialPaths []string
|
|
||||||
if partials == nil {
|
|
||||||
partialPaths = make([]string, 0)
|
|
||||||
} else {
|
|
||||||
partialPaths = partials.paths
|
|
||||||
}
|
|
||||||
allPaths := append(partialPaths, paths...)
|
|
||||||
modTimes := make(map[string]time.Time)
|
|
||||||
for _, path := range allPaths {
|
|
||||||
fileInfo, _ := os.Stat(path)
|
|
||||||
modTimes[path] = fileInfo.ModTime()
|
|
||||||
}
|
|
||||||
templates := template.Must(template.ParseFiles(allPaths...))
|
|
||||||
return TemplateSet{
|
|
||||||
templates: templates,
|
|
||||||
paths: allPaths,
|
|
||||||
modTimes: modTimes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTemplateSet(paths ...string) TemplateSet {
|
|
||||||
for i, path := range paths {
|
|
||||||
paths[i] = fmt.Sprintf("%s/%s", templateFolder, path)
|
|
||||||
}
|
|
||||||
return newTemplateSet(&partials, paths...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (templateSet *TemplateSet) ExecuteTemplate(wr io.Writer, name string, data any) error {
|
|
||||||
templateSet.reloadTemplatesIfModified()
|
|
||||||
return templateSet.templates.ExecuteTemplate(wr, name, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (templateSet *TemplateSet) reloadTemplateIfModified(path string) {
|
|
||||||
fileInfo, _ := os.Stat(path)
|
|
||||||
modTime := fileInfo.ModTime()
|
|
||||||
if modTime.After(templateSet.modTimes[path]) {
|
|
||||||
fmt.Printf("Reloading template %s...\n", path)
|
|
||||||
templateSet.templates.ParseFiles(path)
|
|
||||||
templateSet.modTimes[path] = modTime
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (templateSet *TemplateSet) reloadTemplatesIfModified() {
|
|
||||||
for _, path := range templateSet.paths {
|
|
||||||
templateSet.reloadTemplateIfModified(path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTemplatePathsInDirectory(directory string) (paths []string, err error) {
|
|
||||||
paths = make([]string, 0)
|
|
||||||
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !info.IsDir() && strings.HasSuffix(path, ".html") {
|
|
||||||
paths = append(paths, path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const templateFolder = "templates"
|
|
||||||
const partialsFolder = templateFolder + "/partials"
|
|
||||||
|
|
||||||
var paths, _ = getTemplatePathsInDirectory(partialsFolder)
|
|
||||||
var partials = newTemplateSet(nil, paths...)
|
|
Reference in a new issue