-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.go
88 lines (76 loc) · 2.1 KB
/
articles.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
package main
import (
"fmt"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gorilla/mux"
"html/template"
"net/http"
"os"
"path"
)
func ArticleHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
file := vars["title"]
context := make(map[string]interface{})
prepareDefaultContext(r, context)
// FIXME: This is realy ugly but works for now
articles := getArticles(namespace)
arts := make([]Article, 0)
for _, n := range articles {
arts = append(arts, Article{
File: n.File[0 : len(n.File)-3],
Title: n.Title,
})
}
context["articles"] = arts
articleText, err := os.ReadFile(path.Join(*datadir, namespace, file+".md"))
if err != nil {
w.WriteHeader(http.StatusNotFound)
// FIXME: Add 404 handling
fmt.Println(err)
}
htmlFlags := html.SkipHTML | html.CommonFlags
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
context["content"] = template.HTML(markdown.ToHTML(articleText, nil, renderer))
t, _ := template.ParseFiles("templates/article.html")
err = t.Execute(w, context)
if err != nil {
fmt.Println(err)
}
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
context := make(map[string]interface{})
prepareDefaultContext(r, context)
context["namespaces"] = retrieveAvailableNamespaces()
w.WriteHeader(http.StatusOK)
t, _ := template.ParseFiles("templates/index.html")
err := t.Execute(w, context)
if err != nil {
fmt.Println(err)
}
}
func NamespaceHandler(w http.ResponseWriter, r *http.Request) {
context := make(map[string]interface{})
prepareDefaultContext(r, context)
vars := mux.Vars(r)
namespace := vars["namespace"]
context["namespaces"] = retrieveAvailableNamespaces()
articles := getArticles(namespace)
arts := make([]Article, 0)
for _, n := range articles {
arts = append(arts, Article{
File: n.File[0 : len(n.File)-3],
Title: n.Title,
})
}
context["articles"] = arts
w.WriteHeader(http.StatusOK)
t, _ := template.ParseFiles("templates/category.html")
err := t.Execute(w, context)
if err != nil {
fmt.Println(err)
}
}