From 7c79f831389d4945a317836b8d25a257c07ad4ed Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Sun, 20 Apr 2025 04:33:37 -0500 Subject: [PATCH] Fix pagination for Hugo 0.146 --- layouts/partials/_internal/pagination.html | 154 +++++++++++++++++++++ layouts/partials/pagination.html | 5 +- 2 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 layouts/partials/_internal/pagination.html diff --git a/layouts/partials/_internal/pagination.html b/layouts/partials/_internal/pagination.html new file mode 100644 index 0000000..995ac56 --- /dev/null +++ b/layouts/partials/_internal/pagination.html @@ -0,0 +1,154 @@ +{{- $validFormats := slice "default" "terse" }} + +{{- $msg1 := "When passing a map to the internal pagination template, one of the elements must be named 'page', and it must be set to the context of the current page." }} +{{- $msg2 := "The 'format' specified in the map passed to the internal pagination template is invalid. Valid choices are: %s." }} + +{{- $page := . }} +{{- $format := "default" }} + +{{- if reflect.IsMap . }} + {{- with .page }} + {{- $page = . }} + {{- else }} + {{- errorf $msg1 }} + {{- end }} + {{- with .format }} + {{- $format = lower . }} + {{- end }} +{{- end }} + +{{- if in $validFormats $format }} + {{- if gt $page.Paginator.TotalPages 1 }} + + {{- end }} +{{- else }} + {{- errorf $msg2 (delimit $validFormats ", ") }} +{{- end -}} + +{{/* Format: default +{{/* --------------------------------------------------------------------- */}} +{{- define "_partials/inline/pagination/default.html" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} +
  • + +
  • + {{- else }} +
  • + +
  • + {{- end }} + {{- end }} + + {{- with .Prev }} +
  • + +
  • + {{- else }} +
  • + +
  • + {{- end }} + + {{- $slots := 5 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} +
  • + {{ $k }} +
  • + {{- else }} +
  • + {{ $k }} +
  • + {{- end }} + {{- end }} + + {{- with .Next }} +
  • + +
  • + {{- else }} +
  • + +
  • + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} +
  • + +
  • + {{- else }} +
  • + +
  • + {{- end }} + {{- end }} + {{- end }} +{{- end -}} + +{{/* Format: terse +{{/* --------------------------------------------------------------------- */}} +{{- define "_partials/inline/pagination/terse.html" }} + {{- with .Paginator }} + {{- $currentPageNumber := .PageNumber }} + + {{- with .First }} + {{- if ne $currentPageNumber .PageNumber }} +
  • + +
  • + {{- end }} + {{- end }} + + {{- with .Prev }} +
  • + +
  • + {{- end }} + + {{- $slots := 3 }} + {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} + {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} + {{- if lt (add (sub $end $start) 1) $slots }} + {{- $start = math.Max 1 (add (sub $end $slots) 1) }} + {{- end }} + + {{- range $k := seq $start $end }} + {{- if eq $.Paginator.PageNumber $k }} +
  • + {{ $k }} +
  • + {{- else }} +
  • + {{ $k }} +
  • + {{- end }} + {{- end }} + + {{- with .Next }} +
  • + +
  • + {{- end }} + + {{- with .Last }} + {{- if ne $currentPageNumber .PageNumber }} +
  • + +
  • + {{- end }} + {{- end }} + {{- end }} +{{- end -}} diff --git a/layouts/partials/pagination.html b/layouts/partials/pagination.html index 4e17fbf..c4c56be 100644 --- a/layouts/partials/pagination.html +++ b/layouts/partials/pagination.html @@ -1,7 +1,6 @@ {{ if gt .Paginator.TotalPages 1 }} {{ end }}