Skip to content

Instantly share code, notes, and snippets.

@alexeckermann
Created June 27, 2011 11:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeckermann/1048722 to your computer and use it in GitHub Desktop.
Save alexeckermann/1048722 to your computer and use it in GitHub Desktop.
The Jekyll plugins used on alexeckermann.com
module Jekyll
class CategoryListBlock < Liquid::Block
include Liquid::StandardFilters
def render(context)
categories = context.registers[:site].categories.keys
result = []
context.stack do
for category in categories.sort do
context['category'] = category
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('category_list', Jekyll::CategoryListBlock)
# This generates a simple block to create a category list.
#
# {% category_list %}
# %li
# %a{:href => '/{{ category }}'} {{ category | capitalize }}
# {% endcategory_list %}
module Jekyll
class CategoryPostBlock < Liquid::Block
include Liquid::StandardFilters
SYNTAX = /(\w+)\s+(#{Liquid::QuotedFragment}+)/
def initialize(tag_name, markup, tokens)
@attributes = {}
if markup =~ SYNTAX
markup.scan(Liquid::TagAttributes) do |key, value|
@attributes[key] = value
markup = markup.gsub(/#{key}:(\s)?#{value}/, '')
end
@categories = markup.split(',').map(&:strip)
end
super
end
def render(context)
posts = []
result = []
@categories.each do |category|
posts = posts.concat context.registers[:site].categories[category]
end
posts = posts.sort_by { |p| -p.date.to_f }
limit = @attributes.key?('limit') ? @attributes['limit'].to_i-1 : posts.count-1
context.stack do
for post in posts[0..limit] do
context['post'] = post
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('category_posts', Jekyll::CategoryPostBlock)
# Lists out posts in a particular category. Accepts multiple category names & limit
#
# {% category_posts tutorials, screencasts limit:3 %}
# %article.cell.post
# %a{:href => '{{ post.url }}'}
# %img{:src => '{{ post.images["box"] }}'}
# %h4
# %a{:href => '{{ post.url }}'} {{ post.title }}
# %p {{ post.description }}
# {% endcategory_posts %}
module Jekyll
class FeaturedPostBlock < Liquid::Block
include Liquid::StandardFilters
def initialize(tag_name, markup, tokens)
super
@limit = markup.to_i - 1
end
def render(context)
featured = context.registers[:site].posts.dup.sort_by { |p| -p.date.to_f }.delete_if { |p| !p.data.key?('featured') || p.data['featured'] == false }
result = []
context.stack do
for post in featured[0..@limit] do
context['post'] = post
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('featured_posts', Jekyll::FeaturedPostBlock)
# Typical featured posts
#
# {% featured_posts 3 %}
# %a{:href => '{{post.url}}'}
# %img{:src => '{{ post.images["banner"] }}'}
# {% endfeatured_posts %}
module Jekyll
class CategoryGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'category_index'
site.categories.keys.each do |category|
paginate(site, category) unless site.config['paginate'].nil?
end
end
end
def paginate(site, category)
category_posts = site.categories[category].sort_by { |p| -p.date.to_f }
pages = CategoryPager.calculate_pages(category_posts, site.config['paginate'].to_i)
(1..pages).each do |num_page|
page = CategorySubPage.new(site, site.source, category)
page.pager = CategoryPager.new(site.config, num_page, category_posts, category, pages)
page.dir = num_page == 1 ? File.join(category) : File.join(category, "/page/#{num_page}")
site.pages << page
end
end
end
class CategoryPager < Pager
attr_reader :category
def initialize(config, page, all_posts, category, num_pages = nil)
@category = category
super config, page, all_posts, num_pages
end
alias_method :original_to_liquid, :to_liquid
def to_liquid
x = original_to_liquid
x['category'] = @category
x
end
end
class CategorySubPage < Page
def initialize(site, base, category)
@site, @base, @name = site, base, 'index.html'
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), 'category_index.html')
self.data['title'] = "#{site.config['cateogry_title_prefix'] || 'Everything in the '}#{category}"
end
end
end
module Jekyll
module Filters
def excerpt(post = nil)
content = post['content']
if content.include?('<!-- more -->')
"#{content.split('<!-- more -->').first} #{read_more_link post['url']}"
else
content
end
end
def read_more_link(url = "")
"<p class=\"continue\"><a href=\"#{url}\">Continue reading this post <span>&rarr;</span></a></p>"
end
def parameterize(text = "")
text.gsub(/[^a-z0-9\-_]+/, '-').split('-').delete_if(&:empty?).join('-')
end
def date_to_words(input = Date.today)
input.strftime('%B %e, %Y')
end
def categories_to_sentence(input = [])
case input.length
when 0
""
when 1
link_to_category input[0].to_s
when 2
"#{link_to_category input[0]} and #{link_to_category input[1]}"
else
"#{input[0...-1].map(&:link_to_category).join(', ')} and #{link_to_category input[-1]}"
end
end
def link_to_category(category)
"<a href=\"/#{category}\">#{category}</a>"
end
def pager_links(pager)
if pager['previous_page'] || pager['next_page']
html = '<div class="pager">'
if pager['previous_page']
if pager['previous_page'] == 1
html << "<div class=\"previous\"><p><a href=\"/#{pager['category']}/\">Newer posts &rarr;</a></p></div>"
else
html << "<div class=\"previous\"><p><a href=\"/#{pager['category']}/page/#{pager['previous_page']}\">Newer posts &rarr;</a></p></div>"
end
end
if pager['next_page']
html << "<div class=\"next\"><p><a href=\"/#{pager['category']}/page/#{pager['next_page']}\">&larr; Older posts</a></p></div>"
end
html << '</div>'
html
end
end
def mail_to(address = '', text = nil)
"<a class=\"mailto\" href=\"#{address.to_s.tr("A-Za-z", "N-ZA-Mn-za-m")}\">#{!text ? address.tr("A-Za-z", "N-ZA-Mn-za-m") : text}</a>"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment