package handlers

import (
	"html/template"
	"log"
	"net/http"

	"streifling.com/jason/cpolis/cmd/articles"
)

func HandleFinishedEdit() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		md := r.PostFormValue("editor-textarea")

		html, err := articles.ConvertToHTML(md)
		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)
	}
}
