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"
|
2018-01-18 09:12:03 +03:00
|
|
|
"sort"
|
2017-03-22 19:59:48 +03:00
|
|
|
"strings"
|
2017-03-24 16:58:37 +03:00
|
|
|
"time"
|
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
|
2017-08-17 17:36:07 +03:00
|
|
|
Meta string
|
2017-03-22 05:46:05 +03:00
|
|
|
RenderedPage string
|
|
|
|
IsLocked bool
|
|
|
|
PassphraseToUnlock string
|
|
|
|
IsEncrypted bool
|
|
|
|
IsPrimedForSelfDestruct bool
|
2017-10-15 16:49:40 +03:00
|
|
|
IsPublished bool
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-18 09:05:07 +03:00
|
|
|
type DirectoryEntry struct {
|
|
|
|
Name string
|
|
|
|
Length int
|
|
|
|
Numchanges int
|
|
|
|
LastEdited time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DirectoryEntry) LastEditTime() string {
|
|
|
|
return d.LastEdited.Format("Mon Jan 2 15:04:05 MST 2006")
|
|
|
|
}
|
|
|
|
|
|
|
|
func DirectoryList() []DirectoryEntry {
|
2017-03-24 16:58:37 +03:00
|
|
|
files, _ := ioutil.ReadDir(pathToData)
|
2018-01-18 09:05:07 +03:00
|
|
|
entries := make([]DirectoryEntry, len(files))
|
2017-03-24 16:58:37 +03:00
|
|
|
for i, f := range files {
|
2018-01-18 09:05:07 +03:00
|
|
|
name := DecodeFileName(f.Name())
|
|
|
|
p := Open(name)
|
|
|
|
entries[i] = DirectoryEntry{
|
|
|
|
Name: name,
|
|
|
|
Length: len(p.Text.GetCurrent()),
|
|
|
|
Numchanges: p.Text.NumEdits(),
|
|
|
|
LastEdited: time.Unix(p.Text.LastEditTime()/1000000000, 0),
|
|
|
|
}
|
2017-03-24 16:58:37 +03:00
|
|
|
}
|
2018-01-18 09:12:03 +03:00
|
|
|
sort.Slice(entries, func(i, j int) bool { return entries[i].LastEdited.Before(entries[j].LastEdited) })
|
2018-01-18 09:05:07 +03:00
|
|
|
return entries
|
2017-03-24 16:58:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeFileName(s string) string {
|
|
|
|
s2, _ := decodeFromBase32(strings.Split(s, ".")[0])
|
|
|
|
return s2
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:53:59 +03:00
|
|
|
// Update cleans the text and updates the versioned text
|
|
|
|
// and generates a new render
|
2017-03-22 05:46:05 +03:00
|
|
|
func (p *Page) Update(newText string) error {
|
2017-04-11 16:53:59 +03:00
|
|
|
// Trim space from end
|
|
|
|
newText = strings.TrimRight(newText, "\n\t ")
|
|
|
|
|
|
|
|
// Update the versioned text
|
2017-03-22 05:46:05 +03:00
|
|
|
p.Text.Update(newText)
|
2017-04-11 16:53:59 +03:00
|
|
|
|
|
|
|
// Render the new page
|
2017-03-22 05:46:05 +03:00
|
|
|
p.Render()
|
2017-04-11 16:53:59 +03:00
|
|
|
|
2017-03-22 05:46:05 +03:00
|
|
|
return p.Save()
|
|
|
|
}
|
|
|
|
|
2017-10-02 14:58:39 +03:00
|
|
|
var rBracketPage = regexp.MustCompile(`\[\[(.*?)\]\]`)
|
|
|
|
|
2017-03-22 05:46:05 +03:00
|
|
|
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)
|
|
|
|
currentText := p.Text.GetCurrent()
|
2017-10-02 14:58:39 +03:00
|
|
|
for _, s := range rBracketPage.FindAllString(currentText, -1) {
|
2017-03-22 19:59:48 +03:00
|
|
|
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-25 02:52:37 +03:00
|
|
|
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 13:41:56 +03:00
|
|
|
func (p *Page) IsNew() bool {
|
|
|
|
return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-10-15 16:49:40 +03:00
|
|
|
|
|
|
|
func (p *Page) Published() bool {
|
|
|
|
return p.IsPublished
|
|
|
|
}
|