+module Jekyll
+
+ class CategoryPage < Page
+ def initialize(site, base, dir, name, category, layout)
+ @site = site
+ @base = base
+ @dir = dir
+ @name = name
+
+ self.process(@name)
+ self.read_yaml(File.join(base, '_layouts'), layout)
+ self.data['category'] = category
+
+ category_title_prefix = site.config['blog']['category_title_prefix'] || 'Category: '
+ self.data['title'] = "#{category_title_prefix}#{category.capitalize}"
+ end
+ end
+
+ class CategoryPageGenerator < Generator
+ safe true
+
+ def generate(site)
+ dir = site.config['blog']['category_dir'] || 'categories'
+ if site.layouts.key? 'category_index'
+ site.categories.each_key do |category|
+ site.pages << CategoryPage.new(site, site.source, dir, "#{category}.html", category, 'category_index.html')
+ end
+ end
+ if site.layouts.key? 'category_feed'
+ site.categories.each_key do |category|
+ site.pages << CategoryPage.new(site, site.source, dir, "#{category}.xml", category, 'category_feed.html')
+ end
+ end
+ end
+ end
+
+ # Adds some extra filters used during the category creation process.
+ module Filters
+
+ # Outputs a list of categories as comma-separated <a> links. This is used
+ # to output the category list for each post on a category page.
+ #
+ # +categories+ is the list of categories to format.
+ #
+ # Returns string
+ def category_links(categories)
+ base_dir = @context.registers[:site].config['blog']['category_dir']
+ categories = categories.sort!.map do |category|
+ category_url = File.join(base_dir, "#{category}.html")
+ # Make sure the category directory begins with a slash.
+ category_dir = "/#{category_dir}" unless category_dir =~ /^\//
+ "<a class='category' href='#{category_url}'>#{category.capitalize}</a>"
+ end
+
+ case categories.length
+ when 0
+ ""
+ when 1
+ categories[0].to_s
+ else
+ categories.join(', ')
+ end
+ end
+ end
+end