From 7b0b720efbf39ded2f72363d36424281df2b0491 Mon Sep 17 00:00:00 2001 From: Pacien Date: Sun, 30 Jun 2013 19:38:24 +0200 Subject: Add contextual methods --- context.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 context.go (limited to 'context.go') 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 +} -- cgit v1.2.3