-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.go
121 lines (100 loc) · 2.51 KB
/
utilities.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
package main
import (
"errors"
"io/ioutil"
"strings"
"github.com/gohugoio/hugo/parser/metadecoders"
)
// Credit: https://stackoverflow.com/a/54426140
func SplitAny(s string, seps string) []string {
splitter := func(r rune) bool {
return strings.ContainsRune(seps, r)
}
return strings.FieldsFunc(s, splitter)
}
func ConvertToHashtag(s string) string {
hashtagPrefix := "#"
words := SplitAny(s, " -_.")
// Collapse the words array to a single, camelCased string,
// and prefix with an octothorpe
if len(words) > 1 {
for i := 1; i < len(words); i++ {
words[i] = strings.Title(strings.ToLower(words[i]))
}
return hashtagPrefix + strings.Join(words, "")
} else {
return hashtagPrefix + words[0]
}
}
func ImageIsLocal(p string, b string) bool {
// If the path starts with http, check to see if it's local or remote
if strings.HasPrefix(p, "http") {
return strings.HasPrefix(p, b)
}
// If it doesn't start with http, it's local, so we return true.
return true
}
func ScanConfigForBaseUrl(p string, f string) (string, error) {
var baseURL string
var format metadecoders.Format
switch f {
case "json":
format = metadecoders.JSON
case "toml":
format = metadecoders.TOML
case "yaml":
format = metadecoders.YAML
default:
err := errors.New("Invalid config file format found")
return "", err
}
content, err := ioutil.ReadFile(p)
if err != nil {
return "", err
}
m, err := metadecoders.Default.UnmarshalToMap(content, format)
if err != nil {
return "", err
}
if m["baseURL"] != nil && len(m["baseURL"].(string)) > 0 {
baseURL = m["baseURL"].(string)
} else {
return "", errors.New("No baseURL value found")
}
return baseURL, nil
}
func ScanConfigForLanguage(p string, f string) (string, error) {
var languageCode string
var format metadecoders.Format
switch f {
case "json":
format = metadecoders.JSON
case "toml":
format = metadecoders.TOML
case "yaml":
format = metadecoders.YAML
default:
err := errors.New("Invalid config file format found")
return "", err
}
content, err := ioutil.ReadFile(p)
if err != nil {
return "", err
}
m, err := metadecoders.Default.UnmarshalToMap(content, format)
if err != nil {
return "", err
}
if m["languageCode"] != nil {
languageCode = m["languageCode"].(string)
} else {
return "", errors.New("No language found")
}
if m["defaultContentLanguage"] != nil {
languageCode = m["defaultContentLanguage"].(string)
}
if len(languageCode) == 0 {
return "", errors.New("No language found")
}
return languageCode[0:2], nil
}