aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-02-18 00:52:10 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-02-18 00:52:10 +0100
commit898cbafe87a2ab4ff347ebaf9e4b7d04506ad764 (patch)
tree59e752cfbd6f7819ce2762af33b7ee3ae307d75a
parentbfe3ddc72d6aa90e32bdcfd2ed5b611578072bd7 (diff)
Switch to Markdown
-rw-r--r--del.go17
-rw-r--r--edit.go18
-rw-r--r--handler.go67
-rw-r--r--home.go11
-rw-r--r--index.go29
-rw-r--r--parse.go61
-rw-r--r--tmpl/doc35
-rw-r--r--view.go22
-rw-r--r--wiki.go8
9 files changed, 74 insertions, 194 deletions
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"}}
- <h4>{{.}}</h4>
-{{end}}
-
-{{define "link"}}
- <a href="{{.}}">{{.}}</a>
-{{end}}
-
-{{define "img"}}
- <img src="{{.}}">
-{{end}}
-
-{{define "par"}}
- <p>{{.}}</p>
-{{end}}
-
-{{define "pre"}}
- <pre>{{.}}</pre>
-{{end}}
-
-{{define "list"}}
-<ul>
- {{range .}}
- <li>{{.}}</li>
- {{end}}
-</ul>
-{{end}}
-
-{{define "olist"}}
-<ol>
- {{range .}}
- <li>{{.}}</li>
- {{end}}
-</ol>
-{{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)
}