/
wiki.go
125 lines (106 loc) · 3.78 KB
/
wiki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"net/http"
"io/ioutil"
"regexp"
"html/template"
)
type Page struct {
Title string
Body template.HTML
}
func (p *Page) save() error {
filename := "data/" + p.Title + ".txt"
return ioutil.WriteFile(filename, []byte(p.Body), 0600)
}
func loadPage(title string) (*Page, error) {
filename := "data/" + title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: template.HTML(body)}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderMarkdown(p)
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
p := &Page{Title: title, Body: template.HTML(body)}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func redirHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view/FrontPage", http.StatusFound)
}
var templates = make(map[string]*template.Template)
func init() {
for _, tmpl := range []string{"edit", "view"} {
t := template.Must(template.ParseFiles("tmpl/"+tmpl+".html"))
templates[tmpl] = t
}
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates[tmpl].Execute(w, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
const lenPath = len("/view/")
var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
if !titleValidator.MatchString(title) {
http.NotFound(w, r)
return
}
fn(w, r, title)
}
}
func renderMarkdown(p *Page) {
var h1regexp = regexp.MustCompile("(?m)^# (.+) #")
p.Body = template.HTML(h1regexp.ReplaceAll([]byte(p.Body), []byte(`<h1>$1</h1>`)))
var h2regexp = regexp.MustCompile("(?m)^## (.+) ##")
p.Body = template.HTML(h2regexp.ReplaceAll([]byte(p.Body), []byte(`<h2>$1</h2>`)))
var h3regexp = regexp.MustCompile("(?m)^### (.+) ###")
p.Body = template.HTML(h3regexp.ReplaceAll([]byte(p.Body), []byte(`<h3>$1</h3>`)))
var h4regexp = regexp.MustCompile("(?m)^#### (.+) ####")
p.Body = template.HTML(h4regexp.ReplaceAll([]byte(p.Body), []byte(`<h4>$1</h4>`)))
var h5regexp = regexp.MustCompile("(?m)^##### (.+) #####")
p.Body = template.HTML(h5regexp.ReplaceAll([]byte(p.Body), []byte(`<h5>$1</h5>`)))
var h6regexp = regexp.MustCompile("(?m)^###### (.+) ######")
p.Body = template.HTML(h6regexp.ReplaceAll([]byte(p.Body), []byte(`<h5>$1</h5>`)))
var italicregexp = regexp.MustCompile(`(?m) \*(.+)\*`)
p.Body = template.HTML(italicregexp.ReplaceAll([]byte(p.Body), []byte(`<i>$1</i>`)))
var boldregexp = regexp.MustCompile(`(?m)\*\*(.+)\*\*`)
p.Body = template.HTML(boldregexp.ReplaceAll([]byte(p.Body), []byte(`<b>$1</b>`)))
var striketroughregexp = regexp.MustCompile("(?m)~(.+)~")
p.Body = template.HTML(striketroughregexp.ReplaceAll([]byte(p.Body), []byte(`<s>$1</s>`)))
var hrregexp = regexp.MustCompile(`(?m)^[*-_]{3,}$`)
p.Body = template.HTML(hrregexp.ReplaceAll([]byte(p.Body), []byte(`<hr>`)))
}
func main() {
http.HandleFunc("/", redirHandler)
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil)
}