-
Notifications
You must be signed in to change notification settings - Fork 0
/
fts5index.go
335 lines (315 loc) · 8.08 KB
/
fts5index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"time"
"github.com/jaytaylor/html2text"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
//"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/allconfig"
"github.com/gohugoio/hugo/resources/resource"
//"github.com/spf13/cast"
//"github.com/spf13/afero"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
)
const date_fmt = "[02/Jan/2006:15:04:05 -0700]"
const iso_8601 = "2006-01-02 15:04:05"
var (
verbose *bool
)
func main() {
// command-line options
verbose = flag.Bool("v", false, "Verbose error reporting")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
dsn := flag.String("db", "search.db", "SQLite DB to use for the search index")
port := flag.String("p", "localhost:8086", "host address and port to bind to")
tmpl_fn := flag.String("template", "", "Go template for the search page")
do_html := flag.Bool("html", false, "Index HTML files")
do_hugo := flag.Bool("hugo", false, "Index Hugo markdown files")
flag.Parse()
var err error
var f *os.File
var db *sql.DB
// Profiler
if *cpuprofile != "" {
f, err = os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
ctx := context.TODO()
var updated time.Time
if *dsn != "" {
stat, err := os.Stat(*dsn)
if err == nil {
updated = stat.ModTime()
}
db, err = sql.Open("sqlite3", *dsn)
if err != nil {
log.Fatalf("ERROR: opening SQLite DB %q, error: %s", *dsn, err)
}
row := db.QueryRow("SELECT count(sql) FROM sqlite_master WHERE name='search'")
var count int32
err = row.Scan(&count)
if err != nil {
log.Fatal("Could not check table status: ", err)
}
if count == 0 {
_, err = db.Exec("CREATE VIRTUAL TABLE search USING fts5(path UNINDEXED, title, text, summary UNINDEXED)")
if err != nil {
log.Fatal("Could not create search table: ", err)
}
}
} else {
log.Fatal("no SQLite index filename supplied")
}
if *do_html {
before := time.Now()
log.Println("indexing HTML...")
index_html(db, updated)
log.Println("done in", time.Now().Sub(before))
}
if *do_hugo {
before := time.Now()
log.Println("indexing Hugo...")
index_hugo(ctx, db, updated)
log.Println("done in", time.Now().Sub(before))
}
if *tmpl_fn != "" {
tmpl, err := template.ParseFiles(*tmpl_fn)
if err != nil {
log.Fatal("Could not load template: ", *tmpl_fn, ": ", err)
}
serve(db, *port, tmpl)
}
db.Close()
}
// recurse through the parsed HTML tree to extract the title
func extract_title(n *html.Node) string {
if n.Type == html.ElementNode && n.DataAtom == atom.Title {
return n.FirstChild.Data
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
result := extract_title(c)
if result != "" {
return result
}
}
return ""
}
func index_html(db *sql.DB, updated time.Time) {
stmt, err := db.Prepare("INSERT OR REPLACE INTO search (path, title, text) VALUES (?, ?, ?)")
if err != nil {
log.Fatal("Could not prepare insert statement: ", err)
}
// walk the current directory looking for HTML files
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fn := strings.ToLower(path)
if !(strings.HasSuffix(fn, ".html") || strings.HasSuffix(fn, ".htm")) {
return nil
}
stat, err := os.Stat(path)
if stat.ModTime().Before(updated) {
return nil
}
if *verbose {
fmt.Println(path)
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
html_doc, err := html.Parse(f)
if err != nil {
return err
}
f.Close()
title := extract_title(html_doc)
text, err := html2text.FromHTMLNode(html_doc)
if err != nil {
return err
}
if *verbose {
fmt.Println("\t", title)
}
// if *verbose {
// fmt.Println(text);
// }
_, err = stmt.Exec(path, title, text)
return err
})
stmt.Close()
}
func index_hugo(ctx context.Context, db *sql.DB, updated time.Time) {
stmt, err := db.Prepare("INSERT OR REPLACE INTO search (path, title, text, summary) VALUES (?, ?, ?, ?)")
if err != nil {
log.Fatal("Could not prepare insert statement: ", err)
}
osFs := hugofs.Os
//cfg, err := hugolib.LoadConfigDefault(osFs)
configs, err := allconfig.LoadConfig(
allconfig.ConfigSourceDescriptor{
Fs: osFs,
Filename: "config.toml",
//Path: cwd,
//WorkingDir: cwd,
})
if err != nil {
wd, _ := os.Getwd()
log.Fatal("Could not load Hugo config.toml (cwd=", wd, "): ", err)
}
base := configs.Base
// XXX fs := hugofs.NewDefault(cfg)
cfg := config.New()
cfg.Set("publishDir", base.PublishDir)
cfg.Set("publishDirStatic", base.PublishDir)
cfg.Set("publishDirDynamic", base.PublishDir)
cwd, _ := os.Getwd()
cfg.Set("workingDir", cwd)
fs := hugofs.NewDefaultOld(cfg)
sites, err := hugolib.NewHugoSites(deps.DepsCfg{Fs: fs, Configs: configs})
if err != nil {
log.Fatal("Could not load Hugo site(s): ", err)
}
err = sites.Build(hugolib.BuildCfg{SkipRender: true})
if err != nil {
log.Fatal("Could not run render: ", err)
}
for _, p := range sites.Pages() {
if p.Draft() || resource.IsFuture(p) || resource.IsExpired(p) {
continue
}
title := p.Title()
path := p.Permalink()
content, err := p.Content(ctx)
html_src, ok := content.(template.HTML)
if !ok {
if *verbose {
log.Println("Could not get HTML for: ", path, content)
}
continue
}
html_doc, err := html.Parse(strings.NewReader(string(html_src)))
if err != nil {
log.Fatal("Invalid HTML: ", path, err)
}
//log.Println("HTML content", path, html_src)
text, err := html2text.FromHTMLNode(html_doc)
if err != nil {
log.Fatal("Could not load content for: ", path, err)
}
if *verbose {
fmt.Println(path)
fmt.Println("\t", title)
//fmt.Println("\t", p.Summary);
fmt.Println()
}
_, err = stmt.Exec(path, title, text, p.Summary(ctx))
if err != nil {
log.Println("path = ", path, "title = ", title, "text = ", text, "summary = ", p.Summary(ctx))
log.Fatal("Could not write page to DB: ", err)
}
}
stmt.Close()
}
type Result struct {
Path string
Title string
Summary template.HTML
Text string
}
func do_error(w http.ResponseWriter, msg string, query string) {
if query != "" {
log.Println("query error:", query, msg)
}
io.WriteString(w, fmt.Sprintf(`<!doctype html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>Error</h1>
<p>%s</p>
</body>
</html>
`, msg))
}
func min(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
func serve(db *sql.DB, port string, tmpl *template.Template) {
SearchHandler := func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
do_error(w, "Could not parse search form.", "")
return
}
raw_term := r.Form.Get("q")
if raw_term == "" {
do_error(w, "Please enter search terms.", "")
return
}
term, err := fts5_term(raw_term)
if err != nil {
do_error(w, "Malformed search terms.", "")
return
}
query := "SELECT path, title, summary, text FROM search WHERE search=?"
if *verbose {
log.Println("Query: ", query, term)
}
rows, err := db.Query(query, term)
if err != nil {
do_error(w, "Query error: "+err.Error(), term)
return
}
results := make([]Result, 0)
for rows.Next() {
var path, title, summary, text string
err = rows.Scan(&path, &title, &summary, &text)
if err != nil {
do_error(w, "Row error: "+err.Error(), term)
return
}
results = append(results, Result{path, title, template.HTML(summary), text})
log.Printf("query %q result\n\t%s\n\t%s\n", term, path, text[:min(len(text), 72)])
}
data := make(map[string]interface{}, 1)
data["Results"] = results
err = tmpl.Execute(w, data)
if err != nil {
do_error(w, "Template error: "+err.Error(), query)
return
}
}
http.HandleFunc("/search", SearchHandler)
if *verbose {
log.Println("starting web server on ", port)
}
log.Fatal(http.ListenAndServe(port, nil))
}