2012-12-23 03:51:14 +00:00
|
|
|
|
package smplt
|
|
|
|
|
|
|
|
|
|
import (
|
2012-12-24 05:12:11 +00:00
|
|
|
|
"io"
|
2012-12-23 04:32:26 +00:00
|
|
|
|
"mime"
|
|
|
|
|
"path"
|
2012-12-23 03:51:14 +00:00
|
|
|
|
"strings"
|
2012-12-24 20:38:32 +00:00
|
|
|
|
"text/template"
|
2012-12-23 03:51:14 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SIMPLATE_TYPE_RENDERED = "rendered"
|
|
|
|
|
SIMPLATE_TYPE_STATIC = "static"
|
|
|
|
|
SIMPLATE_TYPE_NEGOTIATED = "negotiated"
|
2012-12-23 04:32:26 +00:00
|
|
|
|
SIMPLATE_TYPE_JSON = "json"
|
2012-12-23 03:51:14 +00:00
|
|
|
|
)
|
|
|
|
|
|
2012-12-24 20:38:32 +00:00
|
|
|
|
var (
|
2012-12-25 19:22:21 +00:00
|
|
|
|
SIMPLATE_TYPES = []string{
|
|
|
|
|
SIMPLATE_TYPE_JSON,
|
|
|
|
|
SIMPLATE_TYPE_NEGOTIATED,
|
|
|
|
|
SIMPLATE_TYPE_RENDERED,
|
|
|
|
|
SIMPLATE_TYPE_STATIC,
|
|
|
|
|
}
|
2012-12-24 20:38:32 +00:00
|
|
|
|
simplateGenFileTmpl = template.Must(template.New("smpltgen").Parse(strings.Replace(`
|
2012-12-25 05:06:55 +00:00
|
|
|
|
/* GENERATED FILE - DO NOT EDIT */
|
|
|
|
|
/* Rebuild with simplate filesystem parsing thingy! */
|
|
|
|
|
package smpltgen
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"text/template"
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
"github.com/meatballhat/box-o-sand/gotime/smplt"
|
|
|
|
|
)
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
{{.InitPage.Body}}
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
{{if .HasTemplatePage}}
|
|
|
|
|
const (
|
|
|
|
|
SIMPLATE_TMPL_{{.ConstName}} = __BACKTICK__{{.TemplatePage.Body}}__BACKTICK__
|
|
|
|
|
)
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
var (
|
|
|
|
|
simplateTmpl{{.FuncName}} = template.Must(template.New("{{.FuncName}}").Parse(SIMPLATE_TMPL_{{.ConstName}}))
|
|
|
|
|
)
|
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
|
|
func SimplateHandlerFunc{{.FuncName}}(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
var err error
|
|
|
|
|
ctx := make(map[string]interface{})
|
2012-12-24 20:38:32 +00:00
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
{{range .LogicPages}}
|
|
|
|
|
{{.Body}}
|
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
|
|
{{if .HasTemplatePage}}
|
|
|
|
|
var tmplBuf bytes.Buffer
|
|
|
|
|
err = simplateTmpl{{.FuncName}}.Execute(&tmplBuf, ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
w.Write(smplt.HTTP_500_RESPONSE)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "{{.ContentType}}")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
w.Write(tmplBuf.Bytes())
|
|
|
|
|
{{end}}
|
|
|
|
|
}
|
2012-12-24 20:38:32 +00:00
|
|
|
|
`, "__BACKTICK__", "`", -1)))
|
|
|
|
|
)
|
|
|
|
|
|
2012-12-23 03:51:14 +00:00
|
|
|
|
type Simplate struct {
|
2012-12-23 17:11:48 +00:00
|
|
|
|
Filename string
|
|
|
|
|
Type string
|
|
|
|
|
ContentType string
|
|
|
|
|
InitPage *SimplatePage
|
|
|
|
|
LogicPages []*SimplatePage
|
|
|
|
|
TemplatePage *SimplatePage
|
2012-12-23 16:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SimplatePage struct {
|
|
|
|
|
Body string
|
2012-12-23 03:51:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 19:22:21 +00:00
|
|
|
|
func NewSimplateFromString(filename, content string) *Simplate {
|
2012-12-23 16:07:23 +00:00
|
|
|
|
rawPages := strings.Split(content, "")
|
|
|
|
|
nbreaks := len(rawPages) - 1
|
2012-12-23 03:51:14 +00:00
|
|
|
|
|
|
|
|
|
s := &Simplate{
|
2012-12-23 04:32:26 +00:00
|
|
|
|
Filename: filename,
|
|
|
|
|
Type: SIMPLATE_TYPE_STATIC,
|
|
|
|
|
ContentType: mime.TypeByExtension(path.Ext(filename)),
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if nbreaks == 1 || nbreaks == 2 {
|
2012-12-23 16:07:23 +00:00
|
|
|
|
s.InitPage = &SimplatePage{Body: rawPages[0]}
|
|
|
|
|
s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]})
|
2012-12-23 17:11:48 +00:00
|
|
|
|
|
|
|
|
|
if s.ContentType == "application/json" {
|
|
|
|
|
s.Type = SIMPLATE_TYPE_JSON
|
|
|
|
|
} else {
|
|
|
|
|
s.Type = SIMPLATE_TYPE_RENDERED
|
|
|
|
|
s.TemplatePage = &SimplatePage{Body: rawPages[2]}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 03:51:14 +00:00
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if nbreaks > 2 {
|
|
|
|
|
s.Type = SIMPLATE_TYPE_NEGOTIATED
|
2012-12-23 17:11:48 +00:00
|
|
|
|
s.InitPage = &SimplatePage{Body: rawPages[0]}
|
|
|
|
|
|
|
|
|
|
for _, rawPage := range rawPages {
|
|
|
|
|
s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPage})
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 03:51:14 +00:00
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
2012-12-24 05:12:11 +00:00
|
|
|
|
|
2012-12-24 17:03:21 +00:00
|
|
|
|
func (me *Simplate) Execute(wr io.Writer) error {
|
2012-12-24 20:38:32 +00:00
|
|
|
|
return simplateGenFileTmpl.Execute(wr, me)
|
|
|
|
|
}
|
2012-12-24 17:03:21 +00:00
|
|
|
|
|
2012-12-24 20:38:32 +00:00
|
|
|
|
func (me *Simplate) escapedFilename() string {
|
|
|
|
|
lessDots := strings.Replace(me.Filename, ".", "-DOT-", -1)
|
|
|
|
|
lessSlashes := strings.Replace(lessDots, "/", "-SLASH-", -1)
|
|
|
|
|
return strings.Replace(lessSlashes, " ", "-SPACE-", -1)
|
2012-12-24 05:12:11 +00:00
|
|
|
|
}
|
2012-12-24 17:03:21 +00:00
|
|
|
|
|
|
|
|
|
func (me *Simplate) OutputName() string {
|
|
|
|
|
if me.Type == SIMPLATE_TYPE_STATIC {
|
|
|
|
|
return me.Filename
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-24 20:38:32 +00:00
|
|
|
|
return me.escapedFilename() + ".go"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (me *Simplate) FuncName() string {
|
|
|
|
|
escaped := me.escapedFilename()
|
|
|
|
|
parts := strings.Split(escaped, "-")
|
|
|
|
|
for i, part := range parts {
|
|
|
|
|
var capitalized []string
|
|
|
|
|
capitalized = append(capitalized, strings.ToUpper(string(part[0])))
|
|
|
|
|
capitalized = append(capitalized, strings.ToLower(part[1:]))
|
|
|
|
|
parts[i] = strings.Join(capitalized, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(parts, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (me *Simplate) ConstName() string {
|
|
|
|
|
escaped := me.escapedFilename()
|
|
|
|
|
uppered := strings.ToUpper(escaped)
|
|
|
|
|
return strings.Replace(uppered, "-", "_", -1)
|
2012-12-24 17:03:21 +00:00
|
|
|
|
}
|
2012-12-25 05:06:55 +00:00
|
|
|
|
|
|
|
|
|
func (me *Simplate) HasTemplatePage() bool {
|
|
|
|
|
return len(me.TemplatePage.Body) > 0
|
|
|
|
|
}
|