-
Notifications
You must be signed in to change notification settings - Fork 4
/
handle.go
89 lines (82 loc) · 3.06 KB
/
handle.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
// Package site $PF_IGNORE$
package site
import (
"fmt"
"strings"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/site/download"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/doc"
"projectforge.dev/projectforge/views/layout"
"projectforge.dev/projectforge/views/verror"
"projectforge.dev/projectforge/views/vsite"
)
func Handle(path []string, rc *fasthttp.RequestCtx, as *app.State, ps *cutil.PageState) (string, layout.Page, []string, error) {
if len(path) == 0 {
ps.Data = siteData("Welcome to the marketing site!")
return "", &vsite.Index{}, path, nil
}
var page layout.Page
var err error
bc := path
switch path[0] {
case util.AppKey:
msg := "\n " +
"<meta name=\"go-import\" content=\"projectforge.dev/projectforge git %s\">\n " +
"<meta name=\"go-source\" content=\"projectforge.dev/projectforge %s %s/tree/master{/dir} %s/blob/master{/dir}/{file}#L{line}\">"
ps.HeaderContent = fmt.Sprintf(msg, util.AppSource, util.AppSource, util.AppSource, util.AppSource)
return "", &vsite.GoSource{}, path, nil
case keyFeatures:
switch {
case len(path) == 1:
page, err = featureList(as, ps)
case len(path) == 2:
page, err = featureDetail(path[1], as, ps)
default:
bc, page, err = featureFiles(path, as, ps)
}
case keyDownload:
dls := download.GetLinks(as.BuildInfo.Version)
ps.Data = map[string]any{"base": "https://github.com/kyleu/projectforge/releases/download/v" + as.BuildInfo.Version, "links": dls}
page = &vsite.Download{Links: dls}
case keyInstall:
page, err = mdTemplate("Installation", "This static page contains installation instructions", "installation.md", "code", ps)
case keyContrib:
page, err = mdTemplate("Contributing", "This static page describes how to build "+util.AppName, "contributing.md", "cog", ps)
case keyTech:
page, err = mdTemplate("Technology", "This static page describes the technology used in "+util.AppName, "technology.md", "shield", ps)
default:
page, err = mdTemplate("Documentation", "Documentation for "+util.AppName, path[0]+".md", "", ps)
if err != nil {
page = &verror.NotFound{Path: "/" + strings.Join(path, "/")}
err = nil
}
}
return "", page, bc, err
}
func siteData(result string, kvs ...string) map[string]any {
ret := map[string]any{"app": util.AppName, "url": util.AppURL, "result": result}
for i := 0; i < len(kvs); i += 2 {
ret[kvs[i]] = kvs[i+1]
}
return ret
}
func mdTemplate(title string, description string, path string, icon string, ps *cutil.PageState) (layout.Page, error) {
if icon == "" {
icon = "cog"
}
ps.Data = siteData(title, "description", description)
ps.Title = title
html, err := doc.HTML(path)
if err != nil {
return nil, err
}
if h1Idx := strings.Index(html, "<h1>"); h1Idx > -1 {
ic := fmt.Sprintf(`<svg class="icon" style="width: 36px; height: 36px;"><use xlink:href="#svg-%s" /></svg> `, icon)
html = html[:h1Idx+4] + ic + html[h1Idx+4:]
}
page := &vsite.MarkdownPage{Title: title, HTML: html}
return page, nil
}