From 0a7d1d0ddc213628fcde7c0e24991246836d9f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 7 Aug 2016 23:34:04 +0200 Subject: [PATCH] Fix some corner cases in revised summary handling And clean up the test. See #2309 --- hugolib/page.go | 16 ++++++++++++---- hugolib/page_test.go | 33 +++++---------------------------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 99aa0db16..da9fa4f87 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -246,6 +246,7 @@ type summaryContent struct { } func splitUserDefinedSummaryAndContent(markup string, c []byte) *summaryContent { + c = bytes.TrimSpace(c) startDivider := bytes.Index(c, internalSummaryDivider) if startDivider == -1 { @@ -276,20 +277,27 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) *summaryContent } // Find the closest end/start markup string to the divider - //firstStart := bytes.Index(c[:startDivider], startMarkup) fromIdx := bytes.LastIndex(c[:startDivider], startMarkup) fromStart := startDivider - fromIdx - len(startMarkup) fromEnd := bytes.Index(c[endDivider:], endMarkup) if fromEnd != -1 && fromEnd <= fromStart { endSummary = startDivider + fromEnd + len(endMarkup) - } else if fromStart != -1 { + } else if fromStart != -1 && fromEnd != -1 { endSummary = startDivider - fromStart - len(startMarkup) } withoutDivider := bytes.TrimSpace(append(c[:startDivider], c[endDivider:]...)) - contentWithoutSummary := bytes.TrimSpace(withoutDivider[endSummary:]) - summary := bytes.TrimSpace(withoutDivider[:endSummary]) + + var ( + contentWithoutSummary []byte + summary []byte + ) + + if len(withoutDivider) > 0 { + contentWithoutSummary = bytes.TrimSpace(withoutDivider[endSummary:]) + summary = bytes.TrimSpace(withoutDivider[:endSummary]) + } if addDiv { // For the rst diff --git a/hugolib/page_test.go b/hugolib/page_test.go index da46151ed..641e421b3 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -655,6 +655,11 @@ func TestSplitSummaryAndContent(t *testing.T) { {"markdown", "

a

b

cHUGOMORE42

", "

a

b

c

", "

a

b

c

", ""}, {"markdown", "

a

bHUGOMORE42

c

", "

a

b

", "

a

b

c

", "

c

"}, {"markdown", "

aHUGOMORE42

b

c

", "

a

", "

a

b

c

", "

b

c

"}, + {"markdown", " HUGOMORE42 ", "", "", ""}, + {"markdown", "HUGOMORE42", "", "", ""}, + {"markdown", "

HUGOMORE42", "

", "

", ""}, + {"markdown", "HUGOMORE42

", "", "

", "

"}, + {"markdown", "\n\n

HUGOMORE42

\n", "

", "

", ""}, } { sc := splitUserDefinedSummaryAndContent(this.markup, []byte(this.content)) @@ -664,34 +669,6 @@ func TestSplitSummaryAndContent(t *testing.T) { require.Equal(t, this.expectedContent, string(sc.content), fmt.Sprintf("[%d] Content markup %s", i, this.markup)) require.Equal(t, this.expectedContentWithoutSummary, string(sc.contentWithoutSummary), fmt.Sprintf("[%d] Content without summary, markup %s", i, this.markup)) } - - if true { - return - } - - ad := `

sn

-
-

HUGOMORE42 -Some more text

-
-` - - md := `

Summary Same LineHUGOMORE42

- -

Some more text

` - - sc := splitUserDefinedSummaryAndContent("markdown", []byte(md)) - - require.Equal(t, "adf", string(sc.summary)) - require.Equal(t, "asdf", string(sc.content)) - - if true { - return - } - sc = splitUserDefinedSummaryAndContent("asciidoc", []byte(ad)) - require.Equal(t, "

sn

", string(sc.summary)) - require.Equal(t, "\n
\n

\nSome more text

\n
\n", string(sc.summary)) - } func TestPageWithDelimiter(t *testing.T) {