mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
bca008f315
Former-commit-id: eb4cb02a7cde861255c9b425619ea8bf3d2f90a1 [formerly d603bd8d9e765a82d954b48586357b7593bd873b] [formerly 7ed56a670228947c7b180a7aeba1353a5f74b7c8 [formerly 5c9b621bb564be87a11d35a268d152476e4048d8 [formerlyb6a9c44bce
]]] Former-commit-id: ffa105847b5eeb409bd471f6fc2065d3ca0c14e4 [formerly e6c16a7b3c8b4d8371fc09f7c374d90af16b7bc1] Former-commit-id: 6b70c68d2f8ac7b11dd0631b1133f3c8b16841e9 Former-commit-id:fe8022d128
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) {
|
|
name = strings.ToLower(name) // no case
|
|
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"))
|
|
}
|