forked from monsti/monsti
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.go
114 lines (105 loc) · 3.41 KB
/
render.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
// This file is part of Monsti, a web content management system.
// Copyright 2012-2013 Christian Neumann
//
// Monsti is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Monsti is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Monsti. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
htmlT "html/template"
"path"
"strings"
"pkg.monsti.org/monsti/api/service"
"pkg.monsti.org/monsti/api/util"
"pkg.monsti.org/monsti/api/util/template"
)
// Master template render flags.
type masterTmplFlags uint32
const (
EDIT_VIEW masterTmplFlags = 1 << iota
)
// Environment/context for the master template.
type masterTmplEnv struct {
Node *service.Node
Session *service.UserSession
Title, Description string
Flags masterTmplFlags
}
// splitFirstDir returns the first directory in the given path.
func splitFirstDir(path string) string {
for len(path) > 0 && path[0] == '/' {
path = path[1:]
}
return strings.SplitN(path, "/", 2)[0]
}
// renderInMaster renders the content in the master template.
func renderInMaster(r template.Renderer, content []byte, env masterTmplEnv,
settings *settings, site util.SiteSettings, locale string,
s *service.Session) string {
if env.Flags&EDIT_VIEW != 0 {
ret, err := r.Render("admin/master", template.Context{
"Site": site,
"Page": template.Context{
"Title": env.Title,
"Node": env.Node,
"EditView": env.Flags&EDIT_VIEW != 0,
"Content": htmlT.HTML(content),
},
"Session": env.Session}, locale,
settings.Monsti.GetSiteTemplatesPath(site.Name))
if err != nil {
panic("Can't render: " + err.Error())
}
return ret
}
firstDir := splitFirstDir(env.Node.Path)
getNodeFn := func(path string) (*service.Node, error) {
node, err := s.Monsti().GetNode(site.Name, path)
return node, err
}
getChildrenFn := func(path string) ([]*service.Node, error) {
return s.Monsti().GetChildren(site.Name, path)
}
prinav, err := getNav("/", path.Join("/", firstDir), env.Session.User == nil,
getNodeFn, getChildrenFn)
if err != nil {
panic(fmt.Sprint("Could not get primary navigation: ", err))
}
prinav.MakeAbsolute("/")
var secnav navigation = nil
if env.Node.Path != "/" {
secnav, err = getNav(env.Node.Path, env.Node.Path, env.Session.User == nil,
getNodeFn, getChildrenFn)
if err != nil {
panic(fmt.Sprint("Could not get secondary navigation: ", err))
}
secnav.MakeAbsolute(env.Node.Path)
}
title := getNodeTitle(env.Node)
ret, err := r.Render("master", template.Context{
"Site": site,
"Page": template.Context{
"Node": env.Node,
"PrimaryNav": prinav,
"SecondaryNav": secnav,
"EditView": env.Flags&EDIT_VIEW != 0,
"Title": title,
"Content": htmlT.HTML(content),
"ShowSecondaryNav": len(secnav) > 0},
"Session": env.Session}, locale,
settings.Monsti.GetSiteTemplatesPath(site.Name))
if err != nil {
panic("Can't render: " + err.Error())
}
return ret
}