diff options
Diffstat (limited to 'context.go')
-rw-r--r-- | context.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/context.go b/context.go new file mode 100644 index 0000000..8b41a52 --- /dev/null +++ b/context.go | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "path" | ||
24 | "strings" | ||
25 | ) | ||
26 | |||
27 | type page struct { | ||
28 | Title string | ||
29 | URL string | ||
30 | } | ||
31 | |||
32 | type context struct { | ||
33 | path string | ||
34 | IsCurrent func(params []string, data string) string | ||
35 | IsParent func(params []string, data string) string | ||
36 | } | ||
37 | |||
38 | // Methods accessible in templates | ||
39 | |||
40 | func (c context) URL() string { | ||
41 | p := strings.TrimPrefix(c.path, *settings.sourceDir) | ||
42 | return path.Clean("/" + p) | ||
43 | } | ||
44 | |||
45 | func (c context) Title() string { | ||
46 | _, t := path.Split(strings.TrimRight(c.URL(), "/")) | ||
47 | return t | ||
48 | } | ||
49 | |||
50 | func (c context) SubPages() (subPages []page) { | ||
51 | dirs, _ := ls(c.path) | ||
52 | for _, dir := range dirs { | ||
53 | var page page | ||
54 | page.Title = dir | ||
55 | page.URL = path.Join(c.URL(), dir) | ||
56 | subPages = append(subPages, page) | ||
57 | } | ||
58 | return | ||
59 | } | ||
60 | |||
61 | func (c context) IsRoot() bool { | ||
62 | if c.URL() == "/" { | ||
63 | return true | ||
64 | } | ||
65 | return false | ||
66 | } | ||
67 | |||
68 | func (c context) isCurrent(pageTitle string) bool { | ||
69 | if c.Title() == pageTitle { | ||
70 | return true | ||
71 | } | ||
72 | return false | ||
73 | } | ||
74 | |||
75 | func (c context) isParent(pageTitle string) bool { | ||
76 | for _, parent := range strings.Split(c.URL(), "/") { | ||
77 | if parent == pageTitle { | ||
78 | return true | ||
79 | } | ||
80 | } | ||
81 | return false | ||
82 | } | ||
83 | |||
84 | func makeContext(pagePath, sourceDir, outputDir string, exts []string) (c context) { | ||
85 | c.path = pagePath | ||
86 | c.IsCurrent = func(params []string, data string) string { | ||
87 | if c.isCurrent(strings.Join(params, " ")) { | ||
88 | return data | ||
89 | } | ||
90 | return "" | ||
91 | } | ||
92 | c.IsParent = func(params []string, data string) string { | ||
93 | if c.isParent(strings.Join(params, " ")) { | ||
94 | return data | ||
95 | } | ||
96 | return "" | ||
97 | } | ||
98 | return | ||
99 | } | ||