From 67d5d2f88271affeabf497cd0778b56b8463b503 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 22 Mar 2017 11:19:39 -0600 Subject: [PATCH] List works --- handlers.go | 38 +++++++++++++++++++++++++++ listify.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 21 +++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 listify.go diff --git a/handlers.go b/handlers.go index 0e20a16..a4545b6 100755 --- a/handlers.go +++ b/handlers.go @@ -4,6 +4,7 @@ import ( "html/template" "net/http" "strconv" + "strings" "time" "github.com/gin-contrib/static" @@ -27,6 +28,7 @@ func serve(port string) { router.POST("/prime", handlePrime) router.POST("/lock", handleLock) router.POST("/encrypt", handleEncrypt) + router.DELETE("/listitem", deleteListItem) router.Run(":" + port) } @@ -85,6 +87,7 @@ func handlePageRequest(c *gin.Context) { "VersionsText": versionsText, "IsLocked": p.IsLocked, "IsEncrypted": p.IsEncrypted, + "ListItems": renderList(rawText), }) } @@ -211,3 +214,38 @@ func handleEncrypt(c *gin.Context) { q.Save() c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) } + +func deleteListItem(c *gin.Context) { + lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None")) + page := c.Query("page") // shortcut for c.Request.URL.Query().Get("lastname") + if err == nil { + p := Open(page) + + _, listItems := reorderList(p.Text.GetCurrent()) + newText := p.Text.GetCurrent() + for i, lineString := range listItems { + // fmt.Println(i, lineString, lineNum) + if i+1 == lineNum { + // fmt.Println("MATCHED") + if strings.Contains(lineString, "~~") == false { + // fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n") + newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+strings.TrimSpace(lineString[2:])+"~~"+"\n", 1) + } else { + newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1) + } + p.Update(newText) + break + } + } + + c.JSON(200, gin.H{ + "success": true, + "message": "Done.", + }) + } else { + c.JSON(200, gin.H{ + "success": false, + "message": err.Error(), + }) + } +} diff --git a/listify.go b/listify.go new file mode 100644 index 0000000..5fb2dfd --- /dev/null +++ b/listify.go @@ -0,0 +1,62 @@ +package main + +import ( + "html/template" + "strconv" + "strings" +) + +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 := MarkdownToHtml(listItemsString) + listItems := []template.HTML{} + endItems := []template.HTML{} + for _, lineString := range strings.Split(renderedListString, "\n") { + if len(lineString) > 1 { + if strings.Contains(lineString, "") || strings.Contains(lineString, "") { + 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(currentRawText string) []template.HTML { + listItems, _ := reorderList(currentRawText) + for i := range listItems { + newHTML := strings.Replace(string(listItems[i]), "", ""+``, -1) + newHTML = strings.Replace(newHTML, "", "
  • "+``, -1) + newHTML = strings.Replace(newHTML, "
  • ", "
    ", -1) + newHTML = strings.Replace(newHTML, "
  • "+``, "
  • "+``, -1) + newHTML = strings.Replace(newHTML, "
  • ", "
    ", -1) + listItems[i] = template.HTML([]byte(newHTML)) + } + return listItems +} diff --git a/templates/index.html b/templates/index.html index 034c2da..0dcd1bc 100755 --- a/templates/index.html +++ b/templates/index.html @@ -241,6 +241,22 @@ } }); + $('.deletable').click(function(event) { + event.preventDefault(); + var lineNum = $(this).attr('id') + $.ajax({ + url: "/listitem" + '?' + $.param({ + "lineNum": lineNum, + "page": "{{ .Page }}" + }), + type: 'DELETE', + success: function() { + window.location.reload(true); + } + }); + }); + + }); //]]> @@ -284,6 +300,11 @@ {{end}} {{ end }} + {{ if .ListPage }} + {{ range $index, $element := .ListItems }} + {{ $element }} + {{ end }} + {{ end }}