Create RSS from HTML

This commit is contained in:
Jason Streifling 2024-02-18 12:41:49 +01:00
parent aa034701df
commit ee04a2a351
7 changed files with 67 additions and 10 deletions

View File

@ -7,10 +7,10 @@ import (
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
) )
func ConvertToHTML(markdown string) (string, error) { func ConvertToHTML(md string) (string, error) {
var buf bytes.Buffer 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) return "", fmt.Errorf("error: cmd/articles/markdown.go ConvertToHTML goldmark.Convert(): %v", err)
} }

39
cmd/feed/rss.go Normal file
View File

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

View File

@ -1,23 +1,25 @@
package handlers package handlers
import ( import (
"html/template"
"log" "log"
"net/http" "net/http"
"streifling.com/jason/cpolis/cmd/articles" "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) { 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 { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err) 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)
} }
} }

5
go.mod
View File

@ -2,4 +2,7 @@ module streifling.com/jason/cpolis
go 1.22.0 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
)

8
go.sum
View File

@ -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 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=

View File

@ -5,18 +5,22 @@ import (
"log" "log"
"net/http" "net/http"
"streifling.com/jason/cpolis/cmd/feed"
"streifling.com/jason/cpolis/cmd/handlers" "streifling.com/jason/cpolis/cmd/handlers"
) )
func main() { func main() {
mux := http.NewServeMux() 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.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/editor.html")).Execute(w, nil) 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)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -1,6 +1,7 @@
{{define "page-content"}} {{define "page-content"}}
<form> <form>
<textarea name="editor-textarea"></textarea> <input type="text" name="editor-title" value="Titel">
<textarea name="editor-text"></textarea>
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content"> <input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content">
</form> </form>
{{end}} {{end}}