Archived
Template
1
0
This repository has been archived on 2023-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
Files
golang-base-project/text/html.go

25 lines
552 B
Go
Raw Permalink Normal View History

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)
}