diff --git a/cmd/articles/markdown.go b/cmd/articles/markdown.go index 43a724c..fd7d740 100644 --- a/cmd/articles/markdown.go +++ b/cmd/articles/markdown.go @@ -7,10 +7,10 @@ import ( "github.com/yuin/goldmark" ) -func ConvertToHTML(markdown string) (string, error) { +func ConvertToHTML(md string) (string, error) { var buf bytes.Buffer - if err := goldmark.Convert([]byte(markdown), &buf); err != nil { + if err := goldmark.Convert([]byte(md), &buf); err != nil { return "", fmt.Errorf("error: cmd/articles/markdown.go ConvertToHTML goldmark.Convert(): %v", err) } diff --git a/cmd/feed/rss.go b/cmd/feed/rss.go new file mode 100644 index 0000000..747732e --- /dev/null +++ b/cmd/feed/rss.go @@ -0,0 +1,39 @@ +package feed + +import ( + "fmt" + "time" + + "github.com/gorilla/feeds" +) + +type Feed struct { + *feeds.Feed +} + +func NewFeed(title, link, desc string) Feed { + return Feed{ + Feed: &feeds.Feed{ + Title: title, + Link: &feeds.Link{Href: link}, + Description: desc, + }, + } +} + +func AddToFeed(feed Feed, title, content string) error { + item := feeds.Item{ + Title: title, + Created: time.Now(), + Content: content, + } + feed.Add(&item) + + rss, err := feed.ToRss() + if err != nil { + return fmt.Errorf("error cmd/feed/rss.go AddToFeed feed.ToRss(): %v", err) + } + fmt.Println(rss) + + return nil +} diff --git a/cmd/handlers/editor.go b/cmd/handlers/editor.go index 84291a2..d1fa6ae 100644 --- a/cmd/handlers/editor.go +++ b/cmd/handlers/editor.go @@ -1,23 +1,25 @@ package handlers import ( - "html/template" "log" "net/http" "streifling.com/jason/cpolis/cmd/articles" + "streifling.com/jason/cpolis/cmd/feed" ) -func HandleFinishedEdit() http.HandlerFunc { +func HandleFinishedEdit(f feed.Feed) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - md := r.PostFormValue("editor-textarea") + title := r.PostFormValue("editor-title") + mdContent := r.PostFormValue("editor-text") - html, err := articles.ConvertToHTML(md) + content, err := articles.ConvertToHTML(mdContent) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } - template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", html) + feed.AddToFeed(f, title, content) + // template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem) } } diff --git a/go.mod b/go.mod index 9d3bf68..195a33f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module streifling.com/jason/cpolis go 1.22.0 -require github.com/yuin/goldmark v1.7.0 +require ( + github.com/gorilla/feeds v1.1.2 + github.com/yuin/goldmark v1.7.0 +) diff --git a/go.sum b/go.sum index dd24077..4a42e4c 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,10 @@ +github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw= +github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA= github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= diff --git a/main.go b/main.go index d609b82..d2ba7a1 100644 --- a/main.go +++ b/main.go @@ -5,18 +5,22 @@ import ( "log" "net/http" + "streifling.com/jason/cpolis/cmd/feed" "streifling.com/jason/cpolis/cmd/handlers" ) func main() { mux := http.NewServeMux() + feed := feed.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", + "https://distrikt-ni-st.de", + "Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität") mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/")))) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("web/templates/index.html", "web/templates/editor.html")).Execute(w, nil) }) - mux.HandleFunc("POST /finished-edit/", handlers.HandleFinishedEdit()) + mux.HandleFunc("POST /finished-edit/", handlers.HandleFinishedEdit(feed)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/web/templates/editor.html b/web/templates/editor.html index 010e823..86278e3 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -1,6 +1,7 @@ {{define "page-content"}}
{{end}}