2017-03-22 05:46:05 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-03-22 19:59:48 +03:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-03-22 05:46:05 +03:00
|
|
|
|
|
|
|
"github.com/schollz/versionedtext"
|
|
|
|
)
|
|
|
|
|
2017-03-22 19:59:48 +03:00
|
|
|
// Page is the basic struct
|
2017-03-22 05:46:05 +03:00
|
|
|
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()
|
2017-03-23 06:22:26 +03:00
|
|
|
bJSON, err := ioutil.ReadFile(path.Join(pathToData, encodeToBase32(strings.ToLower(name))+".json"))
|
2017-03-22 05:46:05 +03:00
|
|
|
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
|
|
|
|
}
|
2017-03-22 19:59:48 +03:00
|
|
|
|
|
|
|
// 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)
|
2017-03-22 05:46:05 +03:00
|
|
|
p.RenderedPage = MarkdownToHtml(p.Text.GetCurrent())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Save() error {
|
|
|
|
bJSON, err := json.MarshalIndent(p, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-23 06:22:26 +03:00
|
|
|
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0755)
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Erase() error {
|
2017-03-24 01:24:25 +03:00
|
|
|
log.Trace("Erasing " + p.Name)
|
|
|
|
return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|