Skip to content

Commit

Permalink
some template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Jun 2, 2014
1 parent 04cfeac commit c5768c4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 68 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ expands on that a bit:
with `?v=<32-bit hash>` appended (used to override cache settings on static
files).

- `truncate <length> <value>` - truncate string to given length if it's
longer. In other case, returns original string.

- `strip_html <value>` - remove all HTML tags from string.

### Page interface

- `.Site` - global [site object](#site-interface).
Expand Down
67 changes: 0 additions & 67 deletions processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"bytes"
"errors"
"fmt"
"hash/adler32"
"io"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"time"
)

Expand Down Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
82 changes: 82 additions & 0 deletions template_funcs.go
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit c5768c4

Please sign in to comment.