diff --git a/model/status.go b/model/status.go index cd03a54..a178027 100644 --- a/model/status.go +++ b/model/status.go @@ -3,6 +3,10 @@ package model import ( "errors" "fmt" + "html" + "html/template" + "regexp" + "strings" "time" ) @@ -14,6 +18,24 @@ type Status struct { CreatedAt time.Time } +var urlRegexp = regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`) + +func (s Status) ContentDisplay() string { + content := html.EscapeString(s.Content) + if urlRegexp.MatchString(s.Content) { + matches := urlRegexp.FindAllStringSubmatch(s.Content, -1) + for _, m := range matches { + url := m[0] + content = strings.Replace(content, url, fmt.Sprintf("%s", url, url), 1) + } + } + return content +} + +func (s Status) ContentHtml() template.HTML { + return template.HTML(s.ContentDisplay()) +} + func (s Status) Validate() error { if len(s.Content) == 0 { return errors.New("content is empty") diff --git a/web/handler/common.go b/web/handler/common.go index 932222b..21ef9b9 100644 --- a/web/handler/common.go +++ b/web/handler/common.go @@ -48,7 +48,7 @@ var TplCommonMap = map[string]string{ {{ define "title" }}{{ end }}`, "status": `{{ define "status" }}
{{ .User }} {{ .Face }} {{ .TimeAgo }}
-

{{ .Content }}

+

{{ .ContentHtml }}

{{ end }}`, "status_form": `{{ define "status_form" }}
diff --git a/web/handler/html/common/status.html b/web/handler/html/common/status.html index f06431e..b2f66cc 100644 --- a/web/handler/html/common/status.html +++ b/web/handler/html/common/status.html @@ -1,4 +1,4 @@ {{ define "status" }}
{{ .User }} {{ .Face }} {{ .TimeAgo }}
-

{{ .Content }}

+

{{ .ContentHtml }}

{{ end }} \ No newline at end of file diff --git a/web/handler/user_show.go b/web/handler/user_show.go index 79de4b3..4dd06fb 100644 --- a/web/handler/user_show.go +++ b/web/handler/user_show.go @@ -224,7 +224,7 @@ func (h *Handler) showUserStatusJSONView(w http.ResponseWriter, r *http.Request) var res statusjson if len(statuses) > 0 { res.Author = statuses[0].User - res.Content = statuses[0].Content + res.Content = statuses[0].ContentDisplay() res.Face = statuses[0].Face res.TimeAgo = statuses[0].TimeAgo() }