This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.go
191 lines (160 loc) · 4.47 KB
/
index.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
package main
import (
"github.com/OUCC/SyaroNote/syaro/markdown"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/analyzers/keyword_analyzer"
_ "github.com/blevesearch/bleve/analysis/language/en"
"gopkg.in/fsnotify.v1"
"fmt"
"html"
"os"
"path/filepath"
"strings"
)
const (
BLEVE_PATH = ".syaronote.bleve"
)
var (
bleveIndex bleve.Index
updateIndex = make(chan fsnotify.Event)
)
type wikiPageIndex struct {
Name string `json:"name"`
Title string `json:"title"`
Author string `json:"author"`
Aliases []string `json:"aliases"`
Tags []string `json:"tags"`
Contents string `json:"contents"`
}
// implement bleve.Classifier
func (w *wikiPageIndex) Type() string {
return "wiki"
}
// must be called after setting.wikiRoot is set
func indexLoop() {
blevePath := filepath.Join(setting.wikiRoot, BLEVE_PATH)
var err error
bleveIndex, err = bleve.Open(blevePath)
if err != nil {
indexAll()
}
// loop for updating index
for {
ev := <-updateIndex
wpath := toWikiPath(ev.Name)
switch ev.Op {
case fsnotify.Create, fsnotify.Write:
data, err := loadPageIndex(wpath)
if err != nil {
continue
}
log.Debug("Index %s", wpath)
if bleveIndex.Index(wpath, data) != nil {
log.Debug("Indexing falied: %v", err)
}
case fsnotify.Rename, fsnotify.Remove:
log.Debug("Delete %s", wpath)
if bleveIndex.Delete(wpath) != nil {
log.Debug("Indexing falied: %v", err)
}
}
}
}
func indexAll() {
blevePath := filepath.Join(setting.wikiRoot, BLEVE_PATH)
mapping, err := buildIndexMapping()
if err != nil {
log.Fatalf("Failed to create index: %v", err)
}
bleveIndex, err = bleve.New(blevePath, mapping)
if err != nil {
log.Fatalf("Failed to create index: %v", err)
}
log.Info("Building index...")
batch := bleveIndex.NewBatch()
err = filepath.Walk(setting.wikiRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
wpath := toWikiPath(path)
data, err := loadPageIndex(wpath)
if err != nil {
return nil
}
log.Debug("Indexing %s", wpath)
return batch.Index(wpath, data)
})
if err != nil || bleveIndex.Batch(batch) != nil {
log.Fatalf("Failed to create index: %v", err)
}
log.Info("Index building end")
}
func buildIndexMapping() (*bleve.IndexMapping, error) {
log.Info("Using indexing mode %s", setting.IndexingMode)
textFieldMapping := bleve.NewTextFieldMapping()
textFieldMapping.Analyzer = setting.IndexingMode
keywordFieldMapping := bleve.NewTextFieldMapping()
keywordFieldMapping.Analyzer = keyword_analyzer.Name
keywordFieldMapping.IncludeInAll = false
// keywordFieldMapping.IncludeTermVectors = false
wikiMapping := bleve.NewDocumentStaticMapping()
wikiMapping.AddFieldMappingsAt("name", keywordFieldMapping)
wikiMapping.AddFieldMappingsAt("title", textFieldMapping)
wikiMapping.AddFieldMappingsAt("author", keywordFieldMapping)
wikiMapping.AddFieldMappingsAt("aliases", keywordFieldMapping)
wikiMapping.AddFieldMappingsAt("tags", keywordFieldMapping)
wikiMapping.AddFieldMappingsAt("contents", textFieldMapping)
mapping := bleve.NewIndexMapping()
mapping.DefaultAnalyzer = setting.IndexingMode
mapping.AddDocumentMapping("wiki", wikiMapping)
for _, s := range []string{"name", "title", "contents"} {
log.Debug("Field: %s, Analyzer: %s", s, mapping.FieldAnalyzer(s))
}
return mapping, nil
}
func loadPageIndex(wpath string) (*wikiPageIndex, error) {
if strings.Contains(wpath, string(filepath.Separator)+".") || // exclude hidden files
strings.HasSuffix(wpath, FOLDER_MD) { // exclude _.md
return nil, fmt.Errorf("excluded file")
}
wf, err := loadFile(wpath)
if err != nil {
return nil, err
}
var name string
if wpath == string(filepath.Separator) {
name = wpath
} else {
name = removeExt(wf.Name())
}
var b []byte
switch wf.fileType {
case WIKIFILE_MARKDOWN:
var err error
b, err = wf.read()
if err != nil {
return nil, err
}
case WIKIFILE_FOLDER:
wf_, err := loadFile(filepath.Join(wf.WikiPath, FOLDER_MD))
if err != nil {
return &wikiPageIndex{
Name: name,
}, nil
}
if b, err = wf_.read(); err != nil {
return nil, err
}
case WIKIFILE_OTHER:
return nil, fmt.Errorf("not a markdown")
}
meta := markdown.Meta(b)
return &wikiPageIndex{
Name: name,
Title: html.EscapeString(meta["title"]),
Author: meta["author"],
Aliases: splitCommma(meta["alias"] + "," + meta["aliases"]),
Tags: splitCommma(meta["tag"] + "," + meta["tags"]),
Contents: html.EscapeString(string(b)),
}, nil
}