generated from bmf-san/gobel-client-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presenter.go
57 lines (47 loc) · 1.1 KB
/
presenter.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
package presenter
import (
"embed"
"html/template"
"math/rand"
"regexp"
"time"
)
// Presenter represents the singular of presenter.
type Presenter struct {
templates embed.FS
}
// NewPresenter creates a Presenter.
func NewPresenter(templates embed.FS) *Presenter {
return &Presenter{
templates: templates,
}
}
// Unescape returns safe HTML.
func (p *Presenter) Unescape(text string) template.HTML {
return template.HTML(text)
}
// year returns curernt year.
func (p *Presenter) year() int {
return time.Now().Year()
}
const rgx string = `<.*?>`
// StripTags strips tags from string.
func (p *Presenter) StripTags(s string) string {
r := regexp.MustCompile(rgx)
return r.ReplaceAllString(s, "")
}
const defaultWords int = 200
// Summary returns summarized string.
func (p *Presenter) Summary(s string) string {
r := []rune(s)
var l int = defaultWords
if len(r) < defaultWords {
l = len(r)
}
return string(r[:l]) + "..."
}
// IsAd returns a flag indicating whether to output ads.
func (p *Presenter) IsAd() bool {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(5)%3 == 0
}