From a15101810406c4076947acad418681cc464e16c4 Mon Sep 17 00:00:00 2001
From: Thomas Wood <thomas.wood09@imperial.ac.uk>
Date: Wed, 3 Feb 2016 17:22:45 +0000
Subject: [PATCH] Fix publications ToC

---
 _layouts/toc.html       | 16 ++++++++++++++++
 _plugins/jekyll-toc.rb  | 34 ++++++++++++++++++++++++++++++++++
 publications/index.html | 14 ++------------
 3 files changed, 52 insertions(+), 12 deletions(-)
 create mode 100644 _layouts/toc.html
 create mode 100644 _plugins/jekyll-toc.rb

diff --git a/_layouts/toc.html b/_layouts/toc.html
new file mode 100644
index 0000000..a8090d9
--- /dev/null
+++ b/_layouts/toc.html
@@ -0,0 +1,16 @@
+---
+layout: page
+dataspy: "#sidenav"
+toc: "true"
+---
+<div class="row stickyrow">
+  <div class="col-sm-9">
+    {{ content | parse_toc }}
+  </div>
+  <div class="col-sm-3">
+    <nav class="sidenav fixedsticky" id="sidenav"><ul class="nav list-group">
+      {% for i in page.toc %}<li class="nav-item list-group-item"><a href="#{{i.id}}" class="nav-link">{{i.name}}</a></li>
+{% endfor %}
+    </ul></nav>
+  </div>
+</div>
diff --git a/_plugins/jekyll-toc.rb b/_plugins/jekyll-toc.rb
new file mode 100644
index 0000000..6c3aa58
--- /dev/null
+++ b/_plugins/jekyll-toc.rb
@@ -0,0 +1,34 @@
+require 'nokogiri'
+
+module Jekyll
+  # parse logic is from html-pipeline toc_filter
+  # https://github.com/jch/html-pipeline/blob/v1.1.0/lib/html/pipeline/toc_filter.rb
+  module TableOfContentsFilter
+    PUNCTUATION_REGEXP = RUBY_VERSION > "1.9" ? /[^\p{Word}\- ]/u : /[^\w\- ]/
+
+    def parse_toc(html)
+      toc = []
+      doc = Nokogiri::HTML::DocumentFragment.parse(html)
+      headers = Hash.new(0)
+
+      doc.css('h1, h2, h3, h4, h5, h6').each do |node|
+        text = node.text
+        id = text.downcase
+        id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
+        id.gsub!(' ', '-') # replace spaces with dash
+
+        uniq = (headers[id] > 0) ? "-#{headers[id]}" : ''
+        uniqid = "#{id}#{uniq}"
+        headers[id] += 1
+
+        toc << { 'id' => uniqid, 'name' => text, 'level' => node.name }
+        node['id'] = uniqid
+      end
+      @context.environments.first['page']['toc'] = toc
+
+      doc.inner_html
+    end
+  end
+end
+
+Liquid::Template.register_filter(Jekyll::TableOfContentsFilter)
diff --git a/publications/index.html b/publications/index.html
index f7fea36..11718aa 100644
--- a/publications/index.html
+++ b/publications/index.html
@@ -1,16 +1,6 @@
 ---
 title: Publications
-dataspy: "#sidenav"
 menu: true
+layout: toc
 ---
-<div class="row stickyrow">
-  <div class="col-sm-9">
-    {% bibliography -g year %}
-  </div>
-  <div class="col-sm-3">
-    <nav class="sidenav fixedsticky" id="sidenav"><ul class="nav list-group">
-      {% for group in page.bibliography_groups %}<li class="nav-item list-group-item"><a href="#{{group[0]}}" class="nav-link">{{group[0]}}</a></li>
-{% endfor %}
-    </ul></nav>
-  </div>
-</div>
+{% bibliography -g year %}
-- 
GitLab