first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
135
glossary.go
Normal file
135
glossary.go
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
// glossary.go - codename/term glossary loaded from data/glossary.json.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GlossaryEntry struct {
|
||||
Term string `json:"term"`
|
||||
Aka []string `json:"aka,omitempty"`
|
||||
Definition string `json:"definition"`
|
||||
See []string `json:"see,omitempty"`
|
||||
SeeLinks []seeLink `json:"-"`
|
||||
|
||||
searchLink string
|
||||
}
|
||||
|
||||
type seeLink struct{ Name, Term string }
|
||||
|
||||
func (e GlossaryEntry) DefinitionHTML() template.HTML {
|
||||
return template.HTML(e.Definition)
|
||||
}
|
||||
|
||||
func (e GlossaryEntry) SearchURL() string {
|
||||
if e.searchLink != "" {
|
||||
return e.searchLink
|
||||
}
|
||||
return e.buildSearchURL()
|
||||
}
|
||||
|
||||
func (e GlossaryEntry) buildSearchURL() string {
|
||||
clauses := []searchClause{{Conj: "and", Field: "TE", Value: searchClauseValue(e.Term)}}
|
||||
for _, a := range e.Aka {
|
||||
if strings.TrimSpace(a) != "" {
|
||||
clauses = append(clauses, searchClause{Conj: "or", Field: "TE", Value: searchClauseValue(a)})
|
||||
}
|
||||
}
|
||||
return searchURL(clauses)
|
||||
}
|
||||
|
||||
func searchClauseValue(v string) string {
|
||||
v = strings.TrimSpace(v)
|
||||
if len(splitWords(v)) == 1 && len(splitWords(v)[0]) == len(v) {
|
||||
return v
|
||||
}
|
||||
return `"` + v + `"`
|
||||
}
|
||||
|
||||
func (co *Corpus) glossaryLookup(term string) *GlossaryEntry {
|
||||
return co.glossaryIndex[strings.ToLower(term)]
|
||||
}
|
||||
|
||||
func (co *Corpus) loadGlossary() {
|
||||
p := projectPath("glossary.json")
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
log.Printf("Note: %s not found - glossary page will be empty.", p)
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal(b, &co.glossary); err != nil {
|
||||
warnf("%s is malformed: %v - glossary disabled.", p, err)
|
||||
co.glossary = nil
|
||||
return
|
||||
}
|
||||
co.glossaryIndex = map[string]*GlossaryEntry{}
|
||||
sort.SliceStable(co.glossary, func(i, j int) bool {
|
||||
return normFoldString(co.glossary[i].Term) < normFoldString(co.glossary[j].Term)
|
||||
})
|
||||
for i := range co.glossary {
|
||||
e := &co.glossary[i]
|
||||
e.searchLink = e.buildSearchURL()
|
||||
addIndex := func(key string) {
|
||||
low := strings.ToLower(key)
|
||||
if prev := co.glossaryIndex[low]; prev != nil && prev != e {
|
||||
warnf("glossary term/alias %q appears under both %q and %q - the later entry wins", key, prev.Term, e.Term)
|
||||
}
|
||||
co.glossaryIndex[low] = e
|
||||
}
|
||||
addIndex(e.Term)
|
||||
for _, a := range e.Aka {
|
||||
addIndex(a)
|
||||
}
|
||||
}
|
||||
var unknown []string
|
||||
for i := range co.glossary {
|
||||
e := &co.glossary[i]
|
||||
e.SeeLinks = nil
|
||||
for _, name := range e.See {
|
||||
l := seeLink{Name: name}
|
||||
if t := co.glossaryLookup(name); t != nil {
|
||||
l.Term = t.Term
|
||||
} else {
|
||||
unknown = append(unknown, name)
|
||||
}
|
||||
e.SeeLinks = append(e.SeeLinks, l)
|
||||
}
|
||||
}
|
||||
if len(unknown) > 0 {
|
||||
warnf("glossary: %d see-also names match no term or alias, shown unlinked: %s",
|
||||
len(unknown), strings.Join(firstN(unknown, 8), ", "))
|
||||
}
|
||||
co.buildGlossaryGroups()
|
||||
log.Printf("Glossary loaded: %d terms", len(co.glossary))
|
||||
}
|
||||
|
||||
type glossaryGroup struct {
|
||||
Letter string
|
||||
Entries []GlossaryEntry
|
||||
}
|
||||
|
||||
func (co *Corpus) buildGlossaryGroups() {
|
||||
co.glossaryGroups = nil
|
||||
for _, e := range co.glossary {
|
||||
letter := firstLetter(e.Term)
|
||||
if len(co.glossaryGroups) == 0 || co.glossaryGroups[len(co.glossaryGroups)-1].Letter != letter {
|
||||
co.glossaryGroups = append(co.glossaryGroups, glossaryGroup{Letter: letter})
|
||||
}
|
||||
co.glossaryGroups[len(co.glossaryGroups)-1].Entries = append(co.glossaryGroups[len(co.glossaryGroups)-1].Entries, e)
|
||||
}
|
||||
}
|
||||
|
||||
func (co *Corpus) handleGlossary(w http.ResponseWriter, r *http.Request) {
|
||||
render(w, r, &glossaryPage{
|
||||
Groups: co.glossaryGroups,
|
||||
GroupsHTML: co.preHTML("glossary"),
|
||||
Total: len(co.glossary),
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue