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

Storing DIffs now...might not be compatible?

This commit is contained in:
Zack Scholl 2016-02-07 18:31:47 -05:00
parent 2134f25d3a
commit 965989530c
7 changed files with 80 additions and 31 deletions

25
db.go
View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/sergi/go-diff/diffmatchpatch"
) )
var db *bolt.DB var db *bolt.DB
@ -33,11 +34,13 @@ func Close() {
// Data for storing in DB // Data for storing in DB
type CowyoData struct { type CowyoData struct {
Title string Title string
Text string CurrentText string
Diffs []string
Timestamps []string
} }
func (p *CowyoData) load() error { func (p *CowyoData) load(title string) error {
if !open { if !open {
return fmt.Errorf("db must be opened before saving!") return fmt.Errorf("db must be opened before saving!")
} }
@ -47,9 +50,14 @@ func (p *CowyoData) load() error {
if b == nil { if b == nil {
return nil return nil
} }
k := []byte(p.Title) k := []byte(title)
val := b.Get(k) val := b.Get(k)
if val == nil { if val == nil {
// make new one
p.Title = title
p.CurrentText = ""
p.Diffs = []string{}
p.Timestamps = []string{}
return nil return nil
} }
err = p.decode(val) err = p.decode(val)
@ -65,7 +73,7 @@ func (p *CowyoData) load() error {
return nil return nil
} }
func (p *CowyoData) save() error { func (p *CowyoData) save(newText string) error {
if !open { if !open {
return fmt.Errorf("db must be opened before saving") return fmt.Errorf("db must be opened before saving")
} }
@ -74,6 +82,13 @@ func (p *CowyoData) save() error {
if err != nil { if err != nil {
return fmt.Errorf("create bucket: %s", err) return fmt.Errorf("create bucket: %s", err)
} }
// find diffs
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(p.CurrentText, newText, true)
delta := dmp.DiffToDelta(diffs)
p.CurrentText = newText
p.Timestamps = append(p.Timestamps, time.Now().String())
p.Diffs = append(p.Diffs, delta)
enc, err := p.encode() enc, err := p.encode()
if err != nil { if err != nil {
return fmt.Errorf("could not encode CowyoData: %s", err) return fmt.Errorf("could not encode CowyoData: %s", err)

View File

@ -55,10 +55,14 @@ Options:`)
defer Close() defer Close()
// Default page // Default page
p := CowyoData{"about", about_page} p := CowyoData{"about", about_page, []string{}, []string{}}
p.save() p.save(about_page)
fmt.Println(about_page) fmt.Println(about_page)
var q CowyoData
q.load("SpikySeaSlug")
rebuildTexts(q)
r := gin.Default() r := gin.Default()
r.LoadHTMLGlob("templates/*") r.LoadHTMLGlob("templates/*")
r.GET("/", newNote) r.GET("/", newNote)

View File

@ -56,12 +56,12 @@ func serveStaticFile(c *gin.Context, option string) {
} }
func renderMarkdown(c *gin.Context, title string) { func renderMarkdown(c *gin.Context, title string) {
p := CowyoData{strings.ToLower(title), ""} var p CowyoData
err := p.load() err := p.load(strings.ToLower(title))
if err != nil { if err != nil {
panic(err) panic(err)
} }
unsafe := blackfriday.MarkdownCommon([]byte(p.Text)) unsafe := blackfriday.MarkdownCommon([]byte(p.CurrentText))
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
html2 := string(html) html2 := string(html)
@ -126,13 +126,13 @@ func renderList(c *gin.Context, title string) {
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true { if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view") c.Redirect(302, "/about/view")
} }
p := CowyoData{strings.ToLower(title), ""} var p CowyoData
err := p.load() err := p.load(strings.ToLower(title))
if err != nil { if err != nil {
panic(err) panic(err)
} }
listItems, _ := reorderList(p.Text) listItems, _ := reorderList(p.CurrentText)
c.HTML(http.StatusOK, "list.tmpl", gin.H{ c.HTML(http.StatusOK, "list.tmpl", gin.H{
"Title": title, "Title": title,
@ -144,25 +144,25 @@ func deleteListItem(c *gin.Context) {
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None")) lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
title := c.Query("title") // shortcut for c.Request.URL.Query().Get("lastname") title := c.Query("title") // shortcut for c.Request.URL.Query().Get("lastname")
if err == nil { if err == nil {
p := CowyoData{strings.ToLower(title), ""} var p CowyoData
err := p.load() err := p.load(strings.ToLower(title))
if err != nil { if err != nil {
panic(err) panic(err)
} }
_, listItems := reorderList(p.Text) _, listItems := reorderList(p.CurrentText)
newText := p.CurrentText
for i, lineString := range listItems { for i, lineString := range listItems {
// fmt.Println(i, lineString, lineNum) // fmt.Println(i, lineString, lineNum)
if i+1 == lineNum { if i+1 == lineNum {
// fmt.Println("MATCHED") // fmt.Println("MATCHED")
if strings.Contains(lineString, "~~") == false { if strings.Contains(lineString, "~~") == false {
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n") // fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
p.Text = strings.Replace(p.Text+"\n", lineString[2:]+"\n", "~~"+lineString[2:]+"~~"+"\n", 1) newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+lineString[2:]+"~~"+"\n", 1)
} else { } else {
p.Text = strings.Replace(p.Text+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1) newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
} }
p.save() p.save(newText)
break break
} }
} }

BIN
static/img/gomascot.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
static/img/raspberrypi.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -1,9 +1,12 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
"github.com/sergi/go-diff/diffmatchpatch"
) )
var animals []string var animals []string
@ -30,7 +33,7 @@ Be cautious about writing sensitive information in the notes as anyone with the
Have fun. Have fun.
**Powered by Raspberry Pi 2** ![jk](http://kodi.tv/wp-content/uploads/DL_Icons_RaspberryPi-new.png)` **Powered by Raspberry Pi and Go** ![Raspberry Pi](/static/img/raspberrypi.png) ![Go Mascot](/static/img/gomascot.png)`
} }
@ -70,3 +73,28 @@ func contentType(filename string) string {
} }
var about_page string var about_page string
func diffRebuildtexts(diffs []diffmatchpatch.Diff) []string {
text := []string{"", ""}
for _, myDiff := range diffs {
if myDiff.Type != diffmatchpatch.DiffInsert {
text[0] += myDiff.Text
}
if myDiff.Type != diffmatchpatch.DiffDelete {
text[1] += myDiff.Text
}
}
return text
}
func rebuildTexts(p CowyoData) {
dmp := diffmatchpatch.New()
current := ""
for i, diff := range p.Diffs {
seq1, _ := dmp.DiffFromDelta(current, diff)
texts_linemode := diffRebuildtexts(seq1)
rebuilt := texts_linemode[len(texts_linemode)-1]
fmt.Println(i, p.Timestamps[i], rebuilt)
current = rebuilt
}
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -40,21 +39,24 @@ func wshandler(w http.ResponseWriter, r *http.Request) {
panic(err) panic(err)
} }
var p CowyoData
err = p.load(m.Title)
fmt.Println("LOADED")
fmt.Println(p)
if err != nil {
panic(err)
}
if m.UpdateServer { if m.UpdateServer {
p := CowyoData{strings.ToLower(m.Title), m.TextData} err := p.save(m.TextData)
err := p.save() fmt.Println("SAVED")
fmt.Println(p)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
if m.UpdateClient { if m.UpdateClient {
p := CowyoData{strings.ToLower(m.Title), ""} m.UpdateClient = len(m.TextData) != len(p.CurrentText)
err := p.load() m.TextData = p.CurrentText
if err != nil {
panic(err)
}
m.UpdateClient = len(m.TextData) != len(p.Text)
m.TextData = p.Text
} }
newMsg, err := json.Marshal(m) newMsg, err := json.Marshal(m)
if err != nil { if err != nil {