Fix: Subject code page order of units

This commit is contained in:
hyzen
2026-05-22 14:05:31 +05:30
parent 41efdf7e06
commit b285fe7e4d
5 changed files with 55 additions and 25 deletions

View File

@@ -28,25 +28,41 @@
<h1>{{ $subjectCode }}</h1>
<ul class="uninotes-list uninotes-list--units">
{{ $seenUnits := slice }}
{{ range .Pages.ByWeight }}
{{ $unitLabel := (.Params.unit | default .Title) }}
{{ if not (in $seenUnits $unitLabel) }}
{{ $seenUnits = $seenUnits | append $unitLabel }}
<li class="uninotes-list__item">
<a class="uninotes-list__link" href="{{ .Params.uniturl }}">
{{ $unitLabel }}
</a>
</li>
{{/* Collect unique unit labels and sort numerically */}}
{{ $seenUnits := slice }}
{{ $unitDicts := slice }}
{{ range .Pages }}
{{ $unitLabel := (.Params.unit | default .Title) }}
{{ if not (in $seenUnits $unitLabel) }}
{{ $seenUnits = $seenUnits | append $unitLabel }}
{{/* Extract unit number for numeric sort, e.g. "Unit 11" → 11 */}}
{{ $uParts := split $unitLabel " " }}
{{ $uLast := index $uParts (sub (len $uParts) 1) }}
{{ $uNumStr := "" }}
{{ range (split $uLast "") }}
{{ if ge . "0" }}{{ if le . "9" }}{{ $uNumStr = printf "%s%s" $uNumStr . }}{{ end }}{{ end }}
{{ end }}
{{ $uNum := int ($uNumStr | default "0") }}
{{ $unitDicts = $unitDicts | append (dict "label" $unitLabel "page" . "num" $uNum) }}
{{ end }}
{{ end }}
{{ $sortedUnitDicts := sort $unitDicts "num" }}
<ul class="uninotes-list uninotes-list--units">
{{ range $sortedUnitDicts }}
<li class="uninotes-list__item">
<a class="uninotes-list__link" href="{{ .page.Params.uniturl }}">
{{ .label }}
</a>
</li>
{{ end }}
</ul>
{{/* Subject prev/next — siblings in same semester */}}
{{/* Subject prev/next — siblings in same semester, sorted numerically by code number */}}
{{ if $semester }}
{{ $semPage := site.GetPage (printf "/semester/%s" ($semester | urlize)) }}
{{ if $semPage }}
{{/* Collect unique subject codes */}}
{{ $siblingCodes := slice }}
{{ range $semPage.Pages }}
{{ $raw := .Params.subjectcode }}
@@ -60,23 +76,37 @@
{{ $siblingCodes = $siblingCodes | append $code }}
{{ end }}
{{ end }}
{{ $siblingCodes = sort $siblingCodes }}
{{ if gt (len $siblingCodes) 1 }}
{{/* Sort subject codes numerically by extracting the number (e.g. "BO DCM1109" → 1109) */}}
{{ $codeDicts := slice }}
{{ range $siblingCodes }}
{{ $code := . }}
{{ $parts := split $code " " }}
{{ $lastPart := index $parts (sub (len $parts) 1) }}
{{ $numStr := "" }}
{{ range (split $lastPart "") }}
{{ if ge . "0" }}{{ if le . "9" }}{{ $numStr = printf "%s%s" $numStr . }}{{ end }}{{ end }}
{{ end }}
{{ $num := int ($numStr | default "0") }}
{{ $codeDicts = $codeDicts | append (dict "code" $code "num" $num) }}
{{ end }}
{{ $sortedCodeDicts := sort $codeDicts "num" }}
{{ if gt (len $sortedCodeDicts) 1 }}
{{ $currentIdx := 0 }}
{{ range $i, $c := $siblingCodes }}
{{ if eq $c $subjectCode }}{{ $currentIdx = $i }}{{ end }}
{{ range $i, $d := $sortedCodeDicts }}
{{ if eq $d.code $subjectCode }}{{ $currentIdx = $i }}{{ end }}
{{ end }}
<nav class="page-nav">
{{ if gt $currentIdx 0 }}
{{ $prevCode := index $siblingCodes (sub $currentIdx 1) }}
{{ $prevCode := (index $sortedCodeDicts (sub $currentIdx 1)).code }}
{{ $prevPage := site.GetPage (printf "/subjectcode/%s" ($prevCode | urlize)) }}
{{ if $prevPage }}
<a class="page-nav__previous-link" href="{{ $prevPage.RelPermalink }}">← {{ $prevCode }}</a>
{{ end }}
{{ end }}
{{ if lt $currentIdx (sub (len $siblingCodes) 1) }}
{{ $nextCode := index $siblingCodes (add $currentIdx 1) }}
{{ if lt $currentIdx (sub (len $sortedCodeDicts) 1) }}
{{ $nextCode := (index $sortedCodeDicts (add $currentIdx 1)).code }}
{{ $nextPage := site.GetPage (printf "/subjectcode/%s" ($nextCode | urlize)) }}
{{ if $nextPage }}
<a class="page-nav__next-link" href="{{ $nextPage.RelPermalink }}">{{ $nextCode }} →</a>