mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
3ba5537c1e
Former-commit-id: e0e540845dd56b55a7f88cddab6f75293318784c [formerly 6304d826ec6c5d7ce7f5c1dc6e821f0ba3488c30] [formerly 50ff4ab9306b5a0f2371699ece8b0baa6e49672f [formerly ff49e21ecc
]]
Former-commit-id: 7212147366f26318f0ce4363edbc977fd688da8e [formerly c8604e60d2eae61659622a1b0424cbe6e74bb927]
Former-commit-id: 3fb6fd7b4e514fcd5e9a9867efd3dcd6819cb893
63 lines
1.3 KiB
Go
Executable File
63 lines
1.3 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/schollz/versionedtext"
|
|
)
|
|
|
|
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
|
|
}
|
|
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"))
|
|
}
|