Archived
Template
1
0

Adds godoc comments

This commit is contained in:
uberswe
2022-01-09 14:22:43 +01:00
parent 2d3c0df4e5
commit 8c37faf016
30 changed files with 154 additions and 68 deletions

24
text/html.go Normal file
View File

@ -0,0 +1,24 @@
package text
import (
"fmt"
"net/url"
"strings"
)
// Nl2Br converts \n to HTML <br> tags
func Nl2Br(s string) string {
return strings.Replace(s, "\n", "<br>", -1)
}
// LinkToHTMLLink converts regular links to HTML links
func LinkToHTMLLink(s string) string {
s = strings.Replace(s, "\n", " \n ", -1)
for _, p := range strings.Split(s, " ") {
u, err := url.ParseRequestURI(p)
if err == nil && u.Scheme != "" {
s = strings.Replace(s, p, fmt.Sprintf("<a href=\"%s\">%s</a>", p, p), 1)
}
}
return strings.Replace(s, " \n ", "\n", -1)
}

15
text/random.go Normal file
View File

@ -0,0 +1,15 @@
package text
import "math/rand"
// RandomString generates a random string of n length. Based on https://stackoverflow.com/a/22892986/1260548
func RandomString(n int) string {
// remove vowels to make it less likely to generate something offensive
var letters = []rune("bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

17
text/segment.go Normal file
View File

@ -0,0 +1,17 @@
package text
import "strings"
// BetweenStrings returns a string between the starting and ending string or an empty string if none could be found
func BetweenStrings(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
return
}
s += len(start)
e := strings.Index(str[s:], end)
if e == -1 {
return
}
return str[s : s+e]
}