Split into packages only manually tested

This commit is contained in:
Bel LaPointe
2019-11-09 18:51:03 -07:00
parent 0d497f4fa8
commit 60dc6bc876
48 changed files with 530 additions and 125 deletions

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
}

1
notes/create_test.go Executable file
View File

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

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%%">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

1
notes/edit_test.go Executable file
View File

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

25
notes/file.go Executable file
View File

@@ -0,0 +1,25 @@
package notes
import (
"errors"
"io/ioutil"
"local/notes-server/filetree"
"github.com/gomarkdown/markdown"
"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,
})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
content := markdown.ToHTML(b, parser, renderer)
return string(content) + "\n", nil
}

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 Normal 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,
}
}

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

1
notes/submit_test.go Executable file
View File

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