Improve summary algorithm

pull/7/head
Jan-Lukas Else 2 years ago
parent 4d452963d5
commit 912b24b9b4
  1. 7
      activitystreams.go
  2. 3
      feeds.go
  3. 4
      posts.go
  4. 30
      postsFuncs.go
  5. 6
      render.go
  6. 4
      templates/post.gohtml
  7. 12
      utils.go

@ -68,12 +68,7 @@ func servePostActivityStreams(w http.ResponseWriter, r *http.Request) {
as.Type = "Note"
}
// Content
if rendered, err := renderMarkdown(p.Content); err == nil {
as.Content = string(rendered)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
as.Content = string(p.html())
// Attachments
if images := p.Parameters[appConfig.Blogs[p.Blog].ActivityStreams.ImagesParameter]; len(images) > 0 {
for _, image := range images {

@ -32,13 +32,12 @@ func generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Reques
Created: now,
}
for _, p := range posts {
htmlContent, _ := renderMarkdown(p.Content)
feed.Add(&feeds.Item{
Title: p.title(),
Link: &feeds.Link{Href: appConfig.Server.PublicAddress + p.Path},
Description: p.summary(),
Id: p.Path,
Content: string(htmlContent),
Content: string(p.html()),
})
}
var feedStr string

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
"reflect"
"strconv"
@ -24,7 +25,8 @@ type post struct {
Blog string `json:"blog"`
Section string `json:"section"`
// Not persisted
Slug string `json:"slug"`
Slug string `json:"slug"`
rendered template.HTML
}
func servePost(w http.ResponseWriter, r *http.Request) {

@ -2,6 +2,8 @@ package main
import (
"context"
"html/template"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
@ -18,20 +20,34 @@ func (p *post) title() string {
return p.firstParameter("title")
}
func (p *post) html() template.HTML {
if p.rendered != "" {
return p.rendered
}
htmlContent, err := renderMarkdown(p.Content)
if err != nil {
log.Fatal(err)
return ""
}
p.rendered = template.HTML(htmlContent)
return p.rendered
}
const summaryDivider = "<!--more-->"
func (p *post) summary() (summary string) {
summary = p.firstParameter("summary")
if summary != "" {
return
}
summaryDivider := "<!--more-->"
rendered, _ := renderMarkdown(p.Content)
if strings.Contains(string(rendered), summaryDivider) {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(strings.Split(string(rendered), summaryDivider)[0]))
html := string(p.html())
if splitted := strings.Split(html, summaryDivider); len(splitted) > 1 {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(splitted[0]))
summary = doc.Text()
return
} else {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
summary = doc.Find("p").First().Text()
}
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(string(rendered)))
summary = firstSentences(doc.Text(), 3)
return
}

@ -47,11 +47,10 @@ func initRendering() error {
}
return template.HTML(htmlContent)
},
// First parameter value
// Post specific
"p": func(p *post, parameter string) string {
return p.firstParameter(parameter)
},
// Post specific
"ps": func(p *post, parameter string) []string {
return p.Parameters[parameter]
},
@ -61,6 +60,9 @@ func initRendering() error {
"title": func(p *post) string {
return p.title()
},
"content": func(p *post) template.HTML {
return p.html()
},
"summary": func(p *post) string {
return p.summary()
},

@ -10,8 +10,8 @@
<p>{{ string .Blog.Lang "publishedon" }} {{ longDate .Data.Published .Blog.Lang }}</p>{{ end }}
{{ if .Data.Updated }}
<p>{{ string .Blog.Lang "updatedon" }} {{ longDate .Data.Updated .Blog.Lang }}</p>{{ end }}
{{ with .Data.Content }}
<div class=e-content>{{ md . }}</div>
{{ if .Data.Content }}
<div class=e-content>{{ content .Data }}</div>
{{ end }}
</article>
{{ $post := .Data }}

@ -19,18 +19,6 @@ func urlize(str string) string {
return newStr
}
func firstSentences(value string, count int) string {
for i := range value {
if value[i] == '.' || value[i] == '!' || value[i] == '?' {
count--
if count == 0 && i < len(value) {
return value[0 : i+1]
}
}
}
return value
}
func sortedStrings(s []string) []string {
sort.Slice(s, func(i, j int) bool {
return strings.ToLower(s[i]) < strings.ToLower(s[j])

Loading…
Cancel
Save