-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
page.go
executable file
·207 lines (175 loc) · 4.61 KB
/
page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package server
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/schollz/versionedtext"
)
// Page is the basic struct
type Page struct {
Site *Site
Name string
Text versionedtext.VersionedText
Meta string
RenderedPage string
IsLocked bool
PassphraseToUnlock string
IsEncrypted bool
IsPrimedForSelfDestruct bool
IsPublished bool
UnlockedFor string
}
func (p Page) LastEditTime() time.Time {
return time.Unix(p.LastEditUnixTime(), 0)
}
func (p Page) LastEditUnixTime() int64 {
return p.Text.LastEditTime() / 1000000000
}
func (s *Site) Open(name string) (p *Page) {
p = new(Page)
p.Site = s
p.Name = name
p.Text = versionedtext.NewVersionedText("")
p.Render()
bJSON, err := ioutil.ReadFile(path.Join(s.PathToData, encodeToBase32(strings.ToLower(name))+".json"))
if err != nil {
return
}
err = json.Unmarshal(bJSON, &p)
if err != nil {
p = new(Page)
}
return p
}
type DirectoryEntry struct {
Path 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 (d DirectoryEntry) Name() string {
return d.Path
}
func (d DirectoryEntry) Size() int64 {
return int64(d.Length)
}
func (d DirectoryEntry) Mode() os.FileMode {
return os.ModePerm
}
func (d DirectoryEntry) ModTime() time.Time {
return d.LastEdited
}
func (d DirectoryEntry) IsDir() bool {
return false
}
func (d DirectoryEntry) Sys() interface{} {
return nil
}
func (s *Site) DirectoryList() []os.FileInfo {
files, _ := ioutil.ReadDir(s.PathToData)
entries := make([]os.FileInfo, len(files))
for i, f := range files {
name := DecodeFileName(f.Name())
p := s.Open(name)
entries[i] = DirectoryEntry{
Path: name,
Length: len(p.Text.GetCurrent()),
Numchanges: p.Text.NumEdits(),
LastEdited: time.Unix(p.Text.LastEditTime()/1000000000, 0),
}
}
sort.Slice(entries, func(i, j int) bool { return entries[i].ModTime().After(entries[j].ModTime()) })
return entries
}
type UploadEntry struct {
os.FileInfo
}
func (s *Site) UploadList() ([]os.FileInfo, error) {
paths, err := filepath.Glob(path.Join(s.PathToData, "sha256*"))
if err != nil {
return nil, err
}
result := make([]os.FileInfo, len(paths))
for i := range paths {
result[i], err = os.Stat(paths[i])
if err != nil {
return result, err
}
}
return result, nil
}
func DecodeFileName(s string) string {
s2, _ := decodeFromBase32(strings.Split(s, ".")[0])
return s2
}
// Update cleans the text and updates the versioned text
// and generates a new render
func (p *Page) Update(newText string) error {
// Trim space from end
newText = strings.TrimRight(newText, "\n\t ")
// Update the versioned text
p.Text.Update(newText)
// Render the new page
p.Render()
return p.Save()
}
var rBracketPage = regexp.MustCompile(`\[\[(.*?)\]\]`)
func (p *Page) Render() {
if p.IsEncrypted {
p.RenderedPage = "<code>" + p.Text.GetCurrent() + "</code>"
return
}
// Convert [[page]] to [page](/page/view)
currentText := p.Text.GetCurrent()
for _, s := range rBracketPage.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 {
p.Site.saveMut.Lock()
defer p.Site.saveMut.Unlock()
bJSON, err := json.MarshalIndent(p, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
}
func (p *Page) ChildPageNames() []string {
prefix := strings.ToLower(p.Name + ": ")
files, err := filepath.Glob(path.Join(p.Site.PathToData, "*"))
if err != nil {
panic("Filepath pattern cannot be malformed")
}
result := []string{}
for i := range files {
basename := filepath.Base(files[i])
if strings.HasSuffix(basename, ".json") {
cname, err := decodeFromBase32(basename[:len(basename)-len(".json")])
if err == nil && strings.HasPrefix(strings.ToLower(cname), prefix) {
result = append(result, cname)
}
}
}
return result
}
func (p *Page) IsNew() bool {
return !exists(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
}
func (p *Page) Erase() error {
p.Site.Logger.Trace("Erasing " + p.Name)
return os.Remove(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
}
func (p *Page) Published() bool {
return p.IsPublished
}