/
rt-hinters.go
138 lines (114 loc) · 3.12 KB
/
rt-hinters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// HipparchiaGoServer
// Copyright: E Gunderson 2022
// License: GNU GENERAL PUBLIC LICENSE 3
// (see LICENSE in the top level directory of the distribution)
package main
import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"sort"
"strings"
)
// RtAuthorHints - /hints/author/_?term=auf --> [{'value': 'Aufidius Bassus [lt0809]'}, {'value': 'Aufustius [lt0401]'}]
func RtAuthorHints(c echo.Context) error {
skg := c.QueryParam("term")
if len(skg) < 2 {
return emptyjsreturn(c)
}
skg = strings.ToLower(skg)
// is what we have a match?
var auu [][2]string
for _, a := range AllAuthors {
var who string
var an string
// [sosthenes], et al. can be found via "sos" or "[so"
if strings.Contains(skg, "[") {
who = a.Cleaname
} else {
who = strings.Replace(a.Cleaname, "[", "", 1)
}
if len(who) >= len(skg) {
an = strings.ToLower(who[0:len(skg)])
}
if an == skg {
ai := [2]string{a.Cleaname, a.UID}
auu = append(auu, ai)
}
}
// trim by active corpora
user := readUUIDCookie(c)
s := SafeSessionRead(user)
var trimmed [][2]string
for _, a := range auu {
co := a[1][0:2]
if s.ActiveCorp[co] {
trimmed = append(trimmed, a)
}
}
var auf []JSStruct
for _, t := range trimmed {
st := fmt.Sprintf(`%s [%s]`, t[0], t[1])
auf = append(auf, JSStruct{st})
}
// sort since we were working with a map
sort.Slice(auf, func(i, j int) bool { return auf[i].V < auf[j].V })
return c.JSONPretty(http.StatusOK, auf, JSONINDENT)
}
// RtLemmaHints - /hints/lemmata/_?term=dol --> [{"value": "dolabella\u00b9"}, {"value": "dolabra"}, {"value": "dolamen"}, ... ]
func RtLemmaHints(c echo.Context) error {
term := c.QueryParam("term")
// can't slice a unicode string...
skg := []rune(term)
if len(skg) < 2 {
return emptyjsreturn(c)
}
skg = stripaccentsRUNE(skg)
nl := string(skg[0:2])
var match []JSStruct
if _, ok := NestedLemm[nl]; ok {
for _, l := range NestedLemm[nl] {
er := l.EntryRune()
potential := stripaccentsRUNE(er[0:len(skg)])
if len(er) >= len(skg) && string(potential) == string(skg) {
// need to filter ab-cedo¹ --> abcedo
match = append(match, JSStruct{l.Entry})
}
}
}
sort.Slice(match, func(i, j int) bool { return match[i].V < match[j].V })
return c.JSONPretty(http.StatusOK, match, JSONINDENT)
}
func RtAuGenreHints(c echo.Context) error {
return basichinter(c, AuGenres)
}
func RtWkGenreHints(c echo.Context) error {
return basichinter(c, WkGenres)
}
func RtAuLocHints(c echo.Context) error {
return basichinter(c, AuLocs)
}
func RtWkLocHints(c echo.Context) error {
return basichinter(c, WkLocs)
}
// basichinter - which substrings of the request are members of the master map?
func basichinter(c echo.Context, mastermap map[string]bool) error {
skg := c.QueryParam("term")
if len(skg) < 2 {
return emptyjsreturn(c)
}
skg = strings.ToLower(skg)
skg = strings.Title(skg)
// is what we have a match?
var ff []string
for f := range mastermap {
if strings.Contains(f, skg) {
ff = append(ff, f)
}
}
fs := make([]JSStruct, len(ff))
for i, f := range ff {
fs[i] = JSStruct{f}
}
return c.JSONPretty(http.StatusOK, fs, JSONINDENT)
}