41 Commits
v0.1 ... v1.12

Author SHA1 Message Date
Bel LaPointe
43d44a4518 Header floats kthnx css and comments 2020-08-07 12:42:50 -06:00
Bel LaPointe
8bf7503c8e Add comments per heading 2020-08-07 12:30:27 -06:00
Bel LaPointe
785215bd3c name from recursive resolved plaintext 2020-08-07 11:35:52 -06:00
Bel LaPointe
952e04815a read only mode a gogo 2020-08-06 21:20:29 -06:00
bel
d6c95a536c fix selection color 2020-04-05 01:00:26 +00:00
bel
9785ef5e1e Add serach bar to search page 2020-03-09 03:09:45 +00:00
bel
3b7aa70e44 fix table and toc colors 2020-03-09 03:06:54 +00:00
Bel LaPointe
c067b426a9 Fix colors breaking floating css because fuck 2020-03-08 20:37:48 -06:00
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
Bel LaPointe
e4632d36ba Manual test versioning looks good 2019-11-21 14:17:45 -07:00
Bel LaPointe
fecd343f1b Impl versions with shell commands 2019-11-21 14:09:46 -07:00
Bel LaPointe
217a221cf4 2019-11-21 13:53:04.526575 -0700 MST m=+0.013733497 2019-11-21 13:53:04 -07:00
Bel LaPointe
b73a962556 Test notes 2019-11-21 13:39:03 -07:00
Bel LaPointe
c88fed0929 testing filetree 2019-11-21 13:26:07 -07:00
Bel LaPointe
a140d0eade Impl complete testing needed 2019-11-21 13:12:30 -07:00
Bel LaPointe
3079cd163f Fix absolute and relative without parent dir paths work 2019-11-21 12:28:30 -07:00
Bel LaPointe
9fc4e63a34 Crap absolute paths to public dont work 2019-11-14 15:01:23 -07:00
Bel LaPointe
1b9067a72d permissions 2019-11-14 14:57:30 -07:00
Bel LaPointe
e6f63a578f Fix pathing with relative non-single-depth root 2019-11-14 11:14:57 -07:00
Bel LaPointe
7964518d36 Testing versioning with git in go in versions 2019-11-12 14:48:28 -07:00
Bel LaPointe
259a8efc70 Impl FTS and some main tests 2019-11-10 07:54:42 -07:00
Bel LaPointe
60dc6bc876 Split into packages only manually tested 2019-11-09 18:51:03 -07:00
bel
0d497f4fa8 Fix nonroot path 2019-11-06 17:44:04 -07:00
bel
c6d43699ef TODO 2019-11-06 17:19:57 -07:00
Bel LaPointe
618b9ed513 Fix serving . but absolute and parents still cause bugs 2019-10-21 09:52:05 -06:00
bel
0595c49fc2 todo 2019-10-20 17:34:02 -06:00
bel
da6eaca26f Pass things around via query params 2019-10-20 16:39:26 -06:00
76 changed files with 2139 additions and 204 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
gollum gollum
public
**.sw* **.sw*
**/**.sw* **/**.sw*
*.sw* *.sw*

38
TODO
View File

@@ -1,7 +1,39 @@
x edit page x edit page
x create page x create page
x create dir x create dir
x main test -
- create,
- header
- text box
- submit
- submit target
- edit,
- header
- text box
- submit
- submit target
- dir,
- header
- create
- create target
- list
- note,
- header
- edit
- edit target
- content
- root-> dir,
- root->file,
- dir->dir,
- dir->file
TOC levels x TOC levels
delete pages x delete pages
search x search
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

File diff suppressed because one or more lines are too long

129
config/rotate.py Executable file
View File

@@ -0,0 +1,129 @@
#! /usr/local/bin/python3
def main(args) :
print("args", args)
for i in args :
rotate(i)
def rotate(x) :
print("input: {}", x)
rgb = hex_to_rgb(x)
print("rgb: {}", rgb)
hsl = rgb_to_hsl(rgb)
print("hsl: {}", hsl)
import os
env = os.environ
if "DEG" in env :
deg = int(env["DEG"])
else :
deg = 110
hsl = rotate_hsl(hsl, deg)
print("hsl': {}", hsl)
rgb = hsl_to_rgb(hsl)
print("rgb': {}", rgb)
print(rgb_to_hex(rgb))
def hex_to_rgb(x) :
if x.startswith("#") :
x = x[1:]
r = x[0:2]
g = x[2:4]
b = x[4:6]
return (
hex_to_dec(r),
hex_to_dec(g),
hex_to_dec(b),
)
def hex_to_dec(x) :
s = 0
mul = 1
for i in range(len(x)):
c = x[len(x)-i-1]
c = c.upper()
digit = ord(c) - ord('0')
if not c.isdigit() :
digit = ord(c) - ord('A') + 10
s += mul * digit
mul *= 16
return s
def rgb_to_hsl(rgb) :
return (
compute_h(rgb),
compute_s(rgb),
compute_l(rgb),
)
def compute_h(rgb) :
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
if delta == 0 :
return 0
if r == cmax :
return 60 * ( ( (g - b)/delta ) % 6)
if g == cmax :
return 60 * ( ( (b - r)/delta ) + 2)
if b == cmax :
return 60 * ( ( (r - g)/delta ) + 4)
def compute_s(rgb) :
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
if delta == 0 :
return 0
return delta / ( 1 - (abs(2*compute_l(rgb)-1)) )
def compute_l(rgb) :
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
return (cmax + cmin) / 2
def compute_hsl_const(rgb) :
rgb = [ i/255.0 for i in rgb ]
return rgb[:] + [max(rgb), min(rgb), max(rgb)-min(rgb)]
def rotate_hsl(hsl, deg) :
return (
(hsl[0] + deg) % 360,
hsl[1],
hsl[2],
)
def hsl_to_rgb(hsl) :
h = hsl[0]
s = hsl[1]
l = hsl[2]
c = s * (1 - abs(2 * l))
x = c * (1 - abs((h / 60) % 2 - 1))
m = l - (c / 2)
rgbp = ()
if h < 60 :
rgbp = (c, x, 0)
if h < 120 :
rgbp = (x, c, 0)
if h < 180 :
rgbp = (0, c, x)
if h < 240 :
rgbp = (0, x, c)
if h < 300 :
rgbp = (x, 0, c)
else:
rgbp = (c, 0, x)
r = rgbp[0]
g = rgbp[1]
b = rgbp[2]
return (
(r+m)*255,
(g+m)*255,
(b+m)*255,
)
def rgb_to_hex(rgb) :
r = min(max(int(rgb[0]), 0), 255)
g = min(max(int(rgb[1]), 0), 255)
b = min(max(int(rgb[2]), 0), 255)
return "#{:02x}{:02x}{:02x}".format(r, g, b)
from sys import argv
main(argv[1:])

1
config/water.css Submodule

Submodule config/water.css added at 576eee5b82

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"os" "os"
@@ -7,7 +7,7 @@ import (
type Dirs []Path type Dirs []Path
func newDirs() *Dirs { func NewDirs() *Dirs {
d := Dirs([]Path{}) d := Dirs([]Path{})
return &d return &d
} }

28
filetree/dirs_test.go Executable file
View File

@@ -0,0 +1,28 @@
package filetree
import (
"local/notes-server/config"
"os"
"testing"
)
func TestDirs(t *testing.T) {
config.Root = "/"
dirs := NewDirs()
info, err := os.Stat("/usr/local")
if err != nil {
t.Fatal(err)
}
p := NewPathFromLocal("/usr")
dirs.Push(p, info)
if len([]Path(*dirs)) != 1 {
t.Error(dirs)
}
first := []Path(*dirs)[0]
if first.Base != "local" {
t.Error(first)
}
if first.Local != "/usr/local" {
t.Error(first)
}
}

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"os" "os"
@@ -7,7 +7,7 @@ import (
type Files []Path type Files []Path
func newFiles() *Files { func NewFiles() *Files {
d := Files([]Path{}) d := Files([]Path{})
return &d return &d
} }

28
filetree/files_test.go Executable file
View File

@@ -0,0 +1,28 @@
package filetree
import (
"local/notes-server/config"
"os"
"testing"
)
func TestFiles(t *testing.T) {
config.Root = "/"
files := NewFiles()
info, err := os.Stat("/etc/hosts")
if err != nil {
t.Fatal(err)
}
p := NewPathFromLocal("/etc")
files.Push(p, info)
if len([]Path(*files)) != 1 {
t.Error(files)
}
first := []Path(*files)[0]
if first.Base != "hosts" {
t.Error(first)
}
if first.Local != "/etc/hosts" {
t.Error(first)
}
}

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"fmt" "fmt"
@@ -16,9 +16,24 @@ type Path struct {
} }
func NewPathFromLocal(p string) Path { func NewPathFromLocal(p string) Path {
splits := strings.SplitN(p, path.Base(config.Root), 2) root := config.Root + "/"
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] href := splits[0]
if len(splits) > 1 { 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)
@@ -67,6 +82,10 @@ func (p Path) MultiLink() string {
return full return full
} }
func (p Path) FullLI() string {
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.HREF)
}
func (p Path) LI() string { func (p Path) LI() string {
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base) return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base)
} }

77
filetree/path_test.go Executable file
View File

@@ -0,0 +1,77 @@
package filetree
import (
"local/notes-server/config"
"testing"
)
func TestPathIs(t *testing.T) {
p := Path{Local: "/dev/null"}
if ok := p.IsDir(); ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); !ok {
t.Fatal(ok, p)
}
p = Path{Local: "/tmp"}
if ok := p.IsDir(); !ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); ok {
t.Fatal(ok, p)
}
}
func TestNewPathFromLocal(t *testing.T) {
cases := []struct {
in string
root string
href string
local string
}{
{
in: "/wiki/wiki/b/a.md",
root: "/wiki/wiki",
href: "/notes/b/a.md",
local: "/wiki/wiki/b/a.md",
},
{
in: "/wiki/b/a.md",
root: "/wiki",
href: "/notes/b/a.md",
local: "/wiki/b/a.md",
},
{
in: "/wiki/a.md",
root: "/wiki",
href: "/notes/a.md",
local: "/wiki/a.md",
},
{
in: "/b/a.md",
root: "/",
href: "/notes/b/a.md",
local: "/b/a.md",
},
{
in: "/a.md",
root: "/",
href: "/notes/a.md",
local: "/a.md",
},
}
for i, c := range cases {
config.Root = c.root
p := NewPathFromLocal(c.in)
if p.HREF != c.href {
t.Fatal(i, "href", p.HREF, c.href, c, p)
}
if p.Local != c.local {
t.Fatal(i, "local", p.Local, c.local, c, p)
}
}
}

19
filetree/paths.go Executable file
View File

@@ -0,0 +1,19 @@
package filetree
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 {
content += path.LI() + "\n"
}
}
content += "</ul>\n"
return content
}

30
filetree/paths_test.go Executable file
View File

@@ -0,0 +1,30 @@
package filetree
import (
"strings"
"testing"
)
func TestPaths(t *testing.T) {
paths := Paths([]Path{
NewPathFromURL("/notes/a/b"),
NewPathFromURL("/notes/c/d"),
NewPathFromURL("/notes/e/f"),
})
list := paths.List()
if strings.Count(list, "<li>") != 3 {
t.Error(list)
}
if !strings.Contains(list, ">f") {
t.Error(list)
}
list = paths.List(true)
if strings.Count(list, "<li>") != 3 {
t.Error(list)
}
if !strings.Contains(list, ">/notes/a") {
t.Error(list)
}
}

16
main.go
View File

@@ -3,10 +3,12 @@ package main
import ( import (
"local/notes-server/config" "local/notes-server/config"
"local/notes-server/server" "local/notes-server/server"
"local/notes-server/versions"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time"
) )
func main() { func main() {
@@ -15,6 +17,20 @@ func main() {
panic(err) panic(err)
} }
go func() {
if config.VersionInterval == 0 {
log.Println("versions disabled")
return
}
versions, err := versions.New()
if err != nil {
panic(err)
}
for _ = range time.NewTicker(config.VersionInterval).C {
log.Println(versions.Gitmmit())
}
}()
go func() { go func() {
log.Printf("Serving %q on %q", config.Root, config.Port) log.Printf("Serving %q on %q", config.Root, config.Port)
if err := http.ListenAndServe(config.Port, server); err != nil { if err := http.ListenAndServe(config.Port, server); err != nil {

245
main_test.go Executable file
View File

@@ -0,0 +1,245 @@
package main
import (
"fmt"
"io/ioutil"
"local/notes-server/config"
"local/notes-server/server"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"strings"
"testing"
)
func TestAll(t *testing.T) {
for _, basedir := range []string{os.TempDir(), "./tempDir"} {
os.MkdirAll(basedir, os.ModePerm)
makeFiles(t, basedir)
if basedir[0] == '.' && config.Root[0] != '.' {
config.Root = "./" + config.Root
}
defer os.RemoveAll(config.Root)
log.Println(config.Root)
t.Log("trying with root", config.Root)
s := makeServer(t)
defer s.Close()
testServer(t, s.URL)
if basedir[0] == '.' {
os.RemoveAll(basedir)
}
}
}
func makeFiles(t *testing.T, basedir string) {
d, err := ioutil.TempDir(basedir, "pattern*")
if err != nil {
t.Fatal(err)
}
config.Root = d
for _, dir := range []string{"dirA", "dirB", "."} {
if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil {
t.Fatal(err)
}
for _, file := range []string{"fileA", "fileB"} {
content := fmt.Sprintf("hello from %s/%s/%s", d, dir, file)
err := ioutil.WriteFile(
path.Join(d, dir, file),
[]byte(content),
os.ModePerm,
)
if err != nil {
t.Fatal(err)
}
}
}
}
func makeServer(t *testing.T) *httptest.Server {
s := server.New()
if err := s.Routes(); err != nil {
t.Fatal(err)
}
return httptest.NewServer(s)
}
func testServer(t *testing.T, url string) {
testCreate(t, url)
testEdit(t, url)
testDir(t, url)
testFile(t, url)
testNavRootDir(t, url)
testNavRootFile(t, url)
testNavDirFile(t, url)
}
func testCreate(t *testing.T, url string) {
for _, path := range []string{"dirX/fileX", "fileX"} {
resp, err := http.Get(url + "/create/" + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, path); !ok {
t.Error(ok)
}
if ok := assertHasForm(s, "/submit/"+path); !ok {
t.Error(ok)
}
if ok := assertHasTextArea(s); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
}
func testEdit(t *testing.T, url string) {
for _, path := range []string{"dirX/fileX", "fileX"} {
resp, err := http.Get(url + "/edit/" + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, path); !ok {
t.Error(ok)
}
if ok := assertHasForm(s); !ok {
t.Error(ok)
}
if ok := assertHasTextArea(s); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
}
func testDir(t *testing.T, url string) {
path := url + "/notes/dirA"
resp, err := http.Get(path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, "/notes/dirA"); !ok {
t.Error(ok)
}
if ok := assertHasForm(s, "/create/dirA"); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
func testFile(t *testing.T, url string) {
path := url + "/notes/dirA/fileA"
resp, err := http.Get(path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, "/notes/dirA", "/notes/dirA/fileA"); !ok {
t.Error(ok)
}
if ok := assertHasHref(s, "/edit/dirA/fileA"); !ok {
t.Error(ok)
}
}
func testNavRootDir(t *testing.T, url string) {
resp, err := http.Get(url + "/notes")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/dirA"); !ok {
t.Fatal(s)
}
}
func testNavRootFile(t *testing.T, url string) {
resp, err := http.Get(url + "/notes")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/fileA"); !ok {
t.Fatal(s)
}
}
func testNavDirFile(t *testing.T, url string) {
resp, err := http.Get(url + "/notes/dirA")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/dirA/fileA"); !ok {
t.Fatal(s)
}
}
func assertHasMultilink(body string, segments ...string) bool {
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
return false
}
for i := range segments {
segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/")
}
return assertHasHref(body, segments...)
}
func assertHasForm(body string, action ...string) bool {
return strings.Contains(body, `<form`) && (len(action) == 0 || strings.Contains(body, `action="`))
}
func assertHasTextArea(body string) bool {
return strings.Contains(body, `<textarea`)
}
func assertHasSubmit(body string) bool {
return strings.Contains(body, `<button`) && strings.Contains(body, `type="submit"`)
}
func assertHasHref(body string, segments ...string) bool {
if !strings.Contains(body, `href="`) {
return false
}
for _, segment := range segments {
re := regexp.MustCompile(`a[^>]*href="` + segment + `"`)
if !re.MatchString(body) {
return false
}
}
return true
}

61
notes/comment.go Executable file
View File

@@ -0,0 +1,61 @@
package notes
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"local/notes-server/filetree"
"os"
"path"
"strings"
)
func (n *Notes) Comment(urlPath string, lineno int, comment string) error {
p := filetree.NewPathFromURL(urlPath)
if stat, err := os.Stat(p.Local); err != nil {
return errors.New("cannot comment as it does not exist")
} else if stat.IsDir() {
return errors.New("cannot comment on a dir")
}
f, err := os.Open(p.Local)
if err != nil {
return err
}
defer f.Close()
f2, err := ioutil.TempFile(os.TempDir(), path.Base(p.Local)+".*")
if err != nil {
return err
}
defer f2.Close()
reader := bufio.NewReader(f)
writer := io.Writer(f2)
for i := 0; i < lineno+1; i++ {
line, _, err := reader.ReadLine()
if err != nil {
return err
}
_, err = io.Copy(writer, bytes.NewReader(line))
if err != nil {
return err
}
_, err = writer.Write([]byte("\n"))
if err != nil {
return err
}
}
formatted := "\n"
formatted += fmt.Sprintf("> *%s*\n", comment)
_, err = io.Copy(writer, strings.NewReader(formatted))
if err != nil {
return err
}
_, err = io.Copy(writer, reader)
if err != nil {
return err
}
f2.Close()
return os.Rename(f2.Name(), p.Local)
}

56
notes/comment_test.go Normal file
View File

@@ -0,0 +1,56 @@
package notes
import (
"bytes"
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"strings"
"testing"
)
func TestComment(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "testComment.*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
f, err := ioutil.TempFile(d, "testFile.*")
if err != nil {
t.Fatal(err)
}
f.Write([]byte(`
hello
world
# i have a heading
## i have a subber heading
### i have a[heading with](http://google.com) a hyperlink
## *I think this heading is in italics*
`))
f.Close()
fpath := path.Join("/comment", strings.TrimPrefix(f.Name(), d))
config.Root = d
n := &Notes{}
t.Logf("d=%s, fpath=%s", d, fpath)
if err := n.Comment("/comment/a", 5, "a"); err == nil {
t.Error(err)
}
if err := n.Comment(fpath, -1, "illegal line no"); err != nil {
t.Error(err)
}
if err := n.Comment(fpath, 10000, "big line no"); err == nil {
t.Error(err)
}
if err := n.Comment(fpath, 0, "first_line_OK"); err != nil {
t.Error(err)
} else if b, err := ioutil.ReadFile(f.Name()); err != nil {
t.Error(err)
} else if !bytes.Contains(b, []byte("> *first_line_OK*\n")) {
t.Errorf("%s", b)
}
}

15
notes/create.go Executable file
View File

@@ -0,0 +1,15 @@
package notes
import (
"errors"
"local/notes-server/filetree"
"path"
)
func (n *Notes) Create(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("directory exists")
}
return path.Join("/edit/", p.BaseHREF), nil
}

18
notes/create_test.go Executable file
View File

@@ -0,0 +1,18 @@
package notes
import (
"local/notes-server/config"
"testing"
)
func TestCreate(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
resp, err := n.Create("/create/a")
if err != nil {
t.Error(err)
}
if resp != "/edit/a" {
t.Error(resp)
}
}

11
notes/delete.go Executable file
View File

@@ -0,0 +1,11 @@
package notes
import (
"local/notes-server/filetree"
"os"
)
func (n *Notes) Delete(urlPath string) error {
p := filetree.NewPathFromURL(urlPath)
return os.Remove(p.Local)
}

43
notes/delete_test.go Executable file
View File

@@ -0,0 +1,43 @@
package notes
import (
"io/ioutil"
"local/notes-server/config"
"os"
"testing"
)
func TestDelete(t *testing.T) {
config.Root = "/tmp"
ioutil.WriteFile("/tmp/a", []byte("hi"), os.ModePerm)
n := &Notes{}
if err := n.Delete("/notes/a"); err != nil {
t.Error(err)
}
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)
}
}

28
notes/dir.go Executable file
View File

@@ -0,0 +1,28 @@
package notes
import (
"errors"
"io/ioutil"
"local/notes-server/filetree"
)
func (n *Notes) Dir(urlPath string) (string, string, error) {
p := filetree.NewPathFromURL(urlPath)
if !p.IsDir() {
return "", "", errors.New("not a dir")
}
dirs, files := n.lsDir(p)
return dirs.List(), files.List(), nil
}
func (n *Notes) lsDir(path filetree.Path) (filetree.Paths, filetree.Paths) {
dirs := filetree.NewDirs()
files := filetree.NewFiles()
found, _ := ioutil.ReadDir(path.Local)
for _, f := range found {
dirs.Push(path, f)
files.Push(path, f)
}
return filetree.Paths(*dirs), filetree.Paths(*files)
}

36
notes/dir_test.go Executable file
View File

@@ -0,0 +1,36 @@
package notes
import (
"local/notes-server/config"
"testing"
)
func TestDir(t *testing.T) {
n := &Notes{}
config.Root = "/"
dirs, files, err := n.Dir("/notes/usr/local")
if err != nil {
t.Fatal(err)
}
if len(dirs) == 0 {
t.Fatal(len(dirs))
}
if len(files) == 0 {
t.Fatal(len(files))
}
t.Log(dirs)
t.Log(files)
}
func TestNotesDir(t *testing.T) {
n := &Notes{}
body, body2, err := n.Dir("/notes/usr/local")
if err != nil {
t.Fatal(err)
}
if body == "" || body2 == "" {
t.Fatal(body, body2)
}
t.Logf("%s", body)
t.Logf("%s", body2)
}

36
notes/edit.go Executable file
View File

@@ -0,0 +1,36 @@
package notes
import (
"errors"
"fmt"
"io/ioutil"
"local/notes-server/filetree"
"strings"
)
func (n *Notes) Edit(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
return editFile(p), nil
}
func editFile(p filetree.Path) string {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
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%%; cursor:crosshair;">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

21
notes/edit_test.go Executable file
View File

@@ -0,0 +1,21 @@
package notes
import (
"local/notes-server/config"
"strings"
"testing"
)
func TestEdit(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
if body, err := n.Edit("/notes/a"); err != nil {
t.Error(err)
} else if !strings.Contains(body, "/submit/a") {
t.Error(body)
}
config.Root = "/usr"
if _, err := n.Edit("/notes/local"); err == nil {
t.Error(err)
}
}

106
notes/file.go Executable file
View File

@@ -0,0 +1,106 @@
package notes
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"local/notes-server/filetree"
"log"
"path"
"regexp"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
func (n *Notes) File(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
b, _ := ioutil.ReadFile(p.Local)
renderer := html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.TOC,
RenderNodeHook: n.commentFormer(urlPath, b),
})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
content := markdown.ToHTML(b, parser, renderer)
return string(content) + "\n", nil
}
func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
urlPath = strings.TrimPrefix(urlPath, "/")
urlPath = strings.TrimPrefix(urlPath, strings.Split(urlPath, "/")[0])
lines := bytes.Split(md, []byte("\n"))
cur := -1
nextHeader := func() {
cur++
for cur < len(lines) {
if bytes.Contains(lines[cur], []byte("```")) {
cur++
for cur < len(lines) && !bytes.Contains(lines[cur], []byte("```")) {
cur++
}
cur++
}
if cur >= len(lines) {
break
}
line := lines[cur]
if ok, err := regexp.Match(`^\s*#+\s*[^\s]+\s*$`, line); err != nil {
panic(err)
} else if ok {
return
}
cur++
}
}
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
if heading, ok := node.(*ast.Heading); ok && !entering {
log.Printf("%+v", heading)
nextHeader()
fmt.Fprintf(w, `
<form method="POST" action=%q class="comment">
<input name="lineno" type="number" style="display:none" value="%d"/>
<input autocomplete="off" name="content" type="text"/>
<input type="submit"/>
</form>
`, path.Join("/comment", urlPath)+"#"+heading.HeadingID, cur)
}
return ast.GoToNext, false
}
}
func (n *Notes) commentFormerOld() html.RenderNodeFunc {
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
if heading, ok := node.(*ast.Heading); ok {
if !entering {
literal := ""
ast.WalkFunc(heading, func(n ast.Node, e bool) ast.WalkStatus {
if leaf := n.AsLeaf(); e && leaf != nil {
if literal != "" {
literal += ".*"
}
literal += string(leaf.Literal)
}
return ast.GoToNext
})
level := heading.Level
id := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf(`^[ \t]*%s\s*%s(].*)?\s*$`, strings.Repeat("#", level), literal)))
fmt.Fprintf(w, `
<form method="POST" target="/comment" name="%s" class="comment">
<input autocomplete="off" name="content" type="text"/>
<input type="submit"/>
</form>
`, id)
}
}
return ast.GoToNext, false
}
}

49
notes/file_test.go Executable file
View File

@@ -0,0 +1,49 @@
package notes
import (
"fmt"
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"strings"
"testing"
)
func TestFile(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "until*")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
fmt.Fprintln(f, `
# Hello
## World
* This
* is
* bullets
| My | table | goes |
|----|-------|------|
| h | e | n |
`)
f.Close()
n := &Notes{}
config.Root = "/"
s, err := n.File(path.Join("notes", f.Name()))
if err != nil {
t.Fatal(err)
}
shouldContain := []string{
"tbody",
"h1",
"h2",
}
for _, should := range shouldContain {
if !strings.Contains(s, should) {
t.Fatalf("%s: %s", should, s)
}
}
t.Logf("%s", s)
}

13
notes/notes.go Executable file
View File

@@ -0,0 +1,13 @@
package notes
import "local/notes-server/config"
type Notes struct {
root string
}
func New() *Notes {
return &Notes{
root: config.Root,
}
}

1
notes/notes_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

96
notes/search.go Executable file
View File

@@ -0,0 +1,96 @@
package notes
import (
"bufio"
"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,
func(walked string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
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, searcher *searcher) (bool, error) {
f, err := os.Open(file)
if err != nil {
return false, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if searcher.matches(scanner.Bytes()) {
return true, scanner.Err()
}
}
return false, scanner.Err()
}

54
notes/search_test.go Executable file
View File

@@ -0,0 +1,54 @@
package notes
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestSearch(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "pattern*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
for i := 0; i < 5; i++ {
f, err := ioutil.TempFile(d, fmt.Sprintf("file_%d", i))
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(f, "this file is number %d", i)
f.Close()
}
n := New()
n.root = d
result, err := n.Search("this file")
if err != nil {
t.Fatal(err)
}
if v := len(strings.Split(result, "\n")); v < 7 {
t.Fatal(v, result)
}
result, err = n.Search("4")
if err != nil {
t.Fatal(err)
}
if v := len(strings.Split(result, "\n")); v > 4 {
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)
}
}

14
notes/submit.go Executable file
View File

@@ -0,0 +1,14 @@
package notes
import (
"io/ioutil"
"local/notes-server/filetree"
"os"
"path"
)
func (n *Notes) Submit(urlPath, content string) error {
p := filetree.NewPathFromURL(urlPath)
os.MkdirAll(path.Dir(p.Local), os.ModePerm)
return ioutil.WriteFile(p.Local, []byte(content), os.ModePerm)
}

22
notes/submit_test.go Executable file
View File

@@ -0,0 +1,22 @@
package notes
import (
"io/ioutil"
"local/notes-server/config"
"os"
"testing"
)
func TestSubmit(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
if err := n.Submit("/submit/a", "a"); err != nil {
t.Error(err)
} else if b, err := ioutil.ReadFile("/tmp/a"); err != nil {
t.Error(err)
} else if string(b) != "a" {
t.Error(string(b))
} else if err := os.Remove("/tmp/a"); err != nil {
t.Error(err)
}
}

18
public/A/asdf Executable file
View File

@@ -0,0 +1,18 @@
# h1
hi
## h2
hi
### h3
hi
#### h4
hi
* bullet
* 1

5
public/B/y Executable file
View File

@@ -0,0 +1,5 @@
## B.y
| hello | world |
|-------|-------|
| cont | ent. |

8
public/B/z Executable file
View File

@@ -0,0 +1,8 @@
## B.z
| hello | world |
|-------|-------|
| cont | ent. |
HI

3
public/D/E/F/g Executable file
View File

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

24
server/.notes/create.go Executable file
View File

@@ -0,0 +1,24 @@
package notes
import (
"html"
"local/notes-server/filetree"
"net/http"
"path"
"strings"
)
func (n *Notes) Create(w http.ResponseWriter, r *http.Request) {
content := r.FormValue("base")
content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "")
urlPath := path.Join(r.URL.Path, content)
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
w.WriteHeader(http.StatusBadRequest)
return
}
url := *r.URL
url.Path = path.Join("/edit/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}

1
server/.notes/create_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"net/http/httptest" "net/http/httptest"

43
server/.notes/edit.go Executable file
View File

@@ -0,0 +1,43 @@
package notes
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func (s *Server) edit(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path)
if p.IsDir() {
http.NotFound(w, r)
return
}
head(w, r)
editHead(w, p)
editFile(w, p)
foot(w, r)
}
func editHead(w http.ResponseWriter, p Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
}
func editFile(w http.ResponseWriter, p Path) {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
fmt.Fprintf(w, `
<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>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

1
server/.notes/edit_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

13
server/.notes/notes.go Executable file
View File

@@ -0,0 +1,13 @@
package notes
import "local/notes-server/config"
type Notes struct {
root string
}
func New() *Notes {
return &Notes{
root: config.Root,
}
}

27
server/.notes/rnotes.go Executable file
View File

@@ -0,0 +1,27 @@
package notes
import (
"fmt"
"net/http"
)
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path)
if p.IsDir() {
head(w, r)
notesHead(w, p)
notesDir(p, w, r)
foot(w, r)
} else if p.IsFile() {
head(w, r)
notesHead(w, p)
notesFile(p, w, r)
foot(w, r)
} else {
http.NotFound(w, r)
}
}
func notesHead(w http.ResponseWriter, p Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
}

1
server/.notes/rnotes_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

31
server/.notes/submit.go Executable file
View File

@@ -0,0 +1,31 @@
package notes
import (
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
)
func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
content := r.FormValue("content")
content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "")
p := NewPathFromURL(r.URL.Path)
os.MkdirAll(path.Dir(p.Local), os.ModePerm)
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
} else {
url := *r.URL
url.Path = path.Join("/notes/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}
}

1
server/.notes/submit_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

37
server/comment.go Executable file
View File

@@ -0,0 +1,37 @@
package server
import (
"html"
"local/notes-server/filetree"
"log"
"net/http"
"path"
"strconv"
"strings"
)
func (s *Server) comment(w http.ResponseWriter, r *http.Request) {
log.Println("COMMAND", r.Method, r.FormValue("lineno"), r.FormValue("content"))
if r.Method != "POST" {
http.NotFound(w, r)
return
}
linenos := r.FormValue("lineno")
lineno, err := strconv.Atoi(linenos)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
comment := r.FormValue("content")
comment = html.UnescapeString(comment)
comment = strings.ReplaceAll(comment, "\r", "")
err = s.Notes.Comment(r.URL.Path, lineno, comment)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := *r.URL
url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}

View File

@@ -12,12 +12,12 @@ func (s *Server) create(w http.ResponseWriter, r *http.Request) {
content = html.UnescapeString(content) content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "") content = strings.ReplaceAll(content, "\r", "")
urlPath := path.Join(r.URL.Path, content) urlPath := path.Join(r.URL.Path, content)
p := NewPathFromURL(urlPath) url := *r.URL
if p.IsDir() { path, err := s.Notes.Create(urlPath)
w.WriteHeader(http.StatusBadRequest) if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
url := *r.URL url.Path = path
url.Path = path.Join("/edit/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther) http.Redirect(w, r, url.String(), http.StatusSeeOther)
} }

View File

@@ -1 +0,0 @@
package server

16
server/delete.go Executable file
View File

@@ -0,0 +1,16 @@
package server
import (
"net/http"
"path"
"strings"
)
func (s *Server) delete(w http.ResponseWriter, r *http.Request) {
if err := s.Notes.Delete(r.URL.Path); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
r.URL.Path = strings.Replace(path.Dir(r.URL.Path), "delete", "notes", 1)
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -2,42 +2,22 @@ package server
import ( import (
"fmt" "fmt"
"io/ioutil" "local/notes-server/filetree"
"net/http" "net/http"
"strings"
) )
func (s *Server) edit(w http.ResponseWriter, r *http.Request) { func (s *Server) edit(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path) head(w, r)
if p.IsDir() { editHead(w, filetree.NewPathFromURL(r.URL.Path))
http.NotFound(w, r) edit, err := s.Notes.Edit(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
head(w, r) fmt.Fprintln(w, edit)
editHead(w, p)
editFile(w, p)
foot(w, r) foot(w, r)
} }
func editHead(w http.ResponseWriter, p Path) { func editHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink())) fmt.Fprintln(w, h2(p.MultiLink()))
} }
func editFile(w http.ResponseWriter, p Path) {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
fmt.Fprintf(w, `
<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>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -1 +0,0 @@
package server

47
server/html.go Executable file
View File

@@ -0,0 +1,47 @@
package server
import (
"fmt"
"local/notes-server/config"
"net/http"
)
func head(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.Head + "\n"))
}
func foot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.Foot + "\n"))
}
func block(w http.ResponseWriter, content string) {
fmt.Fprintf(w, "\n<div>\n%s\n</div>\n", content)
}
func h1(content string) string {
return h("1", content)
}
func h2(content string, style ...string) string {
return h("2", content, style...)
}
func h3(content string) string {
return h("3", content)
}
func h4(content string) string {
return h("4", content)
}
func h5(content string) string {
return h("5", content)
}
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

@@ -28,7 +28,7 @@ func TestFoot(t *testing.T) {
func TestBlock(t *testing.T) { func TestBlock(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
block("hi", w) block(w, "hi")
s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".") s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".")
if ok, err := regexp.MatchString("<div>.*hi.*<.div>", s); err != nil { if ok, err := regexp.MatchString("<div>.*hi.*<.div>", s); err != nil {
t.Fatal(err, s) t.Fatal(err, s)
@@ -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

@@ -2,26 +2,97 @@ package server
import ( import (
"fmt" "fmt"
"local/notes-server/config"
"local/notes-server/filetree"
"net/http" "net/http"
"path"
) )
func (s *Server) notes(w http.ResponseWriter, r *http.Request) { func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path) p := filetree.NewPathFromURL(r.URL.Path)
head(w, r)
notesHead(w, p)
defer foot(w, r)
if p.IsDir() { if p.IsDir() {
head(w, r) s.dir(w, r)
notesHead(w, p)
notesDir(p, w, r)
foot(w, r)
} else if p.IsFile() { } else if p.IsFile() {
head(w, r) s.file(w, r)
notesHead(w, p)
notesFile(p, w, r)
foot(w, r)
} else { } else {
http.NotFound(w, r) http.NotFound(w, r)
} }
} }
func notesHead(w http.ResponseWriter, p Path) { func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink())) fmt.Fprintln(w, h2(p.MultiLink()))
htmlSearch(w)
}
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
dirs, files, err := s.Notes.Dir(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
dirHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
block(w, dirs)
block(w, files)
}
func dirHead(w http.ResponseWriter, baseHREF string) {
htmlCreate(w, baseHREF)
htmlDelete(w, baseHREF)
}
func (s *Server) file(w http.ResponseWriter, r *http.Request) {
file, err := s.Notes.File(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fileHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
fmt.Fprintln(w, file)
}
func fileHead(w http.ResponseWriter, baseHREF string) {
htmlEdit(w, baseHREF)
htmlDelete(w, baseHREF)
}
func htmlEdit(w http.ResponseWriter, baseHREF string) {
if config.ReadOnly {
return
}
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) {
if config.ReadOnly {
return
}
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) {
if config.ReadOnly {
return
}
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" >
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
} }

View File

@@ -1 +0,0 @@
package server

View File

@@ -1,23 +0,0 @@
package server
import "testing"
func TestPathIs(t *testing.T) {
p := Path{Local: "/dev/null"}
if ok := p.IsDir(); ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); !ok {
t.Fatal(ok, p)
}
p = Path{Local: "/tmp"}
if ok := p.IsDir(); !ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); ok {
t.Fatal(ok, p)
}
}

View File

@@ -1,12 +0,0 @@
package server
type Paths []Path
func (p Paths) List() string {
content := "<ul>\n"
for _, path := range p {
content += path.LI() + "\n"
}
content += "</ul>\n"
return content
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -2,38 +2,75 @@ package server
import ( import (
"fmt" "fmt"
"local/gziphttp"
"local/notes-server/config"
"local/router" "local/router"
"net/http" "net/http"
"path/filepath"
"strings"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
wildcard := router.Wildcard wildcard := router.Wildcard
endpoints := []struct { endpoints := map[string]struct {
path string
handler http.HandlerFunc handler http.HandlerFunc
}{ }{
{ "/": {
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard), handler: s.root,
handler: s.notes,
}, },
{ fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard), handler: s.gzip(s.authenticate(s.notes)),
handler: s.edit,
}, },
{ fmt.Sprintf("edit/%s%s", wildcard, wildcard): {
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard), handler: s.gzip(s.authenticate(s.edit)),
handler: s.submit,
}, },
{ fmt.Sprintf("delete/%s%s", wildcard, wildcard): {
path: fmt.Sprintf("create/%s%s", wildcard, wildcard), handler: s.gzip(s.authenticate(s.delete)),
handler: s.create, },
fmt.Sprintf("submit/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.submit)),
},
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.create)),
},
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.comment)),
},
fmt.Sprintf("search"): {
handler: s.gzip(s.authenticate(s.search)),
}, },
} }
for _, endpoint := range endpoints { for path, endpoint := range endpoints {
if err := s.Add(endpoint.path, endpoint.handler); err != nil { if config.ReadOnly {
for _, prefix := range []string{"edit/", "delete/", "delete/", "create/", "submit/"} {
if strings.HasPrefix(path, prefix) {
endpoint.handler = http.NotFound
}
}
}
if err := s.Add(path, endpoint.handler); err != nil {
return err return err
} }
} }
return nil return nil
} }
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)
}
}

View File

@@ -37,7 +37,7 @@ func TestServerNotes(t *testing.T) {
t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes()) t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes())
} }
func TestServerNotes(t *testing.T) { func TestServerNotesB(t *testing.T) {
s := New() s := New()
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := &http.Request{ r := &http.Request{

27
server/search.go Executable file
View File

@@ -0,0 +1,27 @@
package server
import (
"fmt"
"html"
"local/notes-server/filetree"
"net/http"
)
func (s *Server) search(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
keywords := r.FormValue("keywords")
keywords = html.UnescapeString(keywords)
results, err := s.Notes.Search(keywords)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
head(w, r)
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
htmlSearch(w)
fmt.Fprintln(w, h1(keywords), results)
foot(w, r)
}

View File

@@ -1,54 +1,35 @@
package server package server
import ( import (
"fmt"
"local/notes-server/config" "local/notes-server/config"
"local/notes-server/notes"
"local/oauth2/oauth2client"
"local/router" "local/router"
"log"
"net/http" "net/http"
) )
type Server struct { type Server struct {
*router.Router *router.Router
Notes *notes.Notes
} }
func New() *Server { func New() *Server {
return &Server{ return &Server{
Router: router.New(), Router: router.New(),
Notes: notes.New(),
} }
} }
func head(w http.ResponseWriter, r *http.Request) { func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc {
w.Write([]byte(config.Head + "\n")) return func(w http.ResponseWriter, r *http.Request) {
if config.OAuthServer != "" {
err := oauth2client.Authenticate(config.OAuthServer, "notes-server", w, r)
if err != nil {
log.Println(err)
return
} }
func foot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.Foot + "\n"))
} }
foo(w, r)
func block(content string, w http.ResponseWriter) {
fmt.Fprintf(w, "\n<div>\n%s\n</div>\n", content)
} }
func h1(content string) string {
return h("1", content)
}
func h2(content string) string {
return h("2", content)
}
func h3(content string) string {
return h("3", content)
}
func h4(content string) string {
return h("4", content)
}
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)
} }

View File

@@ -1,11 +1,9 @@
package server package server
import ( import (
"fmt"
"html" "html"
"io/ioutil" "local/notes-server/filetree"
"net/http" "net/http"
"os"
"path" "path"
"strings" "strings"
) )
@@ -18,14 +16,12 @@ func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
content := r.FormValue("content") content := r.FormValue("content")
content = html.UnescapeString(content) content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "") content = strings.ReplaceAll(content, "\r", "")
p := NewPathFromURL(r.URL.Path) err := s.Notes.Submit(r.URL.Path, content)
os.MkdirAll(path.Dir(p.Local), os.ModePerm) if err != nil {
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError) return
fmt.Fprintln(w, err) }
} else {
url := *r.URL url := *r.URL
url.Path = path.Join("/notes/", p.BaseHREF) url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther) http.Redirect(w, r, url.String(), http.StatusSeeOther)
} }
}

View File

@@ -1 +0,0 @@
package server

67
versions/.versions.go Executable file
View File

@@ -0,0 +1,67 @@
package versions
import (
"local/notes-server/config"
"time"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type Versions struct {
repo *git.Repository
}
func New() (*Versions, error) {
repo, err := git.PlainInit(config.Root, false)
if err != nil {
repo, err = git.PlainOpen(config.Root)
}
return &Versions{
repo: repo,
}, err
}
func (v *Versions) Gitmmit() error {
if err := v.AddAll(); err != nil {
return err
}
if err := v.Commit(); err != nil {
return err
}
return nil
}
func (v *Versions) AddAll() error {
worktree, err := v.worktree()
if err != nil {
return err
}
for _, path := range []string{".", "./*", "./**", "/", "/**", "/*"} {
if err := worktree.AddGlob(path); err != nil {
return err
}
}
return nil
}
func (v *Versions) Commit() error {
worktree, err := v.worktree()
if err != nil {
return err
}
opts := &git.CommitOptions{Author: &object.Signature{}}
_, err = worktree.Commit(time.Now().String(), opts)
return err
}
func (v *Versions) worktree() (*git.Worktree, error) {
worktree, err := v.repo.Worktree()
if err != nil {
return nil, err
}
return worktree, nil
}

65
versions/.versions_test.go Executable file
View File

@@ -0,0 +1,65 @@
package versions
import (
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"testing"
)
func TestVersionsHappy(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "prefix")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
if err := ioutil.WriteFile(path.Join(d, "a.md"), []byte("# Hello"), os.ModePerm); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(d, "b.md"), []byte("# World"), os.ModePerm); err != nil {
t.Fatal(err)
}
config.Root = d
v, err := New()
if err != nil {
t.Error(err)
}
if err := v.AddAll(); err != nil {
t.Error("failed add", err)
}
if err := v.Commit(); err != nil {
t.Error("failed commit", err)
}
if err := v.Gitmmit(); err != nil {
t.Error("failed gitmmit", err)
}
}
func TestVersionsBad(t *testing.T) {
config.Root = "/not/a/real/path"
if _, err := New(); err == nil {
t.Error("passed new from nil path")
}
}
func TestVersionsDirty(t *testing.T) {
if os.Getenv("DIRTY") == "" {
return
}
config.Root = "/tmp/foo"
v, err := New()
if err != nil {
t.Fatal(err)
}
ioutil.WriteFile(path.Join(config.Root, "file.md"), []byte(`
# Hello
## World
I'm a doc
`), os.ModePerm)
if err := v.Gitmmit(); err != nil {
t.Fatal(err)
}
t.Log(v)
}

44
versions/versions.go Executable file
View File

@@ -0,0 +1,44 @@
package versions
import (
"fmt"
"local/notes-server/config"
"os/exec"
"time"
)
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
}
func (v *Versions) Gitmmit() error {
if err := v.AddAll(); err != nil {
return fmt.Errorf("cannot add all: %v", err)
}
if err := v.Commit(); err != nil {
return fmt.Errorf("cannot commit: %v", err)
}
return nil
}
func (v *Versions) AddAll() error {
return v.cmd("git", "add", "-A", ":/")
}
func (v *Versions) Commit() error {
return v.cmd("git", "commit", "-m", time.Now().String())
}
func (v *Versions) cmd(cmd string, args ...string) error {
command := exec.Command(cmd, args...)
command.Dir = config.Root
_, err := command.CombinedOutput()
return err
}

42
versions/versions_test.go Executable file
View File

@@ -0,0 +1,42 @@
package versions
import (
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"testing"
)
func TestVersions(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "prefix")
config.Root = d
if err != nil {
t.Fatal(err)
}
for _, f := range []string{"a", "b"} {
ioutil.WriteFile(path.Join(d, f+".md"), []byte(f), os.ModePerm)
}
v, err := New()
if err != nil {
t.Fatal(err)
}
if err := v.AddAll(); err != nil {
t.Error(err)
}
if err := v.Commit(); err != nil {
t.Error(err)
}
if err := v.Gitmmit(); err == nil {
t.Error(err)
}
for _, f := range []string{"c", "b"} {
ioutil.WriteFile(path.Join(d, f+".md"), []byte("d"), os.ModePerm)
}
if err := v.Gitmmit(); err != nil {
t.Error(config.Root, err)
}
}

View File

@@ -1,25 +0,0 @@
<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;
}
img {
max-height: 400px;
}
</style>
</header>
<body height="100%">
{{{}}}
</body>
<footer>
</footer>
</html>