mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
522c4c6283
Former-commit-id: aefb0d00cd7f493ec8706d890eec1eb1510b7092 [formerly 400d9b4158b49fdd6b6910f772f02fc4f82fd2f6] [formerly 99230db788cdfac4fe4fa7e1266fbe77a54f60bf [formerly 420f56843f
]]
Former-commit-id: d780226ab14e3b022d497021ddc66c7d2e1be423 [formerly 6c38cf48fc1889ddef0dd1f9b7c36f0ed6525907]
Former-commit-id: 82e848729b0ea8b8deb5a310847490f5f22ad114
74 lines
1.7 KiB
Go
Executable File
74 lines
1.7 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/schollz/versionedtext"
|
|
)
|
|
|
|
// Page is the basic struct
|
|
type Page struct {
|
|
Name string
|
|
Text versionedtext.VersionedText
|
|
RenderedPage string
|
|
IsLocked bool
|
|
PassphraseToUnlock string
|
|
IsEncrypted bool
|
|
IsPrimedForSelfDestruct bool
|
|
}
|
|
|
|
func Open(name string) (p *Page) {
|
|
p = new(Page)
|
|
p.Name = name
|
|
p.Text = versionedtext.NewVersionedText("")
|
|
p.Render()
|
|
bJSON, err := ioutil.ReadFile(path.Join(pathToData, encodeToBase32(name)+".json"))
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(bJSON, &p)
|
|
if err != nil {
|
|
p = new(Page)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (p *Page) Update(newText string) error {
|
|
p.Text.Update(newText)
|
|
p.Render()
|
|
return p.Save()
|
|
}
|
|
|
|
func (p *Page) Render() {
|
|
if p.IsEncrypted {
|
|
p.RenderedPage = "<code>" + p.Text.GetCurrent() + "</code>"
|
|
return
|
|
}
|
|
|
|
// Convert [[page]] to [page](/page/view)
|
|
r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]")
|
|
currentText := p.Text.GetCurrent()
|
|
for _, s := range r.FindAllString(currentText, -1) {
|
|
currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1)
|
|
}
|
|
p.Text.Update(currentText)
|
|
p.RenderedPage = MarkdownToHtml(p.Text.GetCurrent())
|
|
}
|
|
|
|
func (p *Page) Save() error {
|
|
bJSON, err := json.MarshalIndent(p, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(p.Name)+".json"), bJSON, 0755)
|
|
}
|
|
|
|
func (p *Page) Erase() error {
|
|
return os.Remove(path.Join(pathToData, encodeToBase32(p.Name)+".json"))
|
|
}
|