From 3a62d54745e2cbfda6772390830042908d725c71 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Fri, 5 Apr 2019 18:11:04 +0100 Subject: hugolib: Consider summary in front matter for .Summary Add the ability to have a `summary` page variable that overrides the auto-generated summary. Logic for obtaining summary becomes: * if summary divider is present in content, use the text above it * if summary variables is present in page metadata, use that * auto-generate summary from first _x_ words of the content Fixes #5800 --- helpers/content.go | 21 +++++++++++++++++++++ helpers/content_test.go | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'helpers') diff --git a/helpers/content.go b/helpers/content.go index be5090c21..3892647bb 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -42,6 +42,12 @@ import ( // SummaryDivider denotes where content summarization should end. The default is "". var SummaryDivider = []byte("") +var ( + openingPTag = []byte("

") + closingPTag = []byte("

") + paragraphIndicator = []byte("/

tags from HTML input in the situation +// where said tags are the only

tags in the input and enclose the content +// of the input (whitespace excluded). +func (c *ContentSpec) TrimShortHTML(input []byte) []byte { + first := bytes.Index(input, paragraphIndicator) + last := bytes.LastIndex(input, paragraphIndicator) + if first == last { + input = bytes.TrimSpace(input) + input = bytes.TrimPrefix(input, openingPTag) + input = bytes.TrimSuffix(input, closingPTag) + input = bytes.TrimSpace(input) + } + return input +} + func isEndOfSentence(r rune) bool { return r == '.' || r == '?' || r == '!' || r == '"' || r == '\n' } diff --git a/helpers/content_test.go b/helpers/content_test.go index 1dd4a2fb8..709c81142 100644 --- a/helpers/content_test.go +++ b/helpers/content_test.go @@ -29,6 +29,28 @@ import ( const tstHTMLContent = "

This is some text.
And some more.

" +func TestTrimShortHTML(t *testing.T) { + tests := []struct { + input, output []byte + }{ + {[]byte(""), []byte("")}, + {[]byte("Plain text"), []byte("Plain text")}, + {[]byte(" \t\n Whitespace text\n\n"), []byte("Whitespace text")}, + {[]byte("

Simple paragraph

"), []byte("Simple paragraph")}, + {[]byte("\n \n \t

\t Whitespace\nHTML \n\t

\n\t"), []byte("Whitespace\nHTML")}, + {[]byte("

Multiple

paragraphs

"), []byte("

Multiple

paragraphs

")}, + {[]byte("

Nested

paragraphs

"), []byte("

Nested

paragraphs

")}, + } + + c := newTestContentSpec() + for i, test := range tests { + output := c.TrimShortHTML(test.input) + if bytes.Compare(test.output, output) != 0 { + t.Errorf("Test %d failed. Expected %q got %q", i, test.output, output) + } + } +} + func TestStripHTML(t *testing.T) { type test struct { input, expected string -- cgit v1.2.3