diff --git a/README.md b/README.md index fd55b43..35bc0d3 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,11 @@ expands on that a bit: with `?v=<32-bit hash>` appended (used to override cache settings on static files). + - `truncate ` - truncate string to given length if it's + longer. In other case, returns original string. + + - `strip_html ` - remove all HTML tags from string. + ### Page interface - `.Site` - global [site object](#site-interface). diff --git a/processors.go b/processors.go index 6864608..9adf733 100644 --- a/processors.go +++ b/processors.go @@ -4,14 +4,11 @@ import ( "bytes" "errors" "fmt" - "hash/adler32" - "io" "os/exec" "path/filepath" "regexp" "sort" "strings" - "text/template" "time" ) @@ -298,67 +295,3 @@ func ProcessRelativize(page *Page, args []string) { }) page.SetContent(rv) } - -// template utilities - -var inventory = map[string]interface{}{} - -func HasChanged(name string, value interface{}) bool { - changed := true - - if inventory[name] == value { - changed = false - } else { - inventory[name] = value - } - - return changed -} - -func Cut(value, begin, end string) (string, error) { - bre, err := regexp.Compile(begin) - if err != nil { - return "", err - } - ere, err := regexp.Compile(end) - if err != nil { - return "", err - } - - bloc := bre.FindIndex([]byte(value)) - eloc := ere.FindIndex([]byte(value)) - - if bloc == nil { - bloc = []int{0, 0} - } - if eloc == nil { - eloc = []int{len(value)} - } - - return value[bloc[1]:eloc[0]], nil -} - -func Hash(value string) string { - h := adler32.New() - io.WriteString(h, value) - return fmt.Sprintf("%x", h.Sum(nil)) -} - -func Versionize(current *Page, value string) string { - page := current.Site.Pages.ByPath(value) - if page == nil { - errhandle(fmt.Errorf( - "trying to versionize page which does not exist: %s, current: %s", - value, current.Path)) - } - c := page.Process().Content() - h := Hash(c) - return current.UrlTo(page) + "?v=" + h -} - -var funcMap = template.FuncMap{ - "changed": HasChanged, - "cut": Cut, - "hash": Hash, - "version": Versionize, -} diff --git a/site.go b/site.go index 626fd95..3b0af92 100644 --- a/site.go +++ b/site.go @@ -17,7 +17,7 @@ type Site struct { } func NewSite(config *SiteConfig) *Site { - template := template.New("no-idea-what-to-pass-here").Funcs(funcMap) + template := template.New("no-idea-what-to-pass-here").Funcs(TemplateFuncMap) template, err := template.ParseFiles(config.Templates...) errhandle(err) diff --git a/template_funcs.go b/template_funcs.go new file mode 100644 index 0000000..3791020 --- /dev/null +++ b/template_funcs.go @@ -0,0 +1,82 @@ +package main + +import ( + "fmt" + "hash/adler32" + "io" + "regexp" + "text/template" +) + +var inventory = map[string]interface{}{} + +func HasChanged(name string, value interface{}) bool { + changed := true + + if inventory[name] == value { + changed = false + } else { + inventory[name] = value + } + + return changed +} + +func Cut(value, begin, end string) (string, error) { + bre, err := regexp.Compile(begin) + if err != nil { + return "", err + } + ere, err := regexp.Compile(end) + if err != nil { + return "", err + } + + bloc := bre.FindIndex([]byte(value)) + eloc := ere.FindIndex([]byte(value)) + + if bloc == nil { + bloc = []int{0, 0} + } + if eloc == nil { + eloc = []int{len(value)} + } + + return value[bloc[1]:eloc[0]], nil +} + +func Hash(value string) string { + h := adler32.New() + io.WriteString(h, value) + return fmt.Sprintf("%x", h.Sum(nil)) +} + +func Versionize(current *Page, value string) string { + page := current.Site.Pages.ByPath(value) + if page == nil { + errhandle(fmt.Errorf( + "trying to versionize page which does not exist: %s, current: %s", + value, current.Path)) + } + c := page.Process().Content() + h := Hash(c) + return current.UrlTo(page) + "?v=" + h +} + +func Truncate(length int, value string) string { + if length > len(value) { length = len(value) } + return value[0:length] +} + +func StripHTML(value string) string { + return regexp.MustCompile("<[^>]+>").ReplaceAllString(value, "") +} + +var TemplateFuncMap = template.FuncMap{ + "changed": HasChanged, + "cut": Cut, + "hash": Hash, + "version": Versionize, + "truncate": Truncate, + "strip_html": StripHTML, +}