From b1886d52f4fc462edb5bdc5c18ff8cdcc3546eb8 Mon Sep 17 00:00:00 2001 From: Pacien Date: Wed, 10 Jul 2013 23:52:21 +0200 Subject: Optimize contextual variables generation --- common.go | 6 ++++-- context.go | 25 +++++++++++-------------- dynamic.go | 2 +- 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 } } + pagePath := strings.TrimPrefix(dirPath, sourceDir) + template := merge(elements) - page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) + page := mustache.Render(string(template), makeContext(pagePath, sourceDir, exts)) - err := fcmd.WriteFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) + err := fcmd.WriteFile(path.Join(outputDir, pagePath, saveAs), []byte(page)) if err != nil { fmt.Println(err) return diff --git a/context.go b/context.go index aff6a5d..a099da4 100644 --- a/context.go +++ b/context.go @@ -27,40 +27,36 @@ import ( type page struct { Title string - URL string + Path string } type context struct { - path string + filePath string + Path string IsCurrent func(params []string, data string) string IsParent func(params []string, data string) string } // Methods accessible in templates -func (c context) URL() string { - p := strings.TrimPrefix(c.path, *settings.sourceDir) - return path.Clean("/" + p) -} - func (c context) Title() string { - _, t := path.Split(strings.TrimRight(c.URL(), "/")) + _, t := path.Split(strings.TrimRight(c.Path, "/")) return t } func (c context) SubPages() (subPages []page) { - dirs, _ := fcmd.Ls(c.path) + dirs, _ := fcmd.Ls(c.filePath) for _, dir := range dirs { var page page page.Title = dir - page.URL = path.Join(c.URL(), dir) + page.Path = path.Join(c.Path, dir) subPages = append(subPages, page) } return } func (c context) IsRoot() bool { - if c.URL() == "/" { + if c.Path == "/" { return true } return false @@ -74,7 +70,7 @@ func (c context) isCurrent(pageTitle string) bool { } func (c context) isParent(pageTitle string) bool { - for _, parent := range strings.Split(c.URL(), "/") { + for _, parent := range strings.Split(c.Path, "/") { if parent == pageTitle { return true } @@ -82,8 +78,9 @@ func (c context) isParent(pageTitle string) bool { return false } -func makeContext(pagePath, sourceDir, outputDir string, exts []string) (c context) { - c.path = pagePath +func makeContext(pagePath, sourceDir string, exts []string) (c context) { + c.Path = path.Clean("/" + pagePath) + c.filePath = path.Join(sourceDir, c.Path) c.IsCurrent = func(params []string, data string) string { if c.isCurrent(strings.Join(params, " ")) { 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) { // render the page template := merge(elements) - page := mustache.Render(string(template), makeContext(path.Join(*settings.sourceDir, request), *settings.sourceDir, *settings.outputDir, settings.exts)) + page := mustache.Render(string(template), makeContext(r.URL.Path, *settings.sourceDir, settings.exts)) // serve the page _, err := w.Write([]byte(page)) -- cgit v1.2.3