generated from ElnuDev/go-project
parent
f558d7f0c1
commit
e70916c6c1
@ -0,0 +1,23 @@
|
||||
{{ 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 }}
|
@ -0,0 +1,23 @@
|
||||
{{ 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 }}
|
@ -0,0 +1,11 @@
|
||||
{{- 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" . -}}
|
@ -0,0 +1 @@
|
||||
{{ define "sitetitle" }}jidict{{ end }}
|
@ -1,11 +1,9 @@
|
||||
{{- 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 "word" . -}}
|
||||
{{- end }}
|
||||
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
|
||||
{{ range .OtherResults -}}
|
||||
{{ template "word" . }}
|
||||
{{- define "title" }}{{ .Query }} search - {{ template "sitetitle" . }}{{- end -}}
|
||||
|
||||
{{- define "value" }}{{ .Query }}{{- end -}}
|
||||
|
||||
{{- define "results" -}}
|
||||
{{- template "entryfull" .Entry -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- template "search" . -}}
|
||||
|
||||
{{- template "index" . -}}
|
@ -1,22 +1,5 @@
|
||||
{{ define "word" }}
|
||||
<div class="box">
|
||||
<h3>
|
||||
{{- if .Kanji -}}
|
||||
<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 }}
|
||||
{{- define "title" }}{{ .Entry.Kanji }} - {{ template "sitetitle" . }}{{- end -}}
|
||||
{{- define "results" -}}
|
||||
{{- template "entryfull" .Entry -}}
|
||||
{{- end -}}
|
||||
{{- template "index" . -}}
|
@ -0,0 +1,86 @@
|
||||
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 new issue