22.12.2018

This commit is contained in:
Mikolaj 2018-12-22 20:04:32 +01:00
parent edfb1bfa98
commit f6b2a37408
7 changed files with 51 additions and 16 deletions

7
bower_components/js/app.js vendored Normal file
View File

@ -0,0 +1,7 @@
$(function(){
$("#content").bind("input change", function(){
$.post("/getHtml", {md: $(this).val()}, function(resp){
$("#md").html(resp.html);
});
})
});

View File

@ -3,6 +3,8 @@ package helpers
import ( import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"github.com/russross/blackfriday"
) )
func GenerateId() string { func GenerateId() string {
@ -10,3 +12,7 @@ func GenerateId() string {
rand.Read(b) rand.Read(b)
return fmt.Sprintf("%x", b) return fmt.Sprintf("%x", b)
} }
func MarkdownToHtml(s string) string {
return string(blackfriday.MarkdownBasic([]byte(s)))
}

25
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers" "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) 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") id := r.FormValue("id")
title := r.FormValue("title") title := r.FormValue("title")
content := r.FormValue("content") contentMd := r.FormValue("content")
contentHtml := helpers.MarkdownToHtml(contentMd)
var post *models.Post var post *models.Post
if id != "" { if id != "" {
post = posts[id] post = posts[id]
post.Title = title post.Title = title
post.Content = content post.ContentHtml = contentHtml
post.ContentMd = contentMd
} else { } else {
post := models.NewPost(helpers.GenerateId(), title, content) post := models.NewPost(helpers.GenerateId(), title, contentHtml, contentMd)
posts[post.Id] = post posts[post.Id] = post
} }
@ -60,16 +63,29 @@ func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, para
rend.Redirect("/") 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() { func main() {
posts = make(map[string]*models.Post, 0) posts = make(map[string]*models.Post, 0)
fmt.Println("Listening port 3000") fmt.Println("Listening port 3000")
m := martini.Classic() m := martini.Classic()
unescapeFuncMap := template.FuncMap{"unescape": unescape}
m.Use(render.Renderer(render.Options{ m.Use(render.Renderer(render.Options{
Directory: "views", Directory: "views",
Layout: "layout", Layout: "layout",
Extensions: []string{".html"}, Extensions: []string{".html"},
Funcs: []template.FuncMap{unescapeFuncMap},
Charset: "UTF-8", Charset: "UTF-8",
IndentJSON: true, IndentJSON: true,
})) }))
@ -81,6 +97,7 @@ func main() {
m.Get("/edit/:id", edit) m.Get("/edit/:id", edit)
m.Post("/savePost", savePost) m.Post("/savePost", savePost)
m.Get("/deletePost/:id", deletePost) m.Get("/deletePost/:id", deletePost)
m.Post("/getHtml", getHtmlPost)
m.Run() m.Run()
} }

View File

@ -1,11 +1,12 @@
package models package models
type Post struct { type Post struct {
Id string Id string
Title string Title string
Content string ContentHtml string
ContentMd string
} }
func NewPost(id, title, content string) *Post { func NewPost(id, title, contentHtml, contentMd string) *Post {
return &Post{id, title, content} return &Post{id, title, contentHtml, contentMd}
} }

View File

@ -10,7 +10,7 @@
<div class="row"> <div class="row">
<div class="col-xs-2"></div> <div class="col-xs-2"></div>
<div class="col-xs-8"> <div class="col-xs-8">
{{ $value.Content }} {{ $value.ContentHtml | unescape }}
</div> </div>
<div class="col-xs-2"></div> <div class="col-xs-2"></div>

View File

@ -32,5 +32,6 @@
<script src="/bower_components/jquery/dist/jquery.min.js"></script> <script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script> <script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/js/app.js"></script>
</body> </body>
</html> </html>

View File

@ -1,5 +1,9 @@
<div class="row"> <div class="row">
<div class="col-xs-4"></div> <div class="col-xs-2">
{{ if.Id }}
<a href="/deletePost/{{.Id}}">Delete</a>
{{ end }}
</div>
<div class="col-xs-4"> <div class="col-xs-4">
<form action="/savePost" method="POST" role="form"> <form action="/savePost" method="POST" role="form">
<input type="hidden" name="id" value="{{.Id}}"/> <input type="hidden" name="id" value="{{.Id}}"/>
@ -9,15 +13,14 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="content">Content</label> <label for="content">Content</label>
<textarea name="content" id="content">{{.Content}}</textarea> <textarea name="content" id="content">{{.ContentMd}}</textarea>
</div> </div>
<button type="submit" class="btn btn-default">Submit</button> <button type="submit" class="btn btn-default">Submit</button>
</form> </form>
</div> </div>
<div class="col-xs-4"> <div class="col-xs-6" id="md">
{{ if.Id }} {{ .ContentHtml | unescape}}
<a href="/deletePost/{{.Id}}">Delete</a>
{{ end }}
</div> </div>
</div> </div>