-
Notifications
You must be signed in to change notification settings - Fork 16
/
wiki.go
executable file
·286 lines (260 loc) · 7.38 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"html/template"
"net/http"
"log"
"fmt"
"flag"
"io/ioutil"
"os"
"regexp"
"path"
"github.com/russross/blackfriday"
"os/exec"
"bytes"
"bufio"
"strings"
"strconv"
)
const (
DIRECTORY = "files"
LOG_LIMIT = "5"
)
type Node struct {
Path string
File string
Content string
Template string
Revision string
Bytes []byte
Dirs []*Directory
Log []*Log
Markdown template.HTML
Revisions bool // Show revisions
Config bool // Whether this is a config node
Password string
}
type Directory struct {
Path string
Name string
Active bool
}
type Log struct {
Hash string
Message string
Time string
Link bool
}
func (node *Node) isHead() bool {
return len(node.Log) > 0 && node.Revision == node.Log[0].Hash
}
// Add node
func (node *Node) GitAdd() *Node {
gitCmd(exec.Command("git", "add", node.File))
return node
}
// Commit node message
func (node *Node) GitCommit(msg string, author string) *Node {
if author != "" {
gitCmd(exec.Command("git", "commit", "-m", msg, fmt.Sprintf("--author='%s <system@g-wiki>'", author)))
} else {
gitCmd(exec.Command("git", "commit", "-m", msg))
}
return node
}
// Fetch node revision
func (node *Node) GitShow() *Node {
buf := gitCmd(exec.Command("git", "show", node.Revision+":"+node.File))
node.Bytes = buf.Bytes()
return node
}
// Fetch node log
func (node *Node) GitLog() *Node {
buf := gitCmd(exec.Command("git", "log", "--pretty=format:%h %ad %s", "--date=relative", "-n", LOG_LIMIT, node.File))
var err error
b := bufio.NewReader(buf)
var bytes []byte
node.Log = make([]*Log, 0)
for (err == nil) {
bytes, err = b.ReadSlice('\n')
logLine := parseLog(bytes)
if logLine == nil {
continue
} else if logLine.Hash != node.Revision {
logLine.Link = true
}
node.Log = append(node.Log, logLine)
}
if node.Revision == "" && len(node.Log) > 0 {
node.Revision = node.Log[0].Hash
node.Log[0].Link = false
}
return node
}
func parseLog(bytes []byte) *Log {
line := string(bytes)
re := regexp.MustCompile(`(.{0,7}) (\d+ \w+ ago) (.*)`)
matches := re.FindStringSubmatch(line)
if len(matches) == 4 {
return &Log{Hash: matches[1], Time: matches[2], Message: matches[3]}
}
return nil
}
func listDirectories(path string) []*Directory {
s := make([]*Directory, 0)
dirPath := ""
for i,dir := range (strings.Split(path, "/")) {
if i == 0 {
dirPath += dir
} else {
dirPath += "/" + dir
}
s = append(s, &Directory{Path: dirPath, Name: dir})
}
if len(s) > 0 {
s[len(s)-1].Active = true
}
return s
}
// Soft reset to specific revision
func (node *Node) GitRevert() *Node {
log.Printf("Reverts %s to revision %s", node, node.Revision)
gitCmd(exec.Command("git", "checkout", node.Revision, "--", node.File))
return node
}
// Run git command, will currently die on all errors
func gitCmd(cmd *exec.Cmd) (*bytes.Buffer) {
cmd.Dir = fmt.Sprintf("%s/", DIRECTORY)
var out bytes.Buffer
cmd.Stdout = &out
runError := cmd.Run()
if runError != nil {
fmt.Printf("Error: (%s) command failed with:\n\"%s\n\"", runError, out.String())
return bytes.NewBuffer([]byte{})
}
return &out
}
// Process node contents
func (node *Node) ToMarkdown() {
node.Markdown = template.HTML(string(blackfriday.MarkdownCommon(node.Bytes)))
}
func (node *Node) accessible() bool {
return (!node.Config || node.Password == "test")
}
func ParseBool(value string) bool {
boolValue, err := strconv.ParseBool(value)
if err != nil {
return false
}
return boolValue
}
func wikiHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
return
}
// Params
content := r.FormValue("content")
edit := r.FormValue("edit")
changelog := r.FormValue("msg")
author := r.FormValue("author")
reset := r.FormValue("revert")
revision := r.FormValue("revision")
password := r.FormValue("password")
filePath := fmt.Sprintf("%s%s.md", DIRECTORY, r.URL.Path)
node := &Node{File: r.URL.Path[1:] + ".md", Path: r.URL.Path, Password: password}
node.Revisions = ParseBool(r.FormValue("revisions"))
node.Dirs = listDirectories(r.URL.Path)
if r.URL.Path == "/config" {
node.Config = true
if node.accessible() {
edit = "true"
} else {
node.Template = "templates/login.tpl"
}
}
if content != "" && changelog != "" && node.accessible() {
bytes := []byte(content)
err := writeFile(bytes, filePath)
if err != nil {
log.Printf("Cant write to file %s, error: ", filePath, err)
} else {
// Wrote file, commit
node.Bytes = bytes
node.GitAdd().GitCommit(changelog, author).GitLog()
node.ToMarkdown()
}
} else if reset != "" && node.accessible() {
// Reset to revision
node.Revision = reset
node.GitRevert().GitCommit("Reverted to: " + node.Revision, author)
node.Revision = ""
node.GitShow().GitLog().ToMarkdown()
} else if node.accessible() {
// Show specific revision
node.Revision = revision
node.GitShow().GitLog()
if edit == "true" || len(node.Bytes) == 0 {
node.Content = string(node.Bytes)
node.Template = "templates/edit.tpl"
} else {
node.ToMarkdown()
}
}
renderTemplate(w, node)
}
func writeFile(bytes []byte, entry string) error {
err := os.MkdirAll(path.Dir(entry), 0777)
if err == nil {
return ioutil.WriteFile(entry, bytes, 0644)
}
return err
}
func renderTemplate(w http.ResponseWriter, node *Node) {
t := template.New("wiki")
var err error
if node.Template != "" {
t, err = template.ParseFiles(node.Template)
if err != nil {
log.Print("Could not parse template", err)
}
} else if node.Markdown != "" {
tpl := "{{ template \"header\" . }}"
if node.isHead() {
tpl += "{{ template \"actions\" .}}"
} else if node.Revision != "" {
tpl += "{{ template \"revision\" . }}"
}
// Add node
tpl += "{{ template \"node\" . }}"
// Show revisions
if node.Revisions {
tpl += "{{ template \"revisions\" . }}"
}
// Footer
tpl += "{{ template \"footer\" . }}"
t.Parse(tpl)
}
// Include the rest
t.ParseFiles("templates/header.tpl", "templates/footer.tpl",
"templates/actions.tpl", "templates/revision.tpl",
"templates/revisions.tpl", "templates/node.tpl")
err = t.Execute(w, node)
if err != nil {
log.Print("Could not execute template: ", err)
}
}
func main() {
// Handlers
http.HandleFunc("/", wikiHandler)
// Static resources
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
var local = flag.String("local", "", "serve as webserver, example: 0.0.0.0:8000")
flag.Parse()
var err error
if *local != "" {
err = http.ListenAndServe(*local, nil)
}
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}