diff options
-rw-r--r-- | cmd/ponzu/generate.go | 90 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-checkbox.tmpl | 5 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-content.tmpl (renamed from cmd/ponzu/contentType.tmpl) | 8 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-custom.tmpl | 6 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-file.tmpl | 4 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-hidden.tmpl | 3 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-input.tmpl | 5 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-richtext.tmpl | 4 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-select.tmpl | 5 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-tags.tmpl | 4 | ||||
-rw-r--r-- | cmd/ponzu/templates/gen-textarea.tmpl | 4 |
11 files changed, 124 insertions, 14 deletions
diff --git a/cmd/ponzu/generate.go b/cmd/ponzu/generate.go index e98e3fe..a4a9d2c 100644 --- a/cmd/ponzu/generate.go +++ b/cmd/ponzu/generate.go @@ -18,8 +18,10 @@ type generateType struct { type generateField struct { Name string + Initial string TypeName string JSONName string + View string } // blog title:string Author:string PostCategory:string content:string some_thing:int @@ -31,10 +33,14 @@ func parseType(args []string) (generateType, error) { fields := args[1:] for _, field := range fields { - f, err := parseField(field) + f, err := parseField(field, t) if err != nil { return generateType{}, err } + // NEW + // set initial (1st character of the type's name) on field so we don't need + // to set the template variable like was done in prior version + f.Initial = t.Initial t.Fields = append(t.Fields, f) } @@ -42,17 +48,29 @@ func parseType(args []string) (generateType, error) { return t, nil } -func parseField(raw string) (generateField, error) { - // title:string +func parseField(raw string, gt generateType) (generateField, error) { + // contents:string or // contents:string:richtext if !strings.Contains(raw, ":") { return generateField{}, fmt.Errorf("Invalid generate argument. [%s]", raw) } - pair := strings.Split(raw, ":") + data := strings.Split(raw, ":") + field := generateField{ - Name: fieldName(pair[0]), - TypeName: strings.ToLower(pair[1]), - JSONName: fieldJSONName(pair[0]), + Name: fieldName(data[0]), + Initial: gt.Initial, + TypeName: strings.ToLower(data[1]), + JSONName: fieldJSONName(data[0]), + } + + fieldType := "input" + if len(data) == 3 { + fieldType = data[2] + } + + err := setFieldView(&field, fieldType) + if err != nil { + return generateField{}, err } return field, nil @@ -116,6 +134,62 @@ func fieldJSONName(name string) string { return name } +// set the specified view inside the editor field for a generated field for a type +func setFieldView(field *generateField, viewType string) error { + var err error + var tmpl *template.Template + buf := &bytes.Buffer{} + + pwd, err := os.Getwd() + if err != nil { + return err + } + + tmplDir := filepath.Join(pwd, "cmd", "ponzu", "templates") + tmplFrom := func(filename string) (*template.Template, error) { + return template.ParseFiles(filepath.Join(tmplDir, filename)) + } + + viewType = strings.ToLower(viewType) + switch viewType { + case "checkbox": + tmpl, err = tmplFrom("gen-checkbox.tmpl") + case "custom": + tmpl, err = tmplFrom("gen-custom.tmpl") + case "file": + tmpl, err = tmplFrom("gen-file.tmpl") + case "hidden": + tmpl, err = tmplFrom("gen-hidden.tmpl") + case "input", "text": + tmpl, err = tmplFrom("gen-input.tmpl") + case "richtext": + tmpl, err = tmplFrom("gen-richtext.tmpl") + case "select": + tmpl, err = tmplFrom("gen-select.tmpl") + case "textarea": + tmpl, err = tmplFrom("gen-textarea.tmpl") + case "tags": + tmpl, err = tmplFrom("gen-tags.tmpl") + default: + msg := fmt.Sprintf("'%s' is not a recognized view type. Using 'input' instead.", viewType) + fmt.Println(msg) + tmpl, err = tmplFrom("gen-input.tmpl") + } + + if err != nil { + return err + } + + err = tmpl.Execute(buf, field) + if err != nil { + return err + } + + field.View = buf.String() + + return nil +} + func isUpper(char rune) bool { if char >= 'A' && char <= 'Z' { return true @@ -163,7 +237,7 @@ func generateContentType(args []string) error { return fmt.Errorf("Failed to parse type args: %s", err.Error()) } - tmplPath := filepath.Join(pwd, "cmd", "ponzu", "contentType.tmpl") + tmplPath := filepath.Join(pwd, "cmd", "ponzu", "templates", "gen-content.tmpl") tmpl, err := template.ParseFiles(tmplPath) if err != nil { return fmt.Errorf("Failed to parse template: %s", err.Error()) diff --git a/cmd/ponzu/templates/gen-checkbox.tmpl b/cmd/ponzu/templates/gen-checkbox.tmpl new file mode 100644 index 0000000..23713dc --- /dev/null +++ b/cmd/ponzu/templates/gen-checkbox.tmpl @@ -0,0 +1,5 @@ +View: editor.Checkbox("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", +}, map[string]string{ + // "value": "Display Name", +}),
\ No newline at end of file diff --git a/cmd/ponzu/contentType.tmpl b/cmd/ponzu/templates/gen-content.tmpl index c16cfd6..2d92b88 100644 --- a/cmd/ponzu/contentType.tmpl +++ b/cmd/ponzu/templates/gen-content.tmpl @@ -20,13 +20,9 @@ func ({{ .Initial }} *{{ .Name }}) MarshalEditor() ([]byte, error) { view, err := editor.Form({{ .Initial }}, // Take note that the first argument to these Input-like functions // is the string version of each {{ .Name }} field, and must follow - // this pattern for auto-decoding and auto-encoding reasons:{{ $initial := .Initial }} + // this pattern for auto-decoding and auto-encoding reasons: {{ range .Fields }}editor.Field{ - View: editor.Input("{{ .Name }}", {{ $initial }}, map[string]string{ - "label": "{{ .Name }}", - "type": "text", - "placeholder": "Enter the {{ .Name }} here", - }), + {{ .View }} }, {{ end }} ) diff --git a/cmd/ponzu/templates/gen-custom.tmpl b/cmd/ponzu/templates/gen-custom.tmpl new file mode 100644 index 0000000..6079f8b --- /dev/null +++ b/cmd/ponzu/templates/gen-custom.tmpl @@ -0,0 +1,6 @@ +View: []byte(` + <div class="input-field col s12"> + <label class="active">{{ .Name }}</label> + <!-- Add your custom editor field view here. --> + </div> + `),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-file.tmpl b/cmd/ponzu/templates/gen-file.tmpl new file mode 100644 index 0000000..7bcaa4c --- /dev/null +++ b/cmd/ponzu/templates/gen-file.tmpl @@ -0,0 +1,4 @@ +View: editor.File("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", + "placeholder": "Upload the {{ .Name }} here", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-hidden.tmpl b/cmd/ponzu/templates/gen-hidden.tmpl new file mode 100644 index 0000000..4b00456 --- /dev/null +++ b/cmd/ponzu/templates/gen-hidden.tmpl @@ -0,0 +1,3 @@ +View: editor.Input("{{ .Name }}", {{ .Initial }}, map[string]string{ + "type": "hidden", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-input.tmpl b/cmd/ponzu/templates/gen-input.tmpl new file mode 100644 index 0000000..8bea12a --- /dev/null +++ b/cmd/ponzu/templates/gen-input.tmpl @@ -0,0 +1,5 @@ +View: editor.Input("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", + "type": "text", + "placeholder": "Enter the {{ .Name }} here", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-richtext.tmpl b/cmd/ponzu/templates/gen-richtext.tmpl new file mode 100644 index 0000000..c7ec18c --- /dev/null +++ b/cmd/ponzu/templates/gen-richtext.tmpl @@ -0,0 +1,4 @@ +View: editor.Richtext("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", + "placeholder": "Enter the {{ .Name }} here", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-select.tmpl b/cmd/ponzu/templates/gen-select.tmpl new file mode 100644 index 0000000..509eb30 --- /dev/null +++ b/cmd/ponzu/templates/gen-select.tmpl @@ -0,0 +1,5 @@ +View: editor.Select("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", +}, map[string]string{ + // "value": "Display Name", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-tags.tmpl b/cmd/ponzu/templates/gen-tags.tmpl new file mode 100644 index 0000000..ca92c94 --- /dev/null +++ b/cmd/ponzu/templates/gen-tags.tmpl @@ -0,0 +1,4 @@ +View: editor.Tags("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", + "placeholder": "+{{ .Name }}", +}),
\ No newline at end of file diff --git a/cmd/ponzu/templates/gen-textarea.tmpl b/cmd/ponzu/templates/gen-textarea.tmpl new file mode 100644 index 0000000..af3dad8 --- /dev/null +++ b/cmd/ponzu/templates/gen-textarea.tmpl @@ -0,0 +1,4 @@ +View: editor.Textarea("{{ .Name }}", {{ .Initial }}, map[string]string{ + "label": "{{ .Name }}", + "placeholder": "Enter the {{ .Name }} here", +}),
\ No newline at end of file |