forked from piranha/gostatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (71 loc) · 1.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
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"fmt"
bf "github.com/russross/blackfriday"
"log"
"os"
"strings"
"unicode"
)
func errhandle(err error) {
if err == nil {
return
}
log.Fatalf("ERR %s\n", err)
os.Exit(1)
}
func out(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func debug(format string, args ...interface{}) {
if !*verbose {
return
}
fmt.Printf(format, args...)
}
func SliceStringIndexOf(haystack []string, needle string) int {
for i, elem := range haystack {
if elem == needle {
return i
}
}
return -1
}
func Markdown(source string) string {
// set up the HTML renderer
flags := 0
flags |= bf.HTML_USE_SMARTYPANTS
flags |= bf.HTML_SMARTYPANTS_FRACTIONS
renderer := bf.HtmlRenderer(flags, "", "")
// set up the parser
ext := 0
ext |= bf.EXTENSION_NO_INTRA_EMPHASIS
ext |= bf.EXTENSION_TABLES
ext |= bf.EXTENSION_FENCED_CODE
ext |= bf.EXTENSION_AUTOLINK
ext |= bf.EXTENSION_STRIKETHROUGH
ext |= bf.EXTENSION_SPACE_HEADERS
return string(bf.Markdown([]byte(source), renderer, ext))
}
func TrimSplitN(s string, sep string, n int) []string {
bits := strings.SplitN(s, sep, n)
for i, bit := range bits {
bits[i] = strings.TrimSpace(bit)
}
return bits
}
func NonEmptySplit(s string, sep string) []string {
bits := strings.Split(s, sep)
out := make([]string, 0)
for _, x := range bits {
if len(x) != 0 {
out = append(out, x)
}
}
return out
}
func Capitalize(s string) string {
return strings.ToUpper(s[0:1]) + strings.Map(unicode.ToLower, s[1:])
}