2016-02-07 16:45:42 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-02-07 19:05:38 +03:00
|
|
|
"strconv"
|
2016-02-07 16:45:42 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newNote(c *gin.Context) {
|
|
|
|
title := randomAlliterateCombo()
|
|
|
|
c.Redirect(302, "/"+title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editNote(c *gin.Context) {
|
|
|
|
title := c.Param("title")
|
|
|
|
if title == "ws" {
|
|
|
|
wshandler(c.Writer, c.Request)
|
2016-02-07 19:05:38 +03:00
|
|
|
} else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
|
2016-02-07 16:45:42 +03:00
|
|
|
c.Redirect(302, "/about/view")
|
|
|
|
} else {
|
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
"Title": title,
|
|
|
|
"ExternalIP": ExternalIP,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func everythingElse(c *gin.Context) {
|
|
|
|
option := c.Param("option")
|
|
|
|
title := c.Param("title")
|
|
|
|
if option == "/view" {
|
|
|
|
renderMarkdown(c, title)
|
|
|
|
} else if option == "/list" {
|
|
|
|
renderList(c, title)
|
|
|
|
} else if title == "static" {
|
|
|
|
serveStaticFile(c, option)
|
|
|
|
} else {
|
|
|
|
c.Redirect(302, "/"+title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveStaticFile(c *gin.Context, option string) {
|
|
|
|
staticFile, err := ioutil.ReadFile("./static" + option)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
} else {
|
|
|
|
c.Data(200, contentType(option), []byte(staticFile))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderMarkdown(c *gin.Context, title string) {
|
|
|
|
p := CowyoData{strings.ToLower(title), ""}
|
|
|
|
err := p.load()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
unsafe := blackfriday.MarkdownCommon([]byte(p.Text))
|
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
|
|
|
c.HTML(http.StatusOK, "view.tmpl", gin.H{
|
|
|
|
"Title": title,
|
|
|
|
"Body": template.HTML(html),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:05:38 +03:00
|
|
|
func reorderList(text string) ([]template.HTML, []string) {
|
|
|
|
listItemsString := ""
|
|
|
|
for _, lineString := range strings.Split(text, "\n") {
|
|
|
|
if len(lineString) > 1 {
|
|
|
|
if string(lineString[0]) != "-" {
|
|
|
|
listItemsString += "- " + lineString + "\n"
|
|
|
|
} else {
|
|
|
|
listItemsString += lineString + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ordering of template.HTML for rendering
|
|
|
|
renderedListString := string(blackfriday.MarkdownCommon([]byte(listItemsString)))
|
|
|
|
listItems := []template.HTML{}
|
|
|
|
endItems := []template.HTML{}
|
|
|
|
for _, lineString := range strings.Split(renderedListString, "\n") {
|
|
|
|
if len(lineString) > 1 {
|
|
|
|
if strings.Contains(lineString, "<del>") || strings.Contains(lineString, "</ul>") {
|
|
|
|
endItems = append(endItems, template.HTML(lineString))
|
|
|
|
} else {
|
|
|
|
listItems = append(listItems, template.HTML(lineString))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ordering of strings for deleting
|
|
|
|
listItemsStringArray := []string{}
|
|
|
|
endItemsStringArray := []string{}
|
|
|
|
for _, lineString := range strings.Split(listItemsString, "\n") {
|
|
|
|
if len(lineString) > 1 {
|
|
|
|
if strings.Contains(lineString, "~~") {
|
|
|
|
endItemsStringArray = append(endItemsStringArray, lineString)
|
|
|
|
} else {
|
|
|
|
listItemsStringArray = append(listItemsStringArray, lineString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(listItems, endItems...), append(listItemsStringArray, endItemsStringArray...)
|
|
|
|
}
|
|
|
|
|
2016-02-07 16:45:42 +03:00
|
|
|
func renderList(c *gin.Context, title string) {
|
2016-02-07 19:05:38 +03:00
|
|
|
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
|
|
|
|
c.Redirect(302, "/about/view")
|
|
|
|
}
|
2016-02-07 16:45:42 +03:00
|
|
|
p := CowyoData{strings.ToLower(title), ""}
|
|
|
|
err := p.load()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-02-07 19:05:38 +03:00
|
|
|
|
|
|
|
listItems, _ := reorderList(p.Text)
|
|
|
|
|
2016-02-07 16:45:42 +03:00
|
|
|
c.HTML(http.StatusOK, "list.tmpl", gin.H{
|
|
|
|
"Title": title,
|
|
|
|
"ListItems": listItems,
|
|
|
|
})
|
|
|
|
}
|
2016-02-07 19:05:38 +03:00
|
|
|
|
|
|
|
func deleteListItem(c *gin.Context) {
|
|
|
|
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
|
|
|
|
title := c.Query("title") // shortcut for c.Request.URL.Query().Get("lastname")
|
|
|
|
if err == nil {
|
|
|
|
p := CowyoData{strings.ToLower(title), ""}
|
|
|
|
err := p.load()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, listItems := reorderList(p.Text)
|
|
|
|
|
|
|
|
for i, lineString := range listItems {
|
2016-02-07 19:11:39 +03:00
|
|
|
// fmt.Println(i, lineString, lineNum)
|
2016-02-07 19:05:38 +03:00
|
|
|
if i+1 == lineNum {
|
2016-02-07 19:11:39 +03:00
|
|
|
// fmt.Println("MATCHED")
|
2016-02-07 19:05:38 +03:00
|
|
|
if strings.Contains(lineString, "~~") == false {
|
2016-02-07 19:11:39 +03:00
|
|
|
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
|
|
|
|
p.Text = strings.Replace(p.Text+"\n", lineString[2:]+"\n", "~~"+lineString[2:]+"~~"+"\n", 1)
|
2016-02-07 19:05:38 +03:00
|
|
|
} else {
|
2016-02-07 19:11:39 +03:00
|
|
|
p.Text = strings.Replace(p.Text+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
|
2016-02-07 19:05:38 +03:00
|
|
|
}
|
|
|
|
p.save()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"message": "Done.",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.JSON(404, gin.H{
|
|
|
|
"message": "?",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|