Skip to content

Instantly share code, notes, and snippets.

@jamesaoverton
Created October 27, 2011 00:37
Show Gist options
  • Save jamesaoverton/1318447 to your computer and use it in GitHub Desktop.
Save jamesaoverton/1318447 to your computer and use it in GitHub Desktop.
Jekyll plugin for generating tag index files and directories.
module Jekyll
class TagIndex < Page
def initialize(site, base, dir, tag)
@site = site
@base = base
@dir = dir
@name = 'index.html'
self.process(@name)
self.data = {}
self.data['layout'] = 'tag_index'
self.data['tag'] = tag
self.data['related'] = []
site.tags[tag].each do |post|
post.tags.each do |rel|
self.data['related'].push(rel)
end
end
self.data['related'] = self.data['related'].uniq
tag_title_prefix = site.config['tag_title_prefix'] || 'Tag: '
self.data['title'] = "#{tag_title_prefix}#{tag}"
end
end
class TagList < Page
def initialize(site, base, dir, tags)
@site = site
@base = base
@dir = dir
@name = 'index.html'
self.process(@name)
self.data = {}
self.data['layout'] = 'tag_list'
self.data['tags'] = tags
self.data['title'] = site.config['tag_index_title'] || 'All Tags'
end
end
class TagGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'tag_index'
dir = site.config['tag_dir'] || 'tags'
site.tags.keys.each do |tag|
write_tag_index(site, File.join(dir, tag.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase), tag)
end
end
if site.layouts.key? 'tag_list'
dir = site.config['tag_dir'] || 'tags'
write_tag_list(site, dir, site.tags.keys.sort)
end
end
def write_tag_index(site, dir, tag)
index = TagIndex.new(site, site.source, dir, tag)
index.render(site.layouts, site.site_payload)
index.write(site.dest)
site.static_files << index
end
def write_tag_list(site, dir, tags)
index = TagList.new(site, site.source, dir, tags)
index.render(site.layouts, site.site_payload)
index.write(site.dest)
site.static_files << index
end
end
end
@jamesaoverton
Copy link
Author

This is a fork of Jose Gonzalez' tag.rb plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment