mirror of https://github.com/jlelse/GoBlog
Improve summary algorithm
This commit is contained in:
parent
4d452963d5
commit
912b24b9b4
|
@ -68,12 +68,7 @@ func servePostActivityStreams(w http.ResponseWriter, r *http.Request) {
|
||||||
as.Type = "Note"
|
as.Type = "Note"
|
||||||
}
|
}
|
||||||
// Content
|
// Content
|
||||||
if rendered, err := renderMarkdown(p.Content); err == nil {
|
as.Content = string(p.html())
|
||||||
as.Content = string(rendered)
|
|
||||||
} else {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Attachments
|
// Attachments
|
||||||
if images := p.Parameters[appConfig.Blogs[p.Blog].ActivityStreams.ImagesParameter]; len(images) > 0 {
|
if images := p.Parameters[appConfig.Blogs[p.Blog].ActivityStreams.ImagesParameter]; len(images) > 0 {
|
||||||
for _, image := range images {
|
for _, image := range images {
|
||||||
|
|
3
feeds.go
3
feeds.go
|
@ -32,13 +32,12 @@ func generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Reques
|
||||||
Created: now,
|
Created: now,
|
||||||
}
|
}
|
||||||
for _, p := range posts {
|
for _, p := range posts {
|
||||||
htmlContent, _ := renderMarkdown(p.Content)
|
|
||||||
feed.Add(&feeds.Item{
|
feed.Add(&feeds.Item{
|
||||||
Title: p.title(),
|
Title: p.title(),
|
||||||
Link: &feeds.Link{Href: appConfig.Server.PublicAddress + p.Path},
|
Link: &feeds.Link{Href: appConfig.Server.PublicAddress + p.Path},
|
||||||
Description: p.summary(),
|
Description: p.summary(),
|
||||||
Id: p.Path,
|
Id: p.Path,
|
||||||
Content: string(htmlContent),
|
Content: string(p.html()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
var feedStr string
|
var feedStr string
|
||||||
|
|
2
posts.go
2
posts.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -25,6 +26,7 @@ type post struct {
|
||||||
Section string `json:"section"`
|
Section string `json:"section"`
|
||||||
// Not persisted
|
// Not persisted
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
|
rendered template.HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
func servePost(w http.ResponseWriter, r *http.Request) {
|
func servePost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
@ -18,20 +20,34 @@ func (p *post) title() string {
|
||||||
return p.firstParameter("title")
|
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) {
|
func (p *post) summary() (summary string) {
|
||||||
summary = p.firstParameter("summary")
|
summary = p.firstParameter("summary")
|
||||||
if summary != "" {
|
if summary != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
summaryDivider := "<!--more-->"
|
html := string(p.html())
|
||||||
rendered, _ := renderMarkdown(p.Content)
|
if splitted := strings.Split(html, summaryDivider); len(splitted) > 1 {
|
||||||
if strings.Contains(string(rendered), summaryDivider) {
|
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(splitted[0]))
|
||||||
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(strings.Split(string(rendered), summaryDivider)[0]))
|
|
||||||
summary = doc.Text()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,11 +47,10 @@ func initRendering() error {
|
||||||
}
|
}
|
||||||
return template.HTML(htmlContent)
|
return template.HTML(htmlContent)
|
||||||
},
|
},
|
||||||
// First parameter value
|
// Post specific
|
||||||
"p": func(p *post, parameter string) string {
|
"p": func(p *post, parameter string) string {
|
||||||
return p.firstParameter(parameter)
|
return p.firstParameter(parameter)
|
||||||
},
|
},
|
||||||
// Post specific
|
|
||||||
"ps": func(p *post, parameter string) []string {
|
"ps": func(p *post, parameter string) []string {
|
||||||
return p.Parameters[parameter]
|
return p.Parameters[parameter]
|
||||||
},
|
},
|
||||||
|
@ -61,6 +60,9 @@ func initRendering() error {
|
||||||
"title": func(p *post) string {
|
"title": func(p *post) string {
|
||||||
return p.title()
|
return p.title()
|
||||||
},
|
},
|
||||||
|
"content": func(p *post) template.HTML {
|
||||||
|
return p.html()
|
||||||
|
},
|
||||||
"summary": func(p *post) string {
|
"summary": func(p *post) string {
|
||||||
return p.summary()
|
return p.summary()
|
||||||
},
|
},
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
<p>{{ string .Blog.Lang "publishedon" }} {{ longDate .Data.Published .Blog.Lang }}</p>{{ end }}
|
<p>{{ string .Blog.Lang "publishedon" }} {{ longDate .Data.Published .Blog.Lang }}</p>{{ end }}
|
||||||
{{ if .Data.Updated }}
|
{{ if .Data.Updated }}
|
||||||
<p>{{ string .Blog.Lang "updatedon" }} {{ longDate .Data.Updated .Blog.Lang }}</p>{{ end }}
|
<p>{{ string .Blog.Lang "updatedon" }} {{ longDate .Data.Updated .Blog.Lang }}</p>{{ end }}
|
||||||
{{ with .Data.Content }}
|
{{ if .Data.Content }}
|
||||||
<div class=e-content>{{ md . }}</div>
|
<div class=e-content>{{ content .Data }}</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</article>
|
</article>
|
||||||
{{ $post := .Data }}
|
{{ $post := .Data }}
|
||||||
|
|
12
utils.go
12
utils.go
|
@ -19,18 +19,6 @@ func urlize(str string) string {
|
||||||
return newStr
|
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 {
|
func sortedStrings(s []string) []string {
|
||||||
sort.Slice(s, func(i, j int) bool {
|
sort.Slice(s, func(i, j int) bool {
|
||||||
return strings.ToLower(s[i]) < strings.ToLower(s[j])
|
return strings.ToLower(s[i]) < strings.ToLower(s[j])
|
||||||
|
|
Loading…
Reference in New Issue