9 Commits
v1.2 ... v1.6

Author SHA1 Message Date
bel
f582410c40 dir remove button with fail on notempty, fix edit and del buttons 2020-01-26 16:17:55 +00:00
Bel LaPointe
f53fc80f68 fix serve . 2020-01-23 08:08:59 -07:00
Bel LaPointe
16335d796b default wrapper 2020-01-23 08:04:51 -07:00
Bel LaPointe
a7df97aae5 fix search paths for root+ 2019-12-17 16:14:32 -07:00
Bel LaPointe
4a7efd4016 Confirm on del 2019-12-17 15:33:32 -07:00
Bel LaPointe
5be556a4cf case insenstive regexp search 2019-12-17 15:31:00 -07:00
bel
72894cd5cc Make header on dirs and files floating fixed 2019-12-07 11:21:47 -07:00
bel
f0a1c21678 Update wrapper for bigger font 2019-12-07 11:03:30 -07:00
bel
807072a77f Unittest and exec file proof on search 2019-12-01 14:16:45 -07:00
14 changed files with 211 additions and 51 deletions

4
TODO
View File

@@ -30,10 +30,10 @@ x main test -
x TOC levels x TOC levels
x delete pages x delete pages
x search x search
FTS x FTS
https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go
x move auth as flag in router x move auth as flag in router
x . and ../** as roots cause bugs in listing and loading and linking x . and ../** as roots cause bugs in listing and loading and linking
x `create` at root is a 400, base= in URL (when `create` input is empty) x `create` at root is a 400, base= in URL (when `create` input is empty)
x versioning
delete top-level pages delete top-level pages
versioning

View File

@@ -7,6 +7,7 @@ import (
"local/args" "local/args"
"log" "log"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
) )
@@ -25,14 +26,14 @@ func init() {
} }
func Refresh() { func Refresh() {
if strings.Contains(fmt.Sprint(os.Args), "-test") { if strings.Contains(fmt.Sprint(os.Args), " -test") {
return return
} }
as := args.NewArgSet() as := args.NewArgSet()
as.Append(args.STRING, "root", "root dir path", "./public") as.Append(args.STRING, "root", "root dir path", "./public")
as.Append(args.STRING, "port", "port to listen on", "49909") as.Append(args.STRING, "port", "port to listen on", "49909")
as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html") as.Append(args.STRING, "wrap", "file with http header/footer", "")
as.Append(args.STRING, "oauth", "oauth URL", "") as.Append(args.STRING, "oauth", "oauth URL", "")
as.Append(args.DURATION, "version", "duration to mark versions", "0s") as.Append(args.DURATION, "version", "duration to mark versions", "0s")
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
@@ -40,20 +41,61 @@ func Refresh() {
} }
wrap := as.Get("wrap").GetString() wrap := as.Get("wrap").GetString()
var b []byte
if len(wrap) > 0 {
log.Printf("reading %v (%T)", wrap, wrap) log.Printf("reading %v (%T)", wrap, wrap)
b, err := ioutil.ReadFile(wrap) var err error
b, err = ioutil.ReadFile(wrap)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} else {
b = []byte(defaultWrapper)
}
bs := bytes.Split(b, []byte("{{{}}}")) bs := bytes.Split(b, []byte("{{{}}}"))
if len(bs) != 2 { if len(bs) != 2 {
panic(len(bs)) panic(len(bs))
} }
Root = as.Get("root").GetString() Root = strings.TrimSuffix(as.Get("root").GetString(), "/")
Root, _ = filepath.Abs(Root)
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])
OAuthServer = as.Get("oauth").GetString() OAuthServer = as.Get("oauth").GetString()
VersionInterval = as.Get("version").GetDuration() VersionInterval = as.Get("version").GetDuration()
} }
const defaultWrapper = `
<html>
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css -->
<style>
@charset "UTF-8";body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;color:#dbdbdb;background:#202b38;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid rgba(0,150,191,.67);margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{margin-top:10px;font-style:normal}address,blockquote cite{font-style:normal}a[href^=mailto]:before{content:"📧 "}a[href^=tel]:before{content:"📞 "}a[href^=sms]:before{content:"💬 "}button,input[type=button],input[type=checkbox],input[type=submit]{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#161f27;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input:not([type=checkbox]):not([type=radio]),select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#324759}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px rgba(0,150,191,.67)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}a{text-decoration:none;color:#41adff}a:hover{text-decoration:underline}code,kbd{background:#161f27;color:#ffbe85;padding:5px;border-radius:6px}pre>code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#161f27}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#161f27;border-radius:6px}::-webkit-scrollbar-thumb{background:#324759;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#415c73}
</style>
<style>
nav {
display: block;
background: #161f27;
padding: .5pt;
border-radius: 6px;
}
nav li li li li {
display: none;
}
img {
max-height: 400px;
}
body {
font-size: 125%;
}
</style>
</header>
<body height="100%">
{{{}}}
</body>
<footer>
</footer>
</html>
`

View File

@@ -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)

View File

@@ -1,15 +1,11 @@
package notes package notes
import ( import (
"errors"
"local/notes-server/filetree" "local/notes-server/filetree"
"os" "os"
) )
func (n *Notes) Delete(urlPath string) error { func (n *Notes) Delete(urlPath string) error {
p := filetree.NewPathFromURL(urlPath) p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return errors.New("path is dir")
}
return os.Remove(p.Local) return os.Remove(p.Local)
} }

View File

@@ -17,4 +17,27 @@ func TestDelete(t *testing.T) {
if _, err := os.Stat("/tmp/a"); err == nil { if _, err := os.Stat("/tmp/a"); err == nil {
t.Error(err) t.Error(err)
} }
d, err := ioutil.TempDir(os.TempDir(), "trydel*")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 3; i++ {
f, err := ioutil.TempFile(d, "file*")
if err != nil {
t.Fatal(err)
}
f.Close()
}
if err := n.Delete(d); err == nil {
t.Error(err)
}
e, err := ioutil.TempDir(os.TempDir(), "trydel*")
if err != nil {
t.Fatal(err)
}
if err := n.Delete(e); err != nil {
t.Error(err)
}
} }

View File

@@ -2,18 +2,57 @@ package notes
import ( import (
"bufio" "bufio"
"bytes" "errors"
"local/notes-server/filetree" "local/notes-server/filetree"
"log" "log"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "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) {
searcher, err := newSearcher(phrase)
if err != nil {
return "", err
}
files := filetree.NewFiles() files := filetree.NewFiles()
err := filepath.Walk(n.root, err = filepath.Walk(n.root,
func(walked string, info os.FileInfo, err error) error { func(walked string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@@ -21,10 +60,13 @@ func (n *Notes) Search(phrase string) (string, error) {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
if splits := strings.Split(info.Name(), "."); len(splits) > 1 && !(strings.HasSuffix(info.Name(), ".md") || strings.HasSuffix(info.Name(), ".txt")) { if size := info.Size(); size < 1 || size > (5*1024*1024) {
return nil return nil
} }
ok, err := grepFile(walked, []byte(phrase)) ok, err := grepFile(walked, searcher)
if err != nil && err.Error() == "bufio.Scanner: token too long" {
err = nil
}
if err == nil && ok { if err == nil && ok {
p := filetree.NewPathFromLocal(path.Dir(walked)) p := filetree.NewPathFromLocal(path.Dir(walked))
files.Push(p, info) files.Push(p, info)
@@ -38,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, phrase []byte) (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
@@ -46,7 +88,7 @@ func grepFile(file string, phrase []byte) (bool, error) {
defer f.Close() defer f.Close()
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
if bytes.Contains(scanner.Bytes(), phrase) { if searcher.matches(scanner.Bytes()) {
return true, scanner.Err() return true, scanner.Err()
} }
} }

View File

@@ -42,3 +42,13 @@ func TestSearch(t *testing.T) {
t.Fatal(v, result) t.Fatal(v, result)
} }
} }
func TestSearchBigFiles(t *testing.T) {
n := New()
n.root = "/usr/local/bin"
_, err := n.Search("this file")
if err != nil {
t.Fatal(err)
}
}

View File

@@ -1,3 +0,0 @@
asdf
this contains my search string

View File

@@ -1,5 +1,18 @@
asdf # h1
searchString
hi
here is my new line ## h2
hi
### h3
hi
#### h4
hi
* bullet
* 1

View File

@@ -1 +1,3 @@
hi # Hello
## World

View File

@@ -22,8 +22,8 @@ func h1(content string) string {
return h("1", content) return h("1", content)
} }
func h2(content string) string { func h2(content string, style ...string) string {
return h("2", content) return h("2", content, style...)
} }
func h3(content string) string { func h3(content string) string {
@@ -38,6 +38,10 @@ func h5(content string) string {
return h("5", content) return h("5", content)
} }
func h(level, content string) string { func h(level, content string, style ...string) string {
return fmt.Sprintf("\n<h%s>\n%s\n</h%s>\n", level, content, level) s := ""
if len(style) > 0 {
s = fmt.Sprintf("style=%q", style[0])
}
return fmt.Sprintf("\n<h%s %s>\n%s\n</h%s>\n", level, s, content, level)
} }

View File

@@ -39,7 +39,7 @@ func TestBlock(t *testing.T) {
func TestH(t *testing.T) { func TestH(t *testing.T) {
s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".") s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".")
if ok, err := regexp.MatchString("<h2>.*hi.*<.h2>", s); err != nil { if ok, err := regexp.MatchString("<h2[ ]*>.*hi.*<.h2>", s); err != nil {
t.Fatal(err, s) t.Fatal(err, s)
} else if !ok { } else if !ok {
t.Fatal(ok, s) t.Fatal(ok, s)

View File

@@ -22,13 +22,8 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
} }
func notesHead(w http.ResponseWriter, p filetree.Path) { func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink())) fmt.Fprintln(w, h2(p.MultiLink(), "margin: 0; position: fixed; padding: .25em; background-color: #202b38; width: 100%; top: 0;"))
fmt.Fprintf(w, ` htmlSearch(w)
<form action=%q method="post">
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
} }
func (s *Server) dir(w http.ResponseWriter, r *http.Request) { func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
@@ -43,12 +38,8 @@ func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
} }
func dirHead(w http.ResponseWriter, baseHREF string) { func dirHead(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, ` htmlCreate(w, baseHREF)
<form action=%q method="get"> htmlDelete(w, baseHREF)
<input type="text" name="base"></input>
<button type="submit">Create</button>
</form>
`, path.Join("/create/", baseHREF))
} }
func (s *Server) file(w http.ResponseWriter, r *http.Request) { func (s *Server) file(w http.ResponseWriter, r *http.Request) {
@@ -62,10 +53,36 @@ func (s *Server) file(w http.ResponseWriter, r *http.Request) {
} }
func fileHead(w http.ResponseWriter, baseHREF string) { func fileHead(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, ` htmlEdit(w, baseHREF)
<a href=%q><input type="button" value="Edit"></input></a> htmlDelete(w, baseHREF)
`, path.Join("/edit/", baseHREF)) }
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Delete"></input></a> func htmlEdit(w http.ResponseWriter, baseHREF string) {
`, path.Join("/delete/", baseHREF)) fmt.Fprintf(w, `<div style='display:inline-block'>
<a href=%q><input type="button" value="Edit"></input></a>
</div><br>`, path.Join("/edit/", baseHREF))
}
func htmlDelete(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, `<div style='display:inline-block'>
<a href=%q><input type="button" value="Delete" onclick="return confirm('Delete?');"></input></a>
</div><br>`, path.Join("/delete/", baseHREF))
}
func htmlCreate(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, `
<form action=%q method="get">
<input type="text" name="base"></input>
<button type="submit">Create</button>
</form>
`, path.Join("/create/", baseHREF))
}
func htmlSearch(w http.ResponseWriter) {
fmt.Fprintf(w, `
<form action=%q method="post" style="padding-top: 2.5em">
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
} }

View File

@@ -18,6 +18,9 @@
img { img {
max-height: 400px; max-height: 400px;
} }
body {
font-size: 125%;
}
</style> </style>
</header> </header>
<body height="100%"> <body height="100%">