diff --git a/blog.go b/blog.go index 7f050a9..6f795bd 100644 --- a/blog.go +++ b/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 } diff --git a/configuration.go b/configuration.go index 47ea09e..5f8fb30 100644 --- a/configuration.go +++ b/configuration.go @@ -14,10 +14,15 @@ type MailBoxConfiguration struct { SSL string InBox string } +type ServiceConfiguration struct { + Address string + Port string +} type BlogConfiguration struct { Title string ShortName string MailBox MailBoxConfiguration + Service ServiceConfiguration } func (configuration *BlogConfiguration) Prompt() error { @@ -31,7 +36,7 @@ func (configuration *BlogConfiguration) Prompt() error { return err } if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(configuration.ShortName) { - return fmt.Errorf("short same invalid") + return fmt.Errorf("short name invalid") } /* @@ -73,5 +78,15 @@ func (configuration *BlogConfiguration) Prompt() error { return err } + configuration.Service.Address, err = prompt("Listen address", nil, "127.0.0.1", configuration.Service.Address, false) + if err != nil { + return err + } + + configuration.Service.Port, err = prompt("Listen port", nil, "8080", configuration.Service.Port, false) + if err != nil { + return err + } + return nil } diff --git a/html/entry.tmpl b/html/entry.tmpl index 8db3a12..3214418 100644 --- a/html/entry.tmpl +++ b/html/entry.tmpl @@ -3,14 +3,23 @@
+