From fb5ee8d09ee2d9d9ef31659a15df968e37f88375 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 22 Mar 2017 11:19:39 -0600 Subject: [PATCH] List works Former-commit-id: d123891bd72ab552a24bbccfd614270de340dac5 [formerly ce7c2d1b9dbdcaf5ef2d665e647833f065c18246] [formerly fc2e4872180a9f264e57e348e2ec4e5896781e74 [formerly 67d5d2f88271affeabf497cd0778b56b8463b503]] Former-commit-id: 1d9e245d81cab6cfe197bc52b516154c7429421b [formerly 1f22520a0e2509d0a509a592d1fed50002eccebc] Former-commit-id: b60b97411ea8c9b54b26b8058aeda0f1f0466b64 --- 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 }}