aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien2013-07-10 23:52:21 +0200
committerPacien2013-07-10 23:52:21 +0200
commitb1886d52f4fc462edb5bdc5c18ff8cdcc3546eb8 (patch)
tree89c682b63ae326cf4c860e2f64afd8960623a50f
parent8d1ff0e59f9473b9c98196874ee62e5625380fed (diff)
downloadfoldaweb-b1886d52f4fc462edb5bdc5c18ff8cdcc3546eb8.tar.gz
Optimize contextual variables generation
-rw-r--r--common.go6
-rw-r--r--context.go25
-rw-r--r--dynamic.go2
3 files changed, 16 insertions, 17 deletions
diff --git a/common.go b/common.go
index 1a78017..9985aaa 100644
--- a/common.go
+++ b/common.go
@@ -98,10 +98,12 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s
98 } 98 }
99 } 99 }
100 100
101 pagePath := strings.TrimPrefix(dirPath, sourceDir)
102
101 template := merge(elements) 103 template := merge(elements)
102 page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) 104 page := mustache.Render(string(template), makeContext(pagePath, sourceDir, exts))
103 105
104 err := fcmd.WriteFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) 106 err := fcmd.WriteFile(path.Join(outputDir, pagePath, saveAs), []byte(page))
105 if err != nil { 107 if err != nil {
106 fmt.Println(err) 108 fmt.Println(err)
107 return 109 return
diff --git a/context.go b/context.go
index aff6a5d..a099da4 100644
--- a/context.go
+++ b/context.go
@@ -27,40 +27,36 @@ import (
27 27
28type page struct { 28type page struct {
29 Title string 29 Title string
30 URL string 30 Path string
31} 31}
32 32
33type context struct { 33type context struct {
34 path string 34 filePath string
35 Path string
35 IsCurrent func(params []string, data string) string 36 IsCurrent func(params []string, data string) string
36 IsParent func(params []string, data string) string 37 IsParent func(params []string, data string) string
37} 38}
38 39
39// Methods accessible in templates 40// Methods accessible in templates
40 41
41func (c context) URL() string {
42 p := strings.TrimPrefix(c.path, *settings.sourceDir)
43 return path.Clean("/" + p)
44}
45
46func (c context) Title() string { 42func (c context) Title() string {
47 _, t := path.Split(strings.TrimRight(c.URL(), "/")) 43 _, t := path.Split(strings.TrimRight(c.Path, "/"))
48 return t 44 return t
49} 45}
50 46
51func (c context) SubPages() (subPages []page) { 47func (c context) SubPages() (subPages []page) {
52 dirs, _ := fcmd.Ls(c.path) 48 dirs, _ := fcmd.Ls(c.filePath)
53 for _, dir := range dirs { 49 for _, dir := range dirs {
54 var page page 50 var page page
55 page.Title = dir 51 page.Title = dir
56 page.URL = path.Join(c.URL(), dir) 52 page.Path = path.Join(c.Path, dir)
57 subPages = append(subPages, page) 53 subPages = append(subPages, page)
58 } 54 }
59 return 55 return
60} 56}
61 57
62func (c context) IsRoot() bool { 58func (c context) IsRoot() bool {
63 if c.URL() == "/" { 59 if c.Path == "/" {
64 return true 60 return true
65 } 61 }
66 return false 62 return false
@@ -74,7 +70,7 @@ func (c context) isCurrent(pageTitle string) bool {
74} 70}
75 71
76func (c context) isParent(pageTitle string) bool { 72func (c context) isParent(pageTitle string) bool {
77 for _, parent := range strings.Split(c.URL(), "/") { 73 for _, parent := range strings.Split(c.Path, "/") {
78 if parent == pageTitle { 74 if parent == pageTitle {
79 return true 75 return true
80 } 76 }
@@ -82,8 +78,9 @@ func (c context) isParent(pageTitle string) bool {
82 return false 78 return false
83} 79}
84 80
85func makeContext(pagePath, sourceDir, outputDir string, exts []string) (c context) { 81func makeContext(pagePath, sourceDir string, exts []string) (c context) {
86 c.path = pagePath 82 c.Path = path.Clean("/" + pagePath)
83 c.filePath = path.Join(sourceDir, c.Path)
87 c.IsCurrent = func(params []string, data string) string { 84 c.IsCurrent = func(params []string, data string) string {
88 if c.isCurrent(strings.Join(params, " ")) { 85 if c.isCurrent(strings.Join(params, " ")) {
89 return data 86 return data
diff --git a/dynamic.go b/dynamic.go
index e089eeb..e15c529 100644
--- a/dynamic.go
+++ b/dynamic.go
@@ -51,7 +51,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
51 51
52 // render the page 52 // render the page
53 template := merge(elements) 53 template := merge(elements)
54 page := mustache.Render(string(template), makeContext(path.Join(*settings.sourceDir, request), *settings.sourceDir, *settings.outputDir, settings.exts)) 54 page := mustache.Render(string(template), makeContext(r.URL.Path, *settings.sourceDir, settings.exts))
55 55
56 // serve the page 56 // serve the page
57 _, err := w.Write([]byte(page)) 57 _, err := w.Write([]byte(page))