Add hot tempalte reloading, WIP pos

This commit is contained in:
Elnu 2023-07-21 10:34:56 -07:00
parent e00df6aac6
commit 442f727d8a
4 changed files with 73 additions and 15 deletions

View file

@ -30,7 +30,12 @@ func LoadDict() error {
type Entry struct {
Kanji string
Reading string
Definitions []string
Definitions []Definition
}
type Definition struct {
Definition string
PartOfSpeech string
}
func ParseEntry(entry jmdict.JmdictEntry) Entry {
@ -42,11 +47,18 @@ func ParseEntry(entry jmdict.JmdictEntry) Entry {
if len(entry.Readings) > 0 {
reading = entry.Readings[0].Reading
}
var definitions []string
var definitions []Definition
if len(entry.Sense) > 0 && len(entry.Sense[0].Glossary) > 0 {
definitions = make([]string, len(entry.Sense[0].Glossary))
definitions = make([]Definition, len(entry.Sense[0].Glossary))
for i, glossary := range entry.Sense[0].Glossary {
definitions[i] = glossary.Content
var pos string
if len(entry.Sense[0].PartsOfSpeech) > i+1 {
pos = entry.Sense[0].PartsOfSpeech[i]
}
definitions[i] = Definition{
Definition: glossary.Content,
PartOfSpeech: pos,
}
}
}
return Entry{

View file

@ -0,0 +1,3 @@
{{ define "definition" }}
{{ if .PartOfSpeech }}<chip>{{ .PartOfSpeech }}</chip> {{ end }}{{ .Definition -}}
{{ end }}

View file

@ -9,11 +9,11 @@
{{- end -}}
</h3>
{{ if le (len .Definitions) 2 -}}
<p>{{- index .Definitions 0 -}}</p>
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else -}}
<ol>
{{- range .Definitions }}
<li>{{- . -}}</li>
<li>{{- template "definition" . -}}</li>
{{- end }}
</ol>
{{- end }}