Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mschoch committed Jul 8, 2015
0 parents commit bcb2961
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
#*
*.sublime-*
*~
.#*
.project
.settings
.DS_Store
/hugoidx
3 changes: 3 additions & 0 deletions README.md
@@ -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.
108 changes: 108 additions & 0 deletions main.go
@@ -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()
}
41 changes: 41 additions & 0 deletions model.go
@@ -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,
}
}

0 comments on commit bcb2961

Please sign in to comment.