diff options
author | Pacien | 2013-07-16 12:50:25 +0200 |
---|---|---|
committer | Pacien | 2013-07-16 12:50:25 +0200 |
commit | 7f4e93785cd49471b4d902620c9ea4d523d874c7 (patch) | |
tree | 035a4b7f0c4de624ea00481e89162251272e4904 /context.go | |
parent | ec4932d86f805864cd35fc27a2f91964b55982d4 (diff) | |
download | foldaweb-7f4e93785cd49471b4d902620c9ea4d523d874c7.tar.gz |
First version
Diffstat (limited to 'context.go')
-rw-r--r-- | context.go | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/context.go b/context.go deleted file mode 100644 index 27a8c4a..0000000 --- a/context.go +++ /dev/null | |||
@@ -1,97 +0,0 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of FoldaWeb <https://github.com/Pacien/FoldaWeb> | ||
4 | |||
5 | FoldaWeb is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU Affero General Public License as published by | ||
7 | the Free Software Foundation, either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | FoldaWeb is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with FoldaWeb. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "github.com/Pacien/fcmd" | ||
24 | "path" | ||
25 | "strings" | ||
26 | ) | ||
27 | |||
28 | type page struct { | ||
29 | Title string | ||
30 | Path string | ||
31 | } | ||
32 | |||
33 | type context struct { | ||
34 | filePath string | ||
35 | Path string | ||
36 | IsCurrent func(params []string, data string) string | ||
37 | IsParent func(params []string, data string) string | ||
38 | } | ||
39 | |||
40 | // Methods accessible in templates | ||
41 | |||
42 | func (c context) Title() string { | ||
43 | _, t := path.Split(strings.TrimRight(c.Path, "/")) | ||
44 | return t | ||
45 | } | ||
46 | |||
47 | func (c context) SubPages() (subPages []page) { | ||
48 | dirs, _ := fcmd.Ls(c.filePath) | ||
49 | for _, dir := range dirs { | ||
50 | var page page | ||
51 | page.Title = dir | ||
52 | page.Path = path.Join(c.Path, dir) | ||
53 | subPages = append(subPages, page) | ||
54 | } | ||
55 | return | ||
56 | } | ||
57 | |||
58 | func (c context) IsRoot() bool { | ||
59 | if c.Path == "/" { | ||
60 | return true | ||
61 | } | ||
62 | return false | ||
63 | } | ||
64 | |||
65 | func (c context) isCurrent(pageTitle string) bool { | ||
66 | if c.Title() == pageTitle { | ||
67 | return true | ||
68 | } | ||
69 | return false | ||
70 | } | ||
71 | |||
72 | func (c context) isParent(pageTitle string) bool { | ||
73 | for _, parent := range strings.Split(c.Path, "/") { | ||
74 | if parent == pageTitle { | ||
75 | return true | ||
76 | } | ||
77 | } | ||
78 | return false | ||
79 | } | ||
80 | |||
81 | func makeContext(pagePath, sourceDir string, exts []string) (c context) { | ||
82 | c.Path = path.Clean("/" + pagePath) | ||
83 | c.filePath = path.Join(sourceDir, c.Path) | ||
84 | c.IsCurrent = func(params []string, data string) string { | ||
85 | if c.isCurrent(strings.Join(params, " ")) { | ||
86 | return data | ||
87 | } | ||
88 | return "" | ||
89 | } | ||
90 | c.IsParent = func(params []string, data string) string { | ||
91 | if c.isParent(strings.Join(params, " ")) { | ||
92 | return data | ||
93 | } | ||
94 | return "" | ||
95 | } | ||
96 | return | ||
97 | } | ||