Permalink
Browse files

Add tag generator for per-tag pages

  • Loading branch information...
1 parent 4a3d688 commit 1daa7dfdc9b5f9f18593efbde5fefec9763f68e2 @datashaman committed Dec 27, 2011
@@ -81,7 +81,7 @@ def render(context)
name, weight = tag
size = size_min + ((size_max - size_min) * weight).to_f
size = sprintf("%.#{@precision}f", size)
- html << "<a style='font-size: #{size}#{unit}' href='/tags.html##{name}'>#{name}</a>\n"
+ html << "<a style='font-size: #{size}#{unit}' href='/tags/#{name}'>#{name}</a>\n"
end
end
@@ -0,0 +1,176 @@
+# encoding: utf-8
+#
+# Marlin Forbes (http://github.com/datashaman):
+# Adapted the tag generator to do tags as well
+#
+# Jekyll tag page generator.
+# http://recursive-design.com/projects/jekyll-plugins/
+#
+# Version: 0.1.4 (201101061053)
+#
+# Copyright (c) 2010 Dave Perrett, http://recursive-design.com/
+# Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
+#
+# A generator that creates tag pages for jekyll sites.
+#
+# Included filters :
+# - tag_links: Outputs the list of tags as comma-separated <a> links.
+# - date_to_html_string: Outputs the post.date as formatted html, with hooks for CSS styling.
+#
+# Available _config.yml settings :
+# - tag_dir: The subfolder to build tag pages in (default is 'tags').
+# - tag_title_prefix: The string used before the tag name in the page title (default is
+# 'Tag: ').
+
+module Jekyll
+
+ # The TagIndex class creates a single tag page for the specified tag.
+ class TagIndex < Page
+
+ # Initializes a new TagIndex.
+ #
+ # +base+ is the String path to the <source>.
+ # +tag_dir+ is the String path between <source> and the tag folder.
+ # +tag+ is the tag currently being processed.
+ def initialize(site, base, tag_dir, tag)
+ @site = site
+ @base = base
+ @dir = tag_dir
+ @name = 'index.html'
+ self.process(@name)
+ # Read the YAML data from the layout page.
+ self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
+ self.data['tag'] = tag
+ # Set the title for this page.
+ title_prefix = site.config['tag_title_prefix'] || 'Tag: '
+ self.data['title'] = "#{title_prefix}#{tag}"
+ # Set the meta-description for this page.
+ meta_description_prefix = site.config['tag_meta_description_prefix'] || 'Tag: '
+ self.data['description'] = "#{meta_description_prefix}#{tag}"
+ end
+
+ end
+
+ # The TagFeed class creates an Atom feed for the specified tag.
+ class TagFeed < Page
+
+ # Initializes a new TagFeed.
+ #
+ # +base+ is the String path to the <source>.
+ # +tag_dir+ is the String path between <source> and the tag folder.
+ # +tag+ is the tag currently being processed.
+ def initialize(site, base, tag_dir, tag)
+ @site = site
+ @base = base
+ @dir = tag_dir
+ @name = 'atom.xml'
+ self.process(@name)
+ # Read the YAML data from the layout page.
+ self.read_yaml(File.join(base, '_includes/custom'), 'tag_feed.xml')
+ self.data['tag'] = tag
+ # Set the title for this page.
+ title_prefix = site.config['tag_title_prefix'] || 'Tag: '
+ self.data['title'] = "#{title_prefix}#{tag}"
+ # Set the meta-description for this page.
+ meta_description_prefix = site.config['tag_meta_description_prefix'] || 'Tag: '
+ self.data['description'] = "#{meta_description_prefix}#{tag}"
+
+ # Set the correct feed URL.
+ self.data['feed_url'] = "#{tag_dir}/#{name}"
+ end
+
+ end
+
+ # The Site class is a built-in Jekyll class with access to global site config information.
+ class Site
+
+ # Creates an instance of TagIndex for each tag page, renders it, and
+ # writes the output to a file.
+ #
+ # +tag_dir+ is the String path to the tag folder.
+ # +tag+ is the tag currently being processed.
+ def write_tag_index(tag_dir, tag)
+ index = TagIndex.new(self, self.source, tag_dir, tag)
+ index.render(self.layouts, site_payload)
+ index.write(self.dest)
+ # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
+ self.pages << index
+
+ # Create an Atom-feed for each index.
+ feed = TagFeed.new(self, self.source, tag_dir, tag)
+ feed.render(self.layouts, site_payload)
+ feed.write(self.dest)
+ # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
+ self.pages << feed
+ end
+
+ # Loops through the list of tag pages and processes each one.
+ def write_tag_indexes
+ if self.layouts.key? 'tag_index'
+ dir = self.config['tag_dir'] || 'tags'
+ self.tags.keys.each do |tag|
+ self.write_tag_index(File.join(dir, tag.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase), tag)
+ end
+
+ # Throw an exception if the layout couldn't be found.
+ else
+ throw "No 'tag_index' layout found."
+ end
+ end
+
+ end
+
+
+ # Jekyll hook - the generate method is called by jekyll, and generates all of the tag pages.
+ class GenerateTags < Generator
+ safe true
+ priority :low
+
+ def generate(site)
+ site.write_tag_indexes
+ end
+
+ end
+
+
+ # Adds some extra filters used during the tag creation process.
+ module Filters
+
+ # Outputs a list of tags as comma-separated <a> links. This is used
+ # to output the tag list for each post on a tag page.
+ #
+ # +tags+ is the list of tags to format.
+ #
+ # Returns string
+ #
+ def tag_links(tags)
+ dir = @context.registers[:site].config['tag_dir']
+ tags = tags.sort!.map do |item|
+ "<a class='tag' href='/#{dir}/#{item.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase}/'>#{item}</a>"
+ end
+
+ case tags.length
+ when 0
+ ""
+ when 1
+ tags[0].to_s
+ else
+ "#{tags[0...-1].join(', ')}, #{tags[-1]}"
+ end
+ end
+
+ # Outputs the post.date as formatted html, with hooks for CSS styling.
+ #
+ # +date+ is the date object to format as HTML.
+ #
+ # Returns string
+ def date_to_html_string(date)
+ result = '<span class="month">' + date.strftime('%b').upcase + '</span> '
+ result += date.strftime('<span class="day">%d</span> ')
+ result += date.strftime('<span class="year">%Y</span> ')
+ result
+ end
+
+ end
+
+end
@@ -0,0 +1,12 @@
+{% capture category %}{{ post.categories | size }}{% endcapture %}
+{% capture tag %}{{ post.tags | size }}{% endcapture %}
+<h1><a href="{{ root_url }}{{ post.url }}">{{post.title}}</a></h1>
+<time datetime="{{ post.date | datetime | date_to_xmlschema }}" pubdate>{{ post.date | date: "<span class='month'>%b</span> <span class='day'>%d</span> <span class='year'>%Y</span>"}}</time>
+<footer>
+ {% if category != '0' %}
+ <div class="categories">posted in {{ post.categories | category_links }}</div>
+ {% endif %}
+ {% if tag != '0' %}
+ <div class="tags">tagged as {{ post.tags | tag_links }}</div>
+ {% endif %}
+</footer>
@@ -4,6 +4,5 @@
<li><a href="{{ root_url }}/category/play/">Play</a></li>
<li><a href="{{ root_url }}/category/work/">Work</a></li>
<li><a href="{{ root_url }}/category/clients/">Clients</a></li>
- <li><a href="{{ root_url }}/tags.html">Tags</a></li>
<li><a href="{{ root_url }}/archives/">Archives</a></li>
</ul>
@@ -0,0 +1,27 @@
+---
+layout: nil
+---
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+ <title><![CDATA[{{ page.title }} | {{ site.title }}]]></title>
+ <link href="{{ site.url }}/{{ page.feed_url }}" rel="self"/>
+ <link href="{{ site.url }}/"/>
+ <updated>{{ site.time | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}/</id>
+ <author>
+ <name><![CDATA[{{ site.author | strip_html }}]]></name>
+ {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
+ </author>
+ <generator uri="http://octopress.org/">Octopress</generator>
+
+ {% for post in site.tags[page.tag] limit: 5 %}
+ <entry>
+ <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
+ <link href="{{ site.url }}{{ post.url }}"/>
+ <updated>{{ post.date | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}{{ post.id }}</id>
+ <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
+ </entry>
+ {% endfor %}
+</feed>
@@ -0,0 +1,10 @@
+{% capture tag %}{% if post %}{{ post.tags | tag_links | size }}{% else %}{{ page.tags | tag_links | size }}{% endif %}{% endcapture %}
+{% unless tag == '0' %}
+<span class="tags">
+ {% if post %}
+ {{ post.tags | tag_links }}
+ {% else %}
+ {{ page.tags | tag_links }}
+ {% endif %}
+</span>
+{% endunless %}
@@ -11,7 +11,7 @@
<h2>{{ year }}</h2>
{% endunless %}
<article>
- {% include archive_post.html %}
+ {% include custom/archive_post.html %}
</article>
{% endfor %}
</div>
@@ -17,6 +17,7 @@ <h1 class="entry-title">{% if site.titlecase %}{{ page.title | titlecase }}{% el
{% if page.author %}{% include post/author.html %}{% endif %}
{% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %}
{% if page.categories %}{% include post/categories.html %}{% endif %}
+ {% if page.tags %}{% include custom/tags.html %}{% endif %}
</p>{% endif %}
{% unless page.sharing == false %}
{% include post/sharing.html %}
@@ -11,6 +11,7 @@
{% include post/author.html %}
{% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %}
{% include post/categories.html %}
+ {% include custom/tags.html %}
</p>
{% unless page.sharing == false %}
{% include post/sharing.html %}
@@ -0,0 +1,17 @@
+---
+layout: page
+footer: false
+---
+
+<div id="blog-archives" class="tag">
+{% for post in site.tags[page.tag] %}
+{% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
+{% unless year == this_year %}
+ {% assign year = this_year %}
+ <h2>{{ year }}</h2>
+{% endunless %}
+<article>
+ {% include custom/archive_post.html %}
+</article>
+{% endfor %}
+</div>
@@ -12,7 +12,7 @@
<h2>{{ year }}</h2>
{% endunless %}
<article>
- {% include archive_post.html %}
+ {% include custom/archive_post.html %}
</article>
{% endfor %}
</div>

0 comments on commit 1daa7df

Please sign in to comment.