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

25
main.go
View File

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

View File

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

View File

@ -10,7 +10,7 @@
<div class="row">
<div class="col-xs-2"></div>
<div class="col-xs-8">
{{ $value.Content }}
{{ $value.ContentHtml | unescape }}
</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/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/js/app.js"></script>
</body>
</html>

View File

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