1

I'm trying to create a static site using Jekyll, with content imported from Blogger. One of the things I want to do is have monthly archives, which I've been able to successfully generate using sample code I found:

module Jekyll

  class ArchiveGenerator < Generator
    safe true
    def generate(site)
      if site.layouts.key? 'archive_index'
        site.posts.group_by{ |c| {"month" => c.date.month, "year" => c.date.year} }.each do |period, posts|
          posts = posts.sort_by { |p| -p.date.to_f }
          archive_dir = File.join(period["year"].to_s(), "%02d" % period["month"].to_s())
          paginate(site, archive_dir, posts)
        end
        site.posts.group_by{ |c| {"year" => c.date.year} }.each do |period, posts|
          posts = posts.sort_by { |p| -p.date.to_f }
          archive_dir = period["year"].to_s()
          paginate(site, archive_dir, posts)
        end
      end
    end

    def paginate(site, dir, posts)
      pages = Pager.calculate_pages(posts, site.config['paginate'].to_i)
      (1..pages).each do |num_page|
        pager = Pager.new(site, num_page, posts, pages)
        archive_path = "/" + dir
        path = archive_path
        if num_page > 1
          path += "/page/#{num_page}"
        end
        newpage = ArchiveIndex.new(site, site.source, path, archive_path, posts)
        newpage.pager = pager
        site.pages << newpage

      end
    end
  end

  class ArchiveIndex < Page
    def initialize(site, base, dir, base_path, posts)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'

      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'archive_index.html')
      self.data['base'] = base_path
    end
  end
end

What I'd like is for the top-level index.html file to be the monthly archive for the most recent month for which there are posts. However, I've tried various ways to specify the top-level directory as the target for the generated file, but have been unsuccessful. Is this possible in Jekyll?

gnuf
  • 2,722
  • 1
  • 25
  • 32

1 Answers1

0

You don't even need a plugin written in Ruby (which the code you posted in your question is) to display the post for the most recent month on the front page.

It's quite easy to do this in Liquid, which means that it will work on GitHub pages (the code in your question won't, because it's a plugin):

<ul>
{% for post in site.posts %}
    {% assign currentmonth = post.date | date: "%B %Y" %}
    {% if prevmonth == null or currentmonth == prevmonth %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    {% else %}
        {% break %}
    {% endif %}
    {% assign prevmonth = currentmonth %}
{% endfor %}
</ul>

Short explanation:

  • We're looping the posts, getting the month and year from each date
    (post.date | date: "%B %Y" will return something like "July 2014")
  • The post will be displayed when the month/year of the current post is equal to the month/year of the previous post
  • month/year of the previous post will be empty in the beginning (prevmonth == null), so we'll always display the first post
  • when we reach the first post from a previous month, we'll exit the loop (with {% break %} - note that {% break %} wasn't supported in earlier Liquid versions)

Bonus:

If you can live with the monthly archives for all months on one single page, it's possible to generate that without plugins as well, with a similar approach.

Community
  • 1
  • 1
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • This seems like a good solution that I will try out. But I was trying to re-use the monthly archive template that I already had, with the navigation buttons (Previous Month, Next Month). The goal would be for index.html to allow the user to navigate to an older month, just as if she had visited the actual archive page. I needed the Ruby code to know which months had archive pages generated (for the rare case that one month goes by without any posts). – gnuf Jul 30 '14 at 19:00
  • More generally, though, is it possible for a plugin to create pages in the top-level directory? – gnuf Jul 30 '14 at 19:01
  • Sorry, no idea. I don't know Ruby, so I don't know how to write Jekyll plugins either. So far, I managed to achieve everything I needed without plugins. – Christian Specht Jul 30 '14 at 20:14