Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created February 1, 2013 07:58
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 komasaru/4690027 to your computer and use it in GitHub Desktop.
Save komasaru/4690027 to your computer and use it in GitHub Desktop.
Octopress plugin to generate a month list.
# 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