/
section.go
223 lines (202 loc) · 4.75 KB
/
section.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"github.com/blevesearch/bleve"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/writeas/go-strip-markdown"
"sort"
"strings"
)
type Wiki struct {
Name string
Slug string
Sections []*Section
Articles []*Article
Index bleve.Index
}
func (self *Wiki) Find(path string) (*Section, *Article) {
parts := strings.Split(path, "/")
if self.Sections == nil && self.Articles == nil {
return nil, nil
}
if self.Articles != nil && len(parts) == 1 {
for _, article := range self.Articles {
if article.Slug == parts[0] {
return nil, article
}
}
}
if self.Sections == nil {
return nil, nil
}
for _, section := range self.Sections {
if section.Slug == parts[0] {
if len(parts) == 1 {
return section, nil
}
return section.Find(strings.Join(parts[1:], "/"))
}
}
return nil, nil
}
func (self *Wiki) Sort() {
if self.Sections != nil {
sort.Sort(BySectionWeight(self.Sections))
for _, section := range self.Sections {
section.Sort()
}
}
if self.Articles != nil {
sort.Sort(ByArticleWeight(self.Articles))
}
}
func (self *Wiki) IndexArticle(path string, article *Article) error {
text := stripmd.Strip(string(article.Content))
return self.Index.Index(path, ArticleData{Name: article.Name, Content: text})
}
func (self *Wiki) IndexSection(path string, section *Section) error {
self.Index.Index(path, ArticleData{Name: section.Name})
return nil
}
func (self *Wiki) Search(queryString string) ([]*SearchResult, error) {
query := bleve.NewMatchPhraseQuery(queryString)
search := bleve.NewSearchRequest(query)
search.Highlight = bleve.NewHighlight()
searchResult, err := self.Index.Search(search)
if err != nil {
return nil, err
}
results := []*SearchResult{}
for _, result := range searchResult.Hits {
section, article := self.Find(result.ID)
if article == nil && section == nil {
return nil, fmt.Errorf("%v section or article not found", result.ID)
}
text := ""
for _, values := range result.Fragments {
for _, value := range values {
text += value
}
}
name := ""
if section != nil {
name = section.Name
}
if article != nil {
name = article.Name
}
results = append(results, &SearchResult{Path: result.ID, Name: name, Text: text})
}
return results, nil
}
type SearchResult struct {
Path string
Name string
Text string
}
type Section struct {
Name string
Icon string
Slug string
Parent *Section
Subsections []*Section
Articles []*Article
Weight int
}
func (self *Section) Sort() {
if self.Subsections != nil {
sort.Sort(BySectionWeight(self.Subsections))
for _, subsection := range self.Subsections {
subsection.Sort()
}
}
if self.Articles != nil {
sort.Sort(ByArticleWeight(self.Articles))
}
}
func (self *Section) Find(path string) (*Section, *Article) {
parts := strings.Split(path, "/")
if len(parts) == 1 && self.Articles != nil {
for _, article := range self.Articles {
if article.Slug == parts[0] {
return nil, article
}
}
}
if self.Subsections == nil {
return nil, nil
}
for _, section := range self.Subsections {
if section.Slug == parts[0] {
if len(parts) == 1 {
return section, nil
}
return section.Find(strings.Join(parts[1:], "/"))
}
}
return nil, nil
}
func (self *Section) GetPath() string {
slugs := []string{self.Slug}
section := self
for section.Parent != nil {
slugs = append(slugs, section.Slug)
}
sort.Reverse(sort.StringSlice(slugs))
return strings.Join(slugs, "/")
}
func (self *Section) GetIcon() string {
if self.Icon == "" {
return "subject"
}
return self.Icon
}
type ArticleData struct {
Name string
Content string
}
type Article struct {
Name string
Slug string
Content []byte
Weight int
Parent *Section
}
func (self *Article) GetHtml() (string, error) {
unsafe := blackfriday.Run(self.Content)
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
return string(html), nil
}
func (self *Article) GetPath() string {
if self.Parent == nil {
return self.Slug
}
return self.Parent.GetPath() + "/" + self.Slug
}
type BySectionWeight []*Section
func (self BySectionWeight) Len() int {
return len(self)
}
func (self BySectionWeight) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self BySectionWeight) Less(i, j int) bool {
if self[i].Weight == self[j].Weight {
return self[i].Slug < self[j].Slug
}
return self[i].Weight < self[j].Weight
}
type ByArticleWeight []*Article
func (self ByArticleWeight) Len() int {
return len(self)
}
func (self ByArticleWeight) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self ByArticleWeight) Less(i, j int) bool {
if self[i].Weight == self[j].Weight {
return self[i].Slug < self[j].Slug
}
return self[i].Weight < self[j].Weight
}