GoBlog/postsFuncs.go

211 lines
5.0 KiB
Go
Raw Normal View History

package main
import (
2021-06-23 17:20:50 +00:00
"fmt"
2020-10-18 09:54:29 +00:00
"html/template"
"log"
"strings"
"time"
2020-10-15 15:32:46 +00:00
gogeouri "git.jlel.se/jlelse/go-geouri"
2020-10-15 15:32:46 +00:00
"github.com/PuerkitoBio/goquery"
"github.com/araddon/dateparse"
2021-06-23 17:20:50 +00:00
"gopkg.in/yaml.v3"
)
func (a *goBlog) fullPostURL(p *post) string {
return a.getFullAddress(p.Path)
}
func (a *goBlog) shortPostURL(p *post) string {
s, err := a.db.shortenPath(p.Path)
2020-12-22 21:15:29 +00:00
if err != nil {
return ""
}
if a.cfg.Server.ShortPublicAddress != "" {
return a.cfg.Server.ShortPublicAddress + s
2020-12-24 10:00:16 +00:00
}
return a.getFullAddress(s)
2020-12-22 21:15:29 +00:00
}
func postParameter(p *post, parameter string) []string {
return p.Parameters[parameter]
}
func postHasParameter(p *post, parameter string) bool {
return len(p.Parameters[parameter]) > 0
}
2020-10-15 15:32:46 +00:00
func (p *post) firstParameter(parameter string) (result string) {
if pp := p.Parameters[parameter]; len(pp) > 0 {
result = pp[0]
}
return
}
func firstPostParameter(p *post, parameter string) string {
return p.firstParameter(parameter)
}
func (a *goBlog) postHtml(p *post, absolute bool) template.HTML {
p.renderMutex.Lock()
defer p.renderMutex.Unlock()
// Check cache
if r, ok := p.renderCache.Load(absolute); ok && r != nil {
return r.(template.HTML)
2020-10-18 09:54:29 +00:00
}
// Render markdown
htmlContent, err := a.renderMarkdown(p.Content, absolute)
2020-10-18 09:54:29 +00:00
if err != nil {
log.Fatal(err)
return ""
}
htmlContentStr := string(htmlContent)
// Add audio to the top
if audio, ok := p.Parameters["audio"]; ok && len(audio) > 0 {
audios := ""
for _, a := range audio {
audios += fmt.Sprintf(`<audio controls preload=none><source src="%s"/></audio>`, a)
}
htmlContentStr = audios + htmlContentStr
}
// Add links to the bottom
if link, ok := p.Parameters["link"]; ok && len(link) > 0 {
links := ""
for _, l := range link {
links += fmt.Sprintf(`<p><a class=u-bookmark-of href="%s" target=_blank rel=noopener>%s</a></p>`, l, l)
}
htmlContentStr += links
}
// Cache
html := template.HTML(htmlContentStr)
p.renderCache.Store(absolute, html)
return html
}
2020-10-18 09:54:29 +00:00
const summaryDivider = "<!--more-->"
func (a *goBlog) postSummary(p *post) (summary string) {
summary = p.firstParameter("summary")
if summary != "" {
return
}
html := string(a.postHtml(p, false))
2020-10-18 09:54:29 +00:00
if splitted := strings.Split(html, summaryDivider); len(splitted) > 1 {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(splitted[0]))
summary = doc.Text()
2020-10-18 09:54:29 +00:00
} else {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
summary = doc.Find("p").First().Text()
}
return
}
func (a *goBlog) postTranslations(p *post) []*post {
translationkey := p.firstParameter("translationkey")
if translationkey == "" {
return nil
}
posts, err := a.db.getPosts(&postsRequestConfig{
parameter: "translationkey",
parameterValue: translationkey,
})
if err != nil || len(posts) == 0 {
return nil
}
translations := []*post{}
for _, t := range posts {
if p.Path != t.Path {
translations = append(translations, t)
}
}
if len(translations) == 0 {
return nil
}
return translations
}
2020-11-17 21:10:13 +00:00
func (p *post) isPublishedSectionPost() bool {
2021-01-15 20:56:46 +00:00
return p.Published != "" && p.Section != "" && p.Status == statusPublished
2020-11-17 21:10:13 +00:00
}
2021-06-23 17:20:50 +00:00
func (a *goBlog) postToMfItem(p *post) *microformatItem {
params := map[string]interface{}{}
for k, v := range p.Parameters {
if l := len(v); l == 1 {
params[k] = v[0]
} else if l > 1 {
params[k] = v
}
}
params["path"] = p.Path
params["section"] = p.Section
params["blog"] = p.Blog
params["published"] = p.Published
params["updated"] = p.Updated
params["status"] = string(p.Status)
params["priority"] = p.Priority
pb, _ := yaml.Marshal(params)
2021-06-23 17:20:50 +00:00
content := fmt.Sprintf("---\n%s---\n%s", string(pb), p.Content)
2021-07-14 16:50:24 +00:00
var mfStatus, mfVisibility string
switch p.Status {
case statusDraft:
mfStatus = "draft"
case statusPublished:
mfStatus = "published"
mfVisibility = "public"
case statusUnlisted:
mfStatus = "published"
mfVisibility = "unlisted"
case statusPrivate:
mfStatus = "published"
mfVisibility = "private"
}
2021-06-23 17:20:50 +00:00
return &microformatItem{
Type: []string{"h-entry"},
Properties: &microformatProperties{
Name: p.Parameters["title"],
Published: []string{p.Published},
Updated: []string{p.Updated},
2021-07-14 16:50:24 +00:00
PostStatus: []string{mfStatus},
Visibility: []string{mfVisibility},
2021-06-23 17:20:50 +00:00
Category: p.Parameters[a.cfg.Micropub.CategoryParam],
Content: []string{content},
URL: []string{a.fullPostURL(p)},
InReplyTo: p.Parameters[a.cfg.Micropub.ReplyParam],
LikeOf: p.Parameters[a.cfg.Micropub.LikeParam],
BookmarkOf: p.Parameters[a.cfg.Micropub.BookmarkParam],
MpSlug: []string{p.Slug},
Audio: p.Parameters[a.cfg.Micropub.AudioParam],
// TODO: Photos
},
}
}
// Public because of rendering
func (p *post) Title() string {
return p.firstParameter("title")
}
func (p *post) GeoURI() *gogeouri.Geo {
loc := p.firstParameter("location")
if loc == "" {
return nil
}
g, _ := gogeouri.Parse(loc)
return g
}
func (p *post) Old() bool {
pub := p.Published
if pub == "" {
return false
}
pubDate, err := dateparse.ParseLocal(pub)
if err != nil {
return false
}
return pubDate.AddDate(1, 0, 0).Before(time.Now())
}