This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
114 lines (96 loc) · 2.53 KB
/
main.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
package main
import (
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/index/store/goleveldb"
"github.com/blevesearch/bleve/mapping"
"github.com/spf13/cobra"
"github.com/spf13/hugo/commands"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/viper"
jww "github.com/spf13/jwalterweatherman"
)
func init() {
HugoIndexCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
hugoCmdV = HugoIndexCmd
}
// flags
var Verbose bool
var hugoCmdV *cobra.Command
var HugoIndexCmd = &cobra.Command{
Use: "hugoidx",
Short: "hugoidx builds a search index of your site",
Long: `hugoidx is the main command, used to build a search index of your Hugo site.`,
Run: func(cmd *cobra.Command, args []string) {
InitializeConfig()
buildindex()
},
}
func InitializeConfig() {
LoadDefaultSettings()
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
viper.Set("Verbose", Verbose)
}
commands.InitializeConfig()
}
func LoadDefaultSettings() {
viper.SetDefault("IndexDir", "search.bleve")
}
func buildindex() {
site := &hugolib.Site{}
err := site.Process()
if err != nil {
jww.FATAL.Printf("error processing site: %v", err)
}
// create/index open the index
index, err := createOpenIndex(viper.GetString("IndexDir"))
if err != nil {
jww.FATAL.Printf("error creating/opening index: %v", err)
}
for _, p := range site.Pages {
// current work around for issue #2
if len(p.Title) <= 0 {
continue
}
jww.INFO.Printf("params: %#v", p.Params)
rpl, err := p.RelPermalink()
if err != nil {
jww.FATAL.Printf("error generating permalink: %v", err)
}
jww.INFO.Printf("Indexing: %s as - %s (% x)", p.Title, rpl, rpl)
pi := NewPageForIndex(p)
err = index.Index(rpl, pi)
if err != nil {
jww.FATAL.Printf("error indexing: %v", err)
}
}
err = index.Close()
if err != nil {
jww.FATAL.Printf("error closing index: %v", err)
}
}
func createOpenIndex(path string) (bleve.Index, error) {
index, err := bleve.Open(path)
if err == bleve.ErrorIndexPathDoesNotExist {
jww.INFO.Println("Creating Index: ", path)
indexMapping, err := buildIndexMapping()
if err != nil {
return nil, err
}
index, err = bleve.NewUsing(path, indexMapping, bleve.Config.DefaultIndexType, goleveldb.Name, nil)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
} else {
jww.INFO.Println("Opening Index: ", path)
}
return index, nil
}
func buildIndexMapping() (mapping.IndexMapping, error) {
rv := bleve.NewIndexMapping()
return rv, nil
}
func main() {
HugoIndexCmd.Execute()
}