hugolib: Fix regression of .Truncated evaluation in manual summaries

This fixes the behavior of .Truncated that was introduced with commit
bef496b97e which was later broken.  The
desired behavior is that .Truncated would evaluate to false when there
was nothing after the user defined summary marker.

This also adds a simple unit test to ensure that this feature isn't
broken again.  The check for content after the user defined summary
marker is done on the raw content instead of the working copy because
some of the markup renderers add elements after the marker, making it
difficult to determine if there is actually any content.

The behavior (evaluating to false when there is no content, just
summary) is also now documented.
This commit is contained in:
Anton Staaf
2017-01-26 11:58:25 -08:00
committed by Bjørn Erik Pedersen
parent 9416fdd334
commit 99fbc75e7a
4 changed files with 59 additions and 45 deletions

View File

@@ -414,6 +414,23 @@ var (
internalSummaryDivider = []byte("HUGOMORE42")
)
// We have to replace the <!--more--> with something that survives all the
// rendering engines.
// TODO(bep) inline replace
func (p *Page) replaceDivider(content []byte) []byte {
sections := bytes.Split(content, helpers.SummaryDivider)
// If the raw content has nothing but whitespace after the summary
// marker then the page shouldn't be marked as truncated. This check
// is simplest against the raw content because different markup engines
// (rst and asciidoc in particular) add div and p elements after the
// summary marker.
p.Truncated = (len(sections) == 2 &&
len(bytes.Trim(sections[1], " \n\r")) > 0)
return bytes.Join(sections, internalSummaryDivider)
}
// Returns the page as summary and main if a user defined split is provided.
func (p *Page) setUserDefinedSummaryIfProvided(rawContentCopy []byte) (*summaryContent, error) {
@@ -428,12 +445,6 @@ func (p *Page) setUserDefinedSummaryIfProvided(rawContentCopy []byte) (*summaryC
return nil, nil
}
p.Truncated = true
if len(sc.content) < 20 {
// only whitespace?
p.Truncated = len(bytes.Trim(sc.content, " \n\r")) > 0
}
p.Summary = helpers.BytesToHTML(sc.summary)
return sc, nil
@@ -441,9 +452,8 @@ func (p *Page) setUserDefinedSummaryIfProvided(rawContentCopy []byte) (*summaryC
// Make this explicit so there is no doubt about what is what.
type summaryContent struct {
summary []byte
content []byte
contentWithoutSummary []byte
summary []byte
content []byte
}
func splitUserDefinedSummaryAndContent(markup string, c []byte) (sc *summaryContent, err error) {
@@ -467,7 +477,6 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) (sc *summaryCont
startMarkup []byte
endMarkup []byte
addDiv bool
divStart = []byte("<div class=\"document\">")
)
switch markup {
@@ -499,20 +508,16 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) (sc *summaryCont
withoutDivider := bytes.TrimSpace(append(c[:startDivider], c[endDivider:]...))
var (
contentWithoutSummary []byte
summary []byte
summary []byte
)
if len(withoutDivider) > 0 {
contentWithoutSummary = bytes.TrimSpace(withoutDivider[endSummary:])
summary = bytes.TrimSpace(withoutDivider[:endSummary])
}
if addDiv {
// For the rst
summary = append(append([]byte(nil), summary...), []byte("</div>")...)
// TODO(bep) include the document class, maybe
contentWithoutSummary = append(divStart, contentWithoutSummary...)
}
if err != nil {
@@ -520,9 +525,8 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) (sc *summaryCont
}
sc = &summaryContent{
summary: summary,
content: withoutDivider,
contentWithoutSummary: contentWithoutSummary,
summary: summary,
content: withoutDivider,
}
return