From 7b0b720efbf39ded2f72363d36424281df2b0491 Mon Sep 17 00:00:00 2001 From: Pacien Date: Sun, 30 Jun 2013 19:38:24 +0200 Subject: Add contextual methods --- common.go | 4 +-- context.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dynamic.go | 4 +-- 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 context.go diff --git a/common.go b/common.go index 460f74d..3547af1 100644 --- a/common.go +++ b/common.go @@ -22,7 +22,7 @@ package main import ( "bytes" "fmt" - "github.com/hoisie/mustache" + "github.com/drbawb/mustache" "github.com/russross/blackfriday" "io/ioutil" "path" @@ -100,7 +100,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s } template := merge(elements) - page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) + page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) 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 @@ +/* + + This file is part of CompileTree (https://github.com/Pacien/CompileTree) + + CompileTree is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CompileTree is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with CompileTree. If not, see . + +*/ + +package main + +import ( + "path" + "strings" +) + +type page struct { + Title string + URL string +} + +type context struct { + 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(), "/")) + return t +} + +func (c context) SubPages() (subPages []page) { + dirs, _ := ls(c.path) + for _, dir := range dirs { + var page page + page.Title = dir + page.URL = path.Join(c.URL(), dir) + subPages = append(subPages, page) + } + return +} + +func (c context) IsRoot() bool { + if c.URL() == "/" { + return true + } + return false +} + +func (c context) isCurrent(pageTitle string) bool { + if c.Title() == pageTitle { + return true + } + return false +} + +func (c context) isParent(pageTitle string) bool { + for _, parent := range strings.Split(c.URL(), "/") { + if parent == pageTitle { + return true + } + } + return false +} + +func makeContext(pagePath, sourceDir, outputDir string, exts []string) (c context) { + c.path = pagePath + c.IsCurrent = func(params []string, data string) string { + if c.isCurrent(strings.Join(params, " ")) { + return data + } + return "" + } + c.IsParent = func(params []string, data string) string { + if c.isParent(strings.Join(params, " ")) { + return data + } + return "" + } + return +} diff --git a/dynamic.go b/dynamic.go index b6388bc..ff567c1 100644 --- a/dynamic.go +++ b/dynamic.go @@ -21,7 +21,7 @@ package main import ( "fmt" - "github.com/hoisie/mustache" + "github.com/drbawb/mustache" "net/http" "path" "strings" @@ -49,7 +49,7 @@ func handle(w http.ResponseWriter, r *http.Request) { // render the page template := merge(elements) - page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) + page := mustache.Render(string(template), makeContext(path.Join(*settings.sourceDir, request), *settings.sourceDir, *settings.outputDir, settings.exts)) // serve the page _, err := w.Write([]byte(page)) -- cgit v1.2.3