2013-09-03 18:38:20 -04:00
|
|
|
package bundle
|
2013-08-31 20:35:17 -04:00
|
|
|
|
|
|
|
import (
|
2014-01-09 17:27:39 -05:00
|
|
|
"errors"
|
|
|
|
"github.com/eknkc/amber"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
"html"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-08-31 20:35:17 -04:00
|
|
|
)
|
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
func Gt(a interface{}, b interface{}) bool {
|
2014-01-09 17:27:39 -05:00
|
|
|
var left, right int64
|
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
left = int64(av.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
left = av.Int()
|
|
|
|
case reflect.String:
|
|
|
|
left, _ = strconv.ParseInt(av.String(), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
|
|
|
|
switch bv.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
right = int64(bv.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
right = bv.Int()
|
|
|
|
case reflect.String:
|
|
|
|
right, _ = strconv.ParseInt(bv.String(), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
return left > right
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
2013-11-10 15:04:51 -05:00
|
|
|
// First is exposed to templates, to iterate over the first N items in a
|
|
|
|
// rangeable list.
|
|
|
|
func First(limit int, seq interface{}) (interface{}, error) {
|
2014-01-09 17:27:39 -05:00
|
|
|
if limit < 1 {
|
|
|
|
return nil, errors.New("can't return negative/empty count of items from sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
seqv := reflect.ValueOf(seq)
|
|
|
|
// this is better than my first pass; ripped from text/template/exec.go indirect():
|
|
|
|
for ; seqv.Kind() == reflect.Ptr || seqv.Kind() == reflect.Interface; seqv = seqv.Elem() {
|
|
|
|
if seqv.IsNil() {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
if seqv.Kind() == reflect.Interface && seqv.NumMethod() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
|
|
|
|
}
|
|
|
|
if limit > seqv.Len() {
|
|
|
|
limit = seqv.Len()
|
|
|
|
}
|
|
|
|
return seqv.Slice(0, limit).Interface(), nil
|
2013-11-10 15:04:51 -05:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
func IsSet(a interface{}, key interface{}) bool {
|
2014-01-09 17:27:39 -05:00
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
kv := reflect.ValueOf(key)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Slice:
|
|
|
|
if int64(av.Len()) > kv.Int() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
if kv.Type() == av.Type().Key() {
|
|
|
|
return av.MapIndex(kv).IsValid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReturnWhenSet(a interface{}, index int) interface{} {
|
2014-01-09 17:27:39 -05:00
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
if av.Len() > index {
|
|
|
|
|
|
|
|
avv := av.Index(index)
|
|
|
|
switch avv.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return avv.Int()
|
|
|
|
case reflect.String:
|
|
|
|
return avv.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
|
2013-12-05 09:43:49 -05:00
|
|
|
func Highlight(in interface{}, lang string) template.HTML {
|
2014-01-09 17:27:39 -05:00
|
|
|
var str string
|
|
|
|
av := reflect.ValueOf(in)
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
str = av.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(strings.TrimSpace(str), "<pre><code>") {
|
|
|
|
str = str[strings.Index(str, "<pre><code>")+11:]
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(strings.TrimSpace(str), "</code></pre>") {
|
|
|
|
str = str[:strings.LastIndex(str, "</code></pre>")]
|
|
|
|
}
|
|
|
|
return template.HTML(helpers.Highlight(html.UnescapeString(str), lang))
|
2013-12-05 09:43:49 -05:00
|
|
|
}
|
|
|
|
|
2013-09-09 07:43:30 -04:00
|
|
|
func SafeHtml(text string) template.HTML {
|
2014-01-09 17:27:39 -05:00
|
|
|
return template.HTML(text)
|
2013-09-09 07:43:30 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:47:21 -04:00
|
|
|
type Template interface {
|
2014-01-09 17:27:39 -05:00
|
|
|
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
|
|
|
|
Lookup(name string) *template.Template
|
|
|
|
Templates() []*template.Template
|
|
|
|
New(name string) *template.Template
|
|
|
|
LoadTemplates(absPath string)
|
|
|
|
AddTemplate(name, tpl string) error
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type templateErr struct {
|
2014-01-09 17:27:39 -05:00
|
|
|
name string
|
|
|
|
err error
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type GoHtmlTemplate struct {
|
2014-01-09 17:27:39 -05:00
|
|
|
template.Template
|
|
|
|
errors []*templateErr
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTemplate() Template {
|
2014-01-09 17:27:39 -05:00
|
|
|
var templates = &GoHtmlTemplate{
|
|
|
|
Template: *template.New(""),
|
|
|
|
errors: make([]*templateErr, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"urlize": helpers.Urlize,
|
|
|
|
"gt": Gt,
|
|
|
|
"isset": IsSet,
|
|
|
|
"echoParam": ReturnWhenSet,
|
|
|
|
"safeHtml": SafeHtml,
|
|
|
|
"first": First,
|
|
|
|
"highlight": Highlight,
|
|
|
|
}
|
|
|
|
|
|
|
|
templates.Funcs(funcMap)
|
|
|
|
|
|
|
|
templates.LoadEmbedded()
|
|
|
|
return templates
|
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
|
2014-01-09 17:27:39 -05:00
|
|
|
func (t *GoHtmlTemplate) LoadEmbedded() {
|
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
|
2014-01-09 17:27:39 -05:00
|
|
|
func (t *GoHtmlTemplate) AddInternalTemplate(prefix, name, tpl string) error {
|
|
|
|
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl)
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHtmlTemplate) AddTemplate(name, tpl string) error {
|
2014-01-09 17:27:39 -05:00
|
|
|
_, err := t.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
}
|
|
|
|
return err
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHtmlTemplate) AddTemplateFile(name, path string) error {
|
2014-01-09 17:27:39 -05:00
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
_, err = t.New(name).Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
}
|
|
|
|
return err
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string {
|
2014-01-09 17:27:39 -05:00
|
|
|
return filepath.ToSlash(path[len(base)+1:])
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
func ignoreDotFile(path string) bool {
|
2014-01-09 17:27:39 -05:00
|
|
|
return filepath.Base(path)[0] == '.'
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:47:21 -04:00
|
|
|
func (t *GoHtmlTemplate) LoadTemplates(absPath string) {
|
2014-01-09 17:27:39 -05:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
if ignoreDotFile(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tplName := t.generateTemplateNameFrom(absPath, path)
|
|
|
|
|
|
|
|
if strings.HasSuffix(path, ".amber") {
|
|
|
|
compiler := amber.New()
|
|
|
|
// Parse the input file
|
|
|
|
if err := compiler.ParseFile(path); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// note t.New(tplName)
|
|
|
|
if _, err := compiler.CompileWithTemplate(t.New(tplName)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
t.AddTemplateFile(tplName, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath.Walk(absPath, walker)
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|