initial commit

This commit is contained in:
Chris Sexton 2016-10-09 14:10:34 -04:00
commit 14a494bac2
10 changed files with 207 additions and 0 deletions

13
Makefile Normal file
View File

@ -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

16
README.md Normal file
View File

@ -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

50
cmd/togoist/togoist.go Normal file
View File

@ -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)
}

7
sample/input.json Normal file
View File

@ -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}
]
}

5
sample/template.htm Normal file
View File

@ -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}}

26
togoist.go Normal file
View File

@ -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))
}

5
web/Makefile Normal file
View File

@ -0,0 +1,5 @@
all: togoist.go web.go
@go generate
clean:
rm -f togoist.js togoist.js.map

46
web/index.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Togoist</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
html, body, .container-fluid, .row, .col-md-4 {
height: 100%;
}
textarea.form-control {
height: 100%;
width: 100%;
}
</style>
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<textarea id="tmpl" rows="8" class="form-control">
TYPE,CONTENT,PRIORITY,INDENT,AUTHOR,RESPONSIBLE,DATE,DATE_LANG,TIMEZONE
{{- range .Tasks}}
{{if .Type}}{{.Type}}{{else}}task{{end}},{{.Content}},{{.Priority}},1,,,,,,
,,,,,,,,
{{- end}}
</textarea>
</div>
<div class="col-md-4">
<textarea id="input" rows="8" class="form-control">
{
"Tasks": [
{"Type": "test", "Content": "Test task 1", "Priority": 4},
{"Content": "Test task 2", "Priority": 4},
{"Content": "Test task 3", "Priority": 4}
]
}
</textarea>
</div>
<div class="col-md-4">
<textarea id="output" rows="8" class="form-control"></textarea>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha256-/SIrNqv8h6QGKDuNoLGA4iret+kyesCkHGzVUUV0shc=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="togoist.js"></script>

3
web/togoist.go Normal file
View File

@ -0,0 +1,3 @@
package main
//go:generate gopherjs build -o togoist.js

36
web/web.go Normal file
View File

@ -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) })
}