diff --git a/bower_components/js/app.js b/bower_components/js/app.js new file mode 100644 index 0000000..2be60af --- /dev/null +++ b/bower_components/js/app.js @@ -0,0 +1,7 @@ +$(function(){ + $("#content").bind("input change", function(){ + $.post("/getHtml", {md: $(this).val()}, function(resp){ + $("#md").html(resp.html); + }); + }) +}); \ No newline at end of file diff --git a/helpers/random.go b/helpers/utils.go similarity index 52% rename from helpers/random.go rename to helpers/utils.go index 6def825..83d635b 100644 --- a/helpers/random.go +++ b/helpers/utils.go @@ -3,6 +3,8 @@ package helpers import ( "crypto/rand" "fmt" + + "github.com/russross/blackfriday" ) func GenerateId() string { @@ -10,3 +12,7 @@ func GenerateId() string { rand.Read(b) return fmt.Sprintf("%x", b) } + +func MarkdownToHtml(s string) string { + return string(blackfriday.MarkdownBasic([]byte(s))) +} diff --git a/main.go b/main.go index cf7352a..b7ba384 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "html/template" "net/http" "git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers" @@ -31,18 +32,20 @@ func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params mar rend.HTML(http.StatusOK, "write", post) } -func savePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) { +func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) { id := r.FormValue("id") title := r.FormValue("title") - content := r.FormValue("content") + contentMd := r.FormValue("content") + contentHtml := helpers.MarkdownToHtml(contentMd) var post *models.Post if id != "" { post = posts[id] post.Title = title - post.Content = content + post.ContentHtml = contentHtml + post.ContentMd = contentMd } else { - post := models.NewPost(helpers.GenerateId(), title, content) + post := models.NewPost(helpers.GenerateId(), title, contentHtml, contentMd) posts[post.Id] = post } @@ -60,16 +63,29 @@ func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, para rend.Redirect("/") } +func getHtmlPost(rend render.Render, w http.ResponseWriter, r *http.Request) { + md := r.FormValue("md") + rend.JSON(http.StatusOK, map[string]interface{}{ + "html": helpers.MarkdownToHtml(md), + }) +} +func unescape(s string) interface{} { + return template.HTML(s) +} + func main() { posts = make(map[string]*models.Post, 0) fmt.Println("Listening port 3000") m := martini.Classic() + unescapeFuncMap := template.FuncMap{"unescape": unescape} + m.Use(render.Renderer(render.Options{ Directory: "views", Layout: "layout", Extensions: []string{".html"}, + Funcs: []template.FuncMap{unescapeFuncMap}, Charset: "UTF-8", IndentJSON: true, })) @@ -81,6 +97,7 @@ func main() { m.Get("/edit/:id", edit) m.Post("/savePost", savePost) m.Get("/deletePost/:id", deletePost) + m.Post("/getHtml", getHtmlPost) m.Run() } diff --git a/models/post.go b/models/post.go index 5af6fae..2fbdf2a 100644 --- a/models/post.go +++ b/models/post.go @@ -1,11 +1,12 @@ package models type Post struct { - Id string - Title string - Content string + Id string + Title string + ContentHtml string + ContentMd string } -func NewPost(id, title, content string) *Post { - return &Post{id, title, content} +func NewPost(id, title, contentHtml, contentMd string) *Post { + return &Post{id, title, contentHtml, contentMd} } diff --git a/views/index.html b/views/index.html index 782f78f..e33fe57 100644 --- a/views/index.html +++ b/views/index.html @@ -10,7 +10,7 @@
- {{ $value.Content }} + {{ $value.ContentHtml | unescape }}
diff --git a/views/layout.html b/views/layout.html index 057ccf7..8a918d1 100644 --- a/views/layout.html +++ b/views/layout.html @@ -32,5 +32,6 @@ + diff --git a/views/write.html b/views/write.html index 74952e4..5592fb0 100644 --- a/views/write.html +++ b/views/write.html @@ -1,5 +1,9 @@
-
+
+ {{ if.Id }} + Delete + {{ end }} +
@@ -9,15 +13,14 @@
- +
-
- {{ if.Id }} - Delete - {{ end }} +
+ {{ .ContentHtml | unescape}} +