-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (128 loc) · 3.35 KB
/
main.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
package main
import (
"html/template"
"io/ioutil"
"net/http"
"os/exec"
"path"
"github.com/bmatcuk/doublestar"
"github.com/julienschmidt/httprouter"
"github.com/namsral/flag"
"github.com/shurcooL/github_flavored_markdown"
)
type Config struct {
data string
bind string
}
func main() {
var config Config
flag.StringVar(&config.data, "data", "./", "path to data")
flag.StringVar(&config.bind, "bind", "0.0.0.0:9000", "[addr]:<port> to bind to")
flag.Parse()
exec.Command("xdg-open", "http://" + config.bind).Run()
ServerInit(config)
}
type Page struct {
Title string
HTML template.HTML
}
func LoadWiki(file string, dir string) (*Page, error) {
filename := path.Join(dir, file)
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
html := github_flavored_markdown.Markdown(body)
return &Page{
Title: filename,
HTML: template.HTML(html),
}, nil
}
type Server struct {
view *template.Template
config Config
router *httprouter.Router
}
func ServerInit(config Config) *Server {
server := &Server{
view: template.Must(template.New("base").Parse(baseTmpl)),
config: config,
router: httprouter.New(),
}
server.router.GET("/", server.RootHandler())
server.router.GET("/w/*file", server.WikiHandler())
http.ListenAndServe(server.config.bind, server.router)
return server
}
func (s *Server) RootHandler() httprouter.Handle {
pages, _ := doublestar.Glob("./**/*.md")
list := ""
for _, page := range pages {
list += "<h2><a href='w/" + page + "'>" + page + "</a></h2>"
}
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.view.Execute(w, &Page{
Title: "Index",
HTML: template.HTML(list),
})
}
}
func (s *Server) WikiHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
file := p.ByName("file")
page, err := LoadWiki(file, s.config.data)
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
s.view.Execute(w, page)
}
}
var baseTmpl = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>{{.Title}}</title>
<style type="text/css">
@import url("https://unpkg.com/spectre.css/dist/spectre.min.css");
@import url("https://unpkg.com/spectre.css/dist/spectre-exp.min.css");
@import url("https://unpkg.com/spectre.css/dist/spectre-icons.min.css");
@import url("https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.8.0/github-markdown.min.css");
.bg-white {
background: #ffffff;
}
code {
color: #5764c6;
}
ul {
list-style: url;
}
</style>
</head>
<body class="bg-white">
<section class="container p-2">
<div class="columns">
<div class="column col-6 col-mx-auto">
<header class="navbar">
<section class="navbar-section">
<ul class="breadcrumb">
<li class="breadcrumb-item"><a href="/">Home</a></li>
<li class="breadcrumb-item"><a href="#">{{.Title}}</a></li>
</ul>
</section>
</header>
</div>
</div>
<div class="divider pb-2"></div>
<div class="columns">
<div class="column col-6 col-xl-8 col-lg-10 col-md-12 col-mx-auto">
<div class="markdown-body">{{.HTML}}</div>
</div>
</div>
</section>
</body>
</html>
`