Ejemplo n.º 1
0
func (p *Page) setAutoSummary() error {
	var summary string
	var truncated bool
	if p.isCJKLanguage {
		summary, truncated = helpers.TruncateWordsByRune(p.PlainWords(), helpers.SummaryLength)
	} else {
		summary, truncated = helpers.TruncateWordsToWholeSentence(p.Plain(), helpers.SummaryLength)
	}
	p.Summary = template.HTML(summary)
	p.Truncated = truncated

	return nil
}
Ejemplo n.º 2
0
func (p *Page) setSummary() {

	// at this point, p.rawContent contains placeholders for the short codes,
	// rendered and ready in p.contentShortcodes

	if bytes.Contains(p.rawContent, helpers.SummaryDivider) {
		sections := bytes.Split(p.rawContent, helpers.SummaryDivider)
		header := sections[0]
		p.Truncated = true
		if len(sections[1]) < 20 {
			// only whitespace?
			p.Truncated = len(bytes.Trim(sections[1], " \n\r")) > 0
		}

		// TODO(bep) consider doing this once only
		renderedHeader := p.renderBytes(header)
		if len(p.contentShortCodes) > 0 {
			tmpContentWithTokensReplaced, err :=
				replaceShortcodeTokens(renderedHeader, shortcodePlaceholderPrefix, p.contentShortCodes)
			if err != nil {
				jww.FATAL.Printf("Failed to replace short code tokens in Summary for %s:\n%s", p.BaseFileName(), err.Error())
			} else {
				renderedHeader = tmpContentWithTokensReplaced
			}
		}
		p.Summary = helpers.BytesToHTML(renderedHeader)
	} else {
		// If hugo defines split:
		// render, strip html, then split
		var summary string
		var truncated bool
		if p.isCJKLanguage {
			summary, truncated = helpers.TruncateWordsByRune(p.PlainWords(), helpers.SummaryLength)
		} else {
			summary, truncated = helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)
		}
		p.Summary = template.HTML(summary)
		p.Truncated = truncated

	}
}