Last active
August 29, 2015 14:13
-
-
Save themattchan/21f0e3c6112874a9e3c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AsideBlock | |
# Insert toggle-able text segments with ease in Jekyll blog posts. | |
# Requires Bootstrap | |
# | |
# Author: Matthew Chan <[email protected]> | |
# Date: January 2015 | |
# License: GNU GPL | |
# | |
# Usage: | |
# {% aside <title> %} ==> auto expands to "(aside) <title>" | |
# <markdown body> | |
# {% endaside %} | |
module Jekyll | |
class AsideBlock < Liquid::Block | |
def initialize(tag_name, text, tokens) | |
super | |
# text = <title>, everything after the aside token in {% aside ... %} | |
@text = "(aside) " + text | |
end | |
require "kramdown" | |
def render(context) | |
content = super | |
content = "#{Kramdown::Document.new(content,:input => 'GFM').to_html}" | |
gensym = rand_id | |
%Q|<div id="aside-#{gensym}" class="panel panel-default"> <div | |
class="panel-heading"> <p class="panel-title"><b> <a | |
href="\#aside-#{gensym}\#aside-text-#{gensym}" data-toggle="collapse"> | |
#{@text} </a> </b></p> </div> <div id="aside-text-#{gensym}" | |
class="panel-body collapse">#{content}</div></div>| | |
end | |
def rand_id | |
o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten | |
string = (0...10).map { o[rand(o.length)] }.join | |
string | |
end | |
end | |
end | |
Liquid::Template.register_tag('aside', Jekyll::AsideBlock) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment