aboutsummaryrefslogtreecommitdiff
path: root/wiki.go
blob: 6a7bbf9c9814029cbe5396a833340a680e1e851b (plain)
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
package main

import (
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"path"

	"github.com/russross/blackfriday"
)

type Page struct {
	Title string
	Body  []byte
	Pages []os.FileInfo
}

func (p *Page) fileName() string {
	return path.Join(*data, p.Title)
}

func (p *Page) save() error {
	return ioutil.WriteFile(p.fileName(), p.Body, 0600)
}

func loadPage(title string) (p *Page, err error) {
	p = &Page{Title: title}
	p.Body, err = ioutil.ReadFile(p.fileName())
	return
}

func (p *Page) del() error {
	return os.Remove(p.fileName())
}

func (p *Page) render(w http.ResponseWriter, tmpl *template.Template) {
	err := tmpl.ExecuteTemplate(w, "root", p)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func (p *Page) HTML() template.HTML {
	return template.HTML(blackfriday.MarkdownCommon(p.Body))
}