-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpage.go
182 lines (150 loc) · 4.31 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
package xlog
import (
"bytes"
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
)
// Markdown is used instead of string to make sure it's clear the string is markdown string
type Markdown string
// a Type that represent a page.
type Page interface {
// Name returns page name without '.md' extension
Name() string
// returns the filename, makes sure it converts slashes to backslashes when
// needed. this is safe to use when trying to access the file that represent the
// page
FileName() string
// checks if the page underlying file exists on disk or not.
Exists() bool
// Renders the page content to HTML. it makes sure all preprocessors are called
Render() template.HTML
// Reads the underlying file and returns the content
Content() Markdown
// Deletes the file and makes sure it triggers the AfterDelete event
Delete() bool
// Overwrite page content with new content. making sure to trigger before and
// after write events.
Write(Markdown) bool
// ModTime Return the last modification time of the underlying file
ModTime() time.Time
// Parses the page content and returns the Abstract Syntax Tree (AST).
// extensions can use it to walk the tree and modify it or collect statistics or
// parts of the page. for example the following "Emoji" function uses it to
// extract the first emoji.
AST() ([]byte, ast.Node)
}
type page struct {
name string
l sync.Mutex
lastUpdate time.Time
ast ast.Node
content *Markdown
}
func (p *page) Name() string {
return p.name
}
func (p *page) FileName() string {
return filepath.FromSlash(p.name) + ".md"
}
func (p *page) Exists() bool {
_, err := os.Stat(p.FileName())
return err == nil
}
func (p *page) Render() template.HTML {
src, ast := p.AST()
var buf bytes.Buffer
if err := MarkdownConverter().Renderer().Render(&buf, src, ast); err != nil {
return template.HTML(err.Error())
}
return template.HTML(buf.String())
}
func (p *page) Content() Markdown {
dat, err := os.ReadFile(p.FileName())
if err != nil {
return ""
}
return Markdown(dat)
}
func (p *page) preProcessedContent() Markdown {
p.l.Lock()
defer p.l.Unlock()
modtime := p.ModTime()
if p.content == nil || !modtime.Equal(p.lastUpdate) {
c := p.Content()
c = PreProcess(c)
p.content = &c
p.lastUpdate = modtime
}
return Markdown(*p.content)
}
func (p *page) Delete() bool {
defer Trigger(PageDeleted, p)
p.clearCache()
if p.Exists() {
err := os.Remove(p.FileName())
if err != nil {
fmt.Printf("Can't delete `%s`, err: %s\n", p.Name(), err)
return false
}
}
return true
}
func (p *page) Write(content Markdown) bool {
defer Trigger(PageChanged, p)
p.clearCache()
name := p.FileName()
os.MkdirAll(filepath.Dir(name), 0700)
content = Markdown(strings.ReplaceAll(string(content), "\r\n", "\n"))
if err := os.WriteFile(name, []byte(content), 0644); err != nil {
fmt.Printf("Can't write `%s`, err: %s\n", p.Name(), err)
return false
}
return true
}
func (p *page) ModTime() time.Time {
s, err := os.Stat(p.FileName())
if err != nil {
return time.Time{}
}
return s.ModTime()
}
func (p *page) AST() (source []byte, tree ast.Node) {
lastModified := p.lastUpdate
content := p.preProcessedContent()
if p.ast == nil || p.lastUpdate != lastModified {
p.ast = MarkdownConverter().Parser().Parse(text.NewReader([]byte(content)))
}
return []byte(content), p.ast
}
func (p *page) clearCache() {
p.content = nil
p.ast = nil
p.lastUpdate = time.Time{}
}
// DynamicPage implement Page interface and allow extensions to define a page to
// be passed to templates without having underlying file on desk
type DynamicPage struct {
NameVal string
RenderFn func() template.HTML
}
func (DynamicPage) FileName() string { return "" }
func (DynamicPage) Exists() bool { return false }
func (DynamicPage) Content() Markdown { return "" }
func (DynamicPage) Delete() bool { return false }
func (DynamicPage) Write(Markdown) bool { return false }
func (DynamicPage) ModTime() time.Time { return time.Time{} }
func (DynamicPage) AST() ([]byte, ast.Node) { return nil, nil }
func (d DynamicPage) Name() string { return d.NameVal }
func (d DynamicPage) Render() template.HTML {
if d.RenderFn != nil {
return d.RenderFn()
}
return ""
}