Debian Codegraph @

internal/redirect/redirect.go

raw symbols SCIP
1 package redirectscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/"
5 "io/ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/"
6 "logscip-go gomod github.com/golang/go/src go1.13 log/"
7 "net/httpscip-go gomod github.com/golang/go/src go1.13 `net/http`/"
8 "path/filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/"
9 "sortscip-go gomod github.com/golang/go/src go1.13 sort/"
10 "stringsscip-go gomod github.com/golang/go/src go1.13 strings/"
11
12 pbscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/ "github.com/Debian/debiman/internal/protoscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/"
13 "github.com/Debian/debiman/internal/tagscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/tag`/"
14 "github.com/golang/protobuf/protoscip-go gomod github.com/golang/protobuf v1.3.2 `github.com/golang/protobuf/proto`/"
15 "golang.org/x/text/languagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/"
16 )
17
18 type IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry# struct {
19 Namescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Name. string // TODO: string pool
20 Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. string // TODO: enum to save space
21 Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. string // TODO: sort by popcon, TODO: use a string pool
22 Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. string // TODO: use a string pool
23 Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. string // TODO: type: would it make sense to use language.Tag?
24 }
25
26 func (e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) ServingPathscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#ServingPath().(suffix string) string {
27 return "/" + e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. + "/" + e.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. + "/" + e.Namescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Name. + "." + e.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. + "." + e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. + suffix
28 }
29
30 type Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index# struct {
31 Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries. map[string][]IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#
32 Suitesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Suites. map[string]string
33 Langsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Langs. map[string]bool
34 Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections. map[string]bool
35 }
36
37 // TODO(later): the default suite should be the latest stable release
38 const defaultSuitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/defaultSuite. = "bullseye"
39 const defaultLanguagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/defaultLanguage. = "en"
40
41 // bestLanguageMatch is like bestLanguageMatch in rendermanpage.go, but for the redirector index. TODO: can we de-duplicate the code?
42 func bestLanguageMatchscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bestLanguageMatch().(t []languagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/.Tagscip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/Tag#, options []IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry# {
43 // ensure that en comes first, so that language.Matcher treats it as default
44 if options[0].Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. != "en" {
45 for i := 1; i < len(options); i++ {
46 if options[i].Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == "en" {
47 options = append([]IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#{options[i]}, options...)
48 break
49 }
50 }
51 }
52
53 if t == nil {
54 return options[0]
55 }
56
57 tags := make([]languagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/.Tagscip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/Tag#, len(options))
58 for idx, m := range options {
59 tag, err := tagscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/tag`/.FromLocalescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/tag`/FromLocale().(m.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.)
60 if err != nil {
61 panic(fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("Cannot get language.Tag from locale %q: %v", m.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language., err))
62 }
63 tags[idx] = tag
64 }
65
66 matcher := languagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/.NewMatcherscip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/NewMatcher().(tags)
67 tag, _, _ := matcher.Matchscip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/Matcher#Match().(t...)
68 for idx, t := range tags {
69 if t == tag {
70 return options[idx]
71 }
72 }
73 return options[0]
74 }
75
76 func (i Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#) splitscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#split().(path string) (suite string, binarypkg string, name string, section string, lang string) {
77 dir := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.13 strings/TrimPrefix().(filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/Dir().(path), "/")
78 base := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.13 strings/TrimSpace().(filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.13 `path/filepath`/Base().(path))
79 base = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(base, " ", ".", -1)
80 parts := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().(dir, "/")
81 if len(parts) > 0 {
82 if len(parts) == 1 {
83 if _, ok := i.Suitesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Suites.[parts[0]]; ok {
84 suite = parts[0]
85 } else if i.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[parts[0]] {
86 // legacy manpages.debian.org
87 section = parts[0]
88 } else {
89 if i.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[base] {
90 // man.freebsd.org
91 section = base
92 base = parts[0]
93 } else {
94 binarypkg = parts[0]
95 }
96 }
97 } else if len(parts) == 2 && stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(parts[1], "man") && i.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.13 strings/TrimPrefix().(parts[1], "man")] {
98 // legacy manpages.debian.org
99 lang = parts[0]
100 section = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.13 strings/TrimPrefix().(parts[1], "man")
101 } else if len(parts) == 2 {
102 suite = parts[0]
103 binarypkg = parts[1]
104 }
105 }
106
107 // the first part can contain dots, so we need to “split from the right”
108 parts = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().(base, ".")
109 if len(parts) == 1 {
110 return suite, binarypkg, base, section, lang
111 }
112
113 // The last part can either be a language or a section
114 consumed := 0
115 if l := parts[len(parts)-1]; i.Langsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Langs.[l] {
116 lang = l
117 consumed++
118 } else if l := parts[len(parts)-1]; i.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[l] {
119 section = l
120 consumed++
121 }
122 // The second to last part (if enough parts are present) can
123 // be a section (because the language was already specified).
124 if len(parts) > 1+consumed {
125 if s := parts[len(parts)-1-consumed]; i.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[s] {
126 section = s
127 consumed++
128 }
129 }
130
131 return suite,
132 binarypkg,
133 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Joinscip-go gomod github.com/golang/go/src go1.13 strings/Join().(parts[:len(parts)-consumed], "."),
134 section,
135 lang
136 }
137
138 type byMainSectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection# []IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#
139
140 func (p byMainSectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#) Lenscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#Len().() int { return len(p) }
141 func (p byMainSectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#) Swapscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#Swap().(i, j int) { p[i], p[j] = p[j], p[i] }
142 func (p byMainSectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#) Lessscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#Less().(i, j int) bool {
143 // Compare main sections first
144 mi := p[i].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1]
145 mj := p[j].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1]
146 if mi < mj {
147 return true
148 }
149 if mi > mj {
150 return false
151 }
152 return len(p[i].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.) > len(p[j].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.)
153 }
154
155 // Default taken from man(1):
156 var mansectscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/mansect. = searchOrderscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/searchOrder().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Splitscip-go gomod github.com/golang/go/src go1.13 strings/Split().("1 n l 8 3 2 3posix 3pm 3perl 3am 5 4 9 6 7", " "))
157
158 func searchOrderscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/searchOrder().(sections []string) map[string]int {
159 order := make(map[string]int)
160 for idx, section := range sections {
161 order[section] = idx
162 }
163 return order
164 }
165
166 type bySectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection# []IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#
167
168 func (p bySectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#) Lenscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#Len().() int { return len(p) }
169 func (p bySectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#) Swapscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#Swap().(i, j int) { p[i], p[j] = p[j], p[i] }
170 func (p bySectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#) Lessscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#Less().(i, j int) bool {
171 oI, okI := mansectscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/mansect.[p[i].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.]
172 oJ, okJ := mansectscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/mansect.[p[j].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.]
173 if okI && okJ { // both sections are in mansect
174 return oI < oJ
175 }
176 if !okI && okJ {
177 return false // sort all mansect sections before custom sections
178 }
179 if okI && !okJ {
180 return true // sort all mansect sections before custom sections
181 }
182 return p[i].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. < p[j].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. // neither are in mansect
183 }
184
185 func (i Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#) Narrowscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Narrow().(acceptLang string, template, ref IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#, entries []IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) []IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry# {
186 t := template // for convenience
187
188 fullyQualified := func() bool {
189 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" || t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == "" || t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. == "" || t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == "" {
190 return false
191 }
192
193 // Verify validity
194 for _, e := range entries {
195 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. &&
196 t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == e.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. &&
197 t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. == e.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. &&
198 t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. {
199 return true
200 }
201 }
202 return false
203 }
204
205 filtered := make([]IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#, len(entries))
206 copy(filtered, entries)
207
208 filter := func(keep func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool) {
209 tmp := filtered[:0]
210 for _, e := range filtered {
211 if !keep(e) {
212 continue
213 }
214 tmp = append(tmp, e)
215 }
216 filtered = tmp
217 }
218
219 if t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. != "" {
220 // Verify the specified language is a valid choice
221 var found bool
222 for _, e := range filtered {
223 if e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. {
224 found = true
225 break
226 }
227 }
228 if !found {
229 t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. = ""
230 }
231 }
232
233 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. != "" {
234 // Verify the specified suite is a valid choice
235 var found bool
236 for _, e := range filtered {
237 if e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. {
238 found = true
239 break
240 }
241 }
242 if !found {
243 t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. = ""
244 }
245 }
246
247 // Narrow down as much as possible upfront. The keep callback is
248 // the logical and of all the keep callbacks below:
249 filter(func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool {
250 return (t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" || e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.) &&
251 (t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. == "" || e.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1] == t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1]) &&
252 (t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == "" || e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.) &&
253 (t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == "" || e.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg.)
254 })
255
256 // suite
257
258 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" {
259 // Prefer redirecting to the suite from the referrer
260 for _, e := range filtered {
261 if e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == ref.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. {
262 t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. = ref.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.
263 break
264 }
265 }
266 // Default to defaultSuite
267 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" {
268 for _, e := range filtered {
269 if e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == defaultSuitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/defaultSuite. {
270 t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. = defaultSuitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/defaultSuite.
271 break
272 }
273 }
274 }
275 // If the manpage is not contained in defaultSuite, use the
276 // first suite we can find for which the manpage is available.
277 if t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" {
278 for _, e := range filtered {
279 t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. = e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.
280 break
281 }
282 }
283 }
284
285 filter(func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool { return t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == "" || e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. == t.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite. })
286 if len(filtered) == 0 {
287 return nil
288 }
289 if fullyQualified() {
290 return filtered
291 }
292
293 // section
294
295 if len(t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.) > 1 {
296 // A subsection was specified. Sort by section, but prefer
297 // subsections so that they get matched first (e.g. “3” will
298 // come after “3edit”).
299 sortscip-go gomod github.com/golang/go/src go1.13 sort/.Stablescip-go gomod github.com/golang/go/src go1.13 sort/Stable().(byMainSectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/byMainSection#(filtered))
300 } else {
301 // No subsection was specified. Sort by section so that
302 // subsections are matched later (e.g. “3edit” will come after
303 // “3”).
304 sortscip-go gomod github.com/golang/go/src go1.13 sort/.Stablescip-go gomod github.com/golang/go/src go1.13 sort/Stable().(bySectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bySection#(filtered))
305 }
306
307 if t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. == "" {
308 // TODO(later): respect the section preference cookie (+test)
309 t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. = filtered[0].Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.
310 }
311
312 filter(func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool { return t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section. == "" || e.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1] == t.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.[:1] })
313 if len(filtered) == 0 {
314 return nil
315 }
316 if fullyQualified() {
317 return filtered
318 }
319
320 // language
321
322 if t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == "" {
323 tags, _, _ := languagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/.ParseAcceptLanguagescip-go gomod golang.org/x/text v0.3.2 `golang.org/x/text/language`/ParseAcceptLanguage().(acceptLang)
324 // ignore err: tags == nil results in the default language
325 best := bestLanguageMatchscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/bestLanguageMatch().(tags, filtered)
326 t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. = best.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.
327 }
328
329 filter(func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool { return t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == "" || e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. == t.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language. })
330 if len(filtered) == 0 {
331 return nil
332 }
333 if fullyQualified() {
334 return filtered
335 }
336
337 // binarypkg
338
339 if t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == "" {
340 t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. = filtered[0].Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg.
341 }
342
343 filter(func(e IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#) bool { return t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == "" || e.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. == t.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg. })
344 if len(filtered) == 0 {
345 return nil
346 }
347 return filtered
348 }
349
350 type NotFoundErrorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError# struct {
351 Manpagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#Manpage. string
352 BestChoicescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#BestChoice. IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#
353 }
354
355 func (e *NotFoundErrorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#) Errorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#Error().() string {
356 return "No such man page"
357 }
358
359 func (i Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#) Redirectscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Redirect().(r *httpscip-go gomod github.com/golang/go/src go1.13 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#) (string, error) {
360 path := r.URLscip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.13 `net/url`/URL#Path.
361
362 if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, "/") ||
363 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, "/index.html") ||
364 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(path, "/contents-") {
365 return "", &NotFoundErrorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#{}
366 }
367
368 suffix := ".html"
369 // If a raw manpage was requested, redirect to raw, not HTML
370 if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, ".gz") && !stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, ".html.gz") {
371 suffix = ".gz"
372 }
373 for stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, ".html") || stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(path, ".gz") {
374 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.13 strings/TrimSuffix().(path, ".gz")
375 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.13 strings/TrimSuffix().(path, ".html")
376 }
377
378 // Parens are converted into dots, so that “i3(1)” becomes
379 // “i3.1.”. Trailing dots are stripped and two dots next to each
380 // other are converted into one.
381 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(path, "(", ".", -1)
382 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(path, ")", ".", -1)
383 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(path, "..", ".", -1)
384 path = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.13 strings/TrimSuffix().(path, ".")
385
386 var suite, binarypkg, name, section, lang string
387 if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(path, "/man") && stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Indexscip-go gomod github.com/golang/go/src go1.13 strings/Index().(path[1:], "/") > -1 {
388 suite, binarypkg, name, section, lang = i.splitLegacyscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#splitLegacy().(path)
389 } else {
390 suite, binarypkg, name, section, lang = i.splitscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#split().(path)
391 }
392 if rewrite, ok := i.Suitesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Suites.[suite]; ok {
393 suite = rewrite
394 }
395 if section == "0" {
396 // legacy manpages.debian.org
397 section = ""
398 }
399
400 logscip-go gomod github.com/golang/go/src go1.13 log/.Printfscip-go gomod github.com/golang/go/src go1.13 log/Printf().("path %q -> suite = %q, binarypkg = %q, name = %q, section = %q, lang = %q", path, suite, binarypkg, name, section, lang)
401
402 lname := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(name)
403 entries, ok := i.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries.[lname]
404 if !ok {
405 // Fall back to joining (originally) whitespace-separated
406 // parts by dashes and underscores, like man(1).
407 entries, ok = i.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries.[stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(lname, ".", "-", -1)]
408 if !ok {
409 entries, ok = i.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries.[stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replace().(lname, ".", "_", -1)]
410 if !ok {
411 return "", &NotFoundErrorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#{Manpagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#Manpage.: name}
412 }
413 }
414 }
415
416 acceptLang := r.Headerscip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.13 `net/http`/Header#Get().("Accept-Language")
417 ref := IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#{
418 Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.: r.FormValuescip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#FormValue().("suite"),
419 Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg.: r.FormValuescip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#FormValue().("binarypkg"),
420 Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.: r.FormValuescip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#FormValue().("section"),
421 Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.: r.FormValuescip-go gomod github.com/golang/go/src go1.13 `net/http`/Request#FormValue().("language"),
422 }
423 filtered := i.Narrowscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Narrow().(acceptLang, IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#{
424 Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.: suite,
425 Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg.: binarypkg,
426 Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.: section,
427 Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.: lang,
428 }, ref, entries)
429
430 if len(filtered) == 0 {
431 // Present the user with another choice for this manpage.
432 var best IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#
433 if name != "index" && name != "favicon" {
434 best = i.Narrowscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Narrow().(acceptLang, IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#{}, ref, entries)[0]
435 }
436 return "", &NotFoundErrorscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#{
437 Manpagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#Manpage.: name,
438 BestChoicescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/NotFoundError#BestChoice.: best}
439 }
440
441 return filtered[0].ServingPathscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#ServingPath().(suffix), nil
442 }
443
444 func IndexFromProtoscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexFromProto().(path string) (Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#, error) {
445 index := Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#{
446 Langsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Langs.: make(map[string]bool),
447 Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.: make(map[string]bool),
448 Suitesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Suites.: make(map[string]string),
449 }
450 b, err := ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/ReadFile().(path)
451 if err != nil {
452 return index, err
453 }
454 var idx pbscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/.Indexscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#
455 if err := protoscip-go gomod github.com/golang/protobuf v1.3.2 `github.com/golang/protobuf/proto`/.Unmarshalscip-go gomod github.com/golang/protobuf v1.3.2 `github.com/golang/protobuf/proto`/Unmarshal().(b, &idx); err != nil {
456 return index, err
457 }
458 index.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries. = make(map[string][]IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#, len(idx.Entryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#Entry.))
459 for _, e := range idx.Entryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#Entry. {
460 name := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(e.Namescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Name.)
461 index.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries.[name] = append(index.Entriesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Entries.[name], IndexEntryscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#{
462 Namescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Name.: e.Namescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Name.,
463 Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Suite.: e.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Suite.,
464 Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Binarypkg.: e.Binarypkgscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Binarypkg.,
465 Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Section.: e.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Section.,
466 Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/IndexEntry#Language.: e.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/IndexEntry#Language.,
467 })
468 }
469 for _, l := range idx.Languagescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#Language. {
470 index.Langsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Langs.[l] = true
471 }
472 index.Suitesscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Suites. = idx.Suitescip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#Suite.
473 for _, l := range idx.Sectionscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/proto`/Index#Section. {
474 index.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.[l] = true
475 }
476 index.Sectionsscip-go gomod github.com/Debian/debiman . `github.com/Debian/debiman/internal/redirect`/Index#Sections.["0"] = true
477
478 return index, nil
479 }