-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
353 lines (318 loc) · 9.54 KB
/
utils.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2013 Dmitry Chestnykh. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package utils contains utility functions.
package utils
import (
"bytes"
"crypto/sha256"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
"gopkg.in/yaml.v3"
)
// UnmarshallYAMLFile reads YAML file and unmarshalls it into data.
func UnmarshallYAMLFile(filename string, data interface{}) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return yaml.Unmarshal(b, data)
}
// CleanPermalink returns a cleaned version of permalink: without
// index.htm[l] ending and starting with slash.
func CleanPermalink(s string) string {
// Strip index filename.
if path.Base(s) == "index.html" || path.Base(s) == "index.htm" {
s = s[:len(s)-len(path.Base(s))]
}
// Make sure it starts with /.
if len(s) > 0 && s[0] != '/' {
s = "/" + s
}
return s
}
// AddIndexIfNeeded adds "index.html" to the filename if
// it ends with a slash. Otherwise returns the original filename.
func AddIndexIfNeeded(filename string) string {
if len(filename) == 0 || filename[len(filename)-1] != '/' {
return filename
}
return filename + "index.html"
}
// StripEndSlash returns a string with ending slash removed,
// or if there was no slash, returns the original string.
func StripEndSlash(s string) string {
// Remove ending slash.
if len(s) > 0 && s[len(s)-1] == '/' {
s = s[:len(s)-1]
}
return s
}
// Hash returns an SHA256 hash of the given string.
func Hash(data []byte) []byte {
h := sha256.Sum256(data)
return h[:]
}
// TemplatedHash replaces ":hash" in template with hexadecimal characters of
// the hash of the input string and returns the result.
func TemplatedHash(template string, input []byte) string {
// 10 bytes of hash is enough to avoid accidental collisions.
hs := NoVowelsHexEncode(Hash(input)[:10])
return strings.Replace(template, ":hash", hs, -1)
}
var dateTemplates = []string{
"2006-01-02 15:04",
"2006-01-02 15:04 -07:00",
"2006-01-02 15:04:05 -07:00",
time.RFC3339,
time.RFC822,
time.UnixDate,
"2006.01.02 15:04",
"2006.01.02",
"2006-01-02",
}
// ParseAnyDate parses date in any of the few allowed formats.
func ParseAnyDate(s string) (d time.Time, err error) {
for _, t := range dateTemplates {
d, err = time.Parse(t, s)
if err == nil {
return
}
}
err = fmt.Errorf("failed to parse date from %q", s)
return
}
// DirExists returns true if the given directory exists.
func DirExist(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.IsDir()
}
// Returns true if filename has one of the given extension.
// Extensions must start with dot.
func HasFileExt(filename string, extensions []string) bool {
ext := filepath.Ext(filename)
for _, v := range extensions {
if v == ext {
return true
}
}
return false
}
// ReplaceExtension replaces file extension with the given string.
// Extension must start with dot.
func ReplaceFileExt(filename string, ext string) string {
oldext := filepath.Ext(filename)
return filename[:len(filename)-len(oldext)] + ext
}
var absPathsRx = []*regexp.Regexp{
regexp.MustCompile(`(?i)<([^>]+\s)(src|href)=(")/([^"]+)`),
regexp.MustCompile(`(?i)<([^>]+\s)(src|href)=(')/([^']+)`),
//TODO support non-quoted attribute values.
}
// AbsPaths adds urlPrefix to paths of src and href attributes
// in html starting with a slash (/).
func AbsPaths(urlPrefix, html string) string {
urlPrefix = StripEndSlash(urlPrefix)
repl := `<$1$2=${3}` + urlPrefix + `/$4`
for _, re := range absPathsRx {
html = re.ReplaceAllString(html, repl)
}
return html
}
// StripTags removes HTML tags.
// Extracted from https://github.com/kennygrant/sanitize
/*
License: BSD License
Copyright (c) 2013 Kenny Grant. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
func StripHTMLTags(s string) (output string) {
// Shortcut strings with no tags in them
if !strings.ContainsAny(s, "<>") {
output = s
} else {
s = strings.Replace(s, "</p>", "\n", -1)
s = strings.Replace(s, "<br>", "\n", -1)
s = strings.Replace(s, "</br>", "\n", -1)
b := bytes.NewBufferString("")
inTag := false
for _, r := range s {
switch r {
case '<':
inTag = true
case '>':
inTag = false
default:
if !inTag {
b.WriteRune(r)
}
}
}
output = b.String()
}
// In case we have missed any tags above, escape the text - removes <, >, &, ' and ".
output = template.HTMLEscapeString(output)
// Remove a few common harmless entities, to arrive at something more like plain text
// This relies on having removed *all* tags above
output = strings.Replace(output, " ", " ", -1)
output = strings.Replace(output, """, "\"", -1)
output = strings.Replace(output, "'", "'", -1)
output = strings.Replace(output, """, "\"", -1)
output = strings.Replace(output, "'", "'", -1)
// NB spaces here are significant - we only allow & not part of entity
output = strings.Replace(output, "& ", "& ", -1)
output = strings.Replace(output, "&amp; ", "& ", -1)
return
}
// NoVowelsHexEncode returns bytes encoded in a hex-like encoding which
// doesn't use vowels.
//
// This is useful to avoid producing substrings, such as "ad", that
// may be blocked by ad-blockers.
func NoVowelsHexEncode(b []byte) string {
const hextable = "0123456789vbcdzf"
dst := make([]byte, len(b)*2)
for i, v := range b {
dst[i*2] = hextable[v>>4]
dst[i*2+1] = hextable[v&0x0f]
}
return string(dst)
}
// OpenURL opens URL in the operating system (probably in the default browser).
func OpenURL(addr string) error {
if _, err := url.Parse(addr); err != nil {
return err
}
switch runtime.GOOS {
case "darwin":
return exec.Command("open", addr).Start()
case "linux", "freebsd", "openbsd":
return exec.Command("xdg-open", addr).Start()
default:
return fmt.Errorf("Don't know how to open browser on %s", runtime.GOOS)
}
}
// Pool is a worker pool for parallel job processing.
type Pool struct {
sync.RWMutex
wg sync.WaitGroup
jobs chan func() error
err error
}
// NewPool creates a new pool which calls fn for each
// added item and stores the first returned error.
func NewPool() *Pool {
parallelism := runtime.GOMAXPROCS(0)
p := &Pool{
jobs: make(chan func() error, parallelism),
}
// Launch workers.
for i := 0; i < parallelism; i++ {
go func() {
for j := range p.jobs {
if err := j(); err != nil {
p.Lock()
if p.err == nil {
p.err = err
}
p.Unlock()
}
p.wg.Done()
}
}()
}
return p
}
// Add adds a new job to pool. Function passed to
// Add will be called for each job in a worker goroutine.
//
// If Add returns false, which happens if one of the
// previously added jobs returned an error, the job
// will not be added to the pool.
//
// After finishing adding items, Wait must be called on the pool
// to wait for unfinished jobs to complete and get the first error.
func (p *Pool) Add(job func() error) bool {
p.RLock()
hasErr := p.err != nil
p.RUnlock()
if hasErr {
return false
}
p.wg.Add(1)
p.jobs <- job
return true
}
// Wait for jobs to complete and return the first error or nil
// if there were no errors.
//
// After calling Wait, the pool can be reused.
func (p *Pool) Wait() error {
p.wg.Wait()
err := p.err
p.err = nil
return err
}
var slugRx = regexp.MustCompile(`[^a-z0-9]+`)
// ToSlug creates a slug from a title,
// for example "Hello, world!" becomes "hello-world".
// Only supports Latin characters, others are replaced with dashes.
func ToSlug(title string) string {
return strings.Trim(slugRx.ReplaceAllString(strings.ToLower(title), "-"), "-")
}
// SplitTags splits a comma-separated list of tags into a slice.
func SplitTags(tags string) []string {
t := strings.Split(tags, ",")
out := make([]string, 0, len(t))
for _, v := range t {
v = strings.TrimSpace(v)
if v != "" {
out = append(out, v)
}
}
return out
}
// OpenEditor launches editor for editing the given file.
func OpenEditor(filename string) error {
switch runtime.GOOS {
case "darwin":
return exec.Command("open", "-t", filename).Start()
case "linux", "freebsd", "openbsd":
return exec.Command("xdg-open", filename).Start()
default:
return fmt.Errorf("don't know how to open editor on %s", runtime.GOOS)
}
}