Misc paginator adjustments
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Apr 2019 19:59:37 +0000 (21:59 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 8 Apr 2019 09:02:12 +0000 (11:02 +0200)
* Rewind paginator for server mode
* Add some more related tests.
* Replace the clumsy scratch constructs in internal paginator template with variables

See #5825

hugolib/page__paginator.go
hugolib/paginator_test.go [new file with mode: 0644]
hugolib/site_render.go
tpl/tplimpl/embedded/templates.autogen.go
tpl/tplimpl/embedded/templates/pagination.html

index 93701e79921b7f0d66bc4aa931ddcb4c487bcd4b..020d0808971bac579a6839cc696dcfb49cf796d6 100644 (file)
@@ -81,3 +81,7 @@ func (p *pagePaginator) Paginator(options ...interface{}) (*page.Pager, error) {
 
        return p.current, nil
 }
+
+func (p *pagePaginator) rewind() {
+       p.current = p.current.First()
+}
diff --git a/hugolib/paginator_test.go b/hugolib/paginator_test.go
new file mode 100644 (file)
index 0000000..d98ec30
--- /dev/null
@@ -0,0 +1,98 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import (
+       "fmt"
+       "testing"
+)
+
+func TestPaginator(t *testing.T) {
+       configFile := `
+baseURL = "https://example.com/foo/"
+paginate = 3
+paginatepath = "thepage"
+
+[languages.en]
+weight = 1
+contentDir = "content/en"
+
+[languages.nn]
+weight = 2
+contentDir = "content/nn"
+
+`
+       b := newTestSitesBuilder(t).WithConfigFile("toml", configFile)
+       var content []string
+       for i := 0; i < 9; i++ {
+               for _, contentDir := range []string{"content/en", "content/nn"} {
+                       content = append(content, fmt.Sprintf(contentDir+"/blog/page%d.md", i), fmt.Sprintf(`---
+title: Page %d
+---
+
+Content.
+`, i))
+               }
+
+       }
+
+       b.WithContent(content...)
+
+       pagTemplate := `
+{{ $pag := $.Paginator }}
+Total: {{ $pag.TotalPages }}
+First: {{ $pag.First.URL }}
+Page Number: {{ $pag.PageNumber }}
+URL: {{ $pag.URL }}
+{{ with $pag.Next }}Next: {{ .URL }}{{ end }}
+{{ with $pag.Prev }}Prev: {{ .URL }}{{ end }}
+{{ range $i, $e := $pag.Pagers }}
+{{ printf "%d: %d/%d  %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}}
+{{ end }}
+`
+
+       b.WithTemplatesAdded("index.html", pagTemplate)
+       b.WithTemplatesAdded("index.xml", pagTemplate)
+
+       b.Build(BuildCfg{})
+
+       b.AssertFileContent("public/index.html",
+               "Page Number: 1",
+               "0: 1/1  true")
+
+       b.AssertFileContent("public/thepage/2/index.html",
+               "Total: 3",
+               "Page Number: 2",
+               "URL: /foo/thepage/2/",
+               "Next: /foo/thepage/3/",
+               "Prev: /foo/",
+               "1: 2/2  true",
+       )
+
+       b.AssertFileContent("public/index.xml",
+               "Page Number: 1",
+               "0: 1/1  true")
+       b.AssertFileContent("public/thepage/2/index.xml",
+               "Page Number: 2",
+               "1: 2/2  true")
+
+       b.AssertFileContent("public/nn/index.html",
+               "Page Number: 1",
+               "0: 1/1  true")
+
+       b.AssertFileContent("public/nn/index.xml",
+               "Page Number: 1",
+               "0: 1/1  true")
+
+}
index 1d8b14b0acf1e1eab742f98414aec10bec5d99df..7607040537ed2ef5217eb05fc0745a0aef377d64 100644 (file)
@@ -172,7 +172,11 @@ func (s *Site) renderPaginator(p *pageState, layouts []string) error {
        d.Type = f
 
        // Rewind
-       p.paginator.current = p.paginator.current.First()
+       p.paginator.rewind()
+       defer func() {
+               // Prepare for any re-rendering in server mode.
+               p.paginator.rewind()
+       }()
 
        // Write alias for page 1
        d.Addends = fmt.Sprintf("/%s/%d", paginatePath, 1)
index d55e5b307a96e8561ed8ae1e30e4b6b1727273d2..89078350e816a9396a2fdff631e573f27ea1713d 100644 (file)
@@ -239,21 +239,22 @@ if (!doNotTrack) {
     <li class="page-item{{ if not $pag.HasPrev }} disabled{{ end }}">
     <a href="{{ if $pag.HasPrev }}{{ $pag.Prev.URL }}{{ end }}" class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
     </li>
-    {{ $.Scratch.Set "__paginator.ellipsed" false }}
+    {{ $ellipsed := false }}
+    {{ $shouldEllipse := false }}
     {{ range $pag.Pagers }}
     {{ $right := sub .TotalPages .PageNumber }}
     {{ $showNumber := or (le .PageNumber 3) (eq $right 0) }}
     {{ $showNumber := or $showNumber (and (gt .PageNumber (sub $pag.PageNumber 2)) (lt .PageNumber (add $pag.PageNumber 2)))  }}
     {{ if $showNumber }}
-        {{ $.Scratch.Set "__paginator.ellipsed" false }}
-        {{ $.Scratch.Set "__paginator.shouldEllipse" false }}
+        {{ $ellipsed = false }}
+        {{ $shouldEllipse = false }}
     {{ else }}
-        {{ $.Scratch.Set "__paginator.shouldEllipse" (not ($.Scratch.Get "__paginator.ellipsed") ) }}
-        {{ $.Scratch.Set "__paginator.ellipsed" true }}
+        {{ $shouldEllipse = not $ellipsed }}
+        {{ $ellipsed = true }}
     {{ end }}
     {{ if $showNumber }}
     <li class="page-item{{ if eq . $pag }} active{{ end }}"><a class="page-link" href="{{ .URL }}">{{ .PageNumber }}</a></li>
-    {{ else if ($.Scratch.Get "__paginator.shouldEllipse") }}
+    {{ else if $shouldEllipse }}
     <li class="page-item disabled"><span aria-hidden="true">&nbsp;&hellip;&nbsp;</span></li>
     {{ end }}
     {{ end }}
index 7995061d9c382406ffdff01e5749ca9d3747737c..1c2d2d82f2f412e9a398897e3bdc2789744a355e 100644 (file)
@@ -9,21 +9,22 @@
     <li class="page-item{{ if not $pag.HasPrev }} disabled{{ end }}">
     <a href="{{ if $pag.HasPrev }}{{ $pag.Prev.URL }}{{ end }}" class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
     </li>
-    {{ $.Scratch.Set "__paginator.ellipsed" false }}
+    {{ $ellipsed := false }}
+    {{ $shouldEllipse := false }}
     {{ range $pag.Pagers }}
     {{ $right := sub .TotalPages .PageNumber }}
     {{ $showNumber := or (le .PageNumber 3) (eq $right 0) }}
     {{ $showNumber := or $showNumber (and (gt .PageNumber (sub $pag.PageNumber 2)) (lt .PageNumber (add $pag.PageNumber 2)))  }}
     {{ if $showNumber }}
-        {{ $.Scratch.Set "__paginator.ellipsed" false }}
-        {{ $.Scratch.Set "__paginator.shouldEllipse" false }}
+        {{ $ellipsed = false }}
+        {{ $shouldEllipse = false }}
     {{ else }}
-        {{ $.Scratch.Set "__paginator.shouldEllipse" (not ($.Scratch.Get "__paginator.ellipsed") ) }}
-        {{ $.Scratch.Set "__paginator.ellipsed" true }}
+        {{ $shouldEllipse = not $ellipsed }}
+        {{ $ellipsed = true }}
     {{ end }}
     {{ if $showNumber }}
     <li class="page-item{{ if eq . $pag }} active{{ end }}"><a class="page-link" href="{{ .URL }}">{{ .PageNumber }}</a></li>
-    {{ else if ($.Scratch.Get "__paginator.shouldEllipse") }}
+    {{ else if $shouldEllipse }}
     <li class="page-item disabled"><span aria-hidden="true">&nbsp;&hellip;&nbsp;</span></li>
     {{ end }}
     {{ end }}