Skip to content

Commit

Permalink
resource: Make resource counters for name and title independent
Browse files Browse the repository at this point in the history
This is the most flexible with the current syntax, and probably what most people would expcect.

Updates #4335
  • Loading branch information
bep committed Jan 29, 2018
1 parent 863a812 commit df20b05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
34 changes: 18 additions & 16 deletions resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
}

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)
Expand All @@ -491,15 +491,16 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
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
}
}
Expand All @@ -508,14 +509,15 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er
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
}
}
Expand Down
12 changes: 5 additions & 7 deletions resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ func TestAssignMetadata(t *testing.T) {
},
}, 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"))

Expand All @@ -305,15 +305,13 @@ func TestAssignMetadata(t *testing.T) {
}, 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())
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",
Expand Down

0 comments on commit df20b05

Please sign in to comment.