15 Commits
v1.0 ... v1.9

Author SHA1 Message Date
bel
da968c2fd4 update color scheme 2020-02-01 23:01:37 +00:00
bel
563eb7bb61 change textarea cursor 2020-01-26 18:41:12 +00:00
bel
b7f13bf33d extract gzip to own package 2020-01-26 18:08:20 +00:00
bel
d73cbe9e0c Add gzip 2020-01-26 17:48:43 +00:00
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
bel
081d50328f Fix no configured user 2019-12-01 13:25:49 -07:00
bel
4e2b7b3c85 Dont list hidden files and hopefully search files by ext only .md 2019-11-24 05:09:29 +00:00
27 changed files with 250 additions and 57 deletions

0
Dockerfile Normal file → Executable file
View File

4
TODO Normal file → Executable file
View File

@@ -30,10 +30,10 @@ x main test -
x TOC levels
x delete pages
x search
FTS
x FTS
https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go
x move auth as flag in router
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 versioning
delete top-level pages
versioning

View File

@@ -7,6 +7,7 @@ import (
"local/args"
"log"
"os"
"path/filepath"
"strings"
"time"
)
@@ -32,7 +33,7 @@ func Refresh() {
as := args.NewArgSet()
as.Append(args.STRING, "root", "root dir path", "./public")
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.DURATION, "version", "duration to mark versions", "0s")
if err := as.Parse(); err != nil {
@@ -40,20 +41,63 @@ func Refresh() {
}
wrap := as.Get("wrap").GetString()
var b []byte
if len(wrap) > 0 {
log.Printf("reading %v (%T)", wrap, wrap)
b, err := ioutil.ReadFile(wrap)
var err error
b, err = ioutil.ReadFile(wrap)
if err != nil {
panic(err)
}
} else {
b = []byte(defaultWrapper)
}
bs := bytes.Split(b, []byte("{{{}}}"))
if len(bs) != 2 {
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(), ":")
Head = string(bs[0])
Foot = string(bs[1])
OAuthServer = as.Get("oauth").GetString()
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%;
filter: hue-rotate(120deg);
background: #3b242b;
}
</style>
</header>
<body height="100%">
{{{}}}
</body>
<footer>
</footer>
</html>
`

View File

@@ -20,9 +20,20 @@ func NewPathFromLocal(p string) Path {
if strings.HasPrefix(root, "./") {
root = root[2:]
}
if strings.HasSuffix(root, "/") {
root = root[:len(root)-1]
}
splits := strings.SplitN(p, root, 2)
for len(splits) > 0 && splits[0] == "" {
splits = splits[1:]
}
if len(splits) == 0 {
splits = []string{"", ""}
}
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 = path.Join("/notes", href)

View File

@@ -5,6 +5,9 @@ type Paths []Path
func (p Paths) List(full ...bool) string {
content := "<ul>\n"
for _, path := range p {
if len(path.Base) > 0 && path.Base[0] == '.' {
continue
}
if len(full) > 0 && full[0] {
content += path.FullLI() + "\n"
} else {

0
main.go Normal file → Executable file
View File

0
main_test.go Normal file → Executable file
View File

4
notes/delete.go Normal file → Executable file
View File

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

23
notes/delete_test.go Normal file → Executable file
View File

@@ -17,4 +17,27 @@ func TestDelete(t *testing.T) {
if _, err := os.Stat("/tmp/a"); err == nil {
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

@@ -28,7 +28,7 @@ func editFile(p filetree.Path) string {
return fmt.Sprintf(`
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
<table style="width:100%%; height: 90%%">
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
<textarea name="content" style="width:100%%; min-height:90%%; cursor:crosshair;">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>

0
notes/notes.go Normal file → Executable file
View File

0
notes/notes_test.go Normal file → Executable file
View File

60
notes/search.go Normal file → Executable file
View File

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

10
notes/search_test.go Normal file → Executable file
View File

@@ -42,3 +42,13 @@ func TestSearch(t *testing.T) {
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
searchString
# h1
hi
here is my new line
## h2
hi
### h3
hi
#### h4
hi
* bullet
* 1

View File

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

0
server/.notes/notes.go Normal file → Executable file
View File

12
server/html.go Normal file → Executable file
View File

@@ -22,8 +22,8 @@ func h1(content string) string {
return h("1", content)
}
func h2(content string) string {
return h("2", content)
func h2(content string, style ...string) string {
return h("2", content, style...)
}
func h3(content string) string {
@@ -38,6 +38,10 @@ func h5(content string) string {
return h("5", content)
}
func h(level, content string) string {
return fmt.Sprintf("\n<h%s>\n%s\n</h%s>\n", level, content, level)
func h(level, content string, style ...string) string {
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) {
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)
} else if !ok {
t.Fatal(ok, s)

55
server/notes.go Normal file → Executable file
View File

@@ -22,13 +22,8 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
}
func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
fmt.Fprintf(w, `
<form action=%q method="post">
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
fmt.Fprintln(w, h2(p.MultiLink(), "margin: 0; position: fixed; padding: .25em; background-color: #202b38; width: 100%; top: 0;"))
htmlSearch(w)
}
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) {
fmt.Fprintf(w, `
<form action=%q method="get">
<input type="text" name="base"></input>
<button type="submit">Create</button>
</form>
`, path.Join("/create/", baseHREF))
htmlCreate(w, baseHREF)
htmlDelete(w, baseHREF)
}
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) {
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Edit"></input></a>
`, path.Join("/edit/", baseHREF))
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Delete"></input></a>
`, path.Join("/delete/", baseHREF))
htmlEdit(w, baseHREF)
htmlDelete(w, baseHREF)
}
func htmlEdit(w http.ResponseWriter, baseHREF string) {
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

@@ -2,8 +2,10 @@ package server
import (
"fmt"
"local/gziphttp"
"local/router"
"net/http"
"path/filepath"
)
func (s *Server) Routes() error {
@@ -18,27 +20,27 @@ func (s *Server) Routes() error {
},
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.authenticate(s.notes),
handler: s.gzip(s.authenticate(s.notes)),
},
{
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.edit),
handler: s.gzip(s.authenticate(s.edit)),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
handler: s.authenticate(s.delete),
handler: s.gzip(s.authenticate(s.delete)),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.submit),
handler: s.gzip(s.authenticate(s.submit)),
},
{
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.authenticate(s.create),
handler: s.gzip(s.authenticate(s.create)),
},
{
path: fmt.Sprintf("search"),
handler: s.authenticate(s.search),
handler: s.gzip(s.authenticate(s.search)),
},
}
@@ -54,3 +56,17 @@ func (s *Server) root(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/notes"
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if gziphttp.Can(r) {
gz := gziphttp.New(w)
defer gz.Close()
w = gz
}
if filepath.Ext(r.URL.Path) == ".css" {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
}
h(w, r)
}
}

0
versions/.versions.go Normal file → Executable file
View File

0
versions/.versions_test.go Normal file → Executable file
View File

2
versions/versions.go Normal file → Executable file
View File

@@ -13,6 +13,8 @@ type Versions struct {
func New() (*Versions, error) {
v := &Versions{}
v.cmd("git", "init")
v.cmd("git", "config", "user.email", "user@user.user")
v.cmd("git", "config", "user.name", "user")
return v, nil
}

0
versions/versions_test.go Normal file → Executable file
View File

5
wrapper.html Normal file → Executable file
View File

@@ -18,6 +18,11 @@
img {
max-height: 400px;
}
body {
font-size: 125%;
filter: hue-rotate(120deg);
background: #3b242b;
}
</style>
</header>
<body height="100%">