Created
February 1, 2013 07:58
-
-
Save komasaru/4690027 to your computer and use it in GitHub Desktop.
Octopress plugin to generate a month list.
This file contains 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
# encoding: utf-8 | |
# | |
# Month List for Octopress | |
module Jekyll | |
class MonthList < Liquid::Tag | |
def initialize(tag_name, markup, tokens) | |
@opts = {} | |
if markup.strip =~ /\s*counter:(\w+)/i | |
@opts['counter'] = ($1 == 'true') | |
markup = markup.strip.sub(/counter:\w+/i,'') | |
end | |
super | |
end | |
def render(context) | |
html = "" | |
posts = context.registers[:site].posts.reverse | |
posts = posts.group_by{|c| {"month" => c.date.month, "year" => c.date.year}} | |
posts.each do |period, post| | |
month_dir = "/#{period["year"]}/#{"%02d" % period["month"]}/" | |
html << "<li><a href='#{month_dir}'>#{period["year"]}-#{"%02d" % period["month"]}" | |
html << " (#{post.count})" if @opts['counter'] | |
html << "</a></li>" | |
end | |
html | |
end | |
end | |
end | |
Liquid::Template.register_tag('month_list', Jekyll::MonthList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment