Skip to content

Commit

Permalink
[WIP] Statically generate ToC
Browse files Browse the repository at this point in the history
Closes #45
  • Loading branch information
realityking committed May 31, 2015
1 parent 99ee16e commit 979140f
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 1,625 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gem 'redcarpet', '~> 3.2.2'

gem 'rake', '~> 10.4.2'
gem 'therubyracer', '~> 0.12.1', platforms: :ruby
gem 'nokogiri'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
rouge (~> 1.0)
minitest (5.6.1)
multi_json (1.11.0)
nokogiri (1.5.6)
padrino-helpers (0.12.5)
i18n (~> 0.6, >= 0.6.7)
padrino-support (= 0.12.5)
Expand Down Expand Up @@ -127,6 +128,7 @@ DEPENDENCIES
middleman (~> 3.3.10)
middleman-gh-pages (~> 0.0.3)
middleman-syntax (~> 2.0.0)
nokogiri
rake (~> 10.4.2)
redcarpet (~> 3.2.2)
rouge (~> 1.9.0)
Expand Down
68 changes: 68 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,74 @@
activate :relative_assets
set :relative_links, true

require 'nokogiri'

helpers do
def body_for(resource)
resource.render(layout: nil)
end

def doc_for(resource)
html = body_for(resource)
Nokogiri::HTML::DocumentFragment.parse(html)
end

def create_level_html(heading_id, toc_level, toc_section, tocText, tocInner)
link = '<a href="#%1">%2</a>%3'
.gsub('%1', heading_id.to_s)
.gsub('%2', tocText)
.gsub('%3', tocInner ? tocInner : '');
'<li data-heading="%4" class="tocify-item%1%5">%3</li>'
.gsub('%1', toc_level == 1 ? ' tocify-header' : '')
.gsub('%2', toc_section.to_s)
.gsub('%3', link)
.gsub('%4', heading_id.to_s)
.gsub('%5', tocInner.length > 0 ? ' has-subheader' : '')
end

def table_of_contents(resource)
toc_html = ''
toc_level = 1
toc_section = 1
level_html = ''

doc_for(resource).css('h1').each do |h1|
ct = h1.xpath('count(following-sibling::h1)')
h2s = h1.xpath("following-sibling::h2[count(following-sibling::h1)=#{ct}]")

level_html = '';
inner_section = 0;

h2s.map.each do |h2|
inner_section += 1;

level_html += create_level_html(h2.attribute('id'),
toc_level + 1,
toc_section + inner_section,
h2.text,
'')
end
if level_html.length > 0
level_html = '<ul class="tocify-subheader">' + level_html + '</ul>';
end

toc_html += create_level_html(id = h1.attribute('id'),
toc_level,
toc_section,
h1.text,
level_html);

toc_section += 1 + inner_section;
end

if toc_html.length > 0
toc_html = '<ul>' + toc_html + '</ul>';
end

return toc_html
end
end

# Build Configuration
configure :build do
activate :minify_css
Expand Down
31 changes: 31 additions & 0 deletions source/javascripts/app/es6shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
enumerable: false,
configurable: true,
writable: true,
value: function(target, firstSource) {
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}

var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}

var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
});
}
18 changes: 2 additions & 16 deletions source/javascripts/app/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@
};

var makeToc = function() {
global.toc = $("#toc").tocify({
selectors: 'h1, h2',
extendPage: false,
theme: 'none',
smoothScroll: false,
showEffectSpeed: 0,
hideEffectSpeed: 180,
ignoreSelector: '.toc-ignore',
highlightOffset: 60,
scrollTo: -1,
scrollHistory: true,
hashGenerator: function (text, element) {
return element.prop('id');
}
}).data('toc-tocify');
global.toc = new Tocify($('#toc'), {});

$("#nav-button").click(function() {
$(".tocify-wrapper").toggleClass('open');
Expand All @@ -37,7 +23,7 @@
// instead of displaying an ugly animation
function animate () {
setTimeout(function() {
toc.setOption('showEffectSpeed', 180);
// toc.setOption('showEffectSpeed', 180);
}, 50);
}

Expand Down
Loading

0 comments on commit 979140f

Please sign in to comment.