Consistent Update Times for Middleman Blog Articles with Git
The default template for an Atom feed in Middleman Blog uses the last modified time of an article’s source file as the article’s last update time. This means that if I build the site on two different machines I will get different last updated times on articles in the two atom feeds. I’d rather the built site look the same regardless of where I build it.
The source code for the site lives in a Git repository which means I have a consistent source for update times that I can rely on. So, I’ve added a helper that asks Git for the last commit time of a file and falls back to its last modified time if the file isn’t currently tracked in Git.
helpers do
def last_update_time(file)
Time.parse `git log -1 --format=%cd #{file} 2>/dev/null`
rescue
File.mtime(file)
end
do
I now use this helper in my Atom template for each article.
xml.entry do
xml.published article.date.to_time.iso8601
xml.updated last_update_time(article.source_file).iso8601
xml.content article.body, "type" => "html"
end