Skip to content

Instantly share code, notes, and snippets.

@alexyoung
Created July 9, 2009 11:02
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alexyoung/143571 to your computer and use it in GitHub Desktop.
Save alexyoung/143571 to your computer and use it in GitHub Desktop.
namespace :tags do
task :generate do
puts 'Generating tags...'
require 'rubygems'
require 'jekyll'
include Jekyll::Filters
options = Jekyll.configuration({})
site = Jekyll::Site.new(options)
site.read_posts('')
html =<<-HTML
---
layout: default
title: Tags
---
<h2>Tags</h2>
HTML
site.categories.sort.each do |category, posts|
html << <<-HTML
<h3 id="#{category}">#{category}</h3>
HTML
html << '<ul class="posts">'
posts.each do |post|
post_data = post.to_liquid
html << <<-HTML
<li>
<div>#{date_to_string post.date}</div>
<a href="#{post.url}">#{post_data['title']}</a>
</li>
HTML
end
html << '</ul>'
end
File.open('tags.html', 'w+') do |file|
file.puts html
end
puts 'Done.'
end
end
@coolaj86
Copy link

coolaj86 commented Aug 6, 2010

A way to do this jekyll only (without ruby), works on github, for example:


---
layout: default
title: Fastr Blog

---
<h2>Categories:</h2>
<ul>
{% for category in site.categories %}
  <li><a href="#{{ category | first }}">{{ category | first }}</a></li>
{% endfor %}
</ul>
<h2>Articles by Category:</h2>
<ul>
{% for category in site.categories %}
  <li><a name="{{ category | first }}">{{ category | first }}</a>
    <ul>
    {% for posts in category %}
      {% for post in posts %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}
</ul>
{% for post in site.categories.quickstart %}
<!-- h2><a href=".{{ post.url }}">{{ post.title }}</a></h2 -->
<!-- {{ post.content }} -->
{% endfor %}
Page generated: {{ site.time | date_to_string }}

I posted to the jekyll mailing list to find out how to get the name of category so that I don't have to hackishly treat it as an array, but no answer yet.

@andrewheiss
Copy link

@coolja86 - That's fantastic! I had something similar working before I upgraded to Jekyll 0.6.2, but something in one of the intervening versions messed up the site.tags hash. Your hackish array treatment works almost perfectly.

One question… have you figured out a way to sort the tags? I used to be able to use for tags in site.tags|sort and that was it, but it doesn't work anymore. Have you had any luck?

Thanks!

@coolaj86
Copy link

@andrewheiss

I tried a few things, but I couldn't get it to work. Any progress?

@andrewheiss
Copy link

After trying to make some Liquid filters, I finally got it to work using a patch to the Jekyll core. If you add elstudio's patch (http://github.com/mojombo/jekyll/issuesearch?state=open&q=sort+#issue/58), it sorts the tags hash using RBTree (you'll have to install that gem). Your tags should be sorted without any modification to your code.

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