shithub: hugo

Download patch

ref: df20b05463fef42aba93d5208e410a7ecc56da5d
parent: 863a812e07193541b42732b0e227f3d320433f01
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Jan 29 05:44:09 EST 2018

resource: Make resource counters for name and title independent

This is the most flexible with the current syntax, and probably what most people would expcect.

Updates #4335

--- a/resource/resource.go
+++ b/resource/resource.go
@@ -464,10 +464,10 @@
 		}
 
 		var (
-			nameSet, titleSet bool
-			currentCounter    = 0
-			counterFound      bool
-			resourceSrcKey    = strings.ToLower(r.Name())
+			nameSet, titleSet                   bool
+			nameCounter, titleCounter           = 0, 0
+			nameCounterFound, titleCounterFound bool
+			resourceSrcKey                      = strings.ToLower(r.Name())
 		)
 
 		ma := r.(metaAssigner)
@@ -491,15 +491,16 @@
 					name, found := meta["name"]
 					if found {
 						name := cast.ToString(name)
-						if !counterFound {
-							counterFound = strings.Contains(name, counterPlaceHolder)
+						if !nameCounterFound {
+							nameCounterFound = strings.Contains(name, counterPlaceHolder)
 						}
-						if counterFound && currentCounter == 0 {
-							currentCounter = counters[srcKey] + 1
-							counters[srcKey] = currentCounter
+						if nameCounterFound && nameCounter == 0 {
+							counterKey := "name_" + srcKey
+							nameCounter = counters[counterKey] + 1
+							counters[counterKey] = nameCounter
 						}
 
-						ma.setName(replaceResourcePlaceholders(name, currentCounter))
+						ma.setName(replaceResourcePlaceholders(name, nameCounter))
 						nameSet = true
 					}
 				}
@@ -508,14 +509,15 @@
 					title, found := meta["title"]
 					if found {
 						title := cast.ToString(title)
-						if !counterFound {
-							counterFound = strings.Contains(title, counterPlaceHolder)
+						if !titleCounterFound {
+							titleCounterFound = strings.Contains(title, counterPlaceHolder)
 						}
-						if counterFound && currentCounter == 0 {
-							currentCounter = counters[srcKey] + 1
-							counters[srcKey] = currentCounter
+						if titleCounterFound && titleCounter == 0 {
+							counterKey := "title_" + srcKey
+							titleCounter = counters[counterKey] + 1
+							counters[counterKey] = titleCounter
 						}
-						ma.setTitle((replaceResourcePlaceholders(title, currentCounter)))
+						ma.setTitle((replaceResourcePlaceholders(title, titleCounter)))
 						titleSet = true
 					}
 				}
--- a/resource/resource_test.go
+++ b/resource/resource_test.go
@@ -280,14 +280,14 @@
 			},
 		}, func(err error) {
 			assert.NoError(err)
-			assert.Equal("Resource #1", logo2.Title())
+			assert.Equal("Resource #2", logo2.Title())
 			assert.Equal("Logo Name #1", logo2.Name())
-			assert.Equal("Resource #2", logo1.Title())
+			assert.Equal("Resource #4", logo1.Title())
 			assert.Equal("Logo Name #2", logo1.Name())
 			assert.Equal("Resource #1", foo2.Title())
-			assert.Equal("Resource #2", foo1.Title())
+			assert.Equal("Resource #3", foo1.Title())
 			assert.Equal("Name #2", foo1.Name())
-			assert.Equal("Resource #3", foo3.Title())
+			assert.Equal("Resource #5", foo3.Title())
 
 			assert.Equal(logo2, resources.GetByPrefix("logo name #1"))
 
@@ -305,7 +305,7 @@
 		}, func(err error) {
 			assert.NoError(err)
 			assert.Equal("Third Logo #1", logo3.Title())
-			assert.Equal("Name #1", logo3.Name())
+			assert.Equal("Name #3", logo3.Name())
 			assert.Equal("Other Logo #1", logo2.Title())
 			assert.Equal("Name #1", logo2.Name())
 			assert.Equal("Other Logo #2", logo1.Title())
@@ -312,8 +312,6 @@
 			assert.Equal("Name #2", logo1.Name())
 
 		}},
-		// Start counting first time :counter is used
-		// See https://github.com/gohugoio/hugo/issues/4335
 		{[]map[string]interface{}{
 			map[string]interface{}{
 				"title": "Third Logo",