|
|
@ -11,6 +11,8 @@ import (
|
|
11
|
11
|
"io/ioutil"
|
|
12
|
12
|
"net/url"
|
|
13
|
13
|
"os"
|
|
|
14
|
"strings"
|
|
|
15
|
"unicode"
|
|
14
|
16
|
|
|
15
|
17
|
"github.com/russross/blackfriday"
|
|
16
|
18
|
"gopkg.in/yaml.v2"
|
|
|
@ -27,6 +29,7 @@ type Post struct {
|
|
27
|
29
|
|
|
28
|
30
|
var flags struct {
|
|
29
|
31
|
writeBack bool
|
|
|
32
|
hashIds bool
|
|
30
|
33
|
reverse bool
|
|
31
|
34
|
css string
|
|
32
|
35
|
title string
|
|
|
@ -72,6 +75,7 @@ article img {
|
|
72
|
75
|
|
|
73
|
76
|
func init() {
|
|
74
|
77
|
flag.BoolVar(&flags.writeBack, "write-back", false, "Rewrite the YAML file with the generated ids")
|
|
|
78
|
flag.BoolVar(&flags.hashIds, "hash-ids", false, "Use hash-based post ids")
|
|
75
|
79
|
flag.BoolVar(&flags.reverse, "reverse", false, "Reverse the order of the articles in the file")
|
|
76
|
80
|
flag.StringVar(&flags.css, "css", defaultStyle, "Custom `css` styles to use")
|
|
77
|
81
|
flag.StringVar(&flags.title, "title", "A blog", "Custom `title` to use")
|
|
|
@ -292,6 +296,13 @@ func exit(err error) {
|
|
292
|
296
|
}
|
|
293
|
297
|
|
|
294
|
298
|
func generateId(p Post) string {
|
|
|
299
|
if flags.hashIds {
|
|
|
300
|
return hashId(p)
|
|
|
301
|
}
|
|
|
302
|
return slugId(p)
|
|
|
303
|
}
|
|
|
304
|
|
|
|
305
|
func hashId(p Post) string {
|
|
295
|
306
|
h := md5.New()
|
|
296
|
307
|
io.WriteString(h, p.Title)
|
|
297
|
308
|
io.WriteString(h, p.Content)
|
|
|
@ -308,3 +319,42 @@ func randomId() string {
|
|
308
|
319
|
|
|
309
|
320
|
return hex.EncodeToString(buf)
|
|
310
|
321
|
}
|
|
|
322
|
|
|
|
323
|
var usedSlugs = map[string]int{}
|
|
|
324
|
|
|
|
325
|
func slugId(p Post) string {
|
|
|
326
|
slug := toSlug(p.Title)
|
|
|
327
|
n, ok := usedSlugs[slug]
|
|
|
328
|
if ok {
|
|
|
329
|
n += 1
|
|
|
330
|
} else {
|
|
|
331
|
n = 1
|
|
|
332
|
}
|
|
|
333
|
usedSlugs[slug] = n
|
|
|
334
|
|
|
|
335
|
if slug != "" && n == 1 {
|
|
|
336
|
return slug
|
|
|
337
|
} else if slug == "" {
|
|
|
338
|
return fmt.Sprintf("%d", n)
|
|
|
339
|
}
|
|
|
340
|
return fmt.Sprintf("%s-%d", slug, n)
|
|
|
341
|
}
|
|
|
342
|
|
|
|
343
|
func toSlug(s string) string {
|
|
|
344
|
lastChar := ' '
|
|
|
345
|
s = strings.Map(func(ch rune) rune {
|
|
|
346
|
var newChar rune
|
|
|
347
|
switch {
|
|
|
348
|
case unicode.IsLetter(ch) || unicode.IsDigit(ch):
|
|
|
349
|
newChar = unicode.ToLower(ch)
|
|
|
350
|
default:
|
|
|
351
|
if lastChar == '-' {
|
|
|
352
|
return -1
|
|
|
353
|
}
|
|
|
354
|
newChar = '-'
|
|
|
355
|
}
|
|
|
356
|
lastChar = newChar
|
|
|
357
|
return newChar
|
|
|
358
|
}, s)
|
|
|
359
|
return strings.Trim(s, "-")
|
|
|
360
|
}
|