diff options
-rw-r--r-- | common.go | 4 | ||||
-rw-r--r-- | context.go | 99 | ||||
-rw-r--r-- | dynamic.go | 4 |
3 files changed, 103 insertions, 4 deletions
@@ -22,7 +22,7 @@ package main | |||
22 | import ( | 22 | import ( |
23 | "bytes" | 23 | "bytes" |
24 | "fmt" | 24 | "fmt" |
25 | "github.com/hoisie/mustache" | 25 | "github.com/drbawb/mustache" |
26 | "github.com/russross/blackfriday" | 26 | "github.com/russross/blackfriday" |
27 | "io/ioutil" | 27 | "io/ioutil" |
28 | "path" | 28 | "path" |
@@ -100,7 +100,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s | |||
100 | } | 100 | } |
101 | 101 | ||
102 | template := merge(elements) | 102 | template := merge(elements) |
103 | page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) | 103 | page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) |
104 | 104 | ||
105 | err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) | 105 | err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) |
106 | if err != nil { | 106 | if err != nil { |
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 | } | ||
@@ -21,7 +21,7 @@ package main | |||
21 | 21 | ||
22 | import ( | 22 | import ( |
23 | "fmt" | 23 | "fmt" |
24 | "github.com/hoisie/mustache" | 24 | "github.com/drbawb/mustache" |
25 | "net/http" | 25 | "net/http" |
26 | "path" | 26 | "path" |
27 | "strings" | 27 | "strings" |
@@ -49,7 +49,7 @@ func handle(w http.ResponseWriter, r *http.Request) { | |||
49 | 49 | ||
50 | // render the page | 50 | // render the page |
51 | template := merge(elements) | 51 | template := merge(elements) |
52 | page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) | 52 | page := mustache.Render(string(template), makeContext(path.Join(*settings.sourceDir, request), *settings.sourceDir, *settings.outputDir, settings.exts)) |
53 | 53 | ||
54 | // serve the page | 54 | // serve the page |
55 | _, err := w.Write([]byte(page)) | 55 | _, err := w.Write([]byte(page)) |