From 14a494bac21203a93940a548cc10e1c34490351f Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 9 Oct 2016 14:10:34 -0400 Subject: [PATCH] initial commit --- Makefile | 13 +++++++++++ README.md | 16 ++++++++++++++ cmd/togoist/togoist.go | 50 ++++++++++++++++++++++++++++++++++++++++++ sample/input.json | 7 ++++++ sample/template.htm | 5 +++++ togoist.go | 26 ++++++++++++++++++++++ web/Makefile | 5 +++++ web/index.html | 46 ++++++++++++++++++++++++++++++++++++++ web/togoist.go | 3 +++ web/web.go | 36 ++++++++++++++++++++++++++++++ 10 files changed, 207 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/togoist/togoist.go create mode 100644 sample/input.json create mode 100644 sample/template.htm create mode 100644 togoist.go create mode 100644 web/Makefile create mode 100644 web/index.html create mode 100644 web/togoist.go create mode 100644 web/web.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3009e3e --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +all: web togoist + +.PHONY: web + +web: + $(MAKE) -C web + +togoist: cmd/togoist/togoist.go + go build ./cmd/togoist + +clean: + rm -f togoist + $(MAKE) -C web clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..1a422ff --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Togoist + +Togoist is a small program to facilitate using Go's template library to generate CSV templates for Todoist. + +## Installation + +* `go get gitlab.com/chrissexton/togoist` +* `make` + +## Usage + +Open the `index.html` file in the web directory and modify the template and input fields or run `togoist -h` + +## License + +WTFPL diff --git a/cmd/togoist/togoist.go b/cmd/togoist/togoist.go new file mode 100644 index 0000000..5277bbc --- /dev/null +++ b/cmd/togoist/togoist.go @@ -0,0 +1,50 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "os" + + "gitlab.com/chrissexton/togoist" +) + +var tmplPath = flag.String("tmpl", "template.htm", "template file to parse") +var inputPath = flag.String("json", "", "JSON input (empty for stdin)") + +func main() { + flag.Parse() + + f, err := os.Open(*tmplPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not open template file.\n") + return + } + tmplBytes, err := ioutil.ReadAll(f) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not read template file.\n") + return + } + + if *inputPath != "" { + f, err = os.Open(*tmplPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not open input file.\n") + return + } + } else { + f = os.Stdin + } + inputBytes, err := ioutil.ReadAll(f) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not read input file.\n") + return + } + + out, err := togoist.Togo(tmplBytes, inputBytes) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not parse the template.\n%s\n", err) + return + } + fmt.Print(out) +} diff --git a/sample/input.json b/sample/input.json new file mode 100644 index 0000000..519aba3 --- /dev/null +++ b/sample/input.json @@ -0,0 +1,7 @@ +{ + "Tasks": [ + {"Type": "test", "Content": "Test task 1", "Priority": 4}, + {"Content": "Test task 2", "Priority": 4}, + {"Content": "Test task 3", "Priority": 4} + ] +} diff --git a/sample/template.htm b/sample/template.htm new file mode 100644 index 0000000..b43b65d --- /dev/null +++ b/sample/template.htm @@ -0,0 +1,5 @@ +TYPE,CONTENT,PRIORITY,INDENT,AUTHOR,RESPONSIBLE,DATE,DATE_LANG,TIMEZONE +{{- range .Tasks}} +{{if .Type}}{{.Type}}{{else}}task{{end}},{{.Content}},{{.Priority}},1,,,,,, +,,,,,,,, +{{- end}} diff --git a/togoist.go b/togoist.go new file mode 100644 index 0000000..3c690b8 --- /dev/null +++ b/togoist.go @@ -0,0 +1,26 @@ +package togoist + +import ( + "bytes" + "encoding/json" + "html/template" +) + +func TogoString(tmpl, jsonInput string) (string, error) { + t := template.Must(template.New("togoist").Parse(tmpl)) // must is probably wrong + var j interface{} + err := json.Unmarshal([]byte(jsonInput), &j) + if err != nil { + return "", err + } + b := []byte{} + out := bytes.NewBuffer(b) + err = t.Execute(out, j) + if err != nil { + return "", err + } + return out.String(), nil +} +func Togo(tmpl, jsonInput []byte) (string, error) { + return TogoString(string(tmpl), string(jsonInput)) +} diff --git a/web/Makefile b/web/Makefile new file mode 100644 index 0000000..7df3149 --- /dev/null +++ b/web/Makefile @@ -0,0 +1,5 @@ +all: togoist.go web.go + @go generate + +clean: + rm -f togoist.js togoist.js.map diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..4a68f62 --- /dev/null +++ b/web/index.html @@ -0,0 +1,46 @@ + + + Togoist + + + + +
+
+
+ +
+
+ +
+
+ +
+
+
+ + + + diff --git a/web/togoist.go b/web/togoist.go new file mode 100644 index 0000000..b48ee61 --- /dev/null +++ b/web/togoist.go @@ -0,0 +1,3 @@ +package main + +//go:generate gopherjs build -o togoist.js diff --git a/web/web.go b/web/web.go new file mode 100644 index 0000000..f8c065f --- /dev/null +++ b/web/web.go @@ -0,0 +1,36 @@ +package main + +import ( + "time" + + "github.com/bep/debounce" + "github.com/gopherjs/jquery" + "gitlab.com/chrissexton/togoist" +) + +var jQuery = jquery.NewJQuery + +func convert(tmpl, json string, cb func(error, string)) { + go func() { + res, err := togoist.TogoString(tmpl, json) + cb(err, res) + }() +} + +func handleChange() { + tmpl := jQuery("#tmpl").Val() + input := jQuery("#input").Val() + convert(tmpl, input, func(err error, out string) { + if err != nil { + jQuery("#output").SetText(err.Error()) + return + } + jQuery("#output").SetText(out) + }) +} + +func main() { + debounced, _ := debounce.New(time.Second) + jQuery("#tmpl").On(jquery.KEYUP, func() { debounced(handleChange) }) + jQuery("#input").On(jquery.KEYUP, func() { debounced(handleChange) }) +}