Basic I think
parent
019992c4c2
commit
3132ce1a1d
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("<li><a href=%q>", p.String())
|
||||
content += p.base
|
||||
content += "</li>"
|
||||
return content
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
package server
|
||||
|
|
@ -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<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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("<div>.*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("<h2>.*hi.*<.h2>", s); err != nil {
|
||||
t.Fatal(err, s)
|
||||
} else if !ok {
|
||||
t.Fatal(ok, s)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue