2020-09-15 15:01:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-23 17:20:50 +00:00
|
|
|
"fmt"
|
2020-10-18 09:54:29 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
2020-09-15 15:01:53 +00:00
|
|
|
"strings"
|
2021-06-18 12:32:03 +00:00
|
|
|
"time"
|
2020-10-15 15:32:46 +00:00
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
gogeouri "git.jlel.se/jlelse/go-geouri"
|
2020-10-15 15:32:46 +00:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-06-18 12:32:03 +00:00
|
|
|
"github.com/araddon/dateparse"
|
2021-06-23 17:20:50 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-09-15 15:01:53 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) fullPostURL(p *post) string {
|
2021-06-10 20:09:50 +00:00
|
|
|
return a.getFullAddress(p.Path)
|
2020-11-16 17:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
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 ""
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.cfg.Server.ShortPublicAddress != "" {
|
|
|
|
return a.cfg.Server.ShortPublicAddress + s
|
2020-12-24 10:00:16 +00:00
|
|
|
}
|
2021-06-10 20:09:50 +00:00
|
|
|
return a.getFullAddress(s)
|
2020-12-22 21:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 12:32:03 +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) {
|
2020-09-15 15:01:53 +00:00
|
|
|
if pp := p.Parameters[parameter]; len(pp) > 0 {
|
|
|
|
result = pp[0]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
func firstPostParameter(p *post, parameter string) string {
|
|
|
|
return p.firstParameter(parameter)
|
2020-09-15 15:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
func (a *goBlog) postHtml(p *post) template.HTML {
|
2020-10-18 09:54:29 +00:00
|
|
|
if p.rendered != "" {
|
|
|
|
return p.rendered
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
htmlContent, err := a.renderMarkdown(p.Content, false)
|
2020-10-18 09:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
p.rendered = template.HTML(htmlContent)
|
|
|
|
return p.rendered
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
func (a *goBlog) absolutePostHTML(p *post) template.HTML {
|
2020-12-23 13:36:27 +00:00
|
|
|
if p.absoluteRendered != "" {
|
|
|
|
return p.absoluteRendered
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
htmlContent, err := a.renderMarkdown(p.Content, true)
|
2020-12-23 13:36:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
p.absoluteRendered = template.HTML(htmlContent)
|
|
|
|
return p.absoluteRendered
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:54:29 +00:00
|
|
|
const summaryDivider = "<!--more-->"
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
func (a *goBlog) postSummary(p *post) (summary string) {
|
2020-09-15 15:01:53 +00:00
|
|
|
summary = p.firstParameter("summary")
|
|
|
|
if summary != "" {
|
|
|
|
return
|
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
html := string(a.postHtml(p))
|
2020-10-18 09:54:29 +00:00
|
|
|
if splitted := strings.Split(html, summaryDivider); len(splitted) > 1 {
|
|
|
|
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(splitted[0]))
|
2020-09-15 15:01:53 +00:00
|
|
|
summary = doc.Text()
|
2020-10-18 09:54:29 +00:00
|
|
|
} else {
|
|
|
|
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
|
|
|
|
summary = doc.Find("p").First().Text()
|
2020-09-15 15:01:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 18:34:34 +00:00
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
func (a *goBlog) postTranslations(p *post) []*post {
|
2020-10-16 18:34:34 +00:00
|
|
|
translationkey := p.firstParameter("translationkey")
|
|
|
|
if translationkey == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
posts, err := a.db.getPosts(&postsRequestConfig{
|
2020-10-16 18:34:34 +00:00
|
|
|
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-18 12:32:03 +00:00
|
|
|
|
2021-06-23 17:20:50 +00:00
|
|
|
func (a *goBlog) postToMfItem(p *post) *microformatItem {
|
2021-07-12 14:19:28 +00:00
|
|
|
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)
|
|
|
|
return µformatItem{
|
|
|
|
Type: []string{"h-entry"},
|
|
|
|
Properties: µformatProperties{
|
|
|
|
Name: p.Parameters["title"],
|
|
|
|
Published: []string{p.Published},
|
|
|
|
Updated: []string{p.Updated},
|
|
|
|
PostStatus: []string{string(p.Status)},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
// 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())
|
|
|
|
}
|