shithub: hugo

Download patch

ref: 89e31256649aeddec945a2d6dc73478f9a1ce0b8
parent: c936b6c89af8b378145bb78d19ac98f0a1a3d264
author: Joonatan Saarhelo <[email protected]>
date: Tue Oct 25 17:40:32 EDT 2016

Get rid of the rawContentCopy field of the Page struct

It is not needed, because it is only used to store temporary data during `preparePagesForRender`.

--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -458,21 +458,23 @@
 
 				// If in watch mode, we need to keep the original so we can
 				// repeat this process on rebuild.
+				var rawContentCopy []byte
 				if cfg.Watching {
-					p.rawContentCopy = make([]byte, len(p.rawContent))
-					copy(p.rawContentCopy, p.rawContent)
+					rawContentCopy = make([]byte, len(p.rawContent))
+					copy(rawContentCopy, p.rawContent)
 				} else {
 					// Just reuse the same slice.
-					p.rawContentCopy = p.rawContent
+					rawContentCopy = p.rawContent
 				}
 
 				if p.Markup == "markdown" {
-					tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.rawContentCopy)
+					tmpContent, tmpTableOfContents := helpers.ExtractTOC(rawContentCopy)
 					p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
-					p.rawContentCopy = tmpContent
+					rawContentCopy = tmpContent
 				}
 
-				if err := handleShortcodes(p, s.owner.tmpl); err != nil {
+				var err error
+				if rawContentCopy, err = handleShortcodes(p, s.owner.tmpl, rawContentCopy); err != nil {
 					jww.ERROR.Printf("Failed to handle shortcodes for page %s: %s", p.BaseFileName(), err)
 				}
 
@@ -479,15 +481,15 @@
 				if p.Markup != "html" {
 
 					// Now we know enough to create a summary of the page and count some words
-					summaryContent, err := p.setUserDefinedSummaryIfProvided()
+					summaryContent, err := p.setUserDefinedSummaryIfProvided(rawContentCopy)
 
 					if err != nil {
 						jww.ERROR.Printf("Failed to set user defined summary for page %q: %s", p.Path(), err)
 					} else if summaryContent != nil {
-						p.rawContentCopy = summaryContent.content
+						rawContentCopy = summaryContent.content
 					}
 
-					p.Content = helpers.BytesToHTML(p.rawContentCopy)
+					p.Content = helpers.BytesToHTML(rawContentCopy)
 
 					if summaryContent == nil {
 						p.setAutoSummary()
@@ -494,11 +496,11 @@
 					}
 
 				} else {
-					p.Content = helpers.BytesToHTML(p.rawContentCopy)
+					p.Content = helpers.BytesToHTML(rawContentCopy)
 				}
 
 				// no need for this anymore
-				p.rawContentCopy = nil
+				rawContentCopy = nil
 
 				//analyze for raw stats
 				p.analyzePage()
@@ -522,16 +524,16 @@
 	return h.Sites[0].AllPages
 }
 
-func handleShortcodes(p *Page, t tpl.Template) error {
+func handleShortcodes(p *Page, t tpl.Template, rawContentCopy []byte) ([]byte, error) {
 	if len(p.contentShortCodes) > 0 {
 		jww.DEBUG.Printf("Replace %d shortcodes in %q", len(p.contentShortCodes), p.BaseFileName())
 		shortcodes, err := executeShortcodeFuncMap(p.contentShortCodes)
 
 		if err != nil {
-			return err
+			return rawContentCopy, err
 		}
 
-		p.rawContentCopy, err = replaceShortcodeTokens(p.rawContentCopy, shortcodePlaceholderPrefix, shortcodes)
+		rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, shortcodes)
 
 		if err != nil {
 			jww.FATAL.Printf("Failed to replace short code tokens in %s:\n%s", p.BaseFileName(), err.Error())
@@ -538,7 +540,7 @@
 		}
 	}
 
-	return nil
+	return rawContentCopy, nil
 }
 
 func (s *Site) updateBuildStats(page *Page) {
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -76,11 +76,6 @@
 	// when shortcode changes etc.
 	rawContent []byte
 
-	// When running Hugo in watch mode, we do partial rebuilds and have to make
-	// a copy of the rawContent to be prepared for rebuilds when shortcodes etc.
-	// have changed.
-	rawContentCopy []byte
-
 	// state telling if this is a "new page" or if we have rendered it previously.
 	rendered bool
 
@@ -258,9 +253,9 @@
 )
 
 // Returns the page as summary and main if a user defined split is provided.
-func (p *Page) setUserDefinedSummaryIfProvided() (*summaryContent, error) {
+func (p *Page) setUserDefinedSummaryIfProvided(rawContentCopy []byte) (*summaryContent, error) {
 
-	sc, err := splitUserDefinedSummaryAndContent(p.Markup, p.rawContentCopy)
+	sc, err := splitUserDefinedSummaryAndContent(p.Markup, rawContentCopy)
 
 	if err != nil {
 		return nil, err