Convert title and description to plain text

This commit is contained in:
Jason Streifling 2024-02-27 09:03:21 +01:00
parent 8f5739fb68
commit 59029c86a9
7 changed files with 145 additions and 97 deletions

View File

@ -20,3 +20,16 @@ func ConvertToHTML(md string) (string, error) {
return html, nil
}
func ConvertToPlain(md string) (string, error) {
var buf bytes.Buffer
if err := goldmark.Convert([]byte(md), &buf); err != nil {
return "", fmt.Errorf("error converting markdown to html: %v", err)
}
p := bluemonday.StrictPolicy()
plain := p.Sanitize(buf.String())
return plain, nil
}

View File

@ -5,56 +5,45 @@ import (
"html/template"
"log"
"net/http"
"time"
"github.com/gorilla/feeds"
"streifling.com/jason/cpolis/cmd/data"
)
func Login(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := r.PostFormValue("username")
pass := r.PostFormValue("password")
type AddUserData struct {
User string
First string
Last string
Role string
Msg string
}
id, err := db.GetID(user)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
return len(user) == 0 ||
len(pass) == 0 ||
len(pass2) == 0 ||
len(first) == 0 ||
len(last) == 0 ||
len(role) == 0
}
if err := db.CheckPassword(id, pass); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
func checkUserStrings(user, first, last string) (string, int, bool) {
userLen := 15
nameLen := 50
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
if len(user) > userLen {
return "Benutzername", userLen, false
} else if len(first) > nameLen {
return "Vorname", nameLen, false
} else if len(last) > nameLen {
return "Nachname", nameLen, false
} else {
return "", 0, true
}
}
func FinishEdit(feed *data.Feed) http.HandlerFunc {
func CreateUser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("editor-title")
desc := r.PostFormValue("editor-desc")
mdContent := r.PostFormValue("editor-text")
content, err := data.ConvertToHTML(mdContent)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
feed.Add(&feeds.Item{
Title: title,
Created: time.Now(),
Description: desc,
Content: content,
})
feed.Save("tmp/rss.gob")
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
}
}
@ -121,27 +110,3 @@ func AddUser(db *data.DB) http.HandlerFunc {
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}
func WriteArticle() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil)
}
}
func ShowRSS(feed *data.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rss, err := feed.ToRss()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss)
}
}
func CreateUser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
}
}

52
cmd/ui/editor.go Normal file
View File

@ -0,0 +1,52 @@
package ui
import (
"html/template"
"log"
"net/http"
"time"
"github.com/gorilla/feeds"
"streifling.com/jason/cpolis/cmd/data"
)
func WriteArticle() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil)
}
}
func FinishEdit(feed *data.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title, err := data.ConvertToPlain(r.PostFormValue("editor-title"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
desc, err := data.ConvertToPlain(r.PostFormValue("editor-desc"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
content, err := data.ConvertToHTML(r.PostFormValue("editor-text"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
feed.Add(&feeds.Item{
Title: title,
Created: time.Now(),
Description: desc,
Content: content,
})
feed.Save("tmp/rss.gob")
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}

View File

@ -1,25 +0,0 @@
package ui
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
return len(user) == 0 ||
len(pass) == 0 ||
len(pass2) == 0 ||
len(first) == 0 ||
len(last) == 0 ||
len(role) == 0
}
func checkUserStrings(user, first, last string) (string, int, bool) {
userLen := 15
nameLen := 50
if len(user) > userLen {
return "Benutzername", userLen, false
} else if len(first) > nameLen {
return "Vorname", nameLen, false
} else if len(last) > nameLen {
return "Nachname", nameLen, false
} else {
return "", 0, true
}
}

View File

@ -1,9 +0,0 @@
package ui
type AddUserData struct {
User string
First string
Last string
Role string
Msg string
}

21
cmd/ui/rss.go Normal file
View File

@ -0,0 +1,21 @@
package ui
import (
"html/template"
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/data"
)
func ShowRSS(feed *data.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rss, err := feed.ToRss()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss)
}
}

31
cmd/ui/session.go Normal file
View File

@ -0,0 +1,31 @@
package ui
import (
"html/template"
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/data"
)
func Login(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := r.PostFormValue("username")
pass := r.PostFormValue("password")
id, err := db.GetID(user)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := db.CheckPassword(id, pass); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}