-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
51 lines (45 loc) · 1.07 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
package scratch
import (
"bytes"
"html/template"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
const HelpMsg = "Tab indent, Shift+Tab outdent, Ctrl+B bold, Ctrl+I italic, Ctrl+L insert a link, Ctrl+G insert an image"
type Page struct {
Secret []byte
Path string
Modified time.Time
Content template.HTML
}
func cleanHost(host string) string {
if pos := strings.LastIndex(host, ":"); pos >= 0 {
host = strings.TrimRight(host, host[pos:])
}
return host
}
func (p Page) Help() func() template.HTMLAttr {
return func() template.HTMLAttr {
return HelpMsg
}
}
func (p Page) Title(def string) func() template.HTML {
subtitle := "Empty page"
if len(p.Content) > 0 {
subtitle = "Online scratchpad for your convenience"
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader([]byte(p.Content)))
if doc != nil {
if titleSel := doc.Find("h1"); titleSel.Size() > 0 {
subtitle = titleSel.Text()
}
}
}
title := def + ": " + subtitle
if len(p.Secret) > 0 {
title = "🔒 " + title
}
return func() template.HTML {
return template.HTML(title)
}
}