commit 0b4e989b19b9f582079792c29f56bf2a38b418c1 Author: gutmet Date: Mon Dec 5 23:10:20 2022 +0100 Initial diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b698135 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module hedgedoc-watcher + +go 1.14 + +require github.com/russross/blackfriday/v2 v2.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..502a072 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/hedgedoc-watcher.go b/hedgedoc-watcher.go new file mode 100755 index 0000000..970951c --- /dev/null +++ b/hedgedoc-watcher.go @@ -0,0 +1,127 @@ +///bin/sh -c true; exec /usr/bin/env go run "$0" "$@" +package main + +import ( + "encoding/json" + "io/ioutil" + "net/url" + "os" + "os/exec" + "path" + "strings" + "text/template" + "time" + + "github.com/russross/blackfriday/v2" +) + +const ( + DL_LIMIT = 15 * 1024 * 1024 + CACHE_FOLDER = "cache" +) + +const RSS_TEMPLATE string = ` + + +<![CDATA[ {{.ID}} ]]> + + + + + <![CDATA[ {{.Server}}/{{.ID}} ]]> + + + + {{.Modified.Format "Mon, 02 Jan 2006 15:04:05 -0700"}} + + + + +` + +func optPanic(err error) { + if err != nil { + panic(err) + } +} + +func (n *Note) filename() string { + return path.Join(CACHE_FOLDER, n.ID) +} + +func (n *Note) setContent() { + cmd := exec.Command("hedgedoc", "export", "--md", n.ID) + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, "HEDGEDOC_SERVER="+n.Server) + if n.CookiesFile != "" { + cmd.Env = append(cmd.Env, "HEDGEDOC_COOKIES_FILE="+n.CookiesFile) + } + filename, err := cmd.Output() + optPanic(err) + b, err := ioutil.ReadFile(strings.TrimSpace(string(filename))) + optPanic(err) + n.Content = string(blackfriday.Run(b)) + n.Modified = time.Now() +} + +func unmarshalNoteObject(filename string) Note { + bytes, err := ioutil.ReadFile(filename) + if err != nil { + bytes = []byte{} + } + var noteObject Note + err = json.Unmarshal(bytes, ¬eObject) + if err != nil { + noteObject = Note{} + } + return noteObject +} + +func marshalNoteObject(filename string, note Note) { + bytes, err := json.MarshalIndent(note, "", " ") + optPanic(err) + err = ioutil.WriteFile(filename, bytes, 0644) + optPanic(err) +} + +func (newNote *Note) genRSS() { + var chosen Note + oldNote := unmarshalNoteObject(newNote.filename()) + if oldNote.Content != newNote.Content { + chosen = *newNote + } else { + chosen = oldNote + } + err := rssTemplate.Execute(os.Stdout, chosen) + optPanic(err) + marshalNoteObject(newNote.filename(), chosen) +} + +type Note struct { + Server string + ID string + Content string + Modified time.Time + CookiesFile string +} + +func NewNote(server string, ID string) *Note { + n := &Note{Server: server, ID: ID} + n.setContent() + return n +} + +var rssTemplate *template.Template + +func rssify(noteURL string) { + u, err := url.Parse(noteURL) + optPanic(err) + NewNote(u.Scheme+"://"+u.Host, u.Path[1:]).genRSS() +} + +func main() { + rssTemplate = template.Must(template.New("rss").Parse(RSS_TEMPLATE)) + os.Mkdir(CACHE_FOLDER, 0755) + // todo: Cookie-file flag + rssify(os.Args[1]) +}