ianwinter
1/22/2011 - 2:03 AM

Goes in Rakefile for Jekyll for those wanting tag pages

Goes in Rakefile for Jekyll for those wanting tag pages

# Using multi-word tags support from http://tesoriere.com/2010/08/25/automatically-generated-tags-and-tag-clouds-with-jekyll--multiple-word-tags-allowed-/
# If using with jekyll gem, remove bits loop and site.read_bits('') and add require 'rubygems' and require 'jekyll'
# Using full HTML instead of layout include to manually set different page title (tag, not tag_slug) and canonical URL in header


desc 'Build tags pages'
task :build_with_tags do
    sh 'rm -rf _site'
    
    puts "Generating tags..."
    # use custom jekyll that includes archives pages, bits, multiviews, etc
    # https://github.com/stammy/jekyll
    require '/Users/Stammy/Projects/jekyll-stammy/lib/jekyll.rb'
    include Jekyll::Filters

    options = Jekyll.configuration({})
    site = Jekyll::Site.new(options)
    site.read_posts('')
    site.read_bits('')

    # nuke old tags pages, recreate
    FileUtils.rm_rf("tags")
    FileUtils.mkdir_p("tags")

    site.tags.sort.each do |tag, posts|
	  # generate slug-friendly tag
      tag_slug = tag.gsub(' ','-').gsub('&', '&')

      html = <<-HTML
---
layout: nil
---
<!doctype html>

<html lang="en-US">
	<head>
		<meta "charset=utf-8"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>#{tag} &mdash; PaulStamatiou.com</title>

		{% if page.description %}
		<meta content="{{ page.description }}" name="description">
		{% endif %}
		<meta name="description" content="{{ site.description }}"/>
    	<link rel="canonical" href="{{ site.base_url }}/tags/#{tag_slug}"/>

		<link rel="alternate" type="application/rss+xml" title="PaulStamatiou.com Feed" href="http://feeds.feedburner.com/paulstamatiou"/>
		<link rel="stylesheet" href="/sass/screen.css" type="text/css"/>
	</head>
	<body>
	{% include nav.html %}
        <div id="site">
      	<div id="post">
        {% include cta.html %}
        <h1>Posts tagged as <span>#{tag}</span></h1>
        <ul class="posts post_listing">
	{% for post in site.posts %}
          	{% for tag in post.tags %}
          		{% if tag == '#{tag}' %}
            			{% include post_listing.html %}
          		{% endif %}
          	{% endfor %}
        {% endfor %}
        {% for bit in site.bits %}
	        {% for tag in bit.tags %}
		        {% if tag == '#{tag}' %}
            			{% include bit_listing.html %}
		        {% endif %}
	        {% endfor %}
        {% endfor %}
        </ul>
        	<p class="larger">View more in the <a href="{{ site.base_url }}/archives" title="Archives - Paul Stamatiou">archives</a> or <a href="{{ site.base_url }}/search" title="Search">search</a>.</p>
      	</div>
	</div>
    	{% include load_last_js.html %}
	</body>
</html>
HTML
      File.open("tags/#{tag_slug}.html", 'w+') do |file|
        file.puts html
      end
    end

    puts 'Generating tag cloud...'
    html = ''
    max_count = site.tags.map{|t,p| p.count}.max
    site.tags.sort.each do |tag, posts|
      s = posts.count
      if s > 1
        font_size = ((24 - 10.0*(max_count-s)/max_count)*2).to_i/2.0
        tag_slug = tag.gsub(' ','-')
        html << "<a href=\"{{ site.base_url }}/tags/#{tag_slug}\" title=\"Posts tagged #{tag}\" style=\"font-size: #{font_size}px; line-height:#{font_size}px\">#{tag}</a> "
      end
    end
    File.open('_includes/tag_cloud.html', 'w+') do |file|
      file.puts html
    end
    puts 'Done. Now running Jekyll and Compass..'
    jekyll
end


def jekyll
  sh 'jekyll'
  sh 'compass compile'
end