This commit is contained in:
gutmet 2022-12-05 23:10:20 +01:00
commit 0b4e989b19
3 changed files with 134 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module hedgedoc-watcher
go 1.14
require github.com/russross/blackfriday/v2 v2.1.0

2
go.sum Normal file
View File

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

127
hedgedoc-watcher.go Executable file
View File

@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title><![CDATA[ {{.ID}} ]]></title>
<link><![CDATA[ {{.Server}}/{{.ID}} ]]></link>
<description><![CDATA[ {{.ID}} ]]></description>
<item>
<title><![CDATA[ {{.Server}}/{{.ID}} ]]></title>
<content:encoded><![CDATA[ {{.Content}} ]]></content:encoded>
<guid><![CDATA[ {{.Server}}/{{.ID}}/{{.Modified.Format "20060102-150405"}} ]]></guid>
<link><![CDATA[ {{.Server}}/{{.ID}} ]]></link>
<pubDate>{{.Modified.Format "Mon, 02 Jan 2006 15:04:05 -0700"}}</pubDate>
</item>
</channel>
</rss>
`
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, &noteObject)
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])
}