Skip to content

Instantly share code, notes, and snippets.

@meqif
Created September 7, 2010 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meqif/569333 to your computer and use it in GitHub Desktop.
Save meqif/569333 to your computer and use it in GitHub Desktop.
Patch for kernel's jekyll fork. Apply with 'git am 0001-Reduced-repeated-gsubs.-Style-fixes.patch'
From 745ffd39eb7a9661e512761c5861376dae7583b8 Mon Sep 17 00:00:00 2001
From: Ricardo Martins <ricardo@scarybox.net>
Date: Wed, 8 Sep 2010 00:08:32 +0100
Subject: [PATCH] Reduced repeated gsubs. Style fixes.
---
lib/jekyll/toc_helper.rb | 11 ++++++-----
1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/lib/jekyll/toc_helper.rb b/lib/jekyll/toc_helper.rb
index 7cbcabe..acd0454 100644
--- a/lib/jekyll/toc_helper.rb
+++ b/lib/jekyll/toc_helper.rb
@@ -13,8 +13,9 @@ module Jekyll
post.content.gsub!(/h(\d). (\w+( \w+)*)/) do |r|
level = $1
title = $2
- id = title.gsub(/[_*]/, "").gsub(/ /, "_") # title: bla *bla* => bla_bla
- items << [ title.gsub(/[_*]/,""), level ]
+ link_text = title.gsub(/[_*]/, "") # title: bla *bla* => bla_bla
+ id = link_text.gsub(/ /, "_")
+ items << { :link_text => link_text, :level = level.to_i, :id = id }
"h#{level}(##{id}). #{title}"
end
self.process(post, items)
@@ -22,10 +23,10 @@ module Jekyll
def self.process(post, items)
toc = "<div class=\"toc\">\n"
- base_level = items.first[1].to_i - 1
+ base_level = items.first[:level] - 1
items.each do |item|
- toc += "*"*(item[1].to_i - base_level) # <li> level
- toc += " \"#{item[0]}\":##{item[0].gsub(/ /, '_')}\n"
+ depth = item[:level] - base_level
+ toc += "#{"*"*depth} \"#{item[:link_text]}\":##{item[:id]}\n"
end
toc += "</div>"
--
1.7.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment