Reste à prévoir la pagination et la recherche
This commit is contained in:
46
blog.go
46
blog.go
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
@@ -17,8 +18,9 @@ type Post struct {
|
||||
Title string
|
||||
Author string
|
||||
Date string
|
||||
DateTime time.Time
|
||||
HTML template.HTML
|
||||
ShortText string
|
||||
ShortText template.HTML
|
||||
Text string
|
||||
}
|
||||
|
||||
@@ -44,28 +46,25 @@ func (b *Blog) GetPost(Id string) (Post, bool) {
|
||||
return ret, false
|
||||
}
|
||||
func (p *Post) GenerateExcerpt() {
|
||||
fmt.Println("----", p.Title)
|
||||
|
||||
p.ShortText = ""
|
||||
if len(p.HTML) > 0 {
|
||||
fmt.Println("Short text is html", p.HTML)
|
||||
heading, err := p.GetFirstHeading(string(p.HTML))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
paragraph, err := p.GetFirstParagraph(string(p.HTML))
|
||||
paragraph, err := p.GetFirstParagraphs(string(p.HTML))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if len(heading) > 0 {
|
||||
log.Println("heading is:", heading)
|
||||
p.ShortText += heading
|
||||
p.ShortText += template.HTML(heading)
|
||||
}
|
||||
if len(paragraph) > 0 {
|
||||
log.Println("paragraph is:", paragraph)
|
||||
p.ShortText += paragraph
|
||||
p.ShortText += template.HTML(paragraph)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Short text is text", p.Text)
|
||||
p.ShortText = p.Text
|
||||
p.ShortText = template.HTML(p.Text)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,22 +87,31 @@ func (p *Post) GetFirstHeading(html string) (string, error) {
|
||||
|
||||
return htmlContent, nil
|
||||
}
|
||||
func (p *Post) GetFirstParagraph(html string) (string, error) {
|
||||
func (p *Post) GetFirstParagraphs(html string) (string, error) {
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
firstParagraph := doc.Find("p").First()
|
||||
if firstParagraph.Length() == 0 {
|
||||
return "", fmt.Errorf("no paragraph found")
|
||||
}
|
||||
var htmlContent string
|
||||
var numberOfTextChars int
|
||||
|
||||
htmlContent, err := goquery.OuterHtml(firstParagraph)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Eventuellement trouver un moyen de ne plus examiner le reste du document
|
||||
// si 200 caractères atteints
|
||||
doc.Find("p,div,h1,h2,h3,h4,h5").Each(func(i int, s *goquery.Selection) {
|
||||
|
||||
if numberOfTextChars < 200 {
|
||||
|
||||
numberOfTextChars += len(s.Text())
|
||||
|
||||
content, err := goquery.OuterHtml(s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
htmlContent += content
|
||||
}
|
||||
})
|
||||
|
||||
return htmlContent, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user