Create RSS from HTML

This commit is contained in:
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"
)
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)
}

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