-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
186 lines (157 loc) · 3.81 KB
/
html.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
package funcmaps
import (
"fmt"
"html"
"html/template"
"strings"
"time"
gq "github.com/PuerkitoBio/goquery"
"github.com/ikeikeikeike/godic/modules/configs"
"github.com/ikeikeikeike/gopkg/extract/image"
"github.com/ikeikeikeike/gopkg/str"
"github.com/kennygrant/sanitize"
"github.com/mattn/go-runewidth"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
)
func ToAge(bd time.Time) int {
if bd.UnixNano() < 0 {
return 0
}
at := time.Now()
age := at.Year() - bd.Year()
if (at.Month() <= bd.Month()) && (at.Day() <= bd.Day()) {
age -= 1
}
return age
}
func SafeHTML(text string) template.HTML {
return template.HTML(text)
}
func EscapeHTML(text string) string {
return html.EscapeString(text)
}
func SanitizeHTML(text string) string {
content, _ := sanitize.HTMLAllowing(text)
content = sanitize.HTML(text)
return html.EscapeString(content)
}
func MarkdownHTML(markdown string) string {
unsafe := blackfriday.MarkdownCommon([]byte(markdown))
contentHtml := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
return string(contentHtml)
}
func Nl2br(in string) string {
return strings.Replace(in, "\n", "<br />", -1)
}
func DiffLineTypeToStr(diffType int) string {
switch diffType {
case 2:
return "add"
case 3:
return "del"
case 4:
return "tag"
}
return "same"
}
func DiffTypeToStr(diffType int) string {
diffTypes := map[int]string{
1: "add", 2: "modify", 3: "del",
}
return diffTypes[diffType]
}
func Truncate(in string, length int) string {
return runewidth.Truncate(in, length, "...")
}
// Expect sorted string-length.
func AutoLink(html string, names []string) string {
doc, err := gq.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return html
}
for _, name := range names {
if len([]rune(name)) <= 1 {
continue
}
doc.Find("*").EachWithBreak(func(i int, s *gq.Selection) bool {
if strings.Contains(s.Text(), name) {
t := s.Text()
h, _ := s.Html()
if t != "" && h == "" {
html := strings.Replace(t, name, fmt.Sprintf(`<a class="link-underline external" href="/d/%[1]s">%[1]s</a>`, name), 1)
s.ReplaceWithHtml(html)
return false
}
}
return true
})
}
res, err := doc.Find("body").Html()
if err != nil {
return html
}
return res
}
func ExtractAutoLink(html string) (names []string) {
doc, err := gq.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return
}
doc.Find("a.link-underline.external").Each(func(i int, s *gq.Selection) {
names = append(names, s.Text())
})
return
}
type Img struct {
*image.FileInfo
Src string
Alt string
}
func ExtractIMGs(html string) (imgs []*Img) {
doc, err := gq.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return
}
info := image.NewInfo()
info.Header("User-Agent", configs.Settings.UserAgent)
doc.Find("img").Each(func(i int, s *gq.Selection) {
src, ok := s.Attr("src")
if !ok {
return
}
alt, _ := s.Attr("alt")
if alt == "" {
alt, _ = s.Attr("title")
}
src = str.Clean(src)
f, err := info.Fetch(src)
if err == nil {
img := &Img{FileInfo: f, Src: src, Alt: alt}
imgs = append(imgs, img)
}
})
return
}
func GenMetaTitle(params map[string]string) string {
v := configs.Settings.AppName
if name, ok := params["name"]; ok {
return fmt.Sprintf("%[1]s | %[2]sの記事", v, name)
} else {
return fmt.Sprintf("%[1]s | 人気新着の記事まとめ", v)
}
}
func GenMetaKeywords(params map[string]string) string {
if name, ok := params["name"]; ok {
return fmt.Sprintf("%[1]s,%[2]s", configs.Settings.Keywords, name)
} else {
return configs.Settings.Keywords
}
}
func GenMetaDescription(params map[string]string) string {
if name, ok := params["name"]; ok {
return fmt.Sprintf("%[2]sを見るなら%[1]s", configs.Settings.Description, name)
} else {
return configs.Settings.Description
}
}