1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Works so much better

This commit is contained in:
Zack
2016-02-07 11:05:38 -05:00
parent a6d89e5585
commit 19aaf7bb44
14 changed files with 287 additions and 97 deletions

94
routes.go Executable file → Normal file
View File

@ -1,10 +1,10 @@
package main
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
@ -21,7 +21,7 @@ func editNote(c *gin.Context) {
title := c.Param("title")
if title == "ws" {
wshandler(c.Writer, c.Request)
} else if strings.ToLower(title) == "about" && strings.Contains(AllowedIPs, c.ClientIP()) != true {
} else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -34,7 +34,6 @@ func editNote(c *gin.Context) {
func everythingElse(c *gin.Context) {
option := c.Param("option")
title := c.Param("title")
fmt.Println(title, "["+option+"]")
if option == "/view" {
renderMarkdown(c, title)
} else if option == "/list" {
@ -69,21 +68,96 @@ func renderMarkdown(c *gin.Context, title string) {
})
}
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...)
}
func renderList(c *gin.Context, title string) {
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
}
p := CowyoData{strings.ToLower(title), ""}
err := p.load()
if err != nil {
panic(err)
}
listItems := []string{}
for _, line := range strings.Split(p.Text, "\n") {
if len(line) > 1 {
listItems = append(listItems, line)
}
}
fmt.Println(listItems)
listItems, _ := reorderList(p.Text)
c.HTML(http.StatusOK, "list.tmpl", gin.H{
"Title": title,
"ListItems": listItems,
})
}
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 {
if i+1 == lineNum {
if strings.Contains(lineString, "~~") == false {
p.Text = strings.Replace(p.Text, lineString[2:]+"\n", "~~"+lineString[2:]+"~~"+"\n", 1)
} else {
p.Text = strings.Replace(p.Text, lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
}
p.save()
break
}
}
c.JSON(200, gin.H{
"message": "Done.",
})
} else {
c.JSON(404, gin.H{
"message": "?",
})
}
}