shithub: hugo

Download patch

ref: b6ea492b7a6325d04d44eeb00a990a3a0e29e0c0
parent: 6e2f2dd8d3ca61c92a2ee8824fbf05cadef08425
author: Bjørn Erik Pedersen <[email protected]>
date: Tue May 16 06:53:40 EDT 2017

tmplimpl: Add support for ellipsed paginator

Fixes #3466

--- a/tpl/tplimpl/template_embedded.go
+++ b/tpl/tplimpl/template_embedded.go
@@ -123,32 +123,47 @@
 `)
 
 	t.addInternalTemplate("", "pagination.html", `{{ $pag := $.Paginator }}
-    {{ if gt $pag.TotalPages 1 }}
-    <ul class="pagination">
-        {{ with $pag.First }}
-        <li>
-            <a href="{{ .URL }}" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
-        </li>
-        {{ end }}
-        <li
-        {{ if not $pag.HasPrev }}class="disabled"{{ end }}>
-        <a href="{{ if $pag.HasPrev }}{{ $pag.Prev.URL }}{{ end }}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
-        </li>
-        {{ range $pag.Pagers }}
-        <li
-        {{ if eq . $pag }}class="active"{{ end }}><a href="{{ .URL }}">{{ .PageNumber }}</a></li>
-        {{ end }}
-        <li
-        {{ if not $pag.HasNext }}class="disabled"{{ end }}>
-        <a href="{{ if $pag.HasNext }}{{ $pag.Next.URL }}{{ end }}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
-        </li>
-        {{ with $pag.Last }}
-        <li>
-            <a href="{{ .URL }}" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
-        </li>
-        {{ end }}
-    </ul>
-    {{ end }}`)
+{{ if gt $pag.TotalPages 1 }}
+<ul class="pagination">
+    {{ with $pag.First }}
+    <li>
+        <a href="{{ .URL }}" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
+    </li>
+    {{ end }}
+    <li
+    {{ if not $pag.HasPrev }}class="disabled"{{ end }}>
+    <a href="{{ if $pag.HasPrev }}{{ $pag.Prev.URL }}{{ end }}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
+    </li>
+    {{ $.Scratch.Set "__paginator.ellipsed" 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 }}
+    {{ else }}
+        {{ $.Scratch.Set "__paginator.shouldEllipse" (not ($.Scratch.Get "__paginator.ellipsed") ) }}
+        {{ $.Scratch.Set "__paginator.ellipsed" true }}
+    {{ end }}
+    {{ if $showNumber }}
+    <li
+    {{ if eq . $pag }}class="active"{{ end }}><a href="{{ .URL }}">{{ .PageNumber }}</a></li>
+    {{ else if ($.Scratch.Get "__paginator.shouldEllipse") }}
+    <li class="disabled"><span aria-hidden="true">&hellip;</span></li>
+    {{ end }}
+    {{ end }}
+    <li
+    {{ if not $pag.HasNext }}class="disabled"{{ end }}>
+    <a href="{{ if $pag.HasNext }}{{ $pag.Next.URL }}{{ end }}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
+    </li>
+    {{ with $pag.Last }}
+    <li>
+        <a href="{{ .URL }}" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
+    </li>
+    {{ end }}
+</ul>
+{{ end }}`)
 
 	t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
 <script type="text/javascript">