From 58272a076af9c15332553144d09266a3cca56996 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 24 Dec 2012 15:38:32 -0500 Subject: [PATCH] Goofing around with code generation which feels crazy brittle so far and I'm thinking I'd rather do with ast/parser stuff. --- gotime/smplt/simplate.go | 73 +++++++++++++++++++++++++++++--------- gotime/smplt/smplt_test.go | 6 +++- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go index 02abb65..1b51737 100644 --- a/gotime/smplt/simplate.go +++ b/gotime/smplt/simplate.go @@ -1,11 +1,11 @@ package smplt import ( - "bufio" "io" "mime" "path" "strings" + "text/template" ) const ( @@ -15,6 +15,35 @@ const ( SIMPLATE_TYPE_JSON = "json" ) +var ( + simplateGenFileTmpl = template.Must(template.New("smpltgen").Parse(strings.Replace(` + /* GENERATED FILE - DO NOT EDIT */ + /* Rebuild with simplate filesystem parsing thingy! */ + package smpltgen + + import ( + "text/template" + ) + + {{.InitPage.Body}} + + const ( + SIMPLATE_TMPL_{{.ConstName}} = __BACKTICK__{{.TemplatePage.Body}}__BACKTICK__ + ) + + var ( + simplateTmpl{{.FuncName}} = template.Must(template.New("{{.FuncName}}").Parse(SIMPLATE_TMPL_{{.ConstName}})) + ) + + func SimplateHandlerFunc{{.FuncName}}(w http.ResponseWriter, req *http.Request) { + {{range .LogicPages}} + {{.Body}} + {{end}} + } + +`, "__BACKTICK__", "`", -1))) +) + type Simplate struct { Filename string Type string @@ -67,19 +96,13 @@ func SimplateFromString(filename, content string) *Simplate { } func (me *Simplate) Execute(wr io.Writer) error { - outbuf := bufio.NewWriter(wr) + return simplateGenFileTmpl.Execute(wr, me) +} - _, err := outbuf.WriteString("package smplt_gen\n") - if err != nil { - return err - } - - err = outbuf.Flush() - if err != nil { - return err - } - - return nil +func (me *Simplate) escapedFilename() string { + lessDots := strings.Replace(me.Filename, ".", "-DOT-", -1) + lessSlashes := strings.Replace(lessDots, "/", "-SLASH-", -1) + return strings.Replace(lessSlashes, " ", "-SPACE-", -1) } func (me *Simplate) OutputName() string { @@ -87,8 +110,24 @@ func (me *Simplate) OutputName() string { return me.Filename } - lessDots := strings.Replace(me.Filename, ".", "-DOT-", -1) - lessSlashes := strings.Replace(lessDots, "/", "-SLASH-", -1) - lessSpaces := strings.Replace(lessSlashes, " ", "-SPACE-", -1) - return lessSpaces + ".go" + 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) } diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go index 47e7aeb..3837cf9 100644 --- a/gotime/smplt/smplt_test.go +++ b/gotime/smplt/smplt_test.go @@ -241,7 +241,11 @@ func TestRenderedSimplateCanExecuteToWriter(t *testing.T) { func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) { mkTmpDir() - defer rmTmpDir() + if len(os.Getenv("SMPLT_TEST_NOCLEANUP")) > 0 { + fmt.Println("tmpdir =", tmpdir) + } else { + defer rmTmpDir() + } s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) outfile_name := path.Join(tmpdir, s.OutputName())