mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
20dd8d4f20
Former-commit-id: 75492d2819399d026bac979cdfdc9ca3b6cf1466 [formerly e32fd7161849a8e11d2b31f27deb04378f9dcbb4] [formerly da830c5c0d0b73548699f6a6341eb55f5afa130f [formerly 7a780609f7276ff3569323beb61cb12088e17447]]
Former-commit-id: a9a517909321225859867f0b031b8303d35102ae [formerly 59d8c184f6f6359f811b1413b259a01aaee93854]
Former-commit-id: f31efaed6d2b7175ea50f8205ae2fae034163518
Former-commit-id: fc6a38f047
75 lines
1.7 KiB
Go
Executable File
75 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(strings.ToLower(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(strings.ToLower(p.Name))+".json"), bJSON, 0755)
|
|
}
|
|
|
|
func (p *Page) Erase() error {
|
|
log.Trace("Erasing " + p.Name)
|
|
return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
|
}
|