shithub: hell

ref: bc47f7bf1176b75ddd044c3ca296ee024b87d631
dir: /templater.go/

View raw version
package main

import (
	"fmt"
	"os"
)

type templateDefs struct {
	stringer fmt.Stringer
	key      string
}

type templateRenderer struct {
	tempdefs []*templateDefs
}

// returns a new template renderer loaded with standard format keys
func newStatusTemplateRenderer(sf *StatusFormatter) *templateRenderer {
	return &templateRenderer{
		tempdefs: []*templateDefs{
			{key: "content", stringer: &statusContent{sf}},
			{key: "media_descriptions", stringer: &mediaDescriptions{sf}},
			{key: "detail_line", stringer: &detailLine{sf}},
			{key: "username", stringer: &username{sf}},
			{key: "boostuser", stringer: &boostedusername{sf}},
			{key: "boostcontent", stringer: &boostContent{sf}},
			{key: "boosted_media_descriptions", stringer: &boostMediaDescriptions{sf}},
		},
	}
}

func (tr *templateRenderer) render(template string) (string, error) {
	expandMap := func(key string) string {
		for item := range tr.tempdefs {
			if key == tr.tempdefs[item].key {
				return tr.tempdefs[item].stringer.String()
			}
		}
		return fmt.Sprintf("[%%KEY_NOT_FOUND:%s]", key)
	}
	template, long := hyphenate(os.Expand(template, expandMap))
	if long {
		return template + "\n\n", nil
	}
	return template + "\n", nil
}