Fix Truncate - hugo - [fork] hugo port for 9front
 (HTM) git clone git@git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 48b6777ea212073d32c3fb59ea3754d5cf8348de
 (DIR) parent 2bee4a157068edcd9a159ee848bec46c061d34b1
 (HTM) Author: bep <bjorn.erik.pedersen@gmail.com>
       Date:   Thu,  5 Feb 2015 21:44:15 +0100
       
       Fix Truncate
       
       TruncateWordsToWholeSentence knows if the summary is truncated, so let "him" decide.
       
       Fixes #880
       
       Diffstat:
         M helpers/content.go                  |      14 ++++++++------
         M helpers/content_test.go             |      28 ++++++++++++++++++++++++++++
         M hugolib/page.go                     |       7 +++----
       
       3 files changed, 39 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/helpers/content.go b/helpers/content.go
       @@ -271,10 +271,11 @@ func TruncateWords(s string, max int) string {
        }
        
        // TruncateWordsToWholeSentence takes content and an int
       -// and returns entire sentences from content, delimited by the int.
       -func TruncateWordsToWholeSentence(words []string, max int) string {
       -        if max > len(words) {
       -                return strings.Join(words, " ")
       +// and returns entire sentences from content, delimited by the int
       +// and whether it's truncated or not.
       +func TruncateWordsToWholeSentence(words []string, max int) (string, bool) {
       +        if max >= len(words) {
       +                return strings.Join(words, " "), false
                }
        
                for counter, word := range words[max:] {
       @@ -282,11 +283,12 @@ func TruncateWordsToWholeSentence(words []string, max int) string {
                                strings.HasSuffix(word, "?") ||
                                strings.HasSuffix(word, ".\"") ||
                                strings.HasSuffix(word, "!") {
       -                        return strings.Join(words[:max+counter+1], " ")
       +                        upper := max + counter + 1
       +                        return strings.Join(words[:upper], " "), (upper < len(words))
                        }
                }
        
       -        return strings.Join(words[:max], " ")
       +        return strings.Join(words[:max], " "), true
        }
        
        // GetRstContent calls the Python script rst2html as an external helper
 (DIR) diff --git a/helpers/content_test.go b/helpers/content_test.go
       @@ -3,6 +3,7 @@ package helpers
        import (
                "github.com/stretchr/testify/assert"
                "html/template"
       +        "strings"
                "testing"
        )
        
       @@ -33,3 +34,30 @@ func TestStripEmptyNav(t *testing.T) {
        func TestBytesToHTML(t *testing.T) {
                assert.Equal(t, template.HTML("dobedobedo"), BytesToHTML([]byte("dobedobedo")))
        }
       +
       +func TestTruncateWordsToWholeSentence(t *testing.T) {
       +        type test struct {
       +                input, expected string
       +                max             int
       +                truncated       bool
       +        }
       +        data := []test{
       +                {"a b c", "a b c", 12, false},
       +                {"a b c", "a b c", 3, false},
       +                {"a", "a", 1, false},
       +                {"This is a sentence.", "This is a sentence.", 5, false},
       +                {"This is also a sentence!", "This is also a sentence!", 1, false},
       +                {"To be. Or not to be. That's the question.", "To be.", 1, true},
       +                {" \nThis is not a sentence\n ", "This is not a", 4, true},
       +        }
       +        for i, d := range data {
       +                output, truncated := TruncateWordsToWholeSentence(strings.Fields(d.input), d.max)
       +                if d.expected != output {
       +                        t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
       +                }
       +
       +                if d.truncated != truncated {
       +                        t.Errorf("Test %d failed. Expected truncated=%t got %t", i, d.truncated, truncated)
       +                }
       +        }
       +}
 (DIR) diff --git a/hugolib/page.go b/hugolib/page.go
       @@ -190,10 +190,9 @@ func (p *Page) setSummary() {
                } else {
                        // If hugo defines split:
                        // render, strip html, then split
       -                p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)))
       -
       -                // todo bep - check if the Plain() can be trimmed earlier
       -                p.Truncated = len(p.Summary) != len(strings.Trim(p.Plain(), "\n\r "))
       +                summary, truncated := helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)
       +                p.Summary = helpers.BytesToHTML([]byte(summary))
       +                p.Truncated = truncated
        
                }
        }