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("
  • ", p.String()) + content += p.base + content += "
  • " + return content +} diff --git a/server/path_test.go b/server/path_test.go index abb4e43..052e824 100644 --- a/server/path_test.go +++ b/server/path_test.go @@ -1 +1,23 @@ package server + +import ( + "regexp" + "testing" +) + +func TestPathLI(t *testing.T) { + p := Path{ + dir: "a/b/c", + base: "d", + } + link := p.LI() + ok, err := regexp.MatchString(`li.*a.*href="a/b/c/d".d..li`, link) + if err != nil { + t.Fatal(err, link) + } + if !ok { + t.Fatal(ok, link) + } + + t.Log(p, link) +} diff --git a/server/paths.go b/server/paths.go new file mode 100644 index 0000000..022ea5d --- /dev/null +++ b/server/paths.go @@ -0,0 +1,12 @@ +package server + +type Paths []Path + +func (p Paths) List() string { + content := "\n" + return content +} diff --git a/server/paths_test.go b/server/paths_test.go new file mode 100644 index 0000000..abb4e43 --- /dev/null +++ b/server/paths_test.go @@ -0,0 +1 @@ +package server diff --git a/server/server.go b/server/server.go index d85977e..efd72ff 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "local/notes-server/config" "local/router" "net/http" @@ -23,3 +24,31 @@ func head(w http.ResponseWriter, r *http.Request) { func foot(w http.ResponseWriter, r *http.Request) { w.Write([]byte(config.Foot + "\n")) } + +func block(content string, w http.ResponseWriter) { + fmt.Fprintf(w, "\n
    \n%s\n
    \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\n%s\n\n", level, content, level) +} diff --git a/server/server_test.go b/server/server_test.go index 62aa97a..50afffa 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -3,6 +3,7 @@ package server import ( "local/notes-server/config" "net/http/httptest" + "regexp" "strings" "testing" ) @@ -24,3 +25,23 @@ func TestFoot(t *testing.T) { t.Fatal(s) } } + +func TestBlock(t *testing.T) { + w := httptest.NewRecorder() + block("hi", w) + s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".") + if ok, err := regexp.MatchString("
    .*hi.*<.div>", s); err != nil { + t.Fatal(err, s) + } else if !ok { + t.Fatal(ok, s) + } +} + +func TestH(t *testing.T) { + s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".") + if ok, err := regexp.MatchString("

    .*hi.*<.h2>", s); err != nil { + t.Fatal(err, s) + } else if !ok { + t.Fatal(ok, s) + } +}