From b8d31039ec4c2cdbab85c0e9c0e3c6c4d9f7c108 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 00:16:20 +0300
Subject: [PATCH 01/13] Initial auto arrows

---
 layouts/partials/footer.html | 42 +++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index c07802bcc3..db86a98737 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -5,10 +5,46 @@
       </div>
     </div>
 
+    {{ with .Site.Params.menu }}
+      {{ range $index, $element := . }}
+        {{ with index $.Site.Params.menu (sub $index 1)}}
+          {{ $prev := index (last 1 (index $.Site.Sections .)) 0 }}
+          {{ $.Scratch.SetInMap $element "prev" $prev }}
+        {{ end }}
+        {{ with index $.Site.Params.menu (add $index 1)}}
+          {{ $next := index (first 1 (index $.Site.Sections .)) 0 }}
+          {{ $.Scratch.SetInMap $element "next" $next }}
+        {{ end }}
+      {{ end }}
+    {{ else }}
+      {{ range $key, $section := $.Site.Sections }}
+        {{ with $prevSec := $.Scratch.Get "prevIter"}}
+          {{ with index $.Site.Sections . }}
+            {{ $.Scratch.SetInMap $key "prev" (index (last 1 .) 0) }}
+            {{ $.Scratch.SetInMap $prevSec "next" (index (first 1 $section) 0) }}
+          {{ end }}
+        {{ end }}
+        {{ $.Scratch.Set "prevIter" $key }}
+      {{ end }}
+    {{ end }}
     <div id="navigation">
-        {{ $Site := .Site }}
-        {{ with .Params.prev }}<a class="nav nav-prev" href="{{ $Site.BaseURL }}{{ . }}"> <i class="fa fa-chevron-left"></i></a>{{ end }}
-        {{ with .Params.next }}<a class="nav nav-next" href="{{ $Site.BaseURL }}{{ . }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>{{ end }}
+        {{ $secpages := sort (where .Site.RegularPages "Section" .Section) "Weight" }}
+        {{ range $index, $element := $secpages }}
+          {{if eq $element.UniqueID $.UniqueID }}
+            {{ if not (isset $.Params "prev") }}
+              {{ with index $secpages (sub $index 1) | default ($.Scratch.Get $.Section).prev.Page}}
+                <a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
+              {{ end }}
+            {{ end }}
+            {{ if not (isset $.Params "next") }}
+              {{ with index $secpages (add $index 1) | default ($.Scratch.Get $.Section).next.Page}}
+                <a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
+              {{ end }}
+            {{ end }}
+          {{ end }}
+        {{ end }}
+        {{ with .Params.prev }}<a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ . }}"> <i class="fa fa-chevron-left"></i></a>{{ end }}
+        {{ with .Params.next }}<a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ . }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>{{ end }}
     </div>
 
     </section>

From 23e40c6b5b4599840267a6164baa34965ee0eb66 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 01:07:40 +0300
Subject: [PATCH 02/13] Use index pages

---
 layouts/_default/list.html   |  3 +++
 layouts/partials/footer.html |  2 +-
 layouts/partials/header.html |  8 ++++----
 layouts/partials/menu.html   | 38 +++++++++++++++---------------------
 4 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index e69de29bb2..16f6ffc141 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -0,0 +1,3 @@
+{{ partial "header.html" . }}
+{{ .Content }}
+{{ partial "footer.html" . }}
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index db86a98737..86cf6c7695 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -1,5 +1,5 @@
 
-      {{ if .Params.chapter }}
+      {{ if eq .Kind "section" }}
         </div> <!-- end chapter-->
       {{ end }}
       </div>
diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index 030139c576..12ee292785 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -47,7 +47,7 @@
                         <i class="fa fa-bars"></i>
                       </a>
                   </span>
-                {{ if and (not .Params.chapter) (.Params.toc) }}
+                {{ if and (ne .Kind "section") (.Params.toc) }}
                 <span id="toc-menu"><a href=""><i class="fa fa-list-alt"></i></a></span>
                 {{ end }}
                 {{ $type := .Type }}
@@ -67,10 +67,10 @@
               {{ end }}
 
             </div>
-            {{ if .Params.chapter }}
+            {{ if eq .Kind "section" }}
               <div id="chapter">
             {{ end }}
-    	        <div id="body-inner">
-                {{ if not .Params.chapter }}
+              <div id="body-inner">
+                {{ if ne .Kind "section" }}
                 <h1>{{.Title}}</h1>
                 {{ end }}
diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index 5d344a4735..7956836817 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -12,31 +12,27 @@
   <div class="highlightable">
     <ul class="topics">
       {{ $page := . }}
-      {{ if .Site.Params.menu }}
-        {{ $.Scratch.Set "menuItems" (slice) }}
-        {{ range $i, $key := .Site.Params.menu }}
-          {{ if isset $page.Site.Sections $key }}
-            {{ $.Scratch.Add "menuItems" $key }}
-          {{ else }}
-            <li class="dd-item"><div class="notices warning"><p>
-            {{ printf "Unknown section '%s' found in site menu" $key }}
-            </p></div></li>
-          {{ end }}
-        {{ end }}
-      {{ else }}
-        {{ $.Scratch.Set "menuItems" .Site.Sections }}
+      {{ $.Scratch.Set "_pages" (slice) }}
+      {{ range $key, $value := .Site.Sections }}
+        {{ $.Scratch.Add "_pages" ($.Site.GetPage "section" $key) }}
+      {{ end }}
+      {{ $.Scratch.Set "menuItems" (slice)}}
+      {{ range $key, $value := sort ($.Scratch.Get "_pages") "Weight" }}
+        {{ $.Scratch.Add "menuItems" $value.Section }}
       {{ end }}
       {{ $menuItems := $.Scratch.Get "menuItems" }}
       {{ range $i, $key := $menuItems }}
       {{ if ne $key "" }}
 
-      {{ if not $.Site.Params.menu }}
-        {{ $.Scratch.Set "currentItem" (index $page.Site.Sections $i) }}
-      {{ else }}
-        {{ $.Scratch.Set "currentItem" (index $page.Site.Sections $key) }}
+      {{ $value := (index $page.Site.Sections $key) }}
+      {{ with $index := $.Site.GetPage "section" $key }}
+        {{ if .Title }}
+          {{ $.Scratch.Set "first" $index }}
+        {{ else }}
+          {{ $.Scratch.Set (index $value 0).Page }}
+        {{ end }}
       {{ end }}
-      {{ $value := $.Scratch.Get "currentItem" }}
-      {{ $first := (index $value 0).Page }}
+      {{ $first := $.Scratch.Get "first" }}
 
       <li class="dd-item {{ if eq $page.RelPermalink $first.RelPermalink }}active{{ end }} {{if in $page.RelPermalink $first.RelPermalink }}parent{{ end }}" data-nav-id="{{ $first.RelPermalink }}">
         <a href="{{ $first.RelPermalink }}">
@@ -50,17 +46,15 @@
              {{ end }}
            </span>
         </a>
-        {{ if gt $value.Len 1}}
+        {{ if gt $value.Len 0}}
         <ul>
           {{ range $k, $p := $value }}
-          {{ if gt $k 0 }}
             <li class="dd-item {{ if eq $page.RelPermalink $p.Page.RelPermalink }}active{{ end }}" data-nav-id="{{ $p.Page.RelPermalink }}">
               <a href="{{ $p.Page.RelPermalink }}">
                 <span>{{ $p.Page.Title }}    {{ if $page.Site.Params.showVisitedLinks}}  <i class="fa fa-check read-icon">  {{ end }} </i></span>
               </a>
             </li>
           {{ end }}
-          {{ end }}
         </ul>
         {{ end }}
       </li>

From f238cdbc16fa413e036eb669fc66d15708a3b7ca Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 01:31:13 +0300
Subject: [PATCH 03/13] Auto prev/next

---
 archetypes/chapter.md        |  3 ---
 archetypes/default.md        |  2 --
 layouts/partials/footer.html | 38 +++++++++++++-----------------------
 3 files changed, 14 insertions(+), 29 deletions(-)

diff --git a/archetypes/chapter.md b/archetypes/chapter.md
index 3fe15e2cec..1d975216f1 100644
--- a/archetypes/chapter.md
+++ b/archetypes/chapter.md
@@ -1,9 +1,6 @@
 ---
 title: "Some Chapter title"
 weight: 0
-prev: /prev/path
-next: /next/path
-chapter: true
 icon: "<b>X. </b>" # HTML code as prefix in the menu
 ---
 
diff --git a/archetypes/default.md b/archetypes/default.md
index 2b35103feb..e47481e95a 100644
--- a/archetypes/default.md
+++ b/archetypes/default.md
@@ -1,8 +1,6 @@
 ---
 title: "Some Title"
 weight: 5
-prev: /prev/path
-next: /next/path
 toc: true
 ---
 
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 86cf6c7695..ffc2b6922f 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -5,39 +5,29 @@
       </div>
     </div>
 
-    {{ with .Site.Params.menu }}
-      {{ range $index, $element := . }}
-        {{ with index $.Site.Params.menu (sub $index 1)}}
-          {{ $prev := index (last 1 (index $.Site.Sections .)) 0 }}
-          {{ $.Scratch.SetInMap $element "prev" $prev }}
-        {{ end }}
-        {{ with index $.Site.Params.menu (add $index 1)}}
-          {{ $next := index (first 1 (index $.Site.Sections .)) 0 }}
-          {{ $.Scratch.SetInMap $element "next" $next }}
-        {{ end }}
-      {{ end }}
-    {{ else }}
-      {{ range $key, $section := $.Site.Sections }}
-        {{ with $prevSec := $.Scratch.Get "prevIter"}}
-          {{ with index $.Site.Sections . }}
-            {{ $.Scratch.SetInMap $key "prev" (index (last 1 .) 0) }}
-            {{ $.Scratch.SetInMap $prevSec "next" (index (first 1 $section) 0) }}
-          {{ end }}
-        {{ end }}
-        {{ $.Scratch.Set "prevIter" $key }}
+    {{ $.Scratch.Set "_sectionpages" (slice) }}
+    {{ range $sname, $spages := .Site.Sections }}
+      {{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
+    {{ end }}
+    {{ $.Scratch.Set "_allpages" (slice) }}
+    {{ range sort ($.Scratch.Get "_sectionpages") "Weight" }}
+      {{ $.Scratch.Add "_allpages" . }}
+      {{ range index $.Site.Sections .Section }}
+        {{ $.Scratch.Add "_allpages" .Page }}
       {{ end }}
     {{ end }}
+    {{ $allpages := $.Scratch.Get "_allpages" }}
+
     <div id="navigation">
-        {{ $secpages := sort (where .Site.RegularPages "Section" .Section) "Weight" }}
-        {{ range $index, $element := $secpages }}
+        {{ range $index, $element := $allpages }}
           {{if eq $element.UniqueID $.UniqueID }}
             {{ if not (isset $.Params "prev") }}
-              {{ with index $secpages (sub $index 1) | default ($.Scratch.Get $.Section).prev.Page}}
+              {{ with index $allpages (sub $index 1) }}
                 <a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
               {{ end }}
             {{ end }}
             {{ if not (isset $.Params "next") }}
-              {{ with index $secpages (add $index 1) | default ($.Scratch.Get $.Section).next.Page}}
+              {{ with index $allpages (add $index 1) }}
                 <a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
               {{ end }}
             {{ end }}

From 79107d2e75a610999c11c6d57e37fd7f23498a47 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 02:10:33 +0300
Subject: [PATCH 04/13] Backwards compatibility

---
 layouts/_default/list.html   |  2 ++
 layouts/partials/footer.html | 20 +++++++++++++++-----
 layouts/partials/header.html |  7 ++++---
 layouts/partials/menu.html   | 25 ++++++++++++++++---------
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index 16f6ffc141..3902b7741a 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -1,3 +1,5 @@
+{{ if .Content }}
 {{ partial "header.html" . }}
 {{ .Content }}
 {{ partial "footer.html" . }}
+{{ end }}
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index ffc2b6922f..40bbb5f6e5 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -1,17 +1,27 @@
 
-      {{ if eq .Kind "section" }}
+      {{ $isChapter := or (eq .Kind "section") (.Params.chapter)}}
+      {{ if $isChapter }}
         </div> <!-- end chapter-->
       {{ end }}
       </div>
     </div>
 
     {{ $.Scratch.Set "_sectionpages" (slice) }}
-    {{ range $sname, $spages := .Site.Sections }}
-      {{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
+    {{ if $.Site.Params.menu }}
+      {{ range $sname := $.Site.Params.menu }}
+        {{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
+      {{ end }}
+    {{ else }}
+      {{ range $sname, $spages := .Site.Sections }}
+        {{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
+      {{ end }}
+      {{ $.Scratch.Set "_sectionpages" (sort ($.Scratch.Get "_sectionpages") "Weight") }}
     {{ end }}
     {{ $.Scratch.Set "_allpages" (slice) }}
-    {{ range sort ($.Scratch.Get "_sectionpages") "Weight" }}
-      {{ $.Scratch.Add "_allpages" . }}
+    {{ range $.Scratch.Get "_sectionpages" }}
+      {{ if .Content }}
+        {{ $.Scratch.Add "_allpages" . }}
+      {{ end }}
       {{ range index $.Site.Sections .Section }}
         {{ $.Scratch.Add "_allpages" .Page }}
       {{ end }}
diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index 12ee292785..7de4a5924c 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -22,6 +22,7 @@
     {{ partial "style.html" . }}
   </head>
   <body class="" data-url="{{ .RelPermalink }}">
+    {{ $isChapter := or (eq .Kind "section") (.Params.chapter)}}
     {{ partial "menu.html" . }}
         <section id="body">
         <div id="overlay"></div>
@@ -47,7 +48,7 @@
                         <i class="fa fa-bars"></i>
                       </a>
                   </span>
-                {{ if and (ne .Kind "section") (.Params.toc) }}
+                {{ if and (not $isChapter) (.Params.toc) }}
                 <span id="toc-menu"><a href=""><i class="fa fa-list-alt"></i></a></span>
                 {{ end }}
                 {{ $type := .Type }}
@@ -67,10 +68,10 @@
               {{ end }}
 
             </div>
-            {{ if eq .Kind "section" }}
+            {{ if $isChapter }}
               <div id="chapter">
             {{ end }}
               <div id="body-inner">
-                {{ if ne .Kind "section" }}
+                {{ if not $isChapter }}
                 <h1>{{.Title}}</h1>
                 {{ end }}
diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index 7956836817..6b11ea63c1 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -12,27 +12,34 @@
   <div class="highlightable">
     <ul class="topics">
       {{ $page := . }}
-      {{ $.Scratch.Set "_pages" (slice) }}
-      {{ range $key, $value := .Site.Sections }}
-        {{ $.Scratch.Add "_pages" ($.Site.GetPage "section" $key) }}
-      {{ end }}
-      {{ $.Scratch.Set "menuItems" (slice)}}
-      {{ range $key, $value := sort ($.Scratch.Get "_pages") "Weight" }}
-        {{ $.Scratch.Add "menuItems" $value.Section }}
+      {{ if $.Site.Params.menu }}
+        {{ $.Scratch.Set "menuItems" $.Site.Params.menu }}
+      {{ else }}
+        {{ $.Scratch.Set "_pages" (slice) }}
+        {{ range $key, $value := .Site.Sections }}
+          {{ $.Scratch.Add "_pages" ($.Site.GetPage "section" $key) }}
+        {{ end }}
+        {{ $.Scratch.Set "menuItems" (slice)}}
+        {{ range $key, $value := sort ($.Scratch.Get "_pages") "Weight" }}
+          {{ $.Scratch.Add "menuItems" $value.Section }}
+        {{ end }}
       {{ end }}
       {{ $menuItems := $.Scratch.Get "menuItems" }}
       {{ range $i, $key := $menuItems }}
       {{ if ne $key "" }}
 
       {{ $value := (index $page.Site.Sections $key) }}
+      {{ $.Scratch.Set "_value" $value }}
       {{ with $index := $.Site.GetPage "section" $key }}
-        {{ if .Title }}
+        {{ if .Content }}
           {{ $.Scratch.Set "first" $index }}
         {{ else }}
-          {{ $.Scratch.Set (index $value 0).Page }}
+          {{ $.Scratch.Set "first" (index $value 0).Page }}
+          {{ $.Scratch.Set "_value" (after 1 $value) }}
         {{ end }}
       {{ end }}
       {{ $first := $.Scratch.Get "first" }}
+      {{ $value := $.Scratch.Get "_value" }}
 
       <li class="dd-item {{ if eq $page.RelPermalink $first.RelPermalink }}active{{ end }} {{if in $page.RelPermalink $first.RelPermalink }}parent{{ end }}" data-nav-id="{{ $first.RelPermalink }}">
         <a href="{{ $first.RelPermalink }}">

From 1238a71e125a0638ca78d442d89363e960f6e34e Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 01:31:13 +0300
Subject: [PATCH 05/13] Auto prev/next

---
 archetypes/chapter.md        |  3 ---
 archetypes/default.md        |  2 --
 layouts/partials/footer.html | 38 +++++++++++++-----------------------
 layouts/partials/menu.html   |  6 +-----
 4 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/archetypes/chapter.md b/archetypes/chapter.md
index 3fe15e2cec..1d975216f1 100644
--- a/archetypes/chapter.md
+++ b/archetypes/chapter.md
@@ -1,9 +1,6 @@
 ---
 title: "Some Chapter title"
 weight: 0
-prev: /prev/path
-next: /next/path
-chapter: true
 icon: "<b>X. </b>" # HTML code as prefix in the menu
 ---
 
diff --git a/archetypes/default.md b/archetypes/default.md
index 2b35103feb..e47481e95a 100644
--- a/archetypes/default.md
+++ b/archetypes/default.md
@@ -1,8 +1,6 @@
 ---
 title: "Some Title"
 weight: 5
-prev: /prev/path
-next: /next/path
 toc: true
 ---
 
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 86cf6c7695..ffc2b6922f 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -5,39 +5,29 @@
       </div>
     </div>
 
-    {{ with .Site.Params.menu }}
-      {{ range $index, $element := . }}
-        {{ with index $.Site.Params.menu (sub $index 1)}}
-          {{ $prev := index (last 1 (index $.Site.Sections .)) 0 }}
-          {{ $.Scratch.SetInMap $element "prev" $prev }}
-        {{ end }}
-        {{ with index $.Site.Params.menu (add $index 1)}}
-          {{ $next := index (first 1 (index $.Site.Sections .)) 0 }}
-          {{ $.Scratch.SetInMap $element "next" $next }}
-        {{ end }}
-      {{ end }}
-    {{ else }}
-      {{ range $key, $section := $.Site.Sections }}
-        {{ with $prevSec := $.Scratch.Get "prevIter"}}
-          {{ with index $.Site.Sections . }}
-            {{ $.Scratch.SetInMap $key "prev" (index (last 1 .) 0) }}
-            {{ $.Scratch.SetInMap $prevSec "next" (index (first 1 $section) 0) }}
-          {{ end }}
-        {{ end }}
-        {{ $.Scratch.Set "prevIter" $key }}
+    {{ $.Scratch.Set "_sectionpages" (slice) }}
+    {{ range $sname, $spages := .Site.Sections }}
+      {{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
+    {{ end }}
+    {{ $.Scratch.Set "_allpages" (slice) }}
+    {{ range sort ($.Scratch.Get "_sectionpages") "Weight" }}
+      {{ $.Scratch.Add "_allpages" . }}
+      {{ range index $.Site.Sections .Section }}
+        {{ $.Scratch.Add "_allpages" .Page }}
       {{ end }}
     {{ end }}
+    {{ $allpages := $.Scratch.Get "_allpages" }}
+
     <div id="navigation">
-        {{ $secpages := sort (where .Site.RegularPages "Section" .Section) "Weight" }}
-        {{ range $index, $element := $secpages }}
+        {{ range $index, $element := $allpages }}
           {{if eq $element.UniqueID $.UniqueID }}
             {{ if not (isset $.Params "prev") }}
-              {{ with index $secpages (sub $index 1) | default ($.Scratch.Get $.Section).prev.Page}}
+              {{ with index $allpages (sub $index 1) }}
                 <a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
               {{ end }}
             {{ end }}
             {{ if not (isset $.Params "next") }}
-              {{ with index $secpages (add $index 1) | default ($.Scratch.Get $.Section).next.Page}}
+              {{ with index $allpages (add $index 1) }}
                 <a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
               {{ end }}
             {{ end }}
diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index 7956836817..4ad79f2177 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -26,11 +26,7 @@
 
       {{ $value := (index $page.Site.Sections $key) }}
       {{ with $index := $.Site.GetPage "section" $key }}
-        {{ if .Title }}
-          {{ $.Scratch.Set "first" $index }}
-        {{ else }}
-          {{ $.Scratch.Set (index $value 0).Page }}
-        {{ end }}
+        {{ $.Scratch.Set "first" $index }}
       {{ end }}
       {{ $first := $.Scratch.Get "first" }}
 

From 38301a171faf60e49597534710c4223a6dcbcfcc Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 02:43:12 +0300
Subject: [PATCH 06/13] More backwards compatibility

---
 layouts/_default/list.html | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index 3902b7741a..e76566ec64 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -1,5 +1,9 @@
-{{ if .Content }}
 {{ partial "header.html" . }}
+{{ if .Content }}
 {{ .Content }}
-{{ partial "footer.html" . }}
+{{ else }}
+{{ with (index (index .Site.Sections .Section) 0)  }}
+{{ .Page.Content }}
 {{ end }}
+{{ end }}
+{{ partial "footer.html" . }}

From 329d426d86a70a85677160fbcd35d40cef0bd535 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Tue, 14 Mar 2017 02:55:59 +0300
Subject: [PATCH 07/13] More compatibility issues

---
 layouts/_default/list.html   | 1 +
 layouts/partials/footer.html | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index e76566ec64..8d1fdab11d 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -4,6 +4,7 @@
 {{ else }}
 {{ with (index (index .Site.Sections .Section) 0)  }}
 {{ .Page.Content }}
+{{ $.Scratch.Set "uniqueId" .Page.UniqueID }}
 {{ end }}
 {{ end }}
 {{ partial "footer.html" . }}
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 40bbb5f6e5..76d197f506 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -6,6 +6,7 @@
       </div>
     </div>
 
+    {{ if $.Site.Params.autoNav }}
     {{ $.Scratch.Set "_sectionpages" (slice) }}
     {{ if $.Site.Params.menu }}
       {{ range $sname := $.Site.Params.menu }}
@@ -26,11 +27,14 @@
         {{ $.Scratch.Add "_allpages" .Page }}
       {{ end }}
     {{ end }}
-    {{ $allpages := $.Scratch.Get "_allpages" }}
+    {{ end }}
 
     <div id="navigation">
+        {{ if $.Site.Params.autoNav }}
+        {{ $allpages := $.Scratch.Get "_allpages" }}
         {{ range $index, $element := $allpages }}
-          {{if eq $element.UniqueID $.UniqueID }}
+          {{ $uniqueid := ($.Scratch.Get "uniqueId" | default $.UniqueID ) }}
+          {{if eq $element.UniqueID $uniqueid }}
             {{ if not (isset $.Params "prev") }}
               {{ with index $allpages (sub $index 1) }}
                 <a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
@@ -43,6 +47,7 @@
             {{ end }}
           {{ end }}
         {{ end }}
+        {{ end }}
         {{ with .Params.prev }}<a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ . }}"> <i class="fa fa-chevron-left"></i></a>{{ end }}
         {{ with .Params.next }}<a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ . }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>{{ end }}
     </div>

From 9144ca72aa6ce90753b12340757600ae516299f4 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Sun, 26 Mar 2017 05:06:15 +0300
Subject: [PATCH 08/13] Fixed "error calling after: no items left"

---
 layouts/partials/menu.html | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index 6b11ea63c1..67d72026e9 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -35,7 +35,11 @@
           {{ $.Scratch.Set "first" $index }}
         {{ else }}
           {{ $.Scratch.Set "first" (index $value 0).Page }}
-          {{ $.Scratch.Set "_value" (after 1 $value) }}
+          {{ if gt $value.Len 1 }}
+            {{ $.Scratch.Set "_value" (after 1 $value) }}
+          {{ else }}
+            {{ $.Scratch.Set "_value" nil }}
+          {{ end }}
         {{ end }}
       {{ end }}
       {{ $first := $.Scratch.Get "first" }}

From 532a102a870206ae99b47cc9f18edd9e7149b16e Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Sun, 26 Mar 2017 20:15:36 +0300
Subject: [PATCH 09/13] Remove baseurl from automatic navigation arrows

Since it's already included in `Page.URL`
---
 layouts/partials/footer.html | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 76d197f506..8e84557123 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -37,12 +37,12 @@
           {{if eq $element.UniqueID $uniqueid }}
             {{ if not (isset $.Params "prev") }}
               {{ with index $allpages (sub $index 1) }}
-                <a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
+                <a class="nav nav-prev" href="{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
               {{ end }}
             {{ end }}
             {{ if not (isset $.Params "next") }}
               {{ with index $allpages (add $index 1) }}
-                <a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
+                <a class="nav nav-next" href="{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
               {{ end }}
             {{ end }}
           {{ end }}

From f1c65dd9e72b780977cb1a6a47daafe633712bf7 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Sun, 26 Mar 2017 20:30:49 +0300
Subject: [PATCH 10/13] Use relURL for manual navigation

---
 layouts/partials/footer.html | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 8e84557123..d63c32938b 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -48,8 +48,8 @@
           {{ end }}
         {{ end }}
         {{ end }}
-        {{ with .Params.prev }}<a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ . }}"> <i class="fa fa-chevron-left"></i></a>{{ end }}
-        {{ with .Params.next }}<a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ . }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>{{ end }}
+        {{ with .Params.prev }}<a class="nav nav-prev" href="{{ . | relURL }}"> <i class="fa fa-chevron-left"></i></a>{{ end }}
+        {{ with .Params.next }}<a class="nav nav-next" href="{{ . | relURL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>{{ end }}
     </div>
 
     </section>

From eac49beee156ee64bd3ea9da57b0e21edc994aaa Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Mon, 27 Mar 2017 22:14:37 +0300
Subject: [PATCH 11/13] Bump min_version

---
 theme.toml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/theme.toml b/theme.toml
index 4716162269..ee54a6eac4 100644
--- a/theme.toml
+++ b/theme.toml
@@ -8,7 +8,7 @@ description = "Documentation theme for Hugo, based on Grav Learn theme"
 homepage = "https://matcornic.github.io/hugo-learn-doc/basics/what-is-this-hugo-theme/"
 tags = ["documentation", "grav", "learn", "doc"]
 features = ["documentation"]
-min_version = 0.17
+min_version = 0.19
 
 [author]
   name = "Mathieu Cornic"

From 7aa1e70638c641f470dcc5bfb0ac9bcf7e07b6f0 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Mon, 27 Mar 2017 22:57:49 +0300
Subject: [PATCH 12/13] Fixes, see description

* fix menu ordering when mixing chapters and non-chapters
* allow for `chapter` type override in `_index` pages
* fix breadcrumbs
* fix some crashes with empty categories
* use page context in list.html (to avoid setting uniqueid in scratch)
* use UniqueID instead of relative links for page identification
---
 layouts/_default/list.html   | 17 ++++++++++-------
 layouts/partials/footer.html |  5 ++---
 layouts/partials/header.html | 21 ++++++++++++---------
 layouts/partials/menu.html   | 20 ++++++++++++--------
 4 files changed, 36 insertions(+), 27 deletions(-)

diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index 8d1fdab11d..c536b82900 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -1,10 +1,13 @@
-{{ partial "header.html" . }}
 {{ if .Content }}
+{{ partial "header.html" . }}
 {{ .Content }}
-{{ else }}
-{{ with (index (index .Site.Sections .Section) 0)  }}
-{{ .Page.Content }}
-{{ $.Scratch.Set "uniqueId" .Page.UniqueID }}
-{{ end }}
-{{ end }}
 {{ partial "footer.html" . }}
+{{ else }}
+{{ with (index (index .Site.Sections .Section) 0) }}
+{{ with .Page }}
+{{ partial "header.html" . }}
+{{ .Content }}
+{{ partial "footer.html" . }}
+{{ end }}
+{{ end }}
+{{ end }}
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 8e84557123..2c75ee3897 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -1,5 +1,5 @@
 
-      {{ $isChapter := or (eq .Kind "section") (.Params.chapter)}}
+      {{ $isChapter := .Params.chapter | default (eq .Kind "section")}}
       {{ if $isChapter }}
         </div> <!-- end chapter-->
       {{ end }}
@@ -33,8 +33,7 @@
         {{ if $.Site.Params.autoNav }}
         {{ $allpages := $.Scratch.Get "_allpages" }}
         {{ range $index, $element := $allpages }}
-          {{ $uniqueid := ($.Scratch.Get "uniqueId" | default $.UniqueID ) }}
-          {{if eq $element.UniqueID $uniqueid }}
+          {{if eq $element.UniqueID $.UniqueID }}
             {{ if not (isset $.Params "prev") }}
               {{ with index $allpages (sub $index 1) }}
                 <a class="nav nav-prev" href="{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index c61851a5fe..a9882a9cb7 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -22,7 +22,7 @@
     {{ partial "style.html" . }}
   </head>
   <body class="" data-url="{{ .RelPermalink }}">
-    {{ $isChapter := or (eq .Kind "section") (.Params.chapter)}}
+    {{ $isChapter := .Params.chapter | default (eq .Kind "section")}}
     {{ partial "menu.html" . }}
         <section id="body">
         <div id="overlay"></div>
@@ -51,14 +51,17 @@
                 {{ if and (not $isChapter) (.Params.toc) }}
                 <span id="toc-menu"><a href=""><i class="fa fa-list-alt"></i></a></span>
                 {{ end }}
-                {{ $type := .Type }}
-                {{ $relLink := .RelPermalink }}
-                {{ range $name , $value := .Site.Sections }}
-                  {{ if eq $name $type }}
-                    {{ $first := (index $value 0).Page }}
-                    {{ if ne $first.RelPermalink $relLink }}
-                <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
-                    {{ end }}
+                {{ $section := index .Site.Sections .Section }}
+                {{ $sectionPage := $.Site.GetPage "section" $.Section }}
+                {{ if $sectionPage.Content }}
+                  {{ $first := $.Site.GetPage "section" $.Section }}
+                  {{ if ne $first.UniqueID $.UniqueID }}
+              <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
+                  {{ end }}
+                {{ else if gt $section.Len 0 }}
+                  {{ $first := (index $section 0).Page }}
+                  {{ if ne $first.UniqueID $.UniqueID }}
+              <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
                   {{ end }}
                 {{ end }}
                 {{ with .Title }}<span itemprop="title"> {{ . }}</span>{{ end }}
diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index 67d72026e9..8e60b3b658 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -11,13 +11,17 @@
 
   <div class="highlightable">
     <ul class="topics">
-      {{ $page := . }}
       {{ if $.Site.Params.menu }}
         {{ $.Scratch.Set "menuItems" $.Site.Params.menu }}
       {{ else }}
         {{ $.Scratch.Set "_pages" (slice) }}
         {{ range $key, $value := .Site.Sections }}
-          {{ $.Scratch.Add "_pages" ($.Site.GetPage "section" $key) }}
+          {{ $sectionPage := $.Site.GetPage "section" $key }}
+          {{ if $sectionPage.Content }}
+            {{ $.Scratch.Add "_pages" $sectionPage }}
+          {{ else if gt $value.Len 0 }}
+            {{ $.Scratch.Add "_pages" (index $value 0).Page }}
+          {{ end }}
         {{ end }}
         {{ $.Scratch.Set "menuItems" (slice)}}
         {{ range $key, $value := sort ($.Scratch.Get "_pages") "Weight" }}
@@ -28,12 +32,12 @@
       {{ range $i, $key := $menuItems }}
       {{ if ne $key "" }}
 
-      {{ $value := (index $page.Site.Sections $key) }}
+      {{ $value := (index $.Site.Sections $key) }}
       {{ $.Scratch.Set "_value" $value }}
       {{ with $index := $.Site.GetPage "section" $key }}
         {{ if .Content }}
           {{ $.Scratch.Set "first" $index }}
-        {{ else }}
+        {{ else if gt $value.Len 0}}
           {{ $.Scratch.Set "first" (index $value 0).Page }}
           {{ if gt $value.Len 1 }}
             {{ $.Scratch.Set "_value" (after 1 $value) }}
@@ -45,14 +49,14 @@
       {{ $first := $.Scratch.Get "first" }}
       {{ $value := $.Scratch.Get "_value" }}
 
-      <li class="dd-item {{ if eq $page.RelPermalink $first.RelPermalink }}active{{ end }} {{if in $page.RelPermalink $first.RelPermalink }}parent{{ end }}" data-nav-id="{{ $first.RelPermalink }}">
+      <li class="dd-item {{ if eq $.UniqueID $first.UniqueID }}active{{ end }} {{if eq $.Section $first.Section }}parent{{ end }}" data-nav-id="{{ $first.RelPermalink }}">
         <a href="{{ $first.RelPermalink }}">
           <span>
             {{ if isset $first.Params "icon" }}
               {{ printf $first.Params.icon | safeHTML }}
             {{ end }}
              {{ $first.Title }}
-            {{ if $page.Site.Params.showVisitedLinks}}
+            {{ if $.Site.Params.showVisitedLinks}}
               <i class="fa fa-check read-icon"></i>
              {{ end }}
            </span>
@@ -60,9 +64,9 @@
         {{ if gt $value.Len 0}}
         <ul>
           {{ range $k, $p := $value }}
-            <li class="dd-item {{ if eq $page.RelPermalink $p.Page.RelPermalink }}active{{ end }}" data-nav-id="{{ $p.Page.RelPermalink }}">
+            <li class="dd-item {{ if eq $.UniqueID $p.Page.UniqueID }}active{{ end }}" data-nav-id="{{ $p.Page.RelPermalink }}">
               <a href="{{ $p.Page.RelPermalink }}">
-                <span>{{ $p.Page.Title }}    {{ if $page.Site.Params.showVisitedLinks}}  <i class="fa fa-check read-icon">  {{ end }} </i></span>
+                <span>{{ $p.Page.Title }}    {{ if $.Site.Params.showVisitedLinks}}  <i class="fa fa-check read-icon">  {{ end }} </i></span>
               </a>
             </li>
           {{ end }}

From efd220cc3deb4f4c5129611fdf4e07e7515352e3 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Mon, 27 Mar 2017 23:37:46 +0300
Subject: [PATCH 13/13] Fix breadcrumbs on `/content/_index`

---
 layouts/partials/header.html | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index a9882a9cb7..26ce94f20c 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -51,17 +51,19 @@
                 {{ if and (not $isChapter) (.Params.toc) }}
                 <span id="toc-menu"><a href=""><i class="fa fa-list-alt"></i></a></span>
                 {{ end }}
-                {{ $section := index .Site.Sections .Section }}
-                {{ $sectionPage := $.Site.GetPage "section" $.Section }}
-                {{ if $sectionPage.Content }}
-                  {{ $first := $.Site.GetPage "section" $.Section }}
-                  {{ if ne $first.UniqueID $.UniqueID }}
-              <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
-                  {{ end }}
-                {{ else if gt $section.Len 0 }}
-                  {{ $first := (index $section 0).Page }}
-                  {{ if ne $first.UniqueID $.UniqueID }}
-              <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
+                {{ if ne $.Section "" }}
+                  {{ $section := index $.Site.Sections $.Section }}
+                  {{ $sectionPage := $.Site.GetPage "section" $.Section }}
+                  {{ if $sectionPage.Content }}
+                    {{ $first := $.Site.GetPage "section" $.Section }}
+                    {{ if ne $first.UniqueID $.UniqueID }}
+                <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
+                    {{ end }}
+                  {{ else if gt $section.Len 0 }}
+                    {{ $first := (index $section 0).Page }}
+                    {{ if ne $first.UniqueID $.UniqueID }}
+                <a href="{{ $first.RelPermalink }}" itemprop="url"><span itemprop="title">{{ $first.Title }}</span></a> <i class="fa fa-angle-right"></i>
+                    {{ end }}
                   {{ end }}
                 {{ end }}
                 {{ with .Title }}<span itemprop="title"> {{ . }}</span>{{ end }}