mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
Make links clickable in text view of emails
This commit is contained in:
@@ -5,16 +5,20 @@ import (
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"html"
|
||||
"html/template"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var TemplateFuncs = template.FuncMap{
|
||||
"friendlyTime": friendlyTime,
|
||||
"reverse": reverse,
|
||||
"textToHtml": textToHtml,
|
||||
"reverse": reverse,
|
||||
"textToHtml": textToHtml,
|
||||
}
|
||||
|
||||
// From http://daringfireball.net/2010/07/improved_regex_for_matching_urls
|
||||
var urlRE = regexp.MustCompile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))")
|
||||
|
||||
// Friendly date & time rendering
|
||||
func friendlyTime(t time.Time) template.HTML {
|
||||
ty, tm, td := t.Date()
|
||||
@@ -45,6 +49,13 @@ func reverse(name string, things ...interface{}) string {
|
||||
// HTML display
|
||||
func textToHtml(text string) template.HTML {
|
||||
text = html.EscapeString(text)
|
||||
text = urlRE.ReplaceAllStringFunc(text, wrapUrl)
|
||||
replacer := strings.NewReplacer("\r\n", "<br/>\n", "\r", "<br/>\n", "\n", "<br/>\n")
|
||||
return template.HTML(replacer.Replace(text))
|
||||
}
|
||||
|
||||
// wrapUrl wraps a <a href> tag around the provided URL
|
||||
func wrapUrl(url string) string {
|
||||
unescaped := strings.Replace(url, "&", "&", -1)
|
||||
return fmt.Sprintf("<a href=\"%s\" target=\"_blank\">%s</a>", unescaped, url)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user