Repository: gohugoio/hugo (language: go)

Issue:
Repeated use of `:sections` breaks permalink
Using this setting:

```
permalinks:
  posts: "/:sections[1]/:sections[last]"
```

I expect output paths of the form `/section_one/section_last/index.html`, but instead a lot of files are published to the root of the public folder.

Using other variables work fine, e.g.:

```
  posts: "/:sections[1]/:section"
```

### What version of Hugo are you using (`hugo version`)?

```
hugo v0.104.3-58b824581360148f2d91f5cc83f69bd22c1aa331 linux/amd64 BuildDate=2022-10-04T14:25:23Z VendorInfo=gohugoio
```

### Does this issue reproduce with the latest release?

Yes


Discussion / hints:
Clarification...

First, the `permalinks` setting in site configuration does not apply to section pages. See [this proposal](https://github.com/gohugoio/hugo/issues/8523).

Second, the `permalink` needs to be unique for each page, otherwise one page will clobber another. 

#### Example 1 - Bad (demonstrates pages being clobbered)

```
content/
├── posts/
│   ├── _index.md
│   ├── post-1.md
│   ├── post-2.md
│   └── post-3.md
└── _index.md
```

site configuration

```text
[permalinks]
posts = '/:sections[1]/:sections[last]'  # this will not be unique for each page
```

command

```text
$ hugo --printPathWarnings

WARN 2022/11/04 12:29:29 Duplicate target paths: /index.html (4)

$ tree public

public/
├── posts/
│   └── index.html
└── index.html
```

#### Example 2 - Good (but demonstrates the bug)

```
content/
├── posts/
│   ├── _index.md
│   ├── post-1.md
│   ├── post-2.md
│   └── post-3.md
└── _index.md
```

site configuration

```text
[permalinks]
posts = '/:sections[1]/:sections[last]/:slug'
```

command

```text
$ hugo --printPathWarnings
$ tree public

public/
├── post-1/
│   └── index.html
├── post-2/
│   └── index.html
├── post-3/
│   └── index.html
├── posts/
│   └── index.html
└── index.html
```

The issue as reported is valid, as these files are dumped in the root instead of in a section.


I still don't think the original behavior is correct, despite resulting in non-unique filenames.
@stefanv That's why I wrote

> The issue as reported is valid
Minimal reproducible example:

```bash
git clone --single-branch -b hugo-github-issue-10377 https://github.com/jmooring/hugo-testing hugo-github-issue-10377
cd hugo-github-issue-10377
hugo && tree public
```

```text
content/
├── posts/
│   ├── foo/
│   │   ├── bar/
│   │   │   ├── baz/
│   │   │   │   ├── _index.md
│   │   │   │   ├── post-1.md
│   │   │   │   ├── post-2.md
│   │   │   │   └── post-3.md
│   │   │   └── _index.md
│   │   └── _index.md
│   └── _index.md
└── _index.md
```

site config

```toml
[permalinks]
posts = '/:sections[1]/:sections[last]/:slug'
```
Expected output:

```text
public/
├── foo/
│   └── baz/
│       ├── post-1/
│       │   └── index.html
│       ├── post-2/
│       │   └── index.html
│       └── post-3/
│           └── index.html
├── posts/
│   ├── foo/
│   │   ├── bar/
│   │   │   ├── baz/
│   │   │   │   └── index.html
│   │   │   └── index.html
│   │   └── index.html
│   └── index.html
└── index.html
```

Actual output:

```text
public/
├── post-1/
│   └── index.html
├── post-2/
│   └── index.html
├── post-3/
│   └── index.html
├── posts/
│   ├── foo/
│   │   ├── bar/
│   │   │   ├── baz/
│   │   │   │   └── index.html
│   │   │   └── index.html
│   │   └── index.html
│   └── index.html
└── index.html
```






Yea, it's a bug and it will be fixed soonish.
> valid

Apologies, @jmooring, I missed that last sentence.

Fix patch:
diff --git a/resources/page/permalinks.go b/resources/page/permalinks.go
index c31d22a3c67..dbf10220c19 100644
--- a/resources/page/permalinks.go
+++ b/resources/page/permalinks.go
@@ -176,7 +176,7 @@ func (l PermalinkExpander) parse(patterns map[string]string) (map[string]func(Pa
 // can return a string to go in that position in the page (or an error)
 type pageToPermaAttribute func(Page, string) (string, error)
 
-var attributeRegexp = regexp.MustCompile(`:\w+(\[.+\])?`)
+var attributeRegexp = regexp.MustCompile(`:\w+(\[.+?\])?`)
 
 // validate determines if a PathPattern is well-formed
 func (l PermalinkExpander) validate(pp string) bool {


Test patch:
diff --git a/resources/page/integration_test.go b/resources/page/integration_test.go
index 9dc322b4a5e..0761de2499d 100644
--- a/resources/page/integration_test.go
+++ b/resources/page/integration_test.go
@@ -136,3 +136,42 @@ Sort: [éclair emma xylophone zulu]
 ByWeight: alpha|émotion|zulu|
 `)
 }
+
+// See #10377
+func TestPermalinkExpansionSectionsRepeated(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+disableKinds = ["home", "taxonomy", "taxonomyTerm", "sitemap"]
+[outputs]
+home = ["HTML"]
+page = ["HTML"]
+section = ["HTML"]
+[outputFormats]
+[permalinks]
+posts = '/:sections[1]/:sections[last]/:slug'
+-- content/posts/_index.md --
+-- content/posts/a/_index.md --
+-- content/posts/a/b/_index.md --
+-- content/posts/a/b/c/_index.md --
+-- content/posts/a/b/c/d.md --
+---
+title: "D"
+slug: "d"
+---
+D
+-- layouts/_default/single.html --
+RelPermalink: {{ .RelPermalink }}
+
+`
+
+	b := hugolib.NewIntegrationTestBuilder(
+		hugolib.IntegrationTestConfig{
+			T:           t,
+			TxtarString: files,
+		}).Build()
+
+	b.AssertFileContent("public/a/c/d/index.html", "RelPermalink: /a/c/d/")
+
+}
diff --git a/resources/page/permalinks_test.go b/resources/page/permalinks_test.go
index 7baf1650370..07333492fc1 100644
--- a/resources/page/permalinks_test.go
+++ b/resources/page/permalinks_test.go
@@ -42,6 +42,7 @@ var testdataPermalinks = []struct {
 	{"/:2006_01_02_15_04_05.000", true, "/2012_04_06_03_01_59.000"}, // Complicated custom date format
 	{"/:sections/", true, "/a/b/c/"},                                // Sections
 	{"/:sections[last]/", true, "/c/"},                              // Sections
+	{"/:sections[0]/:sections[last]/", true, "/a/c/"},               // Sections
 
 	// Failures
 	{"/blog/:fred", false, ""},