diff --git a/livemd.go b/livemd.go index 24c5257..9b2089a 100644 --- a/livemd.go +++ b/livemd.go @@ -11,6 +11,7 @@ import ( "text/template" "github.com/russross/blackfriday" + "golang.org/x/net/websocket" fsnotify "gopkg.in/fsnotify.v1" ) @@ -19,6 +20,7 @@ var SUFFIXES = [3]string{".md", ".mkd", ".markdown"} var toc []string var tocMutex sync.Mutex var rootTmpl *template.Template +var pageTmpl *template.Template var path string func init() { @@ -27,6 +29,10 @@ func init() { if err != nil { log.Fatal(err) } + pageTmpl, err = template.New("page").Parse(pageTemplate) + if err != nil { + log.Fatal(err) + } } func HasMarkdownSuffix(s string) bool { @@ -65,6 +71,8 @@ func WatcherEventLoop(w *fsnotify.Watcher, done chan bool) { log.Println("Event:", event) // TODO(barakmich): On directory creation, stat path if directory, and watch it. if HasMarkdownSuffix(event.Name) { + if event.Op == fsnotify.Write { + } } case err := <-w.Errors: @@ -87,6 +95,17 @@ func RootFunc(w http.ResponseWriter, r *http.Request) { rootTmpl.Execute(w, string(bytes)) } +func PageFunc(w http.ResponseWriter, r *http.Request) { + subpath := strings.TrimPrefix(r.RequestURI, "/md") + log.Println("New watcher on ", subpath) + pageTmpl.Execute(w, subpath) +} + +func HandleListener(ws *websocket.Conn) { + fmt.Println("WEBSOCKET!", ws.Request().RequestURI) + ws.Close() +} + func main() { path = os.Getenv("PWD") if len(os.Args) > 1 { @@ -111,5 +130,7 @@ func main() { fmt.Println(toc) http.HandleFunc("/", RootFunc) + http.HandleFunc("/md/", PageFunc) + http.Handle("/ws/", websocket.Handler(HandleListener)) http.ListenAndServe(":8080", nil) } diff --git a/page_template.go b/page_template.go new file mode 100644 index 0000000..de450be --- /dev/null +++ b/page_template.go @@ -0,0 +1,26 @@ +package main + +var pageTemplate = ` + + +LiveMarkdown + + + + + + +`