forked from RasmusLindroth/tut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
174 lines (156 loc) · 3.72 KB
/
util.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
package util
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/RasmusLindroth/go-mastodon"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/net/html"
)
type URL struct {
Text string
URL string
Classes []string
}
func CleanHTML(content string) (string, []URL) {
stripped := bluemonday.NewPolicy().AllowElements("p", "br").AllowAttrs("href", "class").OnElements("a").Sanitize(content)
urls := getURLs(stripped)
stripped = bluemonday.NewPolicy().AllowElements("p", "br").Sanitize(content)
stripped = strings.ReplaceAll(stripped, "<br>", "\n")
stripped = strings.ReplaceAll(stripped, "<br/>", "\n")
stripped = strings.ReplaceAll(stripped, "<p>", "")
stripped = strings.ReplaceAll(stripped, "</p>", "\n\n")
stripped = strings.TrimSpace(stripped)
stripped = html.UnescapeString(stripped)
return stripped, urls
}
func getURLs(text string) []URL {
doc := html.NewTokenizer(strings.NewReader(text))
var urls []URL
for {
n := doc.Next()
switch n {
case html.ErrorToken:
return urls
case html.StartTagToken:
token := doc.Token()
if token.Data == "a" {
url := URL{}
var appendUrl = true
for _, a := range token.Attr {
switch a.Key {
case "href":
url.URL = a.Val
url.Text = a.Val
case "class":
url.Classes = strings.Split(a.Val, " ")
if strings.Contains(a.Val, "hashtag") {
appendUrl = false
}
}
}
if appendUrl {
urls = append(urls, url)
}
}
}
}
}
func CmdToString(cmd string) (string, error) {
cmd = strings.TrimPrefix(cmd, "!CMD!")
parts := strings.Split(cmd, " ")
s, err := exec.Command(parts[0], parts[1:]...).CombinedOutput()
return strings.TrimSpace(string(s)), err
}
func MakeDirs() {
cd, err := os.UserConfigDir()
if err != nil {
log.Printf("couldn't find $HOME. Error: %v\n", err)
os.Exit(1)
}
dir := cd + "/tut"
err = os.Mkdir(dir, 0755)
if err != nil && !os.IsExist(err) {
log.Printf("couldn't create dirs. Error: %v\n", err)
os.Exit(1)
}
}
func CheckConfig(filename string) (path string, exists bool, err error) {
cd, err := os.UserConfigDir()
if err != nil {
log.Printf("couldn't find $HOME. Error: %v\n", err)
os.Exit(1)
}
dir := cd + "/tut/"
path = dir + filename
_, err = os.Stat(path)
if os.IsNotExist(err) {
return path, false, nil
} else if err != nil {
return path, true, err
}
return path, true, err
}
func FormatUsername(a mastodon.Account) string {
if a.DisplayName != "" {
return fmt.Sprintf("%s (%s)", a.DisplayName, a.Acct)
}
return a.Acct
}
func CheckPath(input string, inclHidden bool) (string, bool) {
info, err := os.Stat(input)
if err != nil {
return "", false
}
if !inclHidden && strings.HasPrefix(info.Name(), ".") {
return "", false
}
if info.IsDir() {
if input == "/" {
return input, true
}
return input + "/", true
}
return input, true
}
func IsDir(input string) bool {
info, err := os.Stat(input)
if err != nil {
return false
}
return info.IsDir()
}
func FindFiles(s string) []string {
input := filepath.Clean(s)
if len(s) > 2 && s[len(s)-2:] == "/." {
input += "/."
}
var files []string
path, exists := CheckPath(input, true)
if exists {
files = append(files, path)
}
base := filepath.Base(input)
inclHidden := strings.HasPrefix(base, ".") || (len(input) > 1 && input[len(input)-2:] == "/.")
matches, _ := filepath.Glob(input + "*")
if strings.HasSuffix(path, "/") {
matchesDir, _ := filepath.Glob(path + "*")
matches = append(matches, matchesDir...)
}
for _, f := range matches {
p, exists := CheckPath(f, inclHidden)
if exists && p != path {
files = append(files, p)
}
}
return files
}
func StatusOrReblog(s *mastodon.Status) *mastodon.Status {
if s.Reblog != nil {
return s.Reblog
}
return s
}