fix search paths for root+
parent
4a7efd4016
commit
a7df97aae5
|
|
@ -50,7 +50,7 @@ func Refresh() {
|
||||||
panic(len(bs))
|
panic(len(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
Root = as.Get("root").GetString()
|
Root = strings.TrimSuffix(as.Get("root").GetString(), "/")
|
||||||
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
|
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
|
||||||
Head = string(bs[0])
|
Head = string(bs[0])
|
||||||
Foot = string(bs[1])
|
Foot = string(bs[1])
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,20 @@ func NewPathFromLocal(p string) Path {
|
||||||
if strings.HasPrefix(root, "./") {
|
if strings.HasPrefix(root, "./") {
|
||||||
root = root[2:]
|
root = root[2:]
|
||||||
}
|
}
|
||||||
|
if strings.HasSuffix(root, "/") {
|
||||||
|
root = root[:len(root)-1]
|
||||||
|
}
|
||||||
splits := strings.SplitN(p, root, 2)
|
splits := strings.SplitN(p, root, 2)
|
||||||
|
for len(splits) > 0 && splits[0] == "" {
|
||||||
|
splits = splits[1:]
|
||||||
|
}
|
||||||
|
if len(splits) == 0 {
|
||||||
|
splits = []string{"", ""}
|
||||||
|
}
|
||||||
href := splits[0]
|
href := splits[0]
|
||||||
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
if len(splits) == 1 && (splits[0] == root || splits[0] == config.Root) {
|
||||||
|
href = ""
|
||||||
|
} else if splits[0] == "" || splits[0] == "/" {
|
||||||
href = splits[1]
|
href = splits[1]
|
||||||
}
|
}
|
||||||
href = path.Join("/notes", href)
|
href = path.Join("/notes", href)
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,52 @@ package notes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type searcher struct {
|
||||||
|
patterns []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSearcher(phrase string) (*searcher, error) {
|
||||||
|
phrases := strings.Split(phrase, " ")
|
||||||
|
patterns := make([]*regexp.Regexp, 0)
|
||||||
|
for _, phrase := range phrases {
|
||||||
|
if len(phrase) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pattern, err := regexp.Compile("(?i)" + phrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
patterns = append(patterns, pattern)
|
||||||
|
}
|
||||||
|
if len(patterns) < 1 {
|
||||||
|
return nil, errors.New("no search specified")
|
||||||
|
}
|
||||||
|
return &searcher{
|
||||||
|
patterns: patterns,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *searcher) matches(input []byte) bool {
|
||||||
|
for _, pattern := range s.patterns {
|
||||||
|
if !pattern.Match(input) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Notes) Search(phrase string) (string, error) {
|
func (n *Notes) Search(phrase string) (string, error) {
|
||||||
pattern, err := regexp.Compile("(?i)" + phrase)
|
searcher, err := newSearcher(phrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -27,7 +63,7 @@ func (n *Notes) Search(phrase string) (string, error) {
|
||||||
if size := info.Size(); size < 1 || size > (5*1024*1024) {
|
if size := info.Size(); size < 1 || size > (5*1024*1024) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ok, err := grepFile(walked, pattern)
|
ok, err := grepFile(walked, searcher)
|
||||||
if err != nil && err.Error() == "bufio.Scanner: token too long" {
|
if err != nil && err.Error() == "bufio.Scanner: token too long" {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +80,7 @@ func (n *Notes) Search(phrase string) (string, error) {
|
||||||
return filetree.Paths(*files).List(true), err
|
return filetree.Paths(*files).List(true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func grepFile(file string, pattern *regexp.Regexp) (bool, error) {
|
func grepFile(file string, searcher *searcher) (bool, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
|
@ -52,7 +88,7 @@ func grepFile(file string, pattern *regexp.Regexp) (bool, error) {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if pattern.Match(scanner.Bytes()) {
|
if searcher.matches(scanner.Bytes()) {
|
||||||
return true, scanner.Err()
|
return true, scanner.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue