forked from piranha/gostatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
150 lines (126 loc) · 2.53 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
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"fmt"
bf "github.com/russross/blackfriday"
"io"
"os"
"strings"
"unicode"
)
func errhandle(err error) {
if err == nil {
return
}
fmt.Printf("Error: %s\n", err)
}
func errexit(err error) {
if err == nil {
return
}
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
func out(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func debug(format string, args ...interface{}) {
if !opts.Verbose {
return
}
fmt.Printf(format, args...)
os.Stdout.Sync()
}
func drainchannel(out chan string) {
for {
select {
case <-out:
default:
return
}
}
}
func IsDir(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
}
stat, err := file.Stat()
if err != nil {
return false, err
}
return stat.IsDir(), nil
}
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
ext |= bf.EXTENSION_FOOTNOTES
ext |= bf.EXTENSION_HEADER_IDS
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:])
}
func CopyFile(srcPath, dstPath string) (n int64, err error) {
fstat, err := os.Lstat(srcPath)
if fstat.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(srcPath)
if err != nil {
return 0, err
}
err = os.Symlink(target, dstPath)
if err != nil {
return 0, err
}
return 1, nil
}
src, err := os.Open(srcPath)
if err != nil {
return 0, err
}
defer src.Close()
dst, err := os.Create(dstPath)
if err != nil {
return 0, err
}
defer dst.Close()
n, err = io.Copy(dst, src)
return n, err
}