initial commit
This commit is contained in:
commit
14a494bac2
|
@ -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
|
|
@ -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
|
|
@ -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)
|
||||
}
|
|
@ -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}
|
||||
]
|
||||
}
|
|
@ -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}}
|
|
@ -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))
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
all: togoist.go web.go
|
||||
@go generate
|
||||
|
||||
clean:
|
||||
rm -f togoist.js togoist.js.map
|
|
@ -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>
|
|
@ -0,0 +1,3 @@
|
|||
package main
|
||||
|
||||
//go:generate gopherjs build -o togoist.js
|
|
@ -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) })
|
||||
}
|
Loading…
Reference in New Issue