This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bcb2961
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#* | ||
*.sublime-* | ||
*~ | ||
.#* | ||
.project | ||
.settings | ||
.DS_Store | ||
/hugoidx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# hugoidx | ||
|
||
An experimental application to build a [Bleve](http://www.blevesearch.com) index from the pages in your [Hugo](http://gohugo.io) site. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/blevesearch/bleve" | ||
|
||
"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 { | ||
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, "goleveldb", 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() (*bleve.IndexMapping, error) { | ||
rv := bleve.NewIndexMapping() | ||
|
||
return rv, nil | ||
} | ||
|
||
func main() { | ||
HugoIndexCmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/hugo/hugolib" | ||
) | ||
|
||
type Page struct { | ||
Title string `json:"title"` | ||
Type string `json:"type"` | ||
Section string `json:"section"` | ||
Content string `json:"content"` | ||
WordCount float64 `json:"word_count"` | ||
ReadingTime float64 `json:"reading_time"` | ||
Keywords []string `json:"keywords"` | ||
Date time.Time `json:"date"` | ||
LastModified time.Time `json:"last_modified"` | ||
Author string `json:"author"` | ||
} | ||
|
||
func NewPageForIndex(page *hugolib.Page) *Page { | ||
author := "" | ||
if paramsAuthor, ok := page.Params["author"].([]string); ok { | ||
if len(paramsAuthor) > 0 { | ||
author = paramsAuthor[0] | ||
} | ||
} | ||
return &Page{ | ||
Title: page.Title, | ||
Type: page.Type(), | ||
Section: page.Section(), | ||
Content: page.Plain(), | ||
WordCount: float64(page.WordCount), | ||
ReadingTime: float64(page.ReadingTime), | ||
Keywords: page.Keywords, | ||
Date: page.Date, | ||
LastModified: page.Lastmod, | ||
Author: author, | ||
} | ||
} |