Skip to content

Instantly share code, notes, and snippets.

@pizn
Created February 24, 2012 08:47
Show Gist options
  • Select an option

  • Save pizn/1899497 to your computer and use it in GitHub Desktop.

Select an option

Save pizn/1899497 to your computer and use it in GitHub Desktop.

Revisions

  1. pizn created this gist Feb 24, 2012.
    39 changes: 39 additions & 0 deletions category_plugin.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    module Jekyll

    class CategoryIndex < Page
    def initialize(site, base, dir, category)
    @site = site
    @base = base
    @dir = dir
    @name = 'index.html'

    self.process(@name)
    self.read_yaml(File.join(base, '_layouts'), 'category_index.html')
    self.data['category'] = category

    category_title_prefix = site.config['category_title_prefix'] || 'Category: '
    self.data['title'] = "#{category_title_prefix}#{category}"
    end
    end

    class CategoryGenerator < Generator
    safe true

    def generate(site)
    if site.layouts.key? 'category_index'
    dir = site.config['category_dir'] || 'categories'
    site.categories.keys.each do |category|
    write_category_index(site, File.join(dir, category), category)
    end
    end
    end

    def write_category_index(site, dir, category)
    index = CategoryIndex.new(site, site.source, dir, category)
    index.render(site.layouts, site.site_payload)
    index.write(site.dest)
    site.pages << index
    end
    end

    end