63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package matrix
|
|
|
|
import (
|
|
"fmt"
|
|
"html"
|
|
"strings"
|
|
)
|
|
|
|
// PostableStory contains the data needed to format and send a story.
|
|
type PostableStory struct {
|
|
ImageURL string
|
|
Headline string
|
|
ArticleURL string
|
|
Lede string
|
|
Source string
|
|
Channel string
|
|
Platforms []string
|
|
}
|
|
|
|
// formatPost returns plain text and HTML bodies for a Matrix message.
|
|
func formatPost(s *PostableStory) (plain, htmlBody string) {
|
|
// Plain text body
|
|
var plainParts []string
|
|
plainParts = append(plainParts, fmt.Sprintf("**%s** \u2192 %s", s.Headline, s.ArticleURL))
|
|
if s.Lede != "" {
|
|
plainParts = append(plainParts, s.Lede)
|
|
}
|
|
plainParts = append(plainParts, formatSourceTag(s.Source, s.Platforms, false))
|
|
plain = strings.Join(plainParts, "\n")
|
|
|
|
// HTML body
|
|
var htmlParts []string
|
|
htmlParts = append(htmlParts, fmt.Sprintf(
|
|
`<strong><a href="%s">%s</a></strong>`,
|
|
html.EscapeString(s.ArticleURL),
|
|
html.EscapeString(s.Headline),
|
|
))
|
|
if s.Lede != "" {
|
|
htmlParts = append(htmlParts, html.EscapeString(s.Lede))
|
|
}
|
|
htmlParts = append(htmlParts, formatSourceTag(s.Source, s.Platforms, true))
|
|
htmlBody = strings.Join(htmlParts, "<br/>")
|
|
|
|
return plain, htmlBody
|
|
}
|
|
|
|
// formatSourceTag builds the source + platform tags line.
|
|
func formatSourceTag(source string, platforms []string, isHTML bool) string {
|
|
if isHTML {
|
|
parts := []string{fmt.Sprintf("<code>%s</code>", html.EscapeString(strings.ToLower(source)))}
|
|
for _, p := range platforms {
|
|
parts = append(parts, fmt.Sprintf("<code>%s</code>", html.EscapeString(p)))
|
|
}
|
|
return strings.Join(parts, " \u00b7 ")
|
|
}
|
|
|
|
parts := []string{fmt.Sprintf("`%s`", strings.ToLower(source))}
|
|
for _, p := range platforms {
|
|
parts = append(parts, fmt.Sprintf("`%s`", p))
|
|
}
|
|
return strings.Join(parts, " \u00b7 ")
|
|
}
|