From 898cbafe87a2ab4ff347ebaf9e4b7d04506ad764 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 18 Feb 2016 00:52:10 +0100 Subject: Switch to Markdown --- del.go | 17 ---------------- edit.go | 18 ----------------- handler.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ home.go | 11 ----------- index.go | 29 --------------------------- parse.go | 61 -------------------------------------------------------- tmpl/doc | 35 -------------------------------- view.go | 22 --------------------- wiki.go | 8 +++++++- 9 files changed, 74 insertions(+), 194 deletions(-) delete mode 100644 del.go delete mode 100644 edit.go create mode 100644 handler.go delete mode 100644 home.go delete mode 100644 index.go delete mode 100644 parse.go delete mode 100644 tmpl/doc delete mode 100644 view.go diff --git a/del.go b/del.go deleted file mode 100644 index a106129..0000000 --- a/del.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import "net/http" - -func init() { - http.HandleFunc("/del/", delHandler) -} - -func delHandler(w http.ResponseWriter, r *http.Request) { - title := r.URL.Path[len("/del/"):] - p := &Page{Title: title} - if err := p.del(); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - http.Redirect(w, r, "/", http.StatusFound) -} diff --git a/edit.go b/edit.go deleted file mode 100644 index 3a4ca37..0000000 --- a/edit.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "net/http" - "text/template" -) - -func init() { - http.HandleFunc("/edit/", editHandler) -} - -var edittmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/edit")) - -func editHandler(w http.ResponseWriter, r *http.Request) { - title := r.URL.Path[len("/edit/"):] - p, _ := loadPage(title) - p.render(w, edittmpl) -} diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..04d6d94 --- /dev/null +++ b/handler.go @@ -0,0 +1,67 @@ +package main + +import ( + "io/ioutil" + "net/http" + "text/template" +) + +func init() { + http.HandleFunc("/index", indexHandler) + http.HandleFunc("/view/", viewHandler) + http.HandleFunc("/edit/", editHandler) + http.HandleFunc("/del/", delHandler) + http.HandleFunc("/", homeHandler) +} + +var ( + indextmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/index")) + viewtmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/view")) + edittmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/edit")) +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + files, err := ioutil.ReadDir("data") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + p := new(Page) + for _, entry := range files { + file := entry.Name() + if !entry.IsDir() && file[0] != '.' { + p.Pages = append(p.Pages, entry) + } + } + p.render(w, indextmpl) +} + +func viewHandler(w http.ResponseWriter, r *http.Request) { + title := r.URL.Path[len("/view/"):] + p, err := loadPage(title) + if err != nil { + http.Redirect(w, r, "/edit/"+title, http.StatusFound) + return + } + p.render(w, viewtmpl) +} + +func editHandler(w http.ResponseWriter, r *http.Request) { + title := r.URL.Path[len("/edit/"):] + p, _ := loadPage(title) + p.render(w, edittmpl) +} + +func delHandler(w http.ResponseWriter, r *http.Request) { + title := r.URL.Path[len("/del/"):] + p := &Page{Title: title} + if err := p.del(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/", http.StatusFound) +} + +func homeHandler(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/view/Home", http.StatusFound) +} diff --git a/home.go b/home.go deleted file mode 100644 index 490d8a3..0000000 --- a/home.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import "net/http" - -func init() { - http.HandleFunc("/", homeHandler) -} - -func homeHandler(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/view/Home", http.StatusFound) -} diff --git a/index.go b/index.go deleted file mode 100644 index 10160ab..0000000 --- a/index.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "io/ioutil" - "net/http" - "text/template" -) - -func init() { - http.HandleFunc("/index", indexHandler) -} - -var indextmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/index")) - -func indexHandler(w http.ResponseWriter, r *http.Request) { - files, err := ioutil.ReadDir("data") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - p := new(Page) - for _, entry := range files { - file := entry.Name() - if !entry.IsDir() && file[0] != '.' { - p.Pages = append(p.Pages, entry) - } - } - p.render(w, indextmpl) -} diff --git a/parse.go b/parse.go deleted file mode 100644 index ac187f5..0000000 --- a/parse.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "bytes" - "regexp" - "strings" - "text/template" -) - -var ( - doc = template.Must(template.ParseFiles("tmpl/doc")) - intlink = regexp.MustCompile(`\[[^\]]+\]`) - extlink = regexp.MustCompile(`(https?|ftp)://\S+`) - imglink = regexp.MustCompile(`\.(jpe?g|png|gif)$`) -) - -func lines(in string) (ret []string) { - for _, l := range strings.Split(in, "\r\n") { - if len(l) > 1 { - ret = append(ret, l[1:]) - } - } - return -} - -func (p Page) HTML() string { - buf := new(bytes.Buffer) - for _, v := range strings.Split(string(p.Body), "\r\n\r\n") { - if len(v) < 1 { - continue - } - v = intlink.ReplaceAllStringFunc(v, func(s string) string { - tmp := new(bytes.Buffer) - doc.ExecuteTemplate(tmp, "link", s[1:len(s)-1]) - return tmp.String() - }) - v = extlink.ReplaceAllStringFunc(v, func(s string) string { - tmp := new(bytes.Buffer) - if imglink.MatchString(s) { - doc.ExecuteTemplate(tmp, "img", s) - } else { - doc.ExecuteTemplate(tmp, "link", s) - } - return tmp.String() - }) - - switch v[0] { - case ' ': - doc.ExecuteTemplate(buf, "pre", v) - case '-', '*': - doc.ExecuteTemplate(buf, "list", lines(v)) - case '#': - doc.ExecuteTemplate(buf, "olist", lines(v)) - case '=': - doc.ExecuteTemplate(buf, "heading", v[1:]) - default: - doc.ExecuteTemplate(buf, "par", v) - } - } - return buf.String() -} diff --git a/tmpl/doc b/tmpl/doc deleted file mode 100644 index b6b84aa..0000000 --- a/tmpl/doc +++ /dev/null @@ -1,35 +0,0 @@ -{{define "heading"}} -

{{.}}

-{{end}} - -{{define "link"}} - {{.}} -{{end}} - -{{define "img"}} - -{{end}} - -{{define "par"}} -

{{.}}

-{{end}} - -{{define "pre"}} -
{{.}}
-{{end}} - -{{define "list"}} - -{{end}} - -{{define "olist"}} -
    - {{range .}} -
  1. {{.}}
  2. - {{end}} -
-{{end}} diff --git a/view.go b/view.go deleted file mode 100644 index 3a1c47a..0000000 --- a/view.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "net/http" - "text/template" -) - -func init() { - http.HandleFunc("/view/", viewHandler) -} - -var viewtmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/view")) - -func viewHandler(w http.ResponseWriter, r *http.Request) { - title := r.URL.Path[len("/view/"):] - p, err := loadPage(title) - if err != nil { - http.Redirect(w, r, "/edit/"+title, http.StatusFound) - return - } - p.render(w, viewtmpl) -} diff --git a/wiki.go b/wiki.go index cdb49c2..f809e62 100644 --- a/wiki.go +++ b/wiki.go @@ -5,6 +5,8 @@ import ( "net/http" "os" "text/template" + + "github.com/russross/blackfriday" ) type Page struct { @@ -38,6 +40,10 @@ func (p *Page) render(w http.ResponseWriter, tmpl *template.Template) { } } +func (p *Page) HTML() string { + return string(blackfriday.MarkdownCommon([]byte(p.Body))) +} + func main() { - http.ListenAndServe(":8080", nil) + http.ListenAndServe("localhost:8080", nil) } -- cgit v1.2.3