aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-07-23 02:07:08 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-07-23 02:07:08 -0400
commitf949689b4d56daa9988c821e2f0e1b470cfc7275 (patch)
tree30cd47a5bd6a6598c4b10a107e72767c36262522 /pkg
parent8633ab4bc13bf957d7598700338c8d0e251e0cfa (diff)
Update help info
Diffstat (limited to 'pkg')
-rw-r--r--pkg/index/index.go35
-rw-r--r--pkg/server/server.go32
-rw-r--r--pkg/shell/interpreter.go4
3 files changed, 62 insertions, 9 deletions
diff --git a/pkg/index/index.go b/pkg/index/index.go
index 50f642e..42f5051 100644
--- a/pkg/index/index.go
+++ b/pkg/index/index.go
@@ -336,6 +336,39 @@ func (idx Index) Filter(paths []string, numWorkers uint) []string {
return fPaths
}
+// Create a comparison function for documents by field.
+// Allowed fields: path,title,date,filetime,meta
+func NewDocCmp(field string, reverse bool) (func(*Document, *Document) int, bool) {
+ descMod := 1
+ if reverse {
+ descMod = -1
+ }
+ switch field {
+ case "path":
+ return func(a, b *Document) int {
+ return descMod * strings.Compare(a.Path, b.Path)
+ }, true
+ case "title":
+ return func(a, b *Document) int {
+ return descMod * strings.Compare(a.Title, b.Title)
+ }, true
+ case "date":
+ return func(a, b *Document) int {
+ return descMod * a.Date.Compare(b.Date)
+ }, true
+ case "filetime":
+ return func(a, b *Document) int {
+ return descMod * a.FileTime.Compare(b.FileTime)
+ }, true
+ case "meta":
+ return func(a, b *Document) int {
+ return descMod * strings.Compare(a.OtherMeta, b.OtherMeta)
+ }, true
+ }
+
+ return nil, false
+}
+
func ParseDoc(path string, opts ParseOpts) (*Document, error) {
doc := &Document{Path: path, parseOpts: opts}
@@ -430,5 +463,5 @@ func ParseDocs(paths []string, numWorkers uint, opts ParseOpts) (map[string]*Doc
}
func init() {
- linkRegex = regexp.MustCompile(`\[.*\]\(\s*(\S+)\s*\)`)
+ linkRegex = regexp.MustCompile(`\[.*\]\(\s*([^\)]+)\s*\)`)
}
diff --git a/pkg/server/server.go b/pkg/server/server.go
index a7a5395..68750a2 100644
--- a/pkg/server/server.go
+++ b/pkg/server/server.go
@@ -6,6 +6,7 @@ import (
"io"
"log/slog"
"net/http"
+ "slices"
"strings"
"sync"
"time"
@@ -21,11 +22,20 @@ type Server interface {
}
func info(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte(`
- <h1>Atlas Server</h1>
- <p>This is the experimental atlas server!
- Try POSTing a query to <pre>/search</pre></p>
- `))
+ w.Write([]byte(`<h1>Atlas Server</h1>
+<p>This is the experimental atlas server!
+Try POSTing a query to <pre>/search</pre></p>
+<hr>
+<p>You can sort the results using the query param <pre>sortBy</pre>
+<ul>
+<li>path</li>
+<li>title</li>
+<li>date</li>
+<li>filetime</li>
+<li>meta</li>
+</ul>
+You can change the order using <pre>sortOrder</pre> with asc or desc
+</p>`))
}
func NewMux(db *data.Query) *http.ServeMux {
@@ -37,7 +47,7 @@ func NewMux(db *data.Query) *http.ServeMux {
}
mux.HandleFunc("/", info)
- mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
+ mux.HandleFunc("POST /search", func(w http.ResponseWriter, r *http.Request) {
b := &strings.Builder{}
if _, err := io.Copy(b, r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
@@ -70,6 +80,16 @@ func NewMux(db *data.Query) *http.ServeMux {
}
}
+ queryParams := r.URL.Query()
+ if queryParams.Has("sortBy") {
+ sortBy := queryParams.Get("sortBy")
+ sortOrder := queryParams.Get("sortOrder")
+ docCmp, ok := index.NewDocCmp(sortBy, sortOrder == "desc" || sortOrder == "descending")
+ if ok {
+ slices.SortFunc(docs, docCmp)
+ }
+ }
+
if !maxFileTime.IsZero() {
w.Header().Add("Last-Modified", maxFileTime.UTC().Format(http.TimeFormat))
}
diff --git a/pkg/shell/interpreter.go b/pkg/shell/interpreter.go
index c222de0..bb0a441 100644
--- a/pkg/shell/interpreter.go
+++ b/pkg/shell/interpreter.go
@@ -183,7 +183,7 @@ out:
}
switch t.Type {
case ITOK_CMD_HELP:
- printHelp(w)
+ PrintHelp(w)
break out
case ITOK_CMD_EXIT:
return true, nil
@@ -755,7 +755,7 @@ func (inter Interpreter) Tokenize(line string) []IToken {
return tokens
}
-func printHelp(w io.Writer) {
+func PrintHelp(w io.Writer) {
fmt.Fprintln(w, "Shitty debug shell for atlas")
fmt.Fprintln(w, "help - print this help")
fmt.Fprintln(w, "exit - exit interactive mode")