diff --git a/server/dir.go b/server/dir.go index 87f77a1..dc0541c 100644 --- a/server/dir.go +++ b/server/dir.go @@ -1,8 +1,8 @@ package server import ( + "fmt" "io/ioutil" - "log" "net/http" "os" ) @@ -14,11 +14,14 @@ func isDir(path string) bool { func notesDir(path string, w http.ResponseWriter, r *http.Request) { dirs, files := lsDir(path) - log.Println(dirs) - log.Println(files) + content := "" + content += h1(path) + content += dirs.List() + block(content, w) + fmt.Fprintln(w, files.List()) } -func lsDir(p string) ([]Path, []Path) { +func lsDir(p string) (Paths, Paths) { dirs := newDirs() files := newFiles() @@ -27,5 +30,5 @@ func lsDir(p string) ([]Path, []Path) { dirs.Push(p, f) files.Push(p, f) } - return *dirs, *files + return Paths(*dirs), Paths(*files) } diff --git a/server/dir_test.go b/server/dir_test.go index b534ac2..a12ad8d 100644 --- a/server/dir_test.go +++ b/server/dir_test.go @@ -1,6 +1,7 @@ package server import ( + "net/http/httptest" "testing" ) @@ -27,3 +28,10 @@ func TestLsDir(t *testing.T) { t.Log(dirs) t.Log(files) } + +func TestNotesDir(t *testing.T) { + path := "/usr/local" + w := httptest.NewRecorder() + notesDir(path, w, nil) + t.Logf("%s", w.Body.Bytes()) +} diff --git a/server/file.go b/server/file.go index 936fd91..0fba66b 100644 --- a/server/file.go +++ b/server/file.go @@ -1,8 +1,12 @@ package server import ( + "fmt" + "io/ioutil" "net/http" "os" + + "github.com/gomarkdown/markdown" ) func isFile(path string) bool { @@ -11,4 +15,9 @@ func isFile(path string) bool { } func notesFile(path string, w http.ResponseWriter, r *http.Request) { + title := h1(path) + block(title, w) + b, _ := ioutil.ReadFile(path) + content := markdown.ToHTML(b, nil, nil) + fmt.Fprintf(w, "%s\n", content) } diff --git a/server/file_test.go b/server/file_test.go index 7ac1556..fc7d836 100644 --- a/server/file_test.go +++ b/server/file_test.go @@ -1,6 +1,11 @@ package server import ( + "fmt" + "io/ioutil" + "net/http/httptest" + "os" + "strings" "testing" ) @@ -15,3 +20,38 @@ func TestIsFile(t *testing.T) { t.Fatal(ok) } } + +func TestNotesFile(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() + w := httptest.NewRecorder() + notesFile(f.Name(), w, nil) + s := string(w.Body.Bytes()) + shouldContain := []string{ + "tbody", + "h1", + "h2", + } + for _, should := range shouldContain { + if !strings.Contains(s, should) { + t.Fatalf("%s: %s", should, s) + } + } + t.Logf("%s", s) +} diff --git a/server/path.go b/server/path.go index 44bdab9..4ce1b0d 100644 --- a/server/path.go +++ b/server/path.go @@ -1,6 +1,9 @@ package server -import "path" +import ( + "fmt" + "path" +) type Path struct { dir string @@ -10,3 +13,10 @@ type Path struct { func (p Path) String() string { return path.Join(p.dir, p.base) } + +func (p Path) LI() string { + content := fmt.Sprintf("